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

Fix context error in grpc #962

Merged
merged 7 commits into from
Aug 28, 2021
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
13 changes: 0 additions & 13 deletions zrpc/internal/codes/accept.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package codes

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -12,17 +10,6 @@ func Acceptable(err error) bool {
switch status.Code(err) {
case codes.DeadlineExceeded, codes.Internal, codes.Unavailable, codes.DataLoss:
return false
case codes.Unknown:
return acceptableUnknown(err)
default:
return true
}
}

func acceptableUnknown(err error) bool {
switch err {
case context.DeadlineExceeded:
return false
default:
return true
}
Expand Down
1 change: 1 addition & 0 deletions zrpc/internal/codes/accept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestAccept(t *testing.T) {

tests := []struct {
name string
err error
Expand Down
11 changes: 10 additions & 1 deletion zrpc/internal/serverinterceptors/timeoutinterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package serverinterceptors
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"runtime/debug"
"strings"
"sync"
Expand Down Expand Up @@ -46,7 +48,14 @@ func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor
defer lock.Unlock()
return resp, err
case <-ctx.Done():
return nil, ctx.Err()
err := ctx.Err()

if err == context.Canceled {
err = status.Error(codes.Canceled, err.Error())
} else if err == context.DeadlineExceeded {
err = status.Error(codes.DeadlineExceeded, err.Error())
}
return nil, err
}
}
}
23 changes: 22 additions & 1 deletion zrpc/internal/serverinterceptors/timeoutinterceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package serverinterceptors

import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -66,5 +68,24 @@ func TestUnaryTimeoutInterceptor_timeoutExpire(t *testing.T) {
return nil, nil
})
wg.Wait()
assert.Equal(t, context.DeadlineExceeded, err)
assert.EqualValues(t, status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error()), err)
}
func TestUnaryTimeoutInterceptor_cancel(t *testing.T) {
const timeout = time.Minute * 10
interceptor := UnaryTimeoutInterceptor(timeout)
ctx, cancel := context.WithCancel(context.Background())
cancel()

var wg sync.WaitGroup
wg.Add(1)
_, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
FullMethod: "/",
}, func(ctx context.Context, req interface{}) (interface{}, error) {
defer wg.Done()
time.Sleep(time.Millisecond * 50)
return nil, nil
})

wg.Wait()
assert.EqualValues(t, status.Error(codes.Canceled, context.Canceled.Error()), err)
}