Last active
December 19, 2015 10:09
-
-
Save collinvandyck/5938720 to your computer and use it in GitHub Desktop.
squawks about network lag while you're playing minecraft to correlate oddities in the game with digitalocean's spotty network
This file contains hidden or 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 ( | |
"flag" | |
"io/ioutil" | |
"net/http" | |
"os/exec" | |
"time" | |
) | |
func main() { | |
var url = flag.String("url", "http://ocean.collinvandyck.com", "the url to ping") | |
var delayMillis = flag.Int("delay", 1000, "how long to wait between tests") | |
var timeoutMillis = flag.Int("timeout", 500, "when to squawk") | |
flag.Parse() | |
for { | |
errCh := getUrl(*url) | |
timeoutCh := time.After(time.Duration(*timeoutMillis) * time.Millisecond) | |
select { | |
case err := <-errCh: | |
if err != nil { | |
exec.Command("/usr/bin/say", "-v", "cello", "ack error").Run() | |
} | |
case <-timeoutCh: | |
exec.Command("/usr/bin/say", "-v", "cello", "lag lag lag").Run() | |
} | |
time.Sleep(time.Duration(*delayMillis) * time.Millisecond) | |
} | |
} | |
func getUrl(url string) chan error { | |
ch := make(chan error) | |
go func() { | |
resp, err := http.Get(url) | |
if err != nil { | |
ch <- err | |
return | |
} | |
defer resp.Body.Close() | |
_, err = ioutil.ReadAll(resp.Body) | |
if err != nil { | |
ch <- err | |
return | |
} | |
ch <- nil | |
}() | |
return ch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment