- 
                Notifications
    You must be signed in to change notification settings 
- Fork 9
configure mem pool buckets rotation #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -9,6 +9,7 @@ package request | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "math/rand" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|  | @@ -61,6 +62,7 @@ type PoolOptions struct { | |
| BatchMaxSize uint32 | ||
| BatchMaxSizeBytes uint32 | ||
| RequestMaxBytes uint64 | ||
| BatchTimeout time.Duration | ||
| SubmitTimeout time.Duration | ||
| FirstStrikeThreshold time.Duration | ||
| SecondStrikeThreshold time.Duration | ||
|  | @@ -69,6 +71,11 @@ type PoolOptions struct { | |
|  | ||
| // NewPool constructs a new requests pool | ||
| func NewPool(logger types.Logger, inspector RequestInspector, options PoolOptions, striker Striker) *Pool { | ||
| if options.FirstStrikeThreshold < 2*options.BatchTimeout { | ||
| logger.Warnf("FirstStrikeThreshold should be at least 2*BatchTimeout") | ||
| return nil | ||
| } | ||
|  | ||
| rp := &Pool{ | ||
| logger: logger, | ||
| inspector: inspector, | ||
|  | @@ -90,21 +97,32 @@ func (rp *Pool) start() { | |
| rp.pending.Start() | ||
| } | ||
|  | ||
| func (rp *Pool) addRandomnessToFirstStrike() time.Duration { | ||
| if rp.options.BatchTimeout.Milliseconds() == 0 { | ||
| return time.Duration(0) // no randomness | ||
| } | ||
| return time.Duration(randRange(int(2*rp.options.BatchTimeout.Milliseconds()), int(-2*rp.options.BatchTimeout.Milliseconds()))) * time.Millisecond | ||
| } | ||
|  | ||
| func randRange(max, min int) int { | ||
| return rand.Intn(max-min) + min | ||
| } | ||
|  | ||
| func (rp *Pool) createPendingStore() *PendingStore { | ||
| return &PendingStore{ | ||
| Inspector: rp.inspector, | ||
| ReqIDGCInterval: rp.options.AutoRemoveTimeout / 4, | ||
| ReqIDLifetime: rp.options.AutoRemoveTimeout, | ||
| Time: time.NewTicker(time.Second).C, | ||
| Time: time.NewTicker(rp.options.FirstStrikeThreshold / 10).C, | ||
|         
                  tock-ibm marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| StartTime: time.Now(), | ||
| Logger: rp.logger, | ||
| SecondStrikeThreshold: rp.options.SecondStrikeThreshold, | ||
| FirstStrikeThreshold: rp.options.FirstStrikeThreshold, | ||
| FirstStrikeThreshold: rp.options.FirstStrikeThreshold + rp.addRandomnessToFirstStrike(), | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is adding randomness only once, when the pool is created. Wouldn't it make more sense to do it per bucket, or would that be too much? what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will be too much | ||
| OnDelete: func(key string) { | ||
| rp.semaphore.Release(1) | ||
| atomic.AddInt64(&rp.size, -1) | ||
| }, | ||
| Epoch: time.Second, | ||
| Epoch: rp.options.FirstStrikeThreshold / 10, | ||
| FirstStrikeCallback: rp.striker.OnFirstStrikeTimeout, | ||
| SecondStrikeCallback: rp.striker.OnSecondStrikeTimeout, | ||
| } | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets make sure that in the worse case the first strike is still > BatchTimeout. IE compare the BatchTimeout with the FirstStrikeThreshold. It seems to me the FirstStrikeThreshold should be at least 5*BatchTimeout or so...
Even without randomization, if FirstStrikeThreshold < BatchTimeout + network-delay we'll get constant forwarding. So some sanity checks are required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add checks
Currently the batch timeout is 500ms and we are running tests with first strike of 2s