@@ -2,6 +2,7 @@ package http2
22
33import (
44 "bytes"
5+ "context"
56 "encoding/binary"
67 "fmt"
78 "io"
@@ -179,7 +180,7 @@ func skipHeader(frameData []byte) ([]byte, uint32) {
179180 return frameData [5 :], length
180181}
181182
182- func readSplitData (rBody io.ReadCloser ) chan * bytes.Buffer {
183+ func readSplitData (ctx context. Context , rBody io.ReadCloser ) chan * bytes.Buffer {
183184 cbm := make (chan * bytes.Buffer )
184185 go func () {
185186 buf := make ([]byte , 4098 ) // todo configurable
@@ -224,7 +225,14 @@ func readSplitData(rBody io.ReadCloser) chan *bytes.Buffer {
224225 if err != nil {
225226 fmt .Printf ("read SplitedDatas error = %v\n " , err )
226227 }
227- cbm <- bytes .NewBuffer (allDataBody )
228+ select {
229+ case <- ctx .Done ():
230+ close (cbm )
231+ return
232+ default :
233+ cbm <- bytes .NewBuffer (allDataBody )
234+ }
235+
228236 // temp data is sent, and reset wanting data size
229237 fromFrameHeaderDataSize = 0
230238 }
@@ -236,7 +244,15 @@ func readSplitData(rBody io.ReadCloser) chan *bytes.Buffer {
236244
237245func (s * Server ) http2HandleFunction (wi http.ResponseWriter , r * http.Request ) {
238246 // body data from http
239- bodyCh := readSplitData (r .Body )
247+ ctx , cancel := context .WithCancel (context .Background ())
248+ bodyCh := readSplitData (ctx , r .Body )
249+ defer func () {
250+ cancel ()
251+ select {
252+ case <- bodyCh :
253+ default :
254+ }
255+ }()
240256 sendChan := make (chan * bytes.Buffer )
241257 ctrlChan := make (chan http.Header )
242258 errChan := make (chan interface {})
0 commit comments