Skip to content

Instantly share code, notes, and snippets.

@collinvandyck
Last active December 19, 2015 10:09
Show Gist options
  • Save collinvandyck/5938720 to your computer and use it in GitHub Desktop.
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
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