Skip to content

Commit 80d3632

Browse files
author
vcarvajal
committed
Pass postbody directly instead of using io.Reader to avoid extra copies.
1 parent 82e5412 commit 80d3632

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

module.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sigsci
33
import (
44
"bytes"
55
"fmt"
6-
"io"
76
"io/ioutil"
87
"log"
98
"net"
@@ -226,13 +225,7 @@ func (m *Module) inspectorPreRequest(req *http.Request) (inspin2 RPCMsgIn2, out
226225
req.Body = ioutil.NopCloser(bytes.NewBuffer(reqbody))
227226
}
228227

229-
inspin, err := NewRPCMsgInWithModuleConfig(m.config, req, bytes.NewReader(reqbody))
230-
if err != nil {
231-
if m.config.Debug() {
232-
log.Printf("DEBUG: PreRequest call error (%s %s): %s", req.Method, req.RequestURI, err)
233-
}
234-
return
235-
}
228+
inspin := NewRPCMsgInWithModuleConfig(m.config, req, reqbody)
236229
m.extractHeaders(req, inspin)
237230

238231
if m.config.Debug() {
@@ -381,7 +374,7 @@ func NewRPCMsgIn(r *http.Request, postbody []byte, code int, size int64, dur tim
381374
// NewRPCMsgInWithModuleConfig creates a message from a ModuleConfig object
382375
// End-users of the golang module never need to use this
383376
// directly and it is only exposed for performance testing
384-
func NewRPCMsgInWithModuleConfig(mcfg *ModuleConfig, r *http.Request, postbody io.Reader) (*RPCMsgIn, error) {
377+
func NewRPCMsgInWithModuleConfig(mcfg *ModuleConfig, r *http.Request, postbody []byte) *RPCMsgIn {
385378

386379
now := time.Now()
387380

@@ -403,11 +396,6 @@ func NewRPCMsgInWithModuleConfig(mcfg *ModuleConfig, r *http.Request, postbody i
403396
hin = append([][2]string{{"Host", r.Host}}, hin...)
404397
}
405398

406-
body, err := ioutil.ReadAll(postbody)
407-
if err != nil {
408-
return nil, err
409-
}
410-
411399
return &RPCMsgIn{
412400
ModuleVersion: mcfg.ModuleIdentifier(),
413401
ServerVersion: mcfg.ServerIdentifier(),
@@ -425,9 +413,9 @@ func NewRPCMsgInWithModuleConfig(mcfg *ModuleConfig, r *http.Request, postbody i
425413
ResponseCode: -1,
426414
ResponseMillis: 0,
427415
ResponseSize: -1,
428-
PostBody: string(body),
416+
PostBody: string(postbody),
429417
HeadersIn: hin,
430-
}, nil
418+
}
431419
}
432420

433421
// stripPort removes any port from an address (e.g., the client port from the RemoteAddr)

0 commit comments

Comments
 (0)