-
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.
refactor(webserver): remove connection and interceptor related codes …
…into webserver.connection.go and webserver.interceptors.go.
- Loading branch information
Showing
3 changed files
with
105 additions
and
48 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 |
---|---|---|
@@ -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,47 @@ | ||
// 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 ( | ||
"context" | ||
|
||
"github.com/rs/cors" | ||
http_ "github.com/searKing/golang/go/net/http" | ||
"github.com/searKing/golang/pkg/webserver/pkg/recovery" | ||
"github.com/searKing/golang/third_party/google.golang.org/grpc/interceptors/burstlimit" | ||
"github.com/searKing/golang/third_party/google.golang.org/grpc/interceptors/timeoutlimit" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func LocalGatewayWrap[REQ any, RESP any](handler func(ctx context.Context, req REQ) (RESP, error)) func( | ||
ctx context.Context, req REQ) (RESP, error) { | ||
return recovery.WrapRecovery(handler) | ||
} | ||
|
||
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 | ||
} |