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