Skip to content

Commit

Permalink
feat(helpers): add net connection fied into the response wrapper and …
Browse files Browse the repository at this point in the history
…methods to set read/write deadline
  • Loading branch information
jeamon committed Nov 13, 2023
1 parent 2e7ea27 commit 30bb544
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion helpers.response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"time"
)

// CustomResponseWriter is a wrapper for http.ResponseWriter. It is
// used to record response details like status code and body size.
// The underlying network connection is tracked for dynamic read/write
// deadline setup.
type CustomResponseWriter struct {
http.ResponseWriter
conn net.Conn
code int
bytes int
wrote bool
}

// NewCustomResponseWriter provides CustomResponseWriter with 200 as status code.
func NewCustomResponseWriter(rw http.ResponseWriter) *CustomResponseWriter {
func NewCustomResponseWriter(rw http.ResponseWriter, c net.Conn) *CustomResponseWriter {
return &CustomResponseWriter{
ResponseWriter: rw,
conn: c,
code: 200,
}
}
Expand Down Expand Up @@ -78,6 +84,18 @@ func (cw *CustomResponseWriter) Unwrap() http.ResponseWriter {
return cw.ResponseWriter
}

// SetWriteDeadline rewrites the underlying connection write deadline.
// This is called by http.ResponseController SetWriteDeadline method.
func (cw *CustomResponseWriter) SetWriteDeadline(t time.Time) error {
return cw.conn.SetWriteDeadline(t)
}

// SetReadDeadline rewrites the underlying connection read deadline.
// This is called by http.ResponseController SetReadDeadline method.
func (cw *CustomResponseWriter) SetReadDeadline(t time.Time) error {
return cw.conn.SetReadDeadline(t)
}

// APIError is the data model sent when an error occurred during request processing.
type APIError struct {
RequestID string `json:"requestid"`
Expand Down

0 comments on commit 30bb544

Please sign in to comment.