Skip to content

Commit

Permalink
bufio decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolie-ssv committed Dec 6, 2024
1 parent 68ba63e commit 4a05aab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
9 changes: 9 additions & 0 deletions api/bind.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -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
}
25 changes: 4 additions & 21 deletions api/handlers/exporter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package handlers

import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -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'"))
Expand All @@ -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{}
Expand Down

0 comments on commit 4a05aab

Please sign in to comment.