func handle(conn net.Conn) { defer conn.Close() err := conn.SetDeadline(time.Now().Add(Timeout)) if err != nil { log.Errorf("Error setting deadline on conn") return } buf := getBuf(bufSize) defer putBuf(buf) n, err := conn.Read(buf) if err != nil { log.Error("Error reading from conn") return } log.Printf("Got %+q", buf[:n]) resp, err := parseRequest(buf[:n]) if err != nil { log.Error(err) return } n, err = conn.Write(resp) if err != nil || n != len(resp) { log.Errorf("Error writing to conn") return } }
func parseResponse(res *http.Response, v interface{}) { defer res.Body.Close() if c := res.Header.Get("Content-Type"); !strings.Contains(c, "application/json") { log.Error("Unexpected Content-Type") } reader := bufio.NewReader(res.Body) buf, _ := ioutil.ReadAll(reader) err := json.Unmarshal(buf, v) //fmt.Printf("%s\n", buf) //err := json.NewDecoder(res.Body).Decode(v) if err != nil { log.Error(err) } }
func TrapCloser(cl io.Closer, cleanups ...Cleanup) { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP) go func() { for { rawSig := <-c sig, ok := rawSig.(syscall.Signal) if !ok { log.Fatal("not a unix signal") } switch { case sig == syscall.SIGHUP: log.Print("SIGHUP: restart process") err := RestartProcess() if err != nil { log.Fatal("failed to restart: " + err.Error()) } case sig == syscall.SIGINT || sig == syscall.SIGKILL || sig == syscall.SIGTERM: log.Print("shutting down") donec := make(chan bool) go func() { for _, cb := range cleanups { if err := cb(); err != nil { log.Error(err) } } if err := cl.Close(); err != nil { log.Fatalf("Error shutting down: %v", err) } donec <- true }() select { case <-donec: log.Printf("shutdown") os.Exit(0) case <-time.After(5 * time.Second): log.Fatal("Timeout shutting down. Exiting uncleanly.") } default: log.Fatal("Received another signal, should not happen.") } } }() }
func PrepareIntIN(args *[]interface{}, w *[]string, value interface{}, table, field string) error { switch v := value.(type) { case []int: if len(v) == 0 { break } inPart := PrepareIN(len(v)) *w = append(*w, fmt.Sprintf("%s.%s IN (%s)", table, field, inPart)) *args = append(*args, intToIface(v)...) case int: *args = append(*args, v) *w = append(*w, fmt.Sprintf("%s.%s = ?", table, field)) default: err := fmt.Errorf("expected %s type int or []int, got %T", field, v) log.Error(err.Error()) return err } return nil }