File tree Expand file tree Collapse file tree 2 files changed +58
-42
lines changed Expand file tree Collapse file tree 2 files changed +58
-42
lines changed Original file line number Diff line number Diff line change 1+ package transport
2+
3+ import "sync"
4+
5+ var http2BufPool = sync.Pool {
6+ New : func () interface {} {
7+ return make ([]byte , DefaultBufSizeH2 )
8+ },
9+ }
10+
11+ func getHTTP2BufPool () * sync.Pool {
12+ return & http2BufPool
13+ }
Original file line number Diff line number Diff line change @@ -165,48 +165,51 @@ func (h *httpTransportSocket) recvHTTP1(msg *Message) error {
165165}
166166
167167func (h * httpTransportSocket ) recvHTTP2 (msg * Message ) error {
168- // only process if the socket is open
169- select {
170- case <- h .closed :
171- return io .EOF
172- default :
173- }
174-
175- // read streaming body
176-
177- // set max buffer size
178- s := h .ht .opts .BuffSizeH2
179- if s == 0 {
180- s = DefaultBufSizeH2
181- }
182-
183- buf := make ([]byte , s )
184-
185- // read the request body
186- n , err := h .buf .Read (buf )
187- // not an eof error
188- if err != nil {
189- return err
190- }
191-
192- // check if we have data
193- if n > 0 {
194- msg .Body = buf [:n ]
195- }
196-
197- // set headers
198- for k , v := range h .r .Header {
199- if len (v ) > 0 {
200- msg .Header [k ] = v [0 ]
201- } else {
202- msg .Header [k ] = ""
203- }
204- }
205-
206- // set path
207- msg .Header [":path" ] = h .r .URL .Path
208-
209- return nil
168+ // only process if the socket is open
169+ select {
170+ case <- h .closed :
171+ return io .EOF
172+ default :
173+ }
174+
175+ // buffer pool for reuse
176+ var bufPool = getHTTP2BufPool ()
177+
178+ // set max buffer size
179+ s := h .ht .opts .BuffSizeH2
180+ if s == 0 {
181+ s = DefaultBufSizeH2
182+ }
183+
184+ buf := bufPool .Get ().([]byte )
185+ if cap (buf ) < s {
186+ buf = make ([]byte , s )
187+ }
188+ buf = buf [:s ]
189+
190+ n , err := h .buf .Read (buf )
191+ if err != nil {
192+ bufPool .Put (buf )
193+ return err
194+ }
195+
196+ if n > 0 {
197+ msg .Body = make ([]byte , n )
198+ copy (msg .Body , buf [:n ])
199+ }
200+ bufPool .Put (buf )
201+
202+ for k , v := range h .r .Header {
203+ if len (v ) > 0 {
204+ msg .Header [k ] = v [0 ]
205+ } else {
206+ msg .Header [k ] = ""
207+ }
208+ }
209+
210+ msg .Header [":path" ] = h .r .URL .Path
211+
212+ return nil
210213}
211214
212215func (h * httpTransportSocket ) sendHTTP1 (msg * Message ) error {
You can’t perform that action at this time.
0 commit comments