func (mon *Monitor) report(url *http.URL, every int64) { i := 0 for { // Sleep between updates if i > 0 { time.Sleep(every) } i++ // Prepare HTTP request jj, err := mon.dumper.MarshalJSON() if err != nil { continue } //printJSON(jj) repbuf, err := EncodeReport(jj, mon.key) if err != nil { continue } bodylen, body := http.BytesToBody(repbuf) req := &http.Request{ Method: "POST", URL: url, Body: body, ContentLength: int64(bodylen), Close: true, UserAgent: sys.Name + "-Client-Monitor", } // Connect conn, err := net.Dial("tcp", "", url.Host) if err != nil { if conn != nil { conn.Close() } continue } cc := http.NewClientConn(conn, nil) // Send request wch := make(chan int, 1) req.Body = http.NewRunOnClose(req.Body, func() { wch <- 1 }) err = cc.Write(req) if err != nil { cc.Close() conn.Close() } else { <-wch // wait until the request has been sent cc.Close() conn.Close() } } }
func askForGreenLight() bool { url, err := http.ParseURL(sys.TangraServerURL) if err != nil { panic("tangra, bad URL") } req := &http.Request{ Method: "GET", URL: url, Close: true, UserAgent: sys.Name + "-Client-Tangra", } req.Header = make(map[string]string) req.Header["Tonika-Build"] = sys.Build req.Header["Tonika-Released"] = sys.Released // Connect conn, err := net.Dial("tcp", "", url.Host) if err != nil { if conn != nil { conn.Close() } return true } cc := http.NewClientConn(conn, nil) // Send request err = cc.Write(req) if err != nil { cc.Close() conn.Close() return true } resp, err := cc.Read() if err != nil { cc.Close() conn.Close() return true } cc.Close() conn.Close() if resp.Header == nil { return true } green, ok := resp.Header["Green"] if !ok { return true } green = strings.TrimSpace(green) if green == "Halt" { return false } return true }