From 4a05aab2c08fef335a47eace092e1af52af3fdfa Mon Sep 17 00:00:00 2001 From: Anatolie Lupacescu Date: Fri, 6 Dec 2024 08:59:15 +0000 Subject: [PATCH] bufio decoder --- api/bind.go | 9 +++++++++ api/handlers/exporter.go | 25 ++++--------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/api/bind.go b/api/bind.go index e3756e9ece..ed797597f9 100644 --- a/api/bind.go +++ b/api/bind.go @@ -1,6 +1,8 @@ package api import ( + "bufio" + "encoding/json" "errors" "fmt" "net/http" @@ -90,5 +92,12 @@ func Bind(r *http.Request, dest interface{}) error { } } + if r.Header.Get("Content-Type") == "application/json" { + reader := bufio.NewReader(r.Body) + if err := json.NewDecoder(reader).Decode(dest); err != nil { + return err + } + } + return nil } diff --git a/api/handlers/exporter.go b/api/handlers/exporter.go index fc5ec721a1..07165d7c5b 100644 --- a/api/handlers/exporter.go +++ b/api/handlers/exporter.go @@ -1,9 +1,7 @@ package handlers import ( - "encoding/json" "fmt" - "io" "net/http" "time" @@ -47,27 +45,12 @@ func (e *Exporter) Decideds(w http.ResponseWriter, r *http.Request) error { } start := time.Now() + var bind time.Duration + bindStart := time.Now() if err := api.Bind(r, &request); err != nil { return api.BadRequestError(err) } - - readAllStart := time.Now() - var readAll, unmar time.Duration - if r.Header.Get("Content-Type") == "application/json" { - body, err := io.ReadAll(r.Body) - if err != nil { - return api.Error(err) - } - readAll = time.Since(readAllStart) - defer func() { _ = r.Body.Close() }() - - unmarStart := time.Now() - err = json.Unmarshal(body, &request) - if err != nil { - return api.Error(err) - } - unmar = time.Since(unmarStart) - } + bind = time.Since(bindStart) if request.From > request.To { return api.BadRequestError(fmt.Errorf("'from' must be less than or equal to 'to'")) @@ -83,7 +66,7 @@ func (e *Exporter) Decideds(w http.ResponseWriter, r *http.Request) error { dbTime := time.Duration(0) defer func() { - e.Log.Debug("decideds", zap.Duration("total", time.Since(start)), zap.Duration("db", dbTime), zap.Duration("readAll", readAll), zap.Duration("unmar", unmar)) + e.Log.Debug("decideds", zap.Duration("total", time.Since(start)), zap.Duration("db", dbTime), zap.Duration("bind", bind)) }() response.Data = []*ParticipantResponse{}