From cfff0f3b46e18d1af7f7b4e9a2a9dad41e451f86 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Sat, 25 Jul 2020 00:00:34 +0800 Subject: [PATCH 1/2] fix memory leak of request context --- context/input.go | 32 ++++++++++++++++++++++++++++++-- context/output.go | 6 ++++++ router.go | 4 ++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/context/input.go b/context/input.go index 7b522c36..da066a86 100644 --- a/context/input.go +++ b/context/input.go @@ -323,8 +323,36 @@ func (input *BeegoInput) SetParam(key, val string) { // This function is used to clear parameters so they may be reset between filter // passes. func (input *BeegoInput) ResetParams() { - input.pnames = input.pnames[:0] - input.pvalues = input.pvalues[:0] + if len(input.pnames) > 0 { + input.pnames = input.pnames[:0] + } + if len(input.pvalues) > 0 { + input.pvalues = input.pvalues[:0] + } +} + +// ResetData: reset data +func (input *BeegoInput) ResetData() { + input.dataLock.Lock() + if input.data != nil { + input.data = nil + } + input.dataLock.Unlock() +} + +// ResetBody: reset body +func (input *BeegoInput) ResetBody() { + if len(input.RequestBody) > 0 { + input.RequestBody = []byte{} + } +} + +// Clear: clear all data in input +func (input *BeegoInput) Clear() { + input.ResetParams() + input.ResetData() + input.ResetBody() + } // Query returns input data item string by a given string. diff --git a/context/output.go b/context/output.go index 238dcf45..eaa75720 100644 --- a/context/output.go +++ b/context/output.go @@ -50,9 +50,15 @@ func NewOutput() *BeegoOutput { // Reset init BeegoOutput func (output *BeegoOutput) Reset(ctx *Context) { output.Context = ctx + output.Clear() +} + +// Clear: clear all data in output +func (output *BeegoOutput) Clear() { output.Status = 0 } + // Header sets response header item string via given key. func (output *BeegoOutput) Header(key, val string) { output.Context.ResponseWriter.Header().Set(key, val) diff --git a/router.go b/router.go index a993a1af..af0a7ceb 100644 --- a/router.go +++ b/router.go @@ -319,6 +319,10 @@ func (p *ControllerRegister) GetContext() *beecontext.Context { // GiveBackContext put the ctx into pool so that it could be reuse func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) { + // clear input cached data + ctx.Input.Clear() + // clear output cached data + ctx.Output.Clear() p.pool.Put(ctx) } From 2386c9c80d1d38f44f23c134c748810a9f1add0a Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Sun, 26 Jul 2020 22:37:42 +0800 Subject: [PATCH 2/2] delete useless if-stmt --- context/input.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/context/input.go b/context/input.go index da066a86..fb01648f 100644 --- a/context/input.go +++ b/context/input.go @@ -323,28 +323,20 @@ func (input *BeegoInput) SetParam(key, val string) { // This function is used to clear parameters so they may be reset between filter // passes. func (input *BeegoInput) ResetParams() { - if len(input.pnames) > 0 { - input.pnames = input.pnames[:0] - } - if len(input.pvalues) > 0 { - input.pvalues = input.pvalues[:0] - } + input.pnames = input.pnames[:0] + input.pvalues = input.pvalues[:0] } // ResetData: reset data func (input *BeegoInput) ResetData() { input.dataLock.Lock() - if input.data != nil { - input.data = nil - } + input.data = nil input.dataLock.Unlock() } // ResetBody: reset body func (input *BeegoInput) ResetBody() { - if len(input.RequestBody) > 0 { - input.RequestBody = []byte{} - } + input.RequestBody = []byte{} } // Clear: clear all data in input