aboutsummaryrefslogtreecommitdiff
path: root/cmd/main/http.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2021-01-24 12:26:02 -0500
committerMitch Riedstra <mitch@riedstra.us>2021-01-24 12:26:02 -0500
commitbad568d0a26ad348f25a4556266ba98f73b3be02 (patch)
tree6eb909e37d7b65472381a17362283b6765adfc14 /cmd/main/http.go
parent0539b119a4c205a96590b356849215b6e1b1ed35 (diff)
downloadcheckup-bad568d0a26ad348f25a4556266ba98f73b3be02.tar.gz
checkup-bad568d0a26ad348f25a4556266ba98f73b3be02.tar.xz
Run requests concurrently
Diffstat (limited to 'cmd/main/http.go')
-rw-r--r--cmd/main/http.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/cmd/main/http.go b/cmd/main/http.go
new file mode 100644
index 0000000..ce31495
--- /dev/null
+++ b/cmd/main/http.go
@@ -0,0 +1,56 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+
+ "riedstra.dev/go/checkup"
+)
+
+type httpCheck struct {
+ URL string
+ Code int
+}
+
+func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *string) {
+ for hc := range jobs {
+ err := checkup.HttpStatusOK(hc.URL, hc.Code)
+ if err != nil {
+ s := fmt.Sprintf("Checking: %s, %v\n",
+ hc.URL, err)
+ msg <- &s
+ continue
+ }
+ msg <- nil
+ }
+}
+
+func checkStatus(conf *Config) {
+ buf := &bytes.Buffer{}
+
+ hc := make(chan httpCheck)
+ msgs := make(chan *string)
+
+ for i := 1; i <= conf.Workers; i++ {
+ go httpStatusWorker(hc, msgs)
+ }
+
+ go func() {
+ for url, code := range conf.StatusChecks {
+ if code == nil {
+ code = &conf.ExpectedStatusCode
+ }
+ hc <- httpCheck{url, *code}
+ }
+ close(hc)
+ }()
+
+ for i := 0; i < len(conf.StatusChecks); i++ {
+ m := <-msgs
+ if m != nil {
+ buf.Write([]byte(*m))
+ }
+ }
+
+ notify(conf, buf.Bytes())
+}