Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created September 26, 2014 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/8b2c59a72f834ae94a5e to your computer and use it in GitHub Desktop.
Save JosePedroDias/8b2c59a72f834ae94a5e to your computer and use it in GitHub Desktop.
keeps pinging endpoint until response changes
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