Skip to content
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

Return Request context cancelled error #93

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions graphsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ const (
RequestCancelled = ResponseStatusCode(35)
)

// RequestContextCancelledErr is an error message received on the error channel when the request context given by the user is cancelled/times out
type RequestContextCancelledErr struct{}

func (e RequestContextCancelledErr) Error() string {
return "Request Context Cancelled"
}

// RequestFailedBusyErr is an error message received on the error channel when the peer is busy
type RequestFailedBusyErr struct{}

Expand Down
7 changes: 6 additions & 1 deletion requestmanager/requestmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,13 @@ func TestCancelRequestInProgress(t *testing.T) {

testutil.VerifyEmptyResponse(requestCtx, t, returnedResponseChan1)
td.blockChain.VerifyWholeChain(requestCtx, returnedResponseChan2)
testutil.VerifyEmptyErrors(requestCtx, t, returnedErrorChan1)

testutil.VerifyEmptyErrors(requestCtx, t, returnedErrorChan2)

errors := testutil.CollectErrors(requestCtx, t, returnedErrorChan1)
require.Len(t, errors, 1)
_, ok := errors[0].(graphsync.RequestContextCancelledErr)
require.True(t, ok)
}

func TestCancelManagerExitsGracefully(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions requestmanager/responsecollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,24 @@ func (rc *responseCollector) collectResponses(
case <-rc.ctx.Done():
return
case <-requestCtx.Done():
select {
case <-rc.ctx.Done():
case returnedErrors <- graphsync.RequestContextCancelledErr{}:
}
return
case err, ok := <-incomingErrors:
if !ok {
incomingErrors = nil
// even if the `incomingErrors` channel is closed without any error,
// the context could still have timed out in which case we need to inform the caller of the same.
select {
case <-requestCtx.Done():
select {
case <-rc.ctx.Done():
case returnedErrors <- graphsync.RequestContextCancelledErr{}:
}
default:
}
} else {
receivedErrors = append(receivedErrors, err)
}
Expand Down