mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 01:02:29 +00:00
service and client implements last grpc server and client interfaces, improved errors, add signals handling
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"strings"
|
||||
@ -13,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
Dial(name string, version string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
|
||||
grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func New(opts ...Option) (Client, error) {
|
||||
@ -26,28 +27,37 @@ func New(opts ...Option) (Client, error) {
|
||||
}
|
||||
resolver.Register(c.opts.registry.ResolverBuilder())
|
||||
c.pool = newPool(DefaultPoolSize, DefaultPoolTTL, DefaultPoolMaxIdle, DefaultPoolMaxStreams)
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type client struct {
|
||||
pool *pool
|
||||
opts *options
|
||||
}
|
||||
|
||||
func (c client) Dial(name, version string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||
if c.opts.tlsConfig == nil && c.opts.Secure() {
|
||||
c.opts.tlsConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
if c.opts.tlsConfig != nil {
|
||||
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(c.opts.tlsConfig)))
|
||||
c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithTransportCredentials(credentials.NewTLS(c.opts.tlsConfig)))
|
||||
}
|
||||
addr := fmt.Sprintf("%s:///%s", c.opts.registry.String(), name)
|
||||
if version != "" {
|
||||
addr = addr + ":" + strings.TrimSpace(version)
|
||||
c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name)
|
||||
if c.opts.version != "" {
|
||||
c.addr = c.addr + ":" + strings.TrimSpace(c.opts.version)
|
||||
}
|
||||
pc, err := c.pool.getConn(addr, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pc.ClientConn, nil
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type client struct {
|
||||
addr string
|
||||
pool *pool
|
||||
opts *options
|
||||
}
|
||||
|
||||
func (c *client) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
|
||||
pc, err := c.pool.getConn(c.addr, c.opts.dialOptions...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return pc.Invoke(ctx, method, args, reply, opts...)
|
||||
}
|
||||
|
||||
func (c *client) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
pc, err := c.pool.getConn(c.addr, c.opts.dialOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pc.NewStream(ctx, desc, method, opts...)
|
||||
}
|
||||
|
@ -3,13 +3,17 @@ package client
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"gitlab.bertha.cloud/partitio/lab/grpc/registry"
|
||||
)
|
||||
|
||||
type Options interface {
|
||||
Name() string
|
||||
Version() string
|
||||
Registry() registry.Registry
|
||||
TLSConfig() *tls.Config
|
||||
DialOptions() []grpc.DialOption
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
@ -20,6 +24,12 @@ func WithRegistry(registry registry.Registry) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithName(name string) Option {
|
||||
return func(o *options) {
|
||||
o.name = name
|
||||
}
|
||||
}
|
||||
|
||||
func WithVersion(version string) Option {
|
||||
return func(o *options) {
|
||||
o.version = version
|
||||
@ -38,11 +48,23 @@ func WithSecure(s bool) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithDialOptions(opts ...grpc.DialOption) Option {
|
||||
return func(o *options) {
|
||||
o.dialOptions = opts
|
||||
}
|
||||
}
|
||||
|
||||
type options struct {
|
||||
registry registry.Registry
|
||||
version string
|
||||
tlsConfig *tls.Config
|
||||
secure bool
|
||||
registry registry.Registry
|
||||
name string
|
||||
version string
|
||||
tlsConfig *tls.Config
|
||||
secure bool
|
||||
dialOptions []grpc.DialOption
|
||||
}
|
||||
|
||||
func (o *options) Name() string {
|
||||
return o.name
|
||||
}
|
||||
|
||||
func (o *options) Version() string {
|
||||
@ -61,3 +83,6 @@ func (o *options) Secure() bool {
|
||||
return o.secure
|
||||
}
|
||||
|
||||
func (o *options) DialOptions() []grpc.DialOption {
|
||||
return o.dialOptions
|
||||
}
|
||||
|
Reference in New Issue
Block a user