You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The operation of "timeout" should be removed because it may lead to multiple handlers using the same gin.Context simultaneously, thus causing incorrect response results.
#67
Open
skyterra opened this issue
May 8, 2024
· 0 comments
// New wraps a handler and aborts the process of the handler if the timeout is reachedfuncNew(opts...Option) gin.HandlerFunc {
t:=&Timeout{
timeout: defaultTimeout,
handler: nil,
response: defaultResponse,
}
// Loop through each optionfor_, opt:=rangeopts {
ifopt==nil {
panic("timeout Option not be nil")
}
// Call the option giving the instantiatedopt(t)
}
ift.timeout<=0 {
returnt.handler
}
bufPool=&BufferPool{}
returnfunc(c*gin.Context) {
finish:=make(chanstruct{}, 1)
panicChan:=make(chaninterface{}, 1)
w:=c.Writerbuffer:=bufPool.Get()
tw:=NewWriter(w, buffer)
c.Writer=twbuffer.Reset()
gofunc() {
deferfunc() {
ifp:=recover(); p!=nil {
panicChan<-p
}
}()
t.handler(c)
finish<-struct{}{}
}()
select {
casep:=<-panicChan:
tw.FreeBuffer()
c.Writer=wpanic(p)
case<-finish:
c.Next()
tw.mu.Lock()
defertw.mu.Unlock()
dst:=tw.ResponseWriter.Header()
fork, vv:=rangetw.Header() {
dst[k] =vv
}
if_, err:=tw.ResponseWriter.Write(buffer.Bytes()); err!=nil {
panic(err)
}
tw.FreeBuffer()
bufPool.Put(buffer)
case<-time.After(t.timeout):
c.Abort()
tw.mu.Lock()
defertw.mu.Unlock()
tw.timeout=truetw.FreeBuffer()
bufPool.Put(buffer)
c.Writer=wt.response(c)
c.Writer=tw
}
}
}
case <-time.After(t.timeout): c.Abort()
If a timeout occurs and c.Abort() is called here, but the handler function has not completed execution (for example, waiting for database data to be read), it will still continue to execute. In such a case, when c.PureJSON() is called in the handler function, c may have already been used by subsequent requests.
so, i suggest remove timeout operation.
The text was updated successfully, but these errors were encountered:
case <-time.After(t.timeout): c.Abort()
If a timeout occurs and c.Abort() is called here, but the handler function has not completed execution (for example, waiting for database data to be read), it will still continue to execute. In such a case, when c.PureJSON() is called in the handler function, c may have already been used by subsequent requests.
so, i suggest remove timeout operation.
The text was updated successfully, but these errors were encountered: