2020-11-08 18:28:33 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-11-23 14:15:47 +00:00
|
|
|
"go.linka.cloud/grpc/interceptors"
|
2021-07-17 20:40:06 +00:00
|
|
|
"go.linka.cloud/grpc/registry"
|
2020-11-08 18:28:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Options interface {
|
2020-11-27 13:00:45 +00:00
|
|
|
Name() string
|
2020-11-08 18:28:33 +00:00
|
|
|
Version() string
|
|
|
|
Registry() registry.Registry
|
|
|
|
TLSConfig() *tls.Config
|
2020-11-27 13:00:45 +00:00
|
|
|
DialOptions() []grpc.DialOption
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*options)
|
|
|
|
|
|
|
|
func WithRegistry(registry registry.Registry) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.registry = registry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
func WithName(name string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func WithVersion(version string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.version = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 13:04:47 +00:00
|
|
|
func WithAddress(address string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.addr = address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
func WithTLSConfig(conf *tls.Config) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.tlsConfig = conf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSecure(s bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.secure = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
func WithDialOptions(opts ...grpc.DialOption) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.dialOptions = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:15:47 +00:00
|
|
|
func WithInterceptors(i ...interceptors.ClientInterceptors) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
for _, v := range i {
|
|
|
|
o.unaryInterceptors = append(o.unaryInterceptors, v.UnaryClientInterceptor())
|
|
|
|
o.streamInterceptors = append(o.streamInterceptors, v.StreamClientInterceptor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUnaryInterceptors(i ...grpc.UnaryClientInterceptor) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.unaryInterceptors = append(o.unaryInterceptors, i...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithStreamInterceptors(i ...grpc.StreamClientInterceptor) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.streamInterceptors = append(o.streamInterceptors, i...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:28:33 +00:00
|
|
|
type options struct {
|
2020-11-27 13:00:45 +00:00
|
|
|
registry registry.Registry
|
|
|
|
name string
|
|
|
|
version string
|
2021-11-22 13:04:47 +00:00
|
|
|
addr string
|
2020-11-27 13:00:45 +00:00
|
|
|
tlsConfig *tls.Config
|
|
|
|
secure bool
|
|
|
|
dialOptions []grpc.DialOption
|
2021-11-23 14:15:47 +00:00
|
|
|
|
|
|
|
unaryInterceptors []grpc.UnaryClientInterceptor
|
|
|
|
streamInterceptors []grpc.StreamClientInterceptor
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Name() string {
|
|
|
|
return o.name
|
2020-11-08 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Version() string {
|
|
|
|
return o.version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Registry() registry.Registry {
|
|
|
|
return o.registry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) TLSConfig() *tls.Config {
|
|
|
|
return o.tlsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) Secure() bool {
|
|
|
|
return o.secure
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
func (o *options) DialOptions() []grpc.DialOption {
|
|
|
|
return o.dialOptions
|
|
|
|
}
|