@@ -61,6 +61,7 @@ type PoolOptions struct {
6161 BatchMaxSize uint32
6262 BatchMaxSizeBytes uint32
6363 RequestMaxBytes uint64
64+ BatchTimeout time.Duration
6465 SubmitTimeout time.Duration
6566 FirstStrikeThreshold time.Duration
6667 SecondStrikeThreshold time.Duration
@@ -89,6 +90,17 @@ func (rp *Pool) start() {
8990 rp .pending .Start ()
9091}
9192
93+ func (rp * Pool ) addRandomnessToFirstStrike () time.Duration {
94+ if rp .options .BatchTimeout .Milliseconds () == 0 {
95+ return time .Duration (0 ) // no randomness
96+ }
97+ return time .Duration (randRange (int (2 * rp .options .BatchTimeout .Milliseconds ()), int (- 2 * rp .options .BatchTimeout .Milliseconds ()))) * time .Millisecond
98+ }
99+
100+ func randRange (max , min int ) int {
101+ return rand .Intn (max - min ) + min
102+ }
103+
92104func (rp * Pool ) createPendingStore () * PendingStore {
93105 return & PendingStore {
94106 Inspector : rp .inspector ,
@@ -98,11 +110,11 @@ func (rp *Pool) createPendingStore() *PendingStore {
98110 StartTime : time .Now (),
99111 Logger : rp .logger ,
100112 SecondStrikeThreshold : rp .options .SecondStrikeThreshold ,
101- FirstStrikeThreshold : rp .options .FirstStrikeThreshold ,
113+ FirstStrikeThreshold : rp .options .FirstStrikeThreshold + rp . addRandomnessToFirstStrike () ,
102114 OnDelete : func (key string ) {
103115 rp .semaphore .Release (1 )
104116 },
105- Epoch : time . Duration ( rand . Intn ( 50 )) * time . Millisecond + rp .options .FirstStrikeThreshold / 10 ,
117+ Epoch : rp .options .FirstStrikeThreshold / 10 ,
106118 FirstStrikeCallback : rp .striker .OnFirstStrikeTimeout ,
107119 SecondStrikeCallback : rp .striker .OnSecondStrikeTimeout ,
108120 }
0 commit comments