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

Tech Debt Cleanup and Docs Update #219

Merged
merged 8 commits into from
Sep 24, 2021
15 changes: 6 additions & 9 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,23 @@ import (

// IsTerminalSuccessCode returns true if the response code indicates the
// request terminated successfully.
// DEPRECATED: use status.IsSuccess()
func IsTerminalSuccessCode(status graphsync.ResponseStatusCode) bool {
return status == graphsync.RequestCompletedFull ||
status == graphsync.RequestCompletedPartial
return status.IsSuccess()
}

// IsTerminalFailureCode returns true if the response code indicates the
// request terminated in failure.
// DEPRECATED: use status.IsFailure()
func IsTerminalFailureCode(status graphsync.ResponseStatusCode) bool {
return status == graphsync.RequestFailedBusy ||
status == graphsync.RequestFailedContentNotFound ||
status == graphsync.RequestFailedLegal ||
status == graphsync.RequestFailedUnknown ||
status == graphsync.RequestCancelled ||
status == graphsync.RequestRejected
return status.IsFailure()
}

// IsTerminalResponseCode returns true if the response code signals
// the end of the request
// DEPRECATED: use status.IsTerminal()
func IsTerminalResponseCode(status graphsync.ResponseStatusCode) bool {
return IsTerminalSuccessCode(status) || IsTerminalFailureCode(status)
return status.IsTerminal()
}

// Exportable is an interface that can serialize to a protobuf
Expand Down
25 changes: 4 additions & 21 deletions requestmanager/requestmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func (rm *RequestManager) processExtensionsForResponse(p peer.ID, response gsmsg
if !ok {
return false
}
responseError := rm.generateResponseErrorFromStatus(graphsync.RequestFailedUnknown)
responseError := graphsync.RequestFailedUnknown.AsError()
select {
case requestStatus.terminalError <- responseError:
default:
Expand All @@ -541,10 +541,10 @@ func (rm *RequestManager) processExtensionsForResponse(p peer.ID, response gsmsg

func (rm *RequestManager) processTerminations(responses []gsmsg.GraphSyncResponse) {
for _, response := range responses {
if gsmsg.IsTerminalResponseCode(response.Status()) {
if gsmsg.IsTerminalFailureCode(response.Status()) {
if response.Status().IsTerminal() {
if response.Status().IsFailure() {
requestStatus := rm.inProgressRequestStatuses[response.RequestID()]
responseError := rm.generateResponseErrorFromStatus(response.Status())
responseError := response.Status().AsError()
select {
case requestStatus.terminalError <- responseError:
default:
Expand All @@ -556,23 +556,6 @@ func (rm *RequestManager) processTerminations(responses []gsmsg.GraphSyncRespons
}
}

func (rm *RequestManager) generateResponseErrorFromStatus(status graphsync.ResponseStatusCode) error {
switch status {
case graphsync.RequestFailedBusy:
return graphsync.RequestFailedBusyErr{}
case graphsync.RequestFailedContentNotFound:
return graphsync.RequestFailedContentNotFoundErr{}
case graphsync.RequestFailedLegal:
return graphsync.RequestFailedLegalErr{}
case graphsync.RequestFailedUnknown:
return graphsync.RequestFailedUnknownErr{}
case graphsync.RequestCancelled:
return graphsync.RequestCancelledErr{}
default:
return fmt.Errorf("Unknown")
}
}

func (rm *RequestManager) processBlockHooks(p peer.ID, response graphsync.ResponseData, block graphsync.BlockData) error {
result := rm.blockHooks.ProcessBlockHooks(p, response, block)
if len(result.Extensions) > 0 {
Expand Down
44 changes: 44 additions & 0 deletions responsecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,47 @@ var ResponseCodeToName = map[ResponseStatusCode]string{
RequestFailedContentNotFound: "RequestFailedContentNotFound",
RequestCancelled: "RequestCancelled",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you're refactoring, you could automate this table and String method with https://pkg.go.dev/golang.org/x/tools/cmd/stringer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omg where was this tool all my life! You should check over at go-fil-markets and go-data-transfer for some pretty awesome comedy here.

}

// AsError generates an error from the status code for a failing status
func (c ResponseStatusCode) AsError() error {
if c.IsSuccess() {
return nil
}
switch c {
case RequestFailedBusy:
return RequestFailedBusyErr{}
case RequestFailedContentNotFound:
return RequestFailedContentNotFoundErr{}
case RequestFailedLegal:
return RequestFailedLegalErr{}
case RequestFailedUnknown:
return RequestFailedUnknownErr{}
case RequestCancelled:
return RequestCancelledErr{}
default:
return fmt.Errorf("Unknown")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return fmt.Errorf("unknown response status code: %d", c)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

}
}

// IsSuccess returns true if the response code indicates the
// request terminated successfully.
func (c ResponseStatusCode) IsSuccess() bool {
return c == RequestCompletedFull || c == RequestCompletedPartial
}

// IsFailure returns true if the response code indicates the
// request terminated in failure.
func (c ResponseStatusCode) IsFailure() bool {
return c == RequestFailedBusy ||
c == RequestFailedContentNotFound ||
c == RequestFailedLegal ||
c == RequestFailedUnknown ||
c == RequestCancelled ||
c == RequestRejected
}

// IsTerminal returns true if the response code signals
// the end of the request
func (c ResponseStatusCode) IsTerminal() bool {
return c.IsSuccess() || c.IsFailure()
}