@@ -2,18 +2,16 @@ package client
22
33import (
44 "context"
5- "fmt "
5+ "net/http "
66 "time"
77
8+ "connectrpc.com/connect"
89 gen "github.com/devzero-inc/oda/gen/api/v1"
910 "github.com/devzero-inc/oda/logging"
1011
1112 "github.com/rs/zerolog"
12- "google.golang.org/grpc"
13- "google.golang.org/grpc/connectivity"
14- "google.golang.org/grpc/credentials"
15- "google.golang.org/grpc/credentials/insecure"
16- "google.golang.org/grpc/keepalive"
13+
14+ genConnect "github.com/devzero-inc/oda/gen/api/v1/genconnect"
1715)
1816
1917// Config holds configuration for the client connection.
@@ -26,7 +24,7 @@ type Config struct {
2624
2725// Client is a struct that holds the connection to the server
2826type Client struct {
29- conn * grpc. ClientConn
27+ client genConnect. CollectorServiceClient
3028 logger * zerolog.Logger
3129 timeout time.Duration
3230 config Config
@@ -51,68 +49,14 @@ func NewClient(config Config) (*Client, error) {
5149
5250// connect handles connection establishment and configuration
5351func (c * Client ) connect () error {
54- var opts []grpc.DialOption
55-
56- // Setup connection security based on config
57- creds := grpc .WithTransportCredentials (insecure .NewCredentials ())
58- if c .config .SecureConnection {
59- if c .config .CertFile != "" {
60- tlsFromFile , err := credentials .NewClientTLSFromFile (c .config .CertFile , "" )
61- if err != nil {
62- return fmt .Errorf ("failed to create TLS credentials: %w" , err )
63- }
64- creds = grpc .WithTransportCredentials (tlsFromFile )
65- } else {
66- creds = grpc .WithTransportCredentials (credentials .NewClientTLSFromCert (nil , "" ))
67- }
68- }
69- opts = append (opts , creds )
70-
71- // Adding keepalive parameters to manage connection health
72- keepAliveParams := grpc .WithKeepaliveParams (keepalive.ClientParameters {
73- Time : 10 * time .Second , // Ping the server every 10 seconds to keep the connection alive
74- Timeout : 5 * time .Second , // Wait 5 seconds for a pong before closing the connection
75- PermitWithoutStream : true , // Send pings even without active RPCs
76- })
77- opts = append (opts , keepAliveParams )
78-
79- // Dial the server
80- conn , err := grpc .Dial (c .config .Address , opts ... )
81- if err != nil {
82- return fmt .Errorf ("failed to connect to server: %w" , err )
83- }
84-
85- // Set the connection on the client
86- c .conn = conn
87- return nil
88- }
89-
90- // Reconnect attempts to reconnect if the connection is down
91- func (c * Client ) Reconnect () error {
92- if c .conn != nil {
93- c .Close ()
94- }
95- return c .connect ()
96- }
52+ c .client = genConnect .NewCollectorServiceClient (http .DefaultClient , c .config .Address , connect .WithGRPC ())
9753
98- // CheckAndReconnect checks connection health and reconnects if necessary
99- func (c * Client ) CheckAndReconnect () error {
100- if c .conn .GetState () == connectivity .TransientFailure || c .conn .GetState () == connectivity .Shutdown {
101- c .logger .Warn ().Msg ("Connection lost. Attempting to reconnect..." )
102- return c .Reconnect ()
103- }
10454 return nil
10555}
10656
10757// SendCommands sends a list of commands to the server
10858func (c * Client ) SendCommands (commands []* gen.Command , auth * gen.Auth ) error {
10959
110- if err := c .CheckAndReconnect (); err != nil {
111- return fmt .Errorf ("failed to reconnect: %w" , err )
112- }
113-
114- client := gen .NewCollectorServiceClient (c .conn )
115-
11660 req := & gen.SendCommandsRequest {
11761 Commands : commands ,
11862 Auth : auth ,
@@ -121,7 +65,7 @@ func (c *Client) SendCommands(commands []*gen.Command, auth *gen.Auth) error {
12165 ctx , cancel := context .WithTimeout (context .Background (), c .timeout )
12266 defer cancel ()
12367
124- _ , err := client .SendCommands (ctx , req )
68+ _ , err := c . client .SendCommands (ctx , connect . NewRequest ( req ) )
12569 if err != nil {
12670 c .logger .Error ().Err (err ).Msg ("Failed to send commands" )
12771 }
@@ -132,12 +76,6 @@ func (c *Client) SendCommands(commands []*gen.Command, auth *gen.Auth) error {
13276// SendProcesses sends a list of processes to the server
13377func (c * Client ) SendProcesses (processes []* gen.Process , auth * gen.Auth ) error {
13478
135- if err := c .CheckAndReconnect (); err != nil {
136- return fmt .Errorf ("failed to reconnect: %w" , err )
137- }
138-
139- client := gen .NewCollectorServiceClient (c .conn )
140-
14179 req := & gen.SendProcessesRequest {
14280 Processes : processes ,
14381 Auth : auth ,
@@ -146,22 +84,10 @@ func (c *Client) SendProcesses(processes []*gen.Process, auth *gen.Auth) error {
14684 ctx , cancel := context .WithTimeout (context .Background (), c .timeout )
14785 defer cancel ()
14886
149- _ , err := client .SendProcesses (ctx , req )
87+ _ , err := c . client .SendProcesses (ctx , connect . NewRequest ( req ) )
15088 if err != nil {
15189 c .logger .Error ().Err (err ).Msg ("Failed to send processes" )
15290 }
15391
15492 return err
15593}
156-
157- // Close closes the connection to the server
158- func (c * Client ) Close () {
159- if c .conn != nil {
160- err := c .conn .Close ()
161- if err != nil {
162- c .logger .Error ().Err (err ).Msg ("Failed to close the connection" )
163- } else {
164- c .logger .Info ().Msg ("Connection closed successfully" )
165- }
166- }
167- }
0 commit comments