| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | /* |
| Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "reflect" |
| 22 | "strings" |
| 23 | "sync" |
| 24 | "time" |
| 25 | |
| 26 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 27 | grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing" |
| Abhay Kumar | 685507d | 2025-10-06 09:04:09 +0000 | [diff] [blame^] | 28 | grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 29 | "github.com/jhump/protoreflect/dynamic/grpcdynamic" |
| 30 | "github.com/jhump/protoreflect/grpcreflect" |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 32 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 33 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 34 | "github.com/opencord/voltha-protos/v5/go/common" |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 35 | "github.com/opencord/voltha-protos/v5/go/core_service" |
| 36 | "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service" |
| 37 | "github.com/opencord/voltha-protos/v5/go/onu_inter_adapter_service" |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 38 | "google.golang.org/grpc" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 39 | "google.golang.org/grpc/codes" |
| 40 | rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" |
| 41 | "google.golang.org/grpc/status" |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 42 | ) |
| 43 | |
| 44 | type event byte |
| 45 | type state byte |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 46 | type GetServiceClient func(context.Context, *grpc.ClientConn) interface{} |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 47 | type RestartedHandler func(ctx context.Context, endPoint string) error |
| 48 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 49 | const ( |
| 50 | grpcBackoffInitialInterval = "GRPC_BACKOFF_INITIAL_INTERVAL" |
| 51 | grpcBackoffMaxInterval = "GRPC_BACKOFF_MAX_INTERVAL" |
| 52 | grpcBackoffMaxElapsedTime = "GRPC_BACKOFF_MAX_ELAPSED_TIME" |
| 53 | grpcMonitorInterval = "GRPC_MONITOR_INTERVAL" |
| 54 | ) |
| 55 | |
| 56 | const ( |
| 57 | DefaultBackoffInitialInterval = 100 * time.Millisecond |
| 58 | DefaultBackoffMaxInterval = 5 * time.Second |
| 59 | DefaultBackoffMaxElapsedTime = 0 * time.Second // No time limit |
| 60 | DefaultGRPCMonitorInterval = 5 * time.Second |
| 61 | ) |
| 62 | |
| 63 | const ( |
| abhay | 116c4d4 | 2025-03-21 00:35:07 +0530 | [diff] [blame] | 64 | // [VOL-5434] Setting max receive message size to 20 MB, |
| 65 | // Default value of 'defaultServerMaxReceiveMessageSize' is 4 MB |
| 66 | grpcRecvMsgSizeLimit = 20 |
| 67 | ) |
| 68 | |
| 69 | const ( |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 70 | eventConnecting = event(iota) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 71 | eventValidatingConnection |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 72 | eventConnected |
| 73 | eventDisconnected |
| 74 | eventStopped |
| 75 | eventError |
| 76 | |
| 77 | stateConnected = state(iota) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 78 | stateValidatingConnection |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 79 | stateConnecting |
| 80 | stateDisconnected |
| 81 | ) |
| 82 | |
| 83 | type Client struct { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 84 | clientEndpoint string |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 85 | clientContextData string |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 86 | serverEndPoint string |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 87 | remoteServiceName string |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 88 | connection *grpc.ClientConn |
| 89 | connectionLock sync.RWMutex |
| 90 | stateLock sync.RWMutex |
| 91 | state state |
| 92 | service interface{} |
| 93 | events chan event |
| 94 | onRestart RestartedHandler |
| 95 | backoffInitialInterval time.Duration |
| 96 | backoffMaxInterval time.Duration |
| 97 | backoffMaxElapsedTime time.Duration |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 98 | monitorInterval time.Duration |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 99 | done bool |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 100 | livenessLock sync.RWMutex |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 101 | livenessCallback func(timestamp time.Time) |
| 102 | } |
| 103 | |
| 104 | type ClientOption func(*Client) |
| 105 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 106 | func ClientContextData(data string) ClientOption { |
| 107 | return func(args *Client) { |
| 108 | args.clientContextData = data |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func NewClient(clientEndpoint, serverEndpoint, remoteServiceName string, onRestart RestartedHandler, |
| 113 | opts ...ClientOption) (*Client, error) { |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 114 | c := &Client{ |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 115 | clientEndpoint: clientEndpoint, |
| 116 | serverEndPoint: serverEndpoint, |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 117 | remoteServiceName: remoteServiceName, |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 118 | onRestart: onRestart, |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 119 | events: make(chan event, 5), |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 120 | state: stateDisconnected, |
| 121 | backoffInitialInterval: DefaultBackoffInitialInterval, |
| 122 | backoffMaxInterval: DefaultBackoffMaxInterval, |
| 123 | backoffMaxElapsedTime: DefaultBackoffMaxElapsedTime, |
| 124 | monitorInterval: DefaultGRPCMonitorInterval, |
| 125 | } |
| 126 | for _, option := range opts { |
| 127 | option(c) |
| 128 | } |
| 129 | |
| 130 | // Check for environment variables |
| 131 | if err := SetFromEnvVariable(grpcBackoffInitialInterval, &c.backoffInitialInterval); err != nil { |
| 132 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcBackoffInitialInterval}) |
| 133 | } |
| 134 | |
| 135 | if err := SetFromEnvVariable(grpcBackoffMaxInterval, &c.backoffMaxInterval); err != nil { |
| 136 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcBackoffMaxInterval}) |
| 137 | } |
| 138 | |
| 139 | if err := SetFromEnvVariable(grpcBackoffMaxElapsedTime, &c.backoffMaxElapsedTime); err != nil { |
| 140 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcBackoffMaxElapsedTime}) |
| 141 | } |
| 142 | |
| 143 | if err := SetFromEnvVariable(grpcMonitorInterval, &c.monitorInterval); err != nil { |
| 144 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcMonitorInterval}) |
| 145 | } |
| 146 | |
| 147 | logger.Infow(context.Background(), "initialized-client", log.Fields{"client": c}) |
| 148 | |
| 149 | // Sanity check |
| 150 | if c.backoffInitialInterval > c.backoffMaxInterval { |
| 151 | return nil, fmt.Errorf("initial retry delay %v is greater than maximum retry delay %v", c.backoffInitialInterval, c.backoffMaxInterval) |
| 152 | } |
| 153 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 154 | grpc.EnableTracing = true |
| 155 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 156 | return c, nil |
| 157 | } |
| 158 | |
| 159 | func (c *Client) GetClient() (interface{}, error) { |
| 160 | c.connectionLock.RLock() |
| 161 | defer c.connectionLock.RUnlock() |
| 162 | if c.service == nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 163 | return nil, fmt.Errorf("no connection to %s", c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 164 | } |
| 165 | return c.service, nil |
| 166 | } |
| 167 | |
| 168 | // GetCoreServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 169 | // which returns an interface |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 170 | func (c *Client) GetCoreServiceClient() (core_service.CoreServiceClient, error) { |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 171 | c.connectionLock.RLock() |
| 172 | defer c.connectionLock.RUnlock() |
| 173 | if c.service == nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 174 | return nil, fmt.Errorf("no core connection to %s", c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 175 | } |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 176 | client, ok := c.service.(core_service.CoreServiceClient) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 177 | if ok { |
| 178 | return client, nil |
| 179 | } |
| 180 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 181 | } |
| 182 | |
| 183 | // GetOnuAdapterServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 184 | // which returns an interface |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 185 | func (c *Client) GetOnuInterAdapterServiceClient() (onu_inter_adapter_service.OnuInterAdapterServiceClient, error) { |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 186 | c.connectionLock.RLock() |
| 187 | defer c.connectionLock.RUnlock() |
| 188 | if c.service == nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 189 | return nil, fmt.Errorf("no child adapter connection to %s", c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 190 | } |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 191 | client, ok := c.service.(onu_inter_adapter_service.OnuInterAdapterServiceClient) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 192 | if ok { |
| 193 | return client, nil |
| 194 | } |
| 195 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 196 | } |
| 197 | |
| 198 | // GetOltAdapterServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 199 | // which returns an interface |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 200 | func (c *Client) GetOltInterAdapterServiceClient() (olt_inter_adapter_service.OltInterAdapterServiceClient, error) { |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 201 | c.connectionLock.RLock() |
| 202 | defer c.connectionLock.RUnlock() |
| 203 | if c.service == nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 204 | return nil, fmt.Errorf("no parent adapter connection to %s", c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 205 | } |
| khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 206 | client, ok := c.service.(olt_inter_adapter_service.OltInterAdapterServiceClient) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 207 | if ok { |
| 208 | return client, nil |
| 209 | } |
| 210 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 211 | } |
| 212 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 213 | // GetAdapterServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 214 | // which returns an interface |
| 215 | func (c *Client) GetAdapterServiceClient() (adapter_service.AdapterServiceClient, error) { |
| 216 | c.connectionLock.RLock() |
| 217 | defer c.connectionLock.RUnlock() |
| 218 | if c.service == nil { |
| 219 | return nil, fmt.Errorf("no adapter service connection to %s", c.serverEndPoint) |
| 220 | } |
| 221 | client, ok := c.service.(adapter_service.AdapterServiceClient) |
| 222 | if ok { |
| 223 | return client, nil |
| 224 | } |
| 225 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 226 | } |
| 227 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 228 | func (c *Client) Reset(ctx context.Context) { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 229 | logger.Debugw(ctx, "resetting-client-connection", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 230 | c.stateLock.Lock() |
| 231 | defer c.stateLock.Unlock() |
| 232 | if c.state == stateConnected { |
| 233 | c.state = stateDisconnected |
| 234 | c.events <- eventDisconnected |
| 235 | } |
| 236 | } |
| 237 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 238 | // executeWithTimeout runs a sending function (sf) along with a receiving one(rf) and returns an error, if any. |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 239 | // If the deadline elapses first, it returns a grpc DeadlineExceeded error instead. |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 240 | func (c *Client) executeWithTimeout(sf func(*common.Connection) error, rf func() (interface{}, error), conn *common.Connection, d time.Duration) error { |
| 241 | errChan := make(chan error, 1) |
| 242 | go func() { |
| 243 | err := sf(conn) |
| 244 | logger.Debugw(context.Background(), "message-sent", log.Fields{"error": err, "qpi-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 245 | if err == nil { |
| 246 | response, err := rf() |
| 247 | logger.Debugw(context.Background(), "message-received", log.Fields{"error": err, "qpi-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "health": response}) |
| 248 | } |
| 249 | errChan <- err |
| 250 | close(errChan) |
| 251 | }() |
| 252 | t := time.NewTimer(d) |
| 253 | select { |
| 254 | case <-t.C: |
| 255 | return status.Errorf(codes.DeadlineExceeded, "timeout-on-sending-message") |
| 256 | case err := <-errChan: |
| 257 | if !t.Stop() { |
| 258 | <-t.C |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 259 | } |
| 260 | return err |
| 261 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 264 | func (c *Client) monitorConnection(ctx context.Context) { |
| 265 | logger.Debugw(ctx, "monitor-connection-started", log.Fields{"qpi-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 266 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 267 | // If we exit, assume disconnected |
| 268 | defer func() { |
| 269 | c.stateLock.Lock() |
| 270 | if !c.done && (c.state == stateConnected || c.state == stateValidatingConnection) { |
| 271 | // Handle only connected state here. We need the validating state to know if we need to backoff before a retry |
| Sridhar Ravindra | 729e4b0 | 2025-02-10 16:41:14 +0530 | [diff] [blame] | 272 | if c.state == stateConnected { |
| 273 | c.state = stateDisconnected |
| 274 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 275 | logger.Warnw(ctx, "sending-disconnect-event", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "curr-state": stateConnected, "new-state": c.state}) |
| 276 | c.events <- eventDisconnected |
| 277 | } else { |
| 278 | logger.Debugw(ctx, "no-state-change-needed", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "state": c.state, "client-done": c.done}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 279 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 280 | c.stateLock.Unlock() |
| 281 | logger.Debugw(ctx, "monitor-connection-ended", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 282 | }() |
| 283 | |
| 284 | c.connectionLock.RLock() |
| 285 | conn := c.connection |
| 286 | c.connectionLock.RUnlock() |
| 287 | if conn == nil { |
| 288 | logger.Errorw(ctx, "connection-nil", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 289 | return |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 290 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 291 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 292 | // Get a new client using reflection. The server can implement any grpc service, but it |
| 293 | // needs to also implement the "StartKeepAliveStream" API |
| 294 | grpcReflectClient := grpcreflect.NewClient(ctx, rpb.NewServerReflectionClient(conn)) |
| 295 | if grpcReflectClient == nil { |
| 296 | logger.Errorw(ctx, "grpc-reflect-client-nil", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 297 | return |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 298 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 299 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 300 | // Get the list of services - there should be 2 services: a server reflection and the voltha service we are interested in |
| 301 | services, err := grpcReflectClient.ListServices() |
| 302 | if err != nil { |
| 303 | logger.Errorw(ctx, "list-services-error", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "error": err}) |
| 304 | return |
| 305 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 306 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 307 | // Filter out the service |
| 308 | logger.Debugw(ctx, "services", log.Fields{"services": services}) |
| 309 | serviceOfInterest := "" |
| 310 | for _, service := range services { |
| 311 | if strings.EqualFold(service, c.remoteServiceName) { |
| 312 | serviceOfInterest = service |
| 313 | break |
| 314 | } |
| 315 | } |
| 316 | if serviceOfInterest == "" { |
| 317 | logger.Errorw(ctx, "no-service-found", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "services": services, "expected-remote-service": c.remoteServiceName}) |
| 318 | return |
| 319 | } |
| khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 320 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 321 | // Resolve the service |
| 322 | resolvedService, err := grpcReflectClient.ResolveService(serviceOfInterest) |
| 323 | if err != nil { |
| 324 | logger.Errorw(ctx, "service-error", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "service": resolvedService, "error": err}) |
| 325 | return |
| 326 | } |
| 327 | |
| 328 | // Find the method of interest |
| 329 | method := resolvedService.FindMethodByName("GetHealthStatus") |
| 330 | if method == nil { |
| 331 | logger.Errorw(ctx, "nil-method", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "service": resolvedService}) |
| 332 | return |
| 333 | } |
| 334 | logger.Debugw(ctx, "resolved-to-method", log.Fields{"service": resolvedService.GetName(), "method": method.GetName()}) |
| 335 | |
| 336 | // Get a dynamic connection |
| 337 | dynamicConn := grpcdynamic.NewStub(conn) |
| 338 | |
| 339 | // Get the stream and send this client information |
| 340 | streamCtx, streamDone := context.WithCancel(log.WithSpanFromContext(context.Background(), ctx)) |
| 341 | defer streamDone() |
| 342 | stream, err := dynamicConn.InvokeRpcBidiStream(streamCtx, method) |
| 343 | if err != nil { |
| 344 | logger.Errorw(ctx, "stream-error", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "service": resolvedService, "error": err}) |
| 345 | return |
| 346 | } |
| 347 | |
| 348 | clientInfo := &common.Connection{ |
| 349 | Endpoint: c.clientEndpoint, |
| 350 | ContextInfo: c.clientContextData, |
| 351 | KeepAliveInterval: int64(c.monitorInterval), |
| 352 | } |
| 353 | |
| 354 | initialConnection := true |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 355 | loop: |
| 356 | for { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 357 | // Let's send a keep alive message with our info |
| 358 | err := c.executeWithTimeout( |
| 359 | func(conn *common.Connection) error { return stream.SendMsg(conn) }, |
| 360 | func() (interface{}, error) { return stream.RecvMsg() }, |
| 361 | clientInfo, |
| 362 | c.monitorInterval) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 363 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 364 | if err != nil { |
| 365 | // Any error means the far end is gone |
| 366 | logger.Errorw(ctx, "sending-stream-error", log.Fields{"error": err, "api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "context": stream.Context().Err()}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 367 | break loop |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 368 | } |
| 369 | // Send a connect event |
| 370 | if initialConnection { |
| 371 | logger.Debugw(ctx, "first-stream-data-sent", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 372 | c.events <- eventConnected |
| 373 | initialConnection = false |
| 374 | } |
| 375 | logger.Debugw(ctx, "stream-data-sent", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 376 | // Update liveness, if configured |
| 377 | c.livenessLock.RLock() |
| 378 | if c.livenessCallback != nil { |
| 379 | go c.livenessCallback(time.Now()) |
| 380 | } |
| 381 | c.livenessLock.RUnlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 382 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 383 | // Wait to send the next keep alive |
| 384 | keepAliveTimer := time.NewTimer(time.Duration(clientInfo.KeepAliveInterval)) |
| 385 | select { |
| 386 | case <-ctx.Done(): |
| 387 | logger.Warnw(ctx, "context-done", log.Fields{"api-endpont": c.serverEndPoint, "client": c.clientEndpoint}) |
| 388 | break loop |
| 389 | case <-stream.Context().Done(): |
| 390 | logger.Debugw(ctx, "stream-context-done", log.Fields{"api-endpoint": c.serverEndPoint, "stream-info": stream.Context(), "client": c.clientEndpoint}) |
| 391 | break loop |
| 392 | case <-keepAliveTimer.C: |
| 393 | continue |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 394 | } |
| 395 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 396 | if stream != nil { |
| 397 | if err := stream.CloseSend(); err != nil { |
| 398 | logger.Warnw(ctx, "closing-stream-error", log.Fields{"error": err, "api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 399 | } |
| 400 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Start kicks off the adapter agent by trying to connect to the adapter |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 404 | func (c *Client) Start(ctx context.Context, handler GetServiceClient, retry_interceptor ...grpc.UnaryClientInterceptor) { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 405 | logger.Debugw(ctx, "Starting GRPC - Client", log.Fields{"api-endpoint": c.serverEndPoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 406 | |
| 407 | // If the context contains a k8s probe then register services |
| 408 | p := probe.GetProbeFromContext(ctx) |
| 409 | if p != nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 410 | p.RegisterService(ctx, c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 411 | } |
| 412 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 413 | var monitorConnectionCtx context.Context |
| 414 | var monitorConnectionDone func() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 415 | |
| 416 | initialConnection := true |
| 417 | c.events <- eventConnecting |
| 418 | backoff := NewBackoff(c.backoffInitialInterval, c.backoffMaxInterval, c.backoffMaxElapsedTime) |
| 419 | attempt := 1 |
| 420 | loop: |
| 421 | for { |
| 422 | select { |
| 423 | case <-ctx.Done(): |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 424 | logger.Warnw(ctx, "context-closing", log.Fields{"api_endpoint": c.serverEndPoint, "client": c.clientEndpoint, "context": ctx}) |
| 425 | c.connectionLock.Lock() |
| 426 | if !c.done { |
| 427 | c.done = true |
| 428 | c.events <- eventStopped |
| 429 | close(c.events) |
| 430 | } |
| 431 | c.connectionLock.Unlock() |
| 432 | // break loop |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 433 | case event := <-c.events: |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 434 | logger.Debugw(ctx, "received-event", log.Fields{"event": event, "api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 435 | c.connectionLock.RLock() |
| 436 | // On a client stopped, just allow the stop event to go through |
| 437 | if c.done && event != eventStopped { |
| 438 | c.connectionLock.RUnlock() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 439 | logger.Debugw(ctx, "ignoring-event-on-client-stop", log.Fields{"event": event, "api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 440 | continue |
| 441 | } |
| 442 | c.connectionLock.RUnlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 443 | switch event { |
| 444 | case eventConnecting: |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 445 | c.stateLock.Lock() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 446 | logger.Debugw(ctx, "connection-start", log.Fields{"api-endpoint": c.serverEndPoint, "attempts": attempt, "curr-state": c.state, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 447 | if c.state == stateConnected { |
| 448 | c.state = stateDisconnected |
| 449 | } |
| 450 | if c.state != stateConnecting { |
| 451 | c.state = stateConnecting |
| 452 | go func() { |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 453 | var err error |
| 454 | if len(retry_interceptor) > 0 { |
| 455 | err = c.connectToEndpoint(ctx, p, retry_interceptor...) |
| 456 | } else { |
| 457 | err = c.connectToEndpoint(ctx, p) |
| 458 | } |
| 459 | |
| 460 | if err != nil { |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 461 | c.stateLock.Lock() |
| 462 | c.state = stateDisconnected |
| 463 | c.stateLock.Unlock() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 464 | logger.Errorw(ctx, "connection-failed", log.Fields{"api-endpoint": c.serverEndPoint, "attempt": attempt, "client": c.clientEndpoint, "error": err}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 465 | |
| 466 | // Retry connection after a delay |
| 467 | if err = backoff.Backoff(ctx); err != nil { |
| 468 | // Context has closed or reached maximum elapsed time, if set |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 469 | logger.Errorw(ctx, "retry-aborted", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "error": err}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 470 | return |
| 471 | } |
| 472 | attempt += 1 |
| khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 473 | c.connectionLock.RLock() |
| 474 | if !c.done { |
| 475 | c.events <- eventConnecting |
| 476 | } |
| 477 | c.connectionLock.RUnlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 478 | } |
| 479 | }() |
| 480 | } |
| 481 | c.stateLock.Unlock() |
| 482 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 483 | case eventValidatingConnection: |
| 484 | logger.Debugw(ctx, "connection-validation", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 485 | c.stateLock.Lock() |
| 486 | if c.state != stateConnected { |
| 487 | c.state = stateValidatingConnection |
| 488 | } |
| 489 | c.stateLock.Unlock() |
| 490 | monitorConnectionCtx, monitorConnectionDone = context.WithCancel(context.Background()) |
| 491 | go c.monitorConnection(monitorConnectionCtx) |
| 492 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 493 | case eventConnected: |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 494 | attempt = 1 |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 495 | backoff.Reset() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 496 | c.stateLock.Lock() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 497 | logger.Debugw(ctx, "endpoint-connected", log.Fields{"api-endpoint": c.serverEndPoint, "curr-state": c.state, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 498 | if c.state != stateConnected { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 499 | // Setup the service |
| 500 | c.connectionLock.RLock() |
| 501 | conn := c.connection |
| 502 | c.connectionLock.RUnlock() |
| 503 | |
| 504 | subCtx, cancel := context.WithTimeout(ctx, c.backoffMaxInterval) |
| 505 | svc := handler(subCtx, conn) |
| 506 | if svc != nil { |
| 507 | c.service = svc |
| 508 | if p != nil { |
| 509 | p.UpdateStatus(ctx, c.serverEndPoint, probe.ServiceStatusRunning) |
| 510 | } |
| 511 | logger.Infow(ctx, "connected-to-endpoint", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 512 | } else { |
| 513 | // Should never happen, but just in case |
| 514 | logger.Warnw(ctx, "service-is-nil", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 515 | c.events <- eventDisconnected |
| 516 | } |
| 517 | cancel() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 518 | c.state = stateConnected |
| 519 | if initialConnection { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 520 | logger.Debugw(ctx, "initial-endpoint-connection", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 521 | initialConnection = false |
| 522 | } else { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 523 | logger.Debugw(ctx, "endpoint-reconnection", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 524 | // Trigger any callback on a restart |
| 525 | go func() { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 526 | err := c.onRestart(log.WithSpanFromContext(context.Background(), ctx), c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 527 | if err != nil { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 528 | logger.Errorw(ctx, "unable-to-restart-endpoint", log.Fields{"error": err, "api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 529 | } |
| 530 | }() |
| 531 | } |
| 532 | } |
| 533 | c.stateLock.Unlock() |
| 534 | |
| 535 | case eventDisconnected: |
| 536 | if p != nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 537 | p.UpdateStatus(ctx, c.serverEndPoint, probe.ServiceStatusNotReady) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 538 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 539 | connectionValidationFail := false |
| 540 | c.stateLock.Lock() |
| 541 | logger.Debugw(ctx, "endpoint-disconnected", log.Fields{"api-endpoint": c.serverEndPoint, "curr-state": c.state, "client": c.clientEndpoint}) |
| Sridhar Ravindra | 729e4b0 | 2025-02-10 16:41:14 +0530 | [diff] [blame] | 542 | if c.state == stateValidatingConnection { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 543 | connectionValidationFail = true |
| 544 | c.state = stateDisconnected |
| 545 | } |
| 546 | c.stateLock.Unlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 547 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 548 | // Stop the streaming connection |
| 549 | if monitorConnectionDone != nil { |
| 550 | monitorConnectionDone() |
| 551 | monitorConnectionDone = nil |
| 552 | } |
| 553 | |
| 554 | if connectionValidationFail { |
| 555 | // Retry connection after a delay |
| 556 | if err := backoff.Backoff(ctx); err != nil { |
| 557 | // Context has closed or reached maximum elapsed time, if set |
| 558 | logger.Errorw(ctx, "retry-aborted", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "error": err}) |
| 559 | return |
| 560 | } |
| 561 | } |
| 562 | c.connectionLock.RLock() |
| 563 | if !c.done { |
| Sridhar Ravindra | 729e4b0 | 2025-02-10 16:41:14 +0530 | [diff] [blame] | 564 | c.events <- eventConnecting |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 565 | } |
| 566 | c.connectionLock.RUnlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 567 | |
| 568 | case eventStopped: |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 569 | logger.Debugw(ctx, "endpoint-stopped", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 570 | |
| 571 | if monitorConnectionDone != nil { |
| 572 | monitorConnectionDone() |
| 573 | monitorConnectionDone = nil |
| 574 | } |
| 575 | if err := c.closeConnection(ctx, p); err != nil { |
| 576 | logger.Errorw(ctx, "endpoint-closing-connection-failed", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "error": err}) |
| 577 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 578 | break loop |
| 579 | case eventError: |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 580 | logger.Errorw(ctx, "endpoint-error-event", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 581 | default: |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 582 | logger.Errorw(ctx, "endpoint-unknown-event", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "error": event}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 586 | |
| 587 | // Stop the streaming connection |
| 588 | if monitorConnectionDone != nil { |
| 589 | logger.Debugw(ctx, "closing-connection-monitoring", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| 590 | monitorConnectionDone() |
| 591 | } |
| 592 | |
| 593 | logger.Infow(ctx, "client-stopped", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 594 | } |
| 595 | |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 596 | func (c *Client) connectToEndpoint(ctx context.Context, p *probe.Probe, retry_interceptor ...grpc.UnaryClientInterceptor) error { |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 597 | if p != nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 598 | p.UpdateStatus(ctx, c.serverEndPoint, probe.ServiceStatusPreparing) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | c.connectionLock.Lock() |
| 602 | defer c.connectionLock.Unlock() |
| 603 | |
| 604 | if c.connection != nil { |
| 605 | _ = c.connection.Close() |
| 606 | c.connection = nil |
| 607 | } |
| 608 | |
| 609 | c.service = nil |
| 610 | |
| 611 | // Use Interceptors to: |
| 612 | // 1. automatically inject |
| 613 | // 2. publish Open Tracing Spans by this GRPC Client |
| 614 | // 3. detect connection failure on client calls such that the reconnection process can begin |
| Abhay Kumar | 685507d | 2025-10-06 09:04:09 +0000 | [diff] [blame^] | 615 | interceptor_opts := []grpc.UnaryClientInterceptor{ |
| 616 | grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
| 617 | grpc_prometheus.UnaryClientInterceptor, |
| 618 | } |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 619 | |
| Abhay Kumar | 685507d | 2025-10-06 09:04:09 +0000 | [diff] [blame^] | 620 | grpc_prometheus.EnableClientHandlingTimeHistogram() |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 621 | if len(retry_interceptor) > 0 { |
| 622 | interceptor_opts = append(interceptor_opts, retry_interceptor...) |
| 623 | } |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 624 | conn, err := grpc.Dial(c.serverEndPoint, |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 625 | grpc.WithInsecure(), |
| abhay | 116c4d4 | 2025-03-21 00:35:07 +0530 | [diff] [blame] | 626 | grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(grpcRecvMsgSizeLimit*1024*1024)), |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 627 | grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient( |
| 628 | grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
| Abhay Kumar | 685507d | 2025-10-06 09:04:09 +0000 | [diff] [blame^] | 629 | grpc_prometheus.StreamClientInterceptor, |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 630 | )), |
| nikesh.krishnan | b547c1a | 2023-03-11 03:05:16 +0530 | [diff] [blame] | 631 | grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(interceptor_opts...)), |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 632 | ) |
| 633 | |
| 634 | if err == nil { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 635 | c.connection = conn |
| 636 | c.events <- eventValidatingConnection |
| 637 | return nil |
| 638 | } else { |
| 639 | logger.Warnw(ctx, "no-connection-to-endpoint", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint, "error": err}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 640 | } |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 641 | |
| 642 | if p != nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 643 | p.UpdateStatus(ctx, c.serverEndPoint, probe.ServiceStatusFailed) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 644 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 645 | return fmt.Errorf("no connection to api endpoint %s", c.serverEndPoint) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | func (c *Client) closeConnection(ctx context.Context, p *probe.Probe) error { |
| 649 | if p != nil { |
| khenaidoo | b950321 | 2021-12-08 14:22:21 -0500 | [diff] [blame] | 650 | p.UpdateStatus(ctx, c.serverEndPoint, probe.ServiceStatusStopped) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 651 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 652 | logger.Infow(ctx, "client-closing-connection", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 653 | |
| 654 | c.connectionLock.Lock() |
| 655 | defer c.connectionLock.Unlock() |
| 656 | |
| 657 | if c.connection != nil { |
| 658 | err := c.connection.Close() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 659 | c.service = nil |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 660 | c.connection = nil |
| 661 | return err |
| 662 | } |
| 663 | |
| 664 | return nil |
| 665 | } |
| 666 | |
| 667 | func (c *Client) Stop(ctx context.Context) { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 668 | logger.Infow(ctx, "client-stop-request-event-received", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 669 | c.connectionLock.Lock() |
| 670 | defer c.connectionLock.Unlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 671 | if !c.done { |
| khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 672 | c.done = true |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 673 | c.events <- eventStopped |
| 674 | close(c.events) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 675 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 676 | logger.Infow(ctx, "client-stop-request-event-sent", log.Fields{"api-endpoint": c.serverEndPoint, "client": c.clientEndpoint}) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | // SetService is used for testing only |
| 680 | func (c *Client) SetService(srv interface{}) { |
| 681 | c.connectionLock.Lock() |
| 682 | defer c.connectionLock.Unlock() |
| 683 | c.service = srv |
| 684 | } |
| 685 | |
| 686 | func (c *Client) SubscribeForLiveness(callback func(timestamp time.Time)) { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 687 | c.livenessLock.Lock() |
| 688 | defer c.livenessLock.Unlock() |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 689 | c.livenessCallback = callback |
| 690 | } |