Minor tweak to ErrAbandonRequest in go library
Client: go
Make it unwrap to context.Canceled, since the main use case of it is to
be returned in lieu of context.Canceled to avoid extra writing to the
client, so that if user has any processor middleware that checks for
context.Canceled error those would still work.
diff --git a/lib/go/thrift/simple_server.go b/lib/go/thrift/simple_server.go
index 1cfc375..31dfa1e 100644
--- a/lib/go/thrift/simple_server.go
+++ b/lib/go/thrift/simple_server.go
@@ -29,17 +29,6 @@
"time"
)
-// ErrAbandonRequest is a special error server handler implementations can
-// return to indicate that the request has been abandoned.
-//
-// TSimpleServer will check for this error, and close the client connection
-// instead of writing the response/error back to the client.
-//
-// It shall only be used when the server handler implementation know that the
-// client already abandoned the request (by checking that the passed in context
-// is already canceled, for example).
-var ErrAbandonRequest = errors.New("request abandoned")
-
// ServerConnectivityCheckInterval defines the ticker interval used by
// connectivity check in thrift compiled TProcessorFunc implementations.
//
@@ -380,3 +369,28 @@
}
return nil
}
+
+// ErrAbandonRequest is a special error that server handler implementations can
+// return to indicate that the request has been abandoned.
+//
+// TSimpleServer and compiler generated Process functions will check for this
+// error, and close the client connection instead of trying to write the error
+// back to the client.
+//
+// It shall only be used when the server handler implementation know that the
+// client already abandoned the request (by checking that the passed in context
+// is already canceled, for example).
+//
+// It also implements the interface defined by errors.Unwrap and always unwrap
+// to context.Canceled error.
+var ErrAbandonRequest = abandonRequestError{}
+
+type abandonRequestError struct{}
+
+func (abandonRequestError) Error() string {
+ return "request abandoned"
+}
+
+func (abandonRequestError) Unwrap() error {
+ return context.Canceled
+}
diff --git a/lib/go/thrift/simple_server_test.go b/lib/go/thrift/simple_server_test.go
index b92d50f..e0cf151 100644
--- a/lib/go/thrift/simple_server_test.go
+++ b/lib/go/thrift/simple_server_test.go
@@ -287,3 +287,15 @@
t.Fatalf("error when stop server:%v", err)
}
}
+
+func TestErrAbandonRequest(t *testing.T) {
+ if !errors.Is(ErrAbandonRequest, ErrAbandonRequest) {
+ t.Error("errors.Is(ErrAbandonRequest, ErrAbandonRequest) returned false")
+ }
+ if !errors.Is(ErrAbandonRequest, context.Canceled) {
+ t.Error("errors.Is(ErrAbandonRequest, context.Canceled) returned false")
+ }
+ if errors.Is(context.Canceled, ErrAbandonRequest) {
+ t.Error("errors.Is(context.Canceled, ErrAbandonRequest) returned true")
+ }
+}