summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuxuan 'fishy' Wang <yuxuan.wang@reddit.com>2022-05-19 11:09:01 -0700
committerYuxuan 'fishy' Wang <fishywang@gmail.com>2022-05-21 10:08:32 -0700
commit162cbb4894548ac4d016f8e63a15f994b17c4dba (patch)
treef28429e31a2fd9c6c40be4790170a9f2d3bf1565
parentc4e96c75758d94a79dae925b1daae0c950823057 (diff)
downloadthrift-162cbb4894548ac4d016f8e63a15f994b17c4dba.tar.gz
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.
-rw-r--r--lib/go/thrift/simple_server.go36
-rw-r--r--lib/go/thrift/simple_server_test.go12
2 files changed, 37 insertions, 11 deletions
diff --git a/lib/go/thrift/simple_server.go b/lib/go/thrift/simple_server.go
index 1cfc375dd..31dfa1e6d 100644
--- a/lib/go/thrift/simple_server.go
+++ b/lib/go/thrift/simple_server.go
@@ -29,17 +29,6 @@ import (
"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 @@ func (p *TSimpleServer) processRequests(client TTransport) (err error) {
}
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 b92d50f01..e0cf151b9 100644
--- a/lib/go/thrift/simple_server_test.go
+++ b/lib/go/thrift/simple_server_test.go
@@ -287,3 +287,15 @@ func TestStopTimeoutWithSocketTimeout(t *testing.T) {
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")
+ }
+}