func handleConnection(conn net.Conn, jobd *jobserver.JobServer, cbor *codec.CborHandle, reqLogger *logrus.Logger) { defer conn.Close() var reqLog, errLog *logrus.Entry fields := logrus.Fields{ "remote": conn.RemoteAddr(), } errLog = logrus.WithFields(fields) if reqLogger != nil { reqLog = reqLogger.WithFields(fields) } jobdv := reflect.ValueOf(jobd) reader := bufio.NewReader(conn) decoder := codec.NewDecoder(reader, cbor) writer := bufio.NewWriter(conn) encoder := codec.NewEncoder(writer, cbor) for { var request cborrpc.Request err := decoder.Decode(&request) if err == io.EOF { if reqLog != nil { reqLog.Debug("Connection closed") } return } else if err != nil { errLog.WithError(err).Error("Error reading message") return } if reqLog != nil { reqLog.WithFields(logrus.Fields{ "id": request.ID, "method": request.Method, }).Debug("Request") } response := doRequest(jobdv, request) if reqLog != nil { entry := reqLog.WithField("id", response.ID) if response.Error != "" { entry = entry.WithField("error", response.Error) } entry.Debug("Response") } err = encoder.Encode(response) if err != nil { errLog.WithError(err).Error("Error encoding response") return } err = writer.Flush() if err != nil { errLog.WithError(err).Error("Error writing response") return } } }