Created
September 26, 2014 04:25
-
-
Save JosePedroDias/8b2c59a72f834ae94a5e to your computer and use it in GitHub Desktop.
keeps pinging endpoint until response changes
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" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
func fetch(url string) string { | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Fatalf("error: %s", err) | |
os.Exit(2) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
body2 := string(body) | |
return body2 | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatalf("Syntax is: %s <url>", "urlChanged") | |
os.Exit(1) | |
} | |
url := os.Args[1] | |
deltaT := 2 | |
t := 0 | |
firstCall := true | |
var oldBody = "" | |
var newBody string | |
for { | |
newBody = fetch(url) | |
if firstCall { | |
fmt.Println("Got it!") | |
firstCall = false | |
} else if oldBody != newBody { | |
fmt.Printf("%d: changed!\n", t) | |
break | |
} else { | |
fmt.Printf("%d: the same...\n", t) | |
} | |
oldBody = newBody | |
time.Sleep(time.Second * time.Duration(deltaT)) | |
t += deltaT | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment