-
Notifications
You must be signed in to change notification settings - Fork 2
/
nighthawk.go
53 lines (43 loc) · 1.17 KB
/
nighthawk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package nighthawk
import (
"fmt"
"google.golang.org/grpc"
"github.com/khulnasoft/meshplay/meshkit/utils"
nighthawk_client "github.com/khulnasoft/meshplay/getnighthawk/pkg/proto"
)
// Options argument for customizing the client
type Options struct {
ServerHost string
ServerPort int32
}
// Client holds the nighthawk client information
type Client struct {
Handler nighthawk_client.NighthawkServiceClient
connection *grpc.ClientConn
}
// New creates a new instance of the nighthawk client connection
func New(opts Options) (*Client, error) {
if !utils.TcpCheck(&utils.HostPort{
Address: opts.ServerHost,
Port: opts.ServerPort,
}, nil) {
return nil, ErrInvalidEndpoint
}
var dialOptions []grpc.DialOption
dialOptions = append(dialOptions, grpc.WithInsecure())
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", opts.ServerHost, opts.ServerPort), dialOptions...)
if err != nil {
return nil, ErrGRPCDial(err)
}
return &Client{
Handler: nighthawk_client.NewNighthawkServiceClient(conn),
connection: conn,
}, nil
}
// Close closes the client connection
func (c *Client) Close() error {
if c.connection != nil {
return c.connection.Close()
}
return nil
}