Skip to content

Commit

Permalink
GRPC Client wrapper
Browse files Browse the repository at this point in the history
AB#8170
  • Loading branch information
eccles committed May 17, 2024
1 parent d6436b6 commit a0b3d52
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
69 changes: 69 additions & 0 deletions grpcclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package grpcclient

import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type Client struct {
name string
log Logger
address string
conn *grpc.ClientConn
options []grpc.DialOption
}

func (g *Client) Open() error {
if g.conn != nil {
return nil
}

var err error
var conn *grpc.ClientConn

g.log.Debugf("Open %s client at %v", g.name, g.address)
conn, err = grpc.Dial(g.address, g.options...)
if err != nil {
return err
}
g.conn = conn
g.log.Debugf("Open %s client successful", g.name)
return nil
}

func (g *Client) Close() {
if g.conn != nil {
g.log.Debugf("Close %s client at %v", g.name, g.address)
g.conn.Close()
g.conn = nil
}
}

func (g *Client) String() string {
return g.name
}

func (g *Client) Connector() *ClientConn {
return g.conn
}

type ClientOption func(*Client)

func WithDialOptions(d ...DialOption) ClientOption {
return func(t *Client) {
t.options = append(t.options, d...)
}
}

func New(log Logger, name string, address string, opts ...ClientOption) *Client {
t := Client{
name: name,
address: address,
log: log.WithIndex("grpcclient", name),
options: []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())},
}
for _, opt := range opts {
opt(&t)
}
return &t
}
9 changes: 9 additions & 0 deletions grpcclient/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package grpcclient

import (
"google.golang.org/grpc"
)

type UnaryClientInterceptor = grpc.UnaryClientInterceptor
type DialOption = grpc.DialOption
type ClientConn = grpc.ClientConn
7 changes: 7 additions & 0 deletions grpcclient/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package grpcclient

type ClientProvider interface {
Open() error
Close()
String() string
}
7 changes: 7 additions & 0 deletions grpcclient/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package grpcclient

import (
"github.com/datatrails/go-datatrails-common/logger"
)

type Logger = logger.Logger
7 changes: 3 additions & 4 deletions restproxyserver/restproxyserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const (
Expand Down Expand Up @@ -159,9 +158,9 @@ func WithAppendedDialOption(d DialOption) RESTProxyServerOption {
}

// WithPrependedDialOption prepends a grpc dial option.
func WithPrependedDialOption(d DialOption) RESTProxyServerOption {
func WithPrependedDialOptions(d ...DialOption) RESTProxyServerOption {
return func(g *RESTProxyServer) {
g.dialOptions = append([]DialOption{d}, g.dialOptions...)
g.dialOptions = append(d, g.dialOptions...)
}
}

Expand All @@ -186,7 +185,7 @@ func New(log Logger, name string, port string, opts ...RESTProxyServerOption) RE
g := RESTProxyServer{
name: strings.ToLower(name),
port: port,
dialOptions: []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())},
dialOptions: []grpc.DialOption{},
options: []runtime.ServeMuxOption{},
filePaths: []filePath{},
handlers: []HandleChainFunc{},
Expand Down

0 comments on commit a0b3d52

Please sign in to comment.