-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update GrpcInflightMethodLimiter to support interceptors
- Loading branch information
1 parent
50cd2e2
commit ac300a1
Showing
2 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"strings" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/stats" | ||
"google.golang.org/grpc/tap" | ||
|
@@ -19,6 +20,11 @@ type GrpcInflightMethodLimiter interface { | |
// otherwise gRPC-server implementation-specific error will be returned to the client (codes.PermissionDenied in [email protected]). | ||
RPCCallStarting(ctx context.Context, methodName string, md metadata.MD) (context.Context, error) | ||
|
||
// RPCCallProcessing is called by a server interceptor, allowing request pre-processing or blocking to be performed. | ||
// handler should propagate the req. | ||
RPCCallProcessing(ctx context.Context, methodName string) (func(error), error) | ||
|
||
// RPCCallFinished is called when an RPC call is finished being handled. | ||
RPCCallFinished(ctx context.Context) | ||
} | ||
|
||
|
@@ -47,6 +53,31 @@ func (g *grpcInflightLimitCheck) TapHandle(ctx context.Context, info *tap.Info) | |
return g.methodLimiter.RPCCallStarting(ctx, info.FullMethodName, info.Header) | ||
} | ||
|
||
func (g *grpcInflightLimitCheck) UnaryServerInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { | ||
finish, err := g.methodLimiter.RPCCallProcessing(ctx, info.FullMethod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result, err := handler(ctx, req) | ||
if finish != nil { | ||
finish(err) | ||
} | ||
return result, err | ||
|
||
} | ||
|
||
func (g *grpcInflightLimitCheck) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
finish, err := g.methodLimiter.RPCCallProcessing(ss.Context(), info.FullMethod) | ||
if err != nil { | ||
return err | ||
} | ||
err = handler(srv, ss) | ||
if finish != nil { | ||
finish(err) | ||
} | ||
return err | ||
} | ||
|
||
func (g *grpcInflightLimitCheck) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context { | ||
return ctx | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters