-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webserver): add UnaryHandler which returns a unary server handle…
…r for [http handler] --> [grpc server handler] without grpc client dials to grpc server. gRPC-Gateway does not support gRPC interceptors when call gRPC's service handler in process. See: grpc-ecosystem/grpc-gateway#1043
- Loading branch information
Showing
4 changed files
with
113 additions
and
53 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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 The searKing Author. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package webserver | ||
|
||
import ( | ||
"github.com/searKing/golang/pkg/webserver/pkg/stats" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
func (f *Factory) ServerOptions(opts ...grpc.ServerOption) []grpc.ServerOption { | ||
if f.fc.MaxReceiveMessageSizeInBytes > 0 { | ||
opts = append(opts, grpc.MaxRecvMsgSize(f.fc.MaxReceiveMessageSizeInBytes)) | ||
} else { | ||
opts = append(opts, grpc.MaxRecvMsgSize(defaultMaxReceiveMessageSize)) | ||
} | ||
if f.fc.StatsHandling { | ||
// log for the related stats handling (e.g., RPCs, connections). | ||
opts = append(opts, grpc.StatsHandler(&stats.ServerHandler{})) | ||
} | ||
return opts | ||
} | ||
|
||
func (f *Factory) DialOptions(opts ...grpc.DialOption) []grpc.DialOption { | ||
if f.fc.NoGrpcProxy { | ||
opts = append(opts, grpc.WithNoProxy()) | ||
} | ||
if !f.fc.ForceDisableTls { | ||
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
} | ||
if f.fc.MaxReceiveMessageSizeInBytes > 0 { | ||
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(f.fc.MaxReceiveMessageSizeInBytes), grpc.MaxCallSendMsgSize(f.fc.MaxReceiveMessageSizeInBytes))) | ||
} else { | ||
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaultMaxReceiveMessageSize), grpc.MaxCallSendMsgSize(defaultMaxSendMessageSize))) | ||
} | ||
if f.fc.StatsHandling { | ||
// log for the related stats handling (e.g., RPCs, connections). | ||
opts = append(opts, grpc.WithStatsHandler(&stats.ClientHandler{})) | ||
} | ||
|
||
return opts | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2024 The searKing Author. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package webserver | ||
|
||
import ( | ||
"github.com/rs/cors" | ||
"google.golang.org/grpc" | ||
|
||
http_ "github.com/searKing/golang/go/net/http" | ||
"github.com/searKing/golang/pkg/webserver/pkg/recovery" | ||
grpc_ "github.com/searKing/golang/third_party/google.golang.org/grpc" | ||
"github.com/searKing/golang/third_party/google.golang.org/grpc/interceptors/burstlimit" | ||
"github.com/searKing/golang/third_party/google.golang.org/grpc/interceptors/timeoutlimit" | ||
) | ||
|
||
// UnaryHandler returns a new unary server handler. | ||
// | ||
// gRPC-Gateway does not support gRPC interceptors when call gRPC's service handler in process. | ||
// See: https://github.com/grpc-ecosystem/grpc-gateway/issues/1043 | ||
func (f *Factory) UnaryHandler(handlers ...grpc_.UnaryHandlerDecorator) []grpc_.UnaryHandlerDecorator { | ||
// recover | ||
handlers = append(handlers, grpc_.UnaryHandlerDecoratorFunc(recovery.UnaryHandler)) | ||
return handlers | ||
} | ||
|
||
func (f *Factory) UnaryServerInterceptors(interceptors ...grpc.UnaryServerInterceptor) []grpc.UnaryServerInterceptor { | ||
// recover | ||
interceptors = append(interceptors, recovery.UnaryServerInterceptor()) | ||
// handle request timeout | ||
interceptors = append(interceptors, timeoutlimit.UnaryServerInterceptor(f.fc.HandledTimeoutUnary)) | ||
// burst limit | ||
interceptors = append(interceptors, burstlimit.UnaryServerInterceptor(f.fc.MaxConcurrencyUnary, f.fc.BurstLimitTimeoutUnary)) | ||
return interceptors | ||
} | ||
|
||
func (f *Factory) StreamServerInterceptors(interceptors ...grpc.StreamServerInterceptor) []grpc.StreamServerInterceptor { | ||
// recover | ||
interceptors = append(interceptors, recovery.StreamServerInterceptor()) | ||
// handle request timeout | ||
interceptors = append(interceptors, timeoutlimit.StreamServerInterceptor(f.fc.HandledTimeoutUnary)) | ||
// burst limit | ||
interceptors = append(interceptors, burstlimit.StreamServerInterceptor(f.fc.MaxConcurrencyUnary, f.fc.BurstLimitTimeoutUnary)) | ||
return interceptors | ||
} | ||
|
||
func (f *Factory) HttpServerInterceptors(decorators ...http_.HandlerDecorator) []http_.HandlerDecorator { | ||
// cors | ||
decorators = append(decorators, http_.HandlerDecoratorFunc(cors.New(f.fc.Cors).Handler)) | ||
return decorators | ||
} |