|
9 | 9 | // Default size of the pool.
|
10 | 10 | defaultPoolSize = 64
|
11 | 11 |
|
12 |
| - // Default release factor |
| 12 | + // Default release factor. |
13 | 13 | defaultReleaseFactor float32 = 0
|
14 |
| - // and precision. |
15 |
| - defaultReleaseFactorBase uint32 = 100 |
16 | 14 |
|
17 | 15 | // Pool status code.
|
18 | 16 | stateNil = 0
|
@@ -41,7 +39,7 @@ type Pool struct {
|
41 | 39 | // * RF == 0.05
|
42 | 40 | // * RF base == 100
|
43 | 41 | // , means that 5% of items will be drop on the floor.
|
44 |
| - ReleaseFactorBase uint32 |
| 42 | + rfBase uint32 |
45 | 43 | // Function to make new object if pool didn't deliver existing.
|
46 | 44 | New func() interface{}
|
47 | 45 | // Internal storage and status flag.
|
@@ -75,8 +73,13 @@ func (p *Pool) initPool() {
|
75 | 73 | if p.ReleaseFactor > 1.0 {
|
76 | 74 | p.ReleaseFactor = 1.0
|
77 | 75 | }
|
78 |
| - if p.ReleaseFactorBase == 0 { |
79 |
| - p.ReleaseFactorBase = defaultReleaseFactorBase |
| 76 | + if p.rfBase == 0 { |
| 77 | + p.rfBase = 1 |
| 78 | + } |
| 79 | + if p.ReleaseFactor > defaultReleaseFactor && p.ReleaseFactor < 1 { |
| 80 | + for float32(p.rfBase)*p.ReleaseFactor < 1 { |
| 81 | + p.rfBase *= 10 |
| 82 | + } |
80 | 83 | }
|
81 | 84 |
|
82 | 85 | // Check size and init the storage.
|
@@ -113,12 +116,12 @@ func (p *Pool) Get() interface{} {
|
113 | 116 | // Put adds x to the pool.
|
114 | 117 | func (p *Pool) Put(x Releaser) bool {
|
115 | 118 | // Check release factor first.
|
116 |
| - if p.ReleaseFactor > 0 && p.ReleaseFactorBase > 0 { |
| 119 | + if p.ReleaseFactor > 0 && p.rfBase > 0 { |
117 | 120 | rfc := atomic.AddUint32(&p.rfCounter, 1)
|
118 |
| - if rfc >= p.ReleaseFactorBase { |
| 121 | + if rfc >= p.rfBase { |
119 | 122 | // Release factor counter limit reached, reset it.
|
120 | 123 | atomic.StoreUint32(&p.rfCounter, 0)
|
121 |
| - } else if float32(rfc)/float32(p.ReleaseFactorBase) <= p.ReleaseFactor { |
| 124 | + } else if float32(rfc)/float32(p.rfBase) <= p.ReleaseFactor { |
122 | 125 | // Drop x on the floor.
|
123 | 126 | x.Release()
|
124 | 127 | return false
|
|
0 commit comments