-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AB#8170
- Loading branch information
Showing
5 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package grpcclient | ||
|
||
type ClientProvider interface { | ||
Open() error | ||
Close() | ||
String() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters