Skip to content

Commit 490d4b0

Browse files
🧹 chore: Extracted generic releasePooledBinder function (#3841)
Co-authored-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com>
1 parent 4564c10 commit 490d4b0

1 file changed

Lines changed: 17 additions & 49 deletions

File tree

bind.go

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func ReleaseBind(b *Bind) {
5353
bindPool.Put(b)
5454
}
5555

56+
// releasePooledBinder resets a binder and returns it to its pool.
57+
// It should be used with defer to ensure proper cleanup of pooled binders.
58+
func releasePooledBinder[T interface{ Reset() }](pool *sync.Pool, bind T) {
59+
bind.Reset()
60+
binder.PutToThePool(pool, bind)
61+
}
62+
5663
func (b *Bind) release() {
5764
b.ctx = nil
5865
b.dontHandleErrs = true
@@ -119,11 +126,7 @@ func (b *Bind) Header(out any) error {
119126
bind := binder.GetFromThePool[*binder.HeaderBinding](&binder.HeaderBinderPool)
120127
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
121128

122-
// Reset & put binder
123-
defer func() {
124-
bind.Reset()
125-
binder.PutToThePool(&binder.HeaderBinderPool, bind)
126-
}()
129+
defer releasePooledBinder(&binder.HeaderBinderPool, bind)
127130

128131
if err := b.returnErr(bind.Bind(b.ctx.Request(), out)); err != nil {
129132
return err
@@ -137,11 +140,7 @@ func (b *Bind) RespHeader(out any) error {
137140
bind := binder.GetFromThePool[*binder.RespHeaderBinding](&binder.RespHeaderBinderPool)
138141
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
139142

140-
// Reset & put binder
141-
defer func() {
142-
bind.Reset()
143-
binder.PutToThePool(&binder.RespHeaderBinderPool, bind)
144-
}()
143+
defer releasePooledBinder(&binder.RespHeaderBinderPool, bind)
145144

146145
if err := b.returnErr(bind.Bind(b.ctx.Response(), out)); err != nil {
147146
return err
@@ -156,11 +155,7 @@ func (b *Bind) Cookie(out any) error {
156155
bind := binder.GetFromThePool[*binder.CookieBinding](&binder.CookieBinderPool)
157156
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
158157

159-
// Reset & put binder
160-
defer func() {
161-
bind.Reset()
162-
binder.PutToThePool(&binder.CookieBinderPool, bind)
163-
}()
158+
defer releasePooledBinder(&binder.CookieBinderPool, bind)
164159

165160
if err := b.returnErr(bind.Bind(&b.ctx.RequestCtx().Request, out)); err != nil {
166161
return err
@@ -174,11 +169,7 @@ func (b *Bind) Query(out any) error {
174169
bind := binder.GetFromThePool[*binder.QueryBinding](&binder.QueryBinderPool)
175170
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
176171

177-
// Reset & put binder
178-
defer func() {
179-
bind.Reset()
180-
binder.PutToThePool(&binder.QueryBinderPool, bind)
181-
}()
172+
defer releasePooledBinder(&binder.QueryBinderPool, bind)
182173

183174
if err := b.returnErr(bind.Bind(&b.ctx.RequestCtx().Request, out)); err != nil {
184175
return err
@@ -192,11 +183,7 @@ func (b *Bind) JSON(out any) error {
192183
bind := binder.GetFromThePool[*binder.JSONBinding](&binder.JSONBinderPool)
193184
bind.JSONDecoder = b.ctx.App().Config().JSONDecoder
194185

195-
// Reset & put binder
196-
defer func() {
197-
bind.Reset()
198-
binder.PutToThePool(&binder.JSONBinderPool, bind)
199-
}()
186+
defer releasePooledBinder(&binder.JSONBinderPool, bind)
200187

201188
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
202189
return err
@@ -210,11 +197,7 @@ func (b *Bind) CBOR(out any) error {
210197
bind := binder.GetFromThePool[*binder.CBORBinding](&binder.CBORBinderPool)
211198
bind.CBORDecoder = b.ctx.App().Config().CBORDecoder
212199

213-
// Reset & put binder
214-
defer func() {
215-
bind.Reset()
216-
binder.PutToThePool(&binder.CBORBinderPool, bind)
217-
}()
200+
defer releasePooledBinder(&binder.CBORBinderPool, bind)
218201

219202
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
220203
return err
@@ -227,11 +210,7 @@ func (b *Bind) XML(out any) error {
227210
bind := binder.GetFromThePool[*binder.XMLBinding](&binder.XMLBinderPool)
228211
bind.XMLDecoder = b.ctx.App().config.XMLDecoder
229212

230-
// Reset & put binder
231-
defer func() {
232-
bind.Reset()
233-
binder.PutToThePool(&binder.XMLBinderPool, bind)
234-
}()
213+
defer releasePooledBinder(&binder.XMLBinderPool, bind)
235214

236215
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
237216
return err
@@ -248,11 +227,7 @@ func (b *Bind) Form(out any) error {
248227
bind := binder.GetFromThePool[*binder.FormBinding](&binder.FormBinderPool)
249228
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
250229

251-
// Reset & put binder
252-
defer func() {
253-
bind.Reset()
254-
binder.PutToThePool(&binder.FormBinderPool, bind)
255-
}()
230+
defer releasePooledBinder(&binder.FormBinderPool, bind)
256231

257232
if err := b.returnErr(bind.Bind(&b.ctx.RequestCtx().Request, out)); err != nil {
258233
return err
@@ -265,10 +240,7 @@ func (b *Bind) Form(out any) error {
265240
func (b *Bind) URI(out any) error {
266241
bind := binder.GetFromThePool[*binder.URIBinding](&binder.URIBinderPool)
267242

268-
// Reset & put binder
269-
defer func() {
270-
binder.PutToThePool(&binder.URIBinderPool, bind)
271-
}()
243+
defer releasePooledBinder(&binder.URIBinderPool, bind)
272244

273245
if err := b.returnErr(bind.Bind(b.ctx.Route().Params, b.ctx.Params, out)); err != nil {
274246
return err
@@ -282,11 +254,7 @@ func (b *Bind) MsgPack(out any) error {
282254
bind := binder.GetFromThePool[*binder.MsgPackBinding](&binder.MsgPackBinderPool)
283255
bind.MsgPackDecoder = b.ctx.App().Config().MsgPackDecoder
284256

285-
// Reset & put binder
286-
defer func() {
287-
bind.Reset()
288-
binder.PutToThePool(&binder.MsgPackBinderPool, bind)
289-
}()
257+
defer releasePooledBinder(&binder.MsgPackBinderPool, bind)
290258

291259
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
292260
return err

0 commit comments

Comments
 (0)