-
Notifications
You must be signed in to change notification settings - Fork 368
[Issue 1371][producer] Put buffer back in pool only when safe #1372
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3f43f7
Put buffer back in pool only when safe
Gilthoniel a49656f
Recycle a buffer in several async routines
Gilthoniel 48818d8
Fix backoff constructor and tests
Gilthoniel e88d02a
Cleanup writeRequests in connection
Gilthoniel ee826bc
Adapt buffer pool to recycle buffers only when safe
Gilthoniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,8 @@ | |
| *.out | ||
| coverage.html | ||
|
|
||
| perf/perf | ||
| pulsar-perf | ||
| bin | ||
|
|
||
| perf/perf/ | ||
| pulsar-perf/ | ||
| bin/ | ||
| logs/ | ||
| vendor/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,7 @@ type ConnectionListener interface { | |
| type Connection interface { | ||
| SendRequest(requestID uint64, req *pb.BaseCommand, callback func(*pb.BaseCommand, error)) | ||
| SendRequestNoWait(req *pb.BaseCommand) error | ||
| WriteData(ctx context.Context, data Buffer) | ||
| WriteData(ctx context.Context, data *SharedBuffer) | ||
| RegisterListener(id uint64, listener ConnectionListener) error | ||
| UnregisterListener(id uint64) | ||
| AddConsumeHandler(id uint64, handler ConsumerHandler) error | ||
|
|
@@ -132,7 +132,7 @@ type request struct { | |
|
|
||
| type dataRequest struct { | ||
| ctx context.Context | ||
| data Buffer | ||
| data *SharedBuffer | ||
| } | ||
|
|
||
| type connection struct { | ||
|
|
@@ -150,6 +150,7 @@ type connection struct { | |
| logicalAddr *url.URL | ||
| physicalAddr *url.URL | ||
|
|
||
| bufferPool BufferPool | ||
| writeBufferLock sync.Mutex | ||
| writeBuffer Buffer | ||
| reader *connectionReader | ||
|
|
@@ -199,6 +200,7 @@ func newConnection(opts connectionOptions) *connection { | |
| keepAliveInterval: opts.keepAliveInterval, | ||
| logicalAddr: opts.logicalAddr, | ||
| physicalAddr: opts.physicalAddr, | ||
| bufferPool: GetDefaultBufferPool(), | ||
| writeBuffer: NewBuffer(4096), | ||
| log: opts.logger.SubLogger(log.Fields{"remote_addr": opts.physicalAddr}), | ||
| pendingReqs: make(map[uint64]*request), | ||
|
|
@@ -394,6 +396,18 @@ func (c *connection) failLeftRequestsWhenClose() { | |
| } | ||
| } | ||
|
|
||
| func (c *connection) drainWriteRequests() { | ||
| for { | ||
| select { | ||
| case req := <-c.writeRequestsCh: | ||
| c.bufferPool.Put(req.data) | ||
|
|
||
| default: | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (c *connection) run() { | ||
| pingSendTicker := time.NewTicker(c.keepAliveInterval) | ||
| pingCheckTicker := time.NewTicker(c.keepAliveInterval) | ||
|
|
@@ -421,6 +435,7 @@ func (c *connection) run() { | |
| select { | ||
| case <-c.closeCh: | ||
| c.failLeftRequestsWhenClose() | ||
| c.drainWriteRequests() | ||
| return | ||
| case req := <-c.incomingRequestsCh: | ||
| if req == nil { | ||
|
|
@@ -433,6 +448,9 @@ func (c *connection) run() { | |
| } | ||
| c.internalWriteData(req.ctx, req.data) | ||
|
|
||
| // Write request is fully processed so we can release the buffer. | ||
| c.bufferPool.Put(req.data) | ||
|
|
||
| case <-pingSendTicker.C: | ||
| c.sendPing() | ||
| } | ||
|
|
@@ -456,10 +474,19 @@ func (c *connection) runPingCheck(pingCheckTicker *time.Ticker) { | |
| } | ||
| } | ||
|
|
||
| func (c *connection) WriteData(ctx context.Context, data Buffer) { | ||
| func (c *connection) WriteData(ctx context.Context, data *SharedBuffer) { | ||
| written := false | ||
| defer func() { | ||
| if !written { | ||
| // Buffer has failed to be written and can now be reused. | ||
| c.bufferPool.Put(data) | ||
|
Member
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. Can we add a new method like |
||
| } | ||
| }() | ||
|
|
||
| select { | ||
| case c.writeRequestsCh <- &dataRequest{ctx: ctx, data: data}: | ||
| // Channel is not full | ||
| written = true | ||
| return | ||
| case <-ctx.Done(): | ||
| c.log.Debug("Write data context cancelled") | ||
|
|
@@ -472,6 +499,7 @@ func (c *connection) WriteData(ctx context.Context, data Buffer) { | |
| select { | ||
| case c.writeRequestsCh <- &dataRequest{ctx: ctx, data: data}: | ||
| // Successfully wrote on the channel | ||
| written = true | ||
| return | ||
| case <-ctx.Done(): | ||
| c.log.Debug("Write data context cancelled") | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If:
Why not just use the ref count here?