func main()

in pkg/util/nethealth/nethealth.go [66:131]


func main() {
	flag.Parse()
	// Quick object existence check before a full GET
	res, err := http.Head(objectUrl)
	if err != nil {
		log.Fatalf("Failed to find URL %s (%s)", objectUrl, err)
	}
	if res.ContentLength != objectLength {
		log.Fatalf("Length reported (%d) is not equal to expected length (%d)", res.ContentLength, objectLength)
	}
	log.Printf("HTTP HEAD reports content length: %d - running GET\n", res.ContentLength)
	res, err = http.Head(objectHashUrl)
	if err != nil {
		log.Fatalf("Failed to find hash URL %s (%s)", objectHashUrl, err)
	}
	/* Now, setup a Client with a transport with compression disabled and timeouts enabled */
	tr := &http.Transport{
		DisableCompression: true,
	}
	downloadStartTime := time.Now()
	go monitorTimeElapsed(downloadStartTime, time.Duration(timeout))
	client := &http.Client{Transport: tr}
	res, err = client.Get(objectUrl)
	if err != nil {
		log.Fatalf("Failure (%s) while reading %s", err, objectUrl)
	}
	if res.ContentLength != objectLength {
		log.Fatalf("Length reported (%d) is not equal to expected length (%d)", res.ContentLength, objectLength)
	}
	blobData, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal("Failed to read full content", err)
	}
	elapsedMs := int64(time.Since(downloadStartTime) / time.Millisecond)
	bandwidth := (res.ContentLength * 1000) / (elapsedMs * 1024)

	log.Printf("DOWNLOAD: %d bytes %d ms Bandwidth ~ %d KiB/sec\n", res.ContentLength, elapsedMs, bandwidth)
	// Check if this bandwidth exceeds minimum expected
	if minimum*1024 > bandwidth {
		log.Fatalf("ERROR: Minimum bandwidth guarantee of %d MiB/sec not met - network connectivity is slow", minimum)
	}
	// Perform SHA512 hash and compare against the expected hash to check for corruption.
	res, err = client.Get(objectHashUrl)
	if err != nil {
		log.Fatalf("Failure (%s) while reading %s", err, objectHashUrl)
	}
	content, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal("Failed to read full content of hash file", err)
	}
	parts := strings.Split(string(content), " ")
	if len(parts) <= 1 {
		log.Fatalf("Could not parse SHA hash file contents (%s)", content)
	}
	hash := parts[1]
	hash = strings.Trim(hash, "\n ")
	sumBytes := sha512.Sum512(blobData)
	sumString := hex.EncodeToString(sumBytes[0:])
	if strings.Compare(sumString, hash) == 0 {
		log.Println("Hash Matches expected value")
	} else {
		log.Fatalf("ERROR: Hash Mismatch - Computed hash = '%s' Expected hash = '%s'", sumString, hash)
	}
}