-
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.
Merge pull request #27 from ultravioletrs/NOISSUE-grpcclient
NOISSUE - Add GRPC Client
- Loading branch information
Showing
5 changed files
with
140 additions
and
57 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
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,3 @@ | ||
// Package clients contains the domain concept definitions needed to support | ||
// Agent Client functionality. | ||
package clients |
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,17 @@ | ||
package grpc | ||
|
||
import ( | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/ultravioletrs/agent/agent" | ||
agentapi "github.com/ultravioletrs/agent/agent/api/grpc" | ||
) | ||
|
||
// NewClient creates new agent gRPC client instance. | ||
func NewClient(cfg Config) (Client, agent.AgentServiceClient, error) { | ||
client, err := newClient(cfg) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return client, agentapi.NewClient(opentracing.NoopTracer{}, client.Connection(), cfg.Timeout), nil | ||
} |
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,99 @@ | ||
package grpc | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/mainflux/mainflux/pkg/errors" | ||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" | ||
gogrpc "google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
var ( | ||
errGrpcConnect = errors.New("failed to connect to grpc server") | ||
errGrpcClose = errors.New("failed to close grpc connection") | ||
) | ||
|
||
type Config struct { | ||
ClientTLS bool `env:"CLIENT_TLS" envDefault:"false"` | ||
CACerts string `env:"CA_CERTS" envDefault:""` | ||
URL string `env:"URL" envDefault:"localhost:7002"` | ||
Timeout time.Duration `env:"TIMEOUT" envDefault:"1s"` | ||
} | ||
|
||
type Client interface { | ||
// Close closes gRPC connection. | ||
Close() error | ||
|
||
// Secure is used for pretty printing TLS info. | ||
Secure() string | ||
|
||
// Connection returns the gRPC connection. | ||
Connection() *gogrpc.ClientConn | ||
} | ||
|
||
type client struct { | ||
*gogrpc.ClientConn | ||
cfg Config | ||
secure bool | ||
} | ||
|
||
var _ Client = (*client)(nil) | ||
|
||
func newClient(cfg Config) (Client, error) { | ||
conn, secure, err := connect(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &client{ | ||
ClientConn: conn, | ||
cfg: cfg, | ||
secure: secure, | ||
}, nil | ||
} | ||
|
||
func (c *client) Close() error { | ||
if err := c.ClientConn.Close(); err != nil { | ||
return errors.Wrap(errGrpcClose, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) Secure() string { | ||
if c.secure { | ||
return "with TLS" | ||
} | ||
return "without TLS" | ||
} | ||
|
||
func (c *client) Connection() *gogrpc.ClientConn { | ||
return c.ClientConn | ||
} | ||
|
||
// connect creates new gRPC client and connect to gRPC server. | ||
func connect(cfg Config) (*gogrpc.ClientConn, bool, error) { | ||
var opts []gogrpc.DialOption | ||
secure := false | ||
tc := insecure.NewCredentials() | ||
|
||
if cfg.ClientTLS && cfg.CACerts != "" { | ||
var err error | ||
tc, err = credentials.NewClientTLSFromFile(cfg.CACerts, "") | ||
if err != nil { | ||
return nil, secure, err | ||
} | ||
secure = true | ||
} | ||
|
||
opts = append(opts, gogrpc.WithTransportCredentials(tc), gogrpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor())) | ||
|
||
conn, err := gogrpc.Dial(cfg.URL, opts...) | ||
if err != nil { | ||
return nil, secure, errors.Wrap(errGrpcConnect, err) | ||
} | ||
|
||
return conn, secure, nil | ||
} |
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,3 @@ | ||
// Package grpc contains the domain concept definitions needed to support | ||
// Agent Client grpc functionality. | ||
package grpc |