From d084c9a4ffe407d03efc51f7f293dc10ab102d1a Mon Sep 17 00:00:00 2001 From: godcong Date: Fri, 20 Dec 2024 23:34:13 +0800 Subject: [PATCH] feat(service): add client and server options for grpc and http - Add ClientOptions and ServerOptions to Option struct in grpc and http packages - Implement WithClientOptions and WithServerOptions functions for grpc and http - Update client and server initialization to include new options - This change allows for more flexible configuration of grpc and http clients/servers --- service/grpc/client.go | 3 +++ service/grpc/option.go | 29 ++++++++++++++++++++++------- service/grpc/server.go | 3 +++ service/http/client.go | 4 +++- service/http/option.go | 28 +++++++++++++++++++++------- service/http/server.go | 3 +++ 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/service/grpc/client.go b/service/grpc/client.go index b1ad985..7ca201c 100644 --- a/service/grpc/client.go +++ b/service/grpc/client.go @@ -38,6 +38,9 @@ func NewClient(ctx context.Context, cfg *configv1.Service, ss ...OptionSetting) transgrpc.WithTimeout(timeout), transgrpc.WithMiddleware(option.Middlewares...), } + if len(option.ClientOptions) > 0 { + clientOptions = append(clientOptions, option.ClientOptions...) + } if option.Discovery != nil { endpoint := helpers.ServiceName(option.ServiceName) log.Debugf("grpc service [%s] discovery endpoint [%s]", option.ServiceName, endpoint) diff --git a/service/grpc/option.go b/service/grpc/option.go index 0f20b40..f5ac382 100644 --- a/service/grpc/option.go +++ b/service/grpc/option.go @@ -8,16 +8,19 @@ import ( "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/selector" + transgrpc "github.com/go-kratos/kratos/v2/transport/grpc" ) type Option struct { - Prefix string - HostIp string - ServiceName string - Discovery registry.Discovery - NodeFilters []selector.NodeFilter - Middlewares []middleware.Middleware - EndpointFunc func(scheme string, host string, addr string) (string, error) + Prefix string + HostIp string + ServiceName string + Discovery registry.Discovery + NodeFilters []selector.NodeFilter + Middlewares []middleware.Middleware + EndpointFunc func(scheme string, host string, addr string) (string, error) + ClientOptions []transgrpc.ClientOption + ServerOptions []transgrpc.ServerOption } type OptionSetting = func(o *Option) @@ -56,3 +59,15 @@ func WithHostIp(hostIp string) OptionSetting { o.HostIp = hostIp } } + +func WithClientOptions(opts ...transgrpc.ClientOption) OptionSetting { + return func(o *Option) { + o.ClientOptions = append(o.ClientOptions, opts...) + } +} + +func WithServerOptions(opts ...transgrpc.ServerOption) OptionSetting { + return func(o *Option) { + o.ServerOptions = append(o.ServerOptions, opts...) + } +} diff --git a/service/grpc/server.go b/service/grpc/server.go index 15991a0..0a6ef04 100644 --- a/service/grpc/server.go +++ b/service/grpc/server.go @@ -35,6 +35,9 @@ func NewServer(cfg *configv1.Service, ss ...OptionSetting) (*transgrpc.Server, e serverOptions := []transgrpc.ServerOption{ transgrpc.Middleware(option.Middlewares...), } + if len(option.ServerOptions) > 0 { + serverOptions = append(serverOptions, option.ServerOptions...) + } if serviceGrpc := cfg.GetGrpc(); serviceGrpc != nil { if serviceGrpc.Network != "" { serverOptions = append(serverOptions, transgrpc.Network(serviceGrpc.Network)) diff --git a/service/http/client.go b/service/http/client.go index 7d83213..0213118 100644 --- a/service/http/client.go +++ b/service/http/client.go @@ -37,7 +37,9 @@ func NewClient(ctx context.Context, cfg *configv1.Service, ss ...OptionSetting) transhttp.WithTimeout(timeout), transhttp.WithMiddleware(option.Middlewares...), } - + if len(option.ClientOptions) > 0 { + clientOptions = append(clientOptions, option.ClientOptions...) + } if option.Discovery != nil { endpoint := helpers.ServiceName(option.ServiceName) log.Debugf("http service [%s] discovery endpoint [%s]", option.ServiceName, endpoint) diff --git a/service/http/option.go b/service/http/option.go index 2e9f8d5..efe5809 100644 --- a/service/http/option.go +++ b/service/http/option.go @@ -8,16 +8,19 @@ import ( "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/selector" + transhttp "github.com/go-kratos/kratos/v2/transport/http" ) type Option struct { - Prefix string - HostIp string - ServiceName string - Discovery registry.Discovery - NodeFilters []selector.NodeFilter - Middlewares []middleware.Middleware - EndpointFunc func(scheme string, host string, addr string) (string, error) + Prefix string + HostIp string + ServiceName string + Discovery registry.Discovery + NodeFilters []selector.NodeFilter + Middlewares []middleware.Middleware + EndpointFunc func(scheme string, host string, addr string) (string, error) + ClientOptions []transhttp.ClientOption + ServerOptions []transhttp.ServerOption } type OptionSetting = func(o *Option) @@ -57,3 +60,14 @@ func WithHostIp(hostIp string) OptionSetting { o.HostIp = hostIp } } + +func WithClientOptions(options ...transhttp.ClientOption) OptionSetting { + return func(o *Option) { + o.ClientOptions = append(o.ClientOptions, options...) + } +} +func WithServerOptions(options ...transhttp.ServerOption) OptionSetting { + return func(o *Option) { + o.ServerOptions = append(o.ServerOptions, options...) + } +} diff --git a/service/http/server.go b/service/http/server.go index 419f447..bfd6c06 100644 --- a/service/http/server.go +++ b/service/http/server.go @@ -36,6 +36,9 @@ func NewServer(cfg *configv1.Service, ss ...OptionSetting) (*transhttp.Server, e serverOptions := []transhttp.ServerOption{ transhttp.Middleware(option.Middlewares...), } + if len(option.ServerOptions) > 0 { + serverOptions = append(serverOptions, option.ServerOptions...) + } if serviceHttp := cfg.GetHttp(); serviceHttp != nil { if serviceHttp.Network != "" { serverOptions = append(serverOptions, transhttp.Network(serviceHttp.Network))