package main import ( "bytes" "fmt" "time" "riedstra.dev/go/checkup" ) type httpCheck struct { URL string Code int } func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *jobResponse, timeout int) { for hc := range jobs { err := checkup.HttpStatusOK(hc.URL, timeout, hc.Code) if err != nil { s := fmt.Sprintf("Checking: %s, %v", hc.URL, err) msg <- &jobResponse{ Id: hc.URL + "->" + string(hc.Code), Message: s, Err: err, Time: time.Now(), } continue } msg <- nil } } func checkStatus(conf *Config) map[string]*jobResponse { buf := &bytes.Buffer{} hc := make(chan httpCheck) msgs := make(chan *jobResponse) for i := 1; i <= conf.Workers; i++ { go httpStatusWorker(hc, msgs, conf.HTTPTimeout) } go func() { for url, code := range conf.StatusChecks { if code == nil { code = &conf.ExpectedStatusCode } hc <- httpCheck{url, *code} } close(hc) }() out := make(map[string]*jobResponse) for i := 0; i < len(conf.StatusChecks); i++ { resp := <-msgs if resp != nil { buf.Write([]byte(resp.Message)) out[resp.Id] = resp } } // notify(conf, buf.Bytes()) return out }