@@ -11,8 +11,15 @@ import (
1111type Pool [T any ] struct {
1212 countin int32
1313 countout int32
14- pooler Pooler [T ]
15- pool sync.Pool
14+ // 64 bit align
15+
16+ outlim int32
17+ inlim int32
18+ // 64 bit align
19+
20+ pool sync.Pool
21+ pooler Pooler [T ]
22+
1623 noputbak bool
1724}
1825
@@ -23,6 +30,9 @@ func NewPool[T any](pooler Pooler[T]) *Pool[T] {
2330 p .pool .New = func () any {
2431 return & Item [T ]{pool : p }
2532 }
33+ // default limit
34+ p .outlim = 4096
35+ p .inlim = 4096
2636 return p
2737}
2838
@@ -33,6 +43,24 @@ func (pool *Pool[T]) SetNoPutBack(on bool) {
3343 pool .noputbak = on
3444}
3545
46+ // LimitOutput will automatically set new item no-autodestroy
47+ // if countout > outlim.
48+ func (pool * Pool [T ]) LimitOutput (n int32 ) {
49+ if n <= 0 {
50+ panic ("n must > 0" )
51+ }
52+ pool .outlim = n
53+ }
54+
55+ // LimitInputwill automatically set new item no-autodestroy
56+ // if countout > inlim.
57+ func (pool * Pool [T ]) LimitInput (n int32 ) {
58+ if n <= 0 {
59+ panic ("n must > 0" )
60+ }
61+ pool .inlim = n
62+ }
63+
3664func (pool * Pool [T ]) incin () {
3765 atomic .AddInt32 (& pool .countin , 1 )
3866}
@@ -51,10 +79,17 @@ func (pool *Pool[T]) decout() {
5179
5280func (pool * Pool [T ]) newempty () * Item [T ] {
5381 item := pool .pool .Get ().(* Item [T ])
54- if item .stat .hasdestroyed () { // is recycled
82+ isrecycled := item .stat .hasdestroyed ()
83+ if isrecycled {
5584 pool .decin ()
5685 }
5786 item .stat = status (0 )
87+ isfull := atomic .LoadInt32 (& pool .countin ) > pool .inlim ||
88+ atomic .LoadInt32 (& pool .countout ) > pool .outlim
89+ if isfull {
90+ // no out log, no reuse
91+ return item
92+ }
5893 pool .incout ()
5994 return item .setautodestroy ()
6095}
@@ -66,7 +101,8 @@ func (pool *Pool[T]) put(item *Item[T]) {
66101
67102 item .stat .setdestroyed (true )
68103
69- if pool .noputbak {
104+ if pool .noputbak ||
105+ atomic .LoadInt32 (& pool .countin ) > pool .inlim {
70106 return
71107 }
72108 pool .pool .Put (item )
0 commit comments