package main import ( "bytes" "fmt" "time" ) type httpCheck struct { URL string Code int } type httpCheckFunc func(string, int, int) error func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *jobResponse, timeout int, HF httpCheckFunc) { for hc := range jobs { err := HF(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, data map[string]*int, HF httpCheckFunc) 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, HF) } go func() { for url, code := range data { if code == nil { code = &conf.ExpectedStatusCode } hc <- httpCheck{url, *code} } close(hc) }() out := make(map[string]*jobResponse) for i := 0; i < len(data); i++ { resp := <-msgs if resp != nil { buf.Write([]byte(resp.Message)) out[resp.Id] = resp } } // notify(conf, buf.Bytes()) return out }