Channel shutdown process improvements: shutdown error added as an own… #496
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.
Problem:
Ambiguous behavior on Channel shutdown. If you consume using a Channel you cannot get the shutdown cause even if you use NotifyClose chan listener. Delivery chan could be selected (closed) earlier than close listener chan.
Scenario:
Let's assume you have a connection and few consumer channels using it.
The requirement is to restart the consumer job on any unexpected error... on unexpected errors/shutdowns only.
When your connection closes from outside (unexpected error or gracefully closes from parallel goroutine) you have to handle a shutdown cause distinctly with no doubt.
Look at this sample pseudo-code first:
In this scenario you cannot guarantee that closeCh will be selected early than delivCh.
PR proposal:
To store a shutdown cause error into a Channel's field shutdownErr upon shutdown start and provide a getter method to get it any time at any place during shutdown process.
New instance of Channel has default value for shutdownErr with "unexpected shutdown" Reason to provide a small guard for the future. It will not affect current behaviour because it's being set upon shutdown start, but if someday somebody will try to close the chans bypassing shutdown() func it will return a distinct error.