Created
February 4, 2015 21:35
-
-
Save vaskoz/a1c25ce315853aa74471 to your computer and use it in GitHub Desktop.
Use a timeout library to embedded a global timeout within smaller interval timeouts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/getlantern/withtimeout" | |
"net/http" | |
"time" | |
) | |
func main() { | |
ms := 110 // change this to correct number of milliseconds for your connection | |
intervalDuration, _ := time.ParseDuration(fmt.Sprintf("%dms", ms)) | |
timeoutDuration, _ := time.ParseDuration(fmt.Sprintf("%dms", 10*ms)) | |
interestingFunction := func() (interface{}, error) { | |
return http.Get("http://google.com") | |
} | |
intervaledFunctions := func() (interface{}, error) { | |
success, failures := 0, 0 | |
for i := 0; i < 10; i++ { | |
_, t, e := withtimeout.Do(intervalDuration, interestingFunction) | |
if e == nil { | |
success += 1 | |
} | |
if t == true { | |
failures += 1 | |
} | |
} | |
return success, fmt.Errorf("%s", failures) | |
} | |
successes, timedout, failures := withtimeout.Do(timeoutDuration, intervaledFunctions) | |
fmt.Printf("Success: %s, Failures: %s, Timed Out: %s", successes, failures, timedout) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment