Skip to content

Commit

Permalink
feat(service): add client and server options for grpc and http
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
godcong committed Dec 20, 2024
1 parent 37cef51 commit d084c9a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
3 changes: 3 additions & 0 deletions service/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 22 additions & 7 deletions service/grpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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...)
}
}
3 changes: 3 additions & 0 deletions service/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion service/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 21 additions & 7 deletions service/http/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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...)
}
}
3 changes: 3 additions & 0 deletions service/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit d084c9a

Please sign in to comment.