khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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" |
| 28 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 29 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/core_service" |
| 31 | "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service" |
| 32 | "github.com/opencord/voltha-protos/v5/go/onu_inter_adapter_service" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 33 | "google.golang.org/grpc" |
| 34 | "google.golang.org/grpc/keepalive" |
| 35 | ) |
| 36 | |
| 37 | type event byte |
| 38 | type state byte |
| 39 | type SetAndTestServiceHandler func(context.Context, *grpc.ClientConn) interface{} |
| 40 | type RestartedHandler func(ctx context.Context, endPoint string) error |
| 41 | |
| 42 | type contextKey string |
| 43 | |
| 44 | func (c contextKey) String() string { |
| 45 | return string(c) |
| 46 | } |
| 47 | |
| 48 | var ( |
| 49 | grpcMonitorContextKey = contextKey("grpc-monitor") |
| 50 | ) |
| 51 | |
| 52 | const ( |
| 53 | grpcBackoffInitialInterval = "GRPC_BACKOFF_INITIAL_INTERVAL" |
| 54 | grpcBackoffMaxInterval = "GRPC_BACKOFF_MAX_INTERVAL" |
| 55 | grpcBackoffMaxElapsedTime = "GRPC_BACKOFF_MAX_ELAPSED_TIME" |
| 56 | grpcMonitorInterval = "GRPC_MONITOR_INTERVAL" |
| 57 | ) |
| 58 | |
| 59 | const ( |
| 60 | DefaultBackoffInitialInterval = 100 * time.Millisecond |
| 61 | DefaultBackoffMaxInterval = 5 * time.Second |
| 62 | DefaultBackoffMaxElapsedTime = 0 * time.Second // No time limit |
| 63 | DefaultGRPCMonitorInterval = 5 * time.Second |
| 64 | ) |
| 65 | |
| 66 | const ( |
| 67 | connectionErrorSubString = "SubConns are in TransientFailure" |
| 68 | connectionClosedSubstring = "client connection is closing" |
| 69 | connectionError = "connection error" |
| 70 | connectionSystemNotReady = "system is not ready" |
| 71 | ) |
| 72 | |
| 73 | const ( |
| 74 | eventConnecting = event(iota) |
| 75 | eventConnected |
| 76 | eventDisconnected |
| 77 | eventStopped |
| 78 | eventError |
| 79 | |
| 80 | stateConnected = state(iota) |
| 81 | stateConnecting |
| 82 | stateDisconnected |
| 83 | ) |
| 84 | |
| 85 | type Client struct { |
| 86 | apiEndPoint string |
| 87 | connection *grpc.ClientConn |
| 88 | connectionLock sync.RWMutex |
| 89 | stateLock sync.RWMutex |
| 90 | state state |
| 91 | service interface{} |
| 92 | events chan event |
| 93 | onRestart RestartedHandler |
| 94 | backoffInitialInterval time.Duration |
| 95 | backoffMaxInterval time.Duration |
| 96 | backoffMaxElapsedTime time.Duration |
| 97 | activityCheck bool |
| 98 | monitorInterval time.Duration |
| 99 | activeCh chan struct{} |
| 100 | activeChMutex sync.RWMutex |
| 101 | done bool |
| 102 | livenessCallback func(timestamp time.Time) |
| 103 | } |
| 104 | |
| 105 | type ClientOption func(*Client) |
| 106 | |
| 107 | func ActivityCheck(enable bool) ClientOption { |
| 108 | return func(args *Client) { |
| 109 | args.activityCheck = enable |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func NewClient(endpoint string, onRestart RestartedHandler, opts ...ClientOption) (*Client, error) { |
| 114 | c := &Client{ |
| 115 | apiEndPoint: endpoint, |
| 116 | onRestart: onRestart, |
| 117 | events: make(chan event, 1), |
| 118 | state: stateDisconnected, |
| 119 | backoffInitialInterval: DefaultBackoffInitialInterval, |
| 120 | backoffMaxInterval: DefaultBackoffMaxInterval, |
| 121 | backoffMaxElapsedTime: DefaultBackoffMaxElapsedTime, |
| 122 | monitorInterval: DefaultGRPCMonitorInterval, |
| 123 | } |
| 124 | for _, option := range opts { |
| 125 | option(c) |
| 126 | } |
| 127 | |
| 128 | // Check for environment variables |
| 129 | if err := SetFromEnvVariable(grpcBackoffInitialInterval, &c.backoffInitialInterval); err != nil { |
| 130 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcBackoffInitialInterval}) |
| 131 | } |
| 132 | |
| 133 | if err := SetFromEnvVariable(grpcBackoffMaxInterval, &c.backoffMaxInterval); err != nil { |
| 134 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcBackoffMaxInterval}) |
| 135 | } |
| 136 | |
| 137 | if err := SetFromEnvVariable(grpcBackoffMaxElapsedTime, &c.backoffMaxElapsedTime); err != nil { |
| 138 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcBackoffMaxElapsedTime}) |
| 139 | } |
| 140 | |
| 141 | if err := SetFromEnvVariable(grpcMonitorInterval, &c.monitorInterval); err != nil { |
| 142 | logger.Warnw(context.Background(), "failure-reading-env-variable", log.Fields{"error": err, "variable": grpcMonitorInterval}) |
| 143 | } |
| 144 | |
| 145 | logger.Infow(context.Background(), "initialized-client", log.Fields{"client": c}) |
| 146 | |
| 147 | // Sanity check |
| 148 | if c.backoffInitialInterval > c.backoffMaxInterval { |
| 149 | return nil, fmt.Errorf("initial retry delay %v is greater than maximum retry delay %v", c.backoffInitialInterval, c.backoffMaxInterval) |
| 150 | } |
| 151 | |
| 152 | return c, nil |
| 153 | } |
| 154 | |
| 155 | func (c *Client) GetClient() (interface{}, error) { |
| 156 | c.connectionLock.RLock() |
| 157 | defer c.connectionLock.RUnlock() |
| 158 | if c.service == nil { |
| 159 | return nil, fmt.Errorf("no connection to %s", c.apiEndPoint) |
| 160 | } |
| 161 | return c.service, nil |
| 162 | } |
| 163 | |
| 164 | // GetCoreServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 165 | // which returns an interface |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 166 | func (c *Client) GetCoreServiceClient() (core_service.CoreServiceClient, error) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 167 | c.connectionLock.RLock() |
| 168 | defer c.connectionLock.RUnlock() |
| 169 | if c.service == nil { |
| 170 | return nil, fmt.Errorf("no core connection to %s", c.apiEndPoint) |
| 171 | } |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 172 | client, ok := c.service.(core_service.CoreServiceClient) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 173 | if ok { |
| 174 | return client, nil |
| 175 | } |
| 176 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 177 | } |
| 178 | |
| 179 | // GetOnuAdapterServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 180 | // which returns an interface |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 181 | func (c *Client) GetOnuInterAdapterServiceClient() (onu_inter_adapter_service.OnuInterAdapterServiceClient, error) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 182 | c.connectionLock.RLock() |
| 183 | defer c.connectionLock.RUnlock() |
| 184 | if c.service == nil { |
| 185 | return nil, fmt.Errorf("no child adapter connection to %s", c.apiEndPoint) |
| 186 | } |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 187 | client, ok := c.service.(onu_inter_adapter_service.OnuInterAdapterServiceClient) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 188 | if ok { |
| 189 | return client, nil |
| 190 | } |
| 191 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 192 | } |
| 193 | |
| 194 | // GetOltAdapterServiceClient is a helper function that returns a concrete service instead of the GetClient() API |
| 195 | // which returns an interface |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 196 | func (c *Client) GetOltInterAdapterServiceClient() (olt_inter_adapter_service.OltInterAdapterServiceClient, error) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 197 | c.connectionLock.RLock() |
| 198 | defer c.connectionLock.RUnlock() |
| 199 | if c.service == nil { |
| 200 | return nil, fmt.Errorf("no parent adapter connection to %s", c.apiEndPoint) |
| 201 | } |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 202 | client, ok := c.service.(olt_inter_adapter_service.OltInterAdapterServiceClient) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 203 | if ok { |
| 204 | return client, nil |
| 205 | } |
| 206 | return nil, fmt.Errorf("invalid-service-%s", reflect.TypeOf(c.service)) |
| 207 | } |
| 208 | |
| 209 | func (c *Client) Reset(ctx context.Context) { |
| 210 | logger.Debugw(ctx, "resetting-client-connection", log.Fields{"endpoint": c.apiEndPoint}) |
| 211 | c.stateLock.Lock() |
| 212 | defer c.stateLock.Unlock() |
| 213 | if c.state == stateConnected { |
| 214 | c.state = stateDisconnected |
| 215 | c.events <- eventDisconnected |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | func (c *Client) clientInterceptor(ctx context.Context, method string, req interface{}, reply interface{}, |
| 220 | cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 221 | // Nothing to do before intercepting the call |
| 222 | err := invoker(ctx, method, req, reply, cc, opts...) |
| 223 | // On connection failure, start the reconnect process depending on the error response |
| 224 | if err != nil { |
| 225 | logger.Errorw(ctx, "received-error", log.Fields{"error": err, "context": ctx, "endpoint": c.apiEndPoint}) |
| 226 | if strings.Contains(err.Error(), connectionErrorSubString) || |
| 227 | strings.Contains(err.Error(), connectionError) || |
| 228 | strings.Contains(err.Error(), connectionSystemNotReady) || |
| 229 | isGrpcMonitorKeyPresentInContext(ctx) { |
| 230 | c.stateLock.Lock() |
| 231 | if c.state == stateConnected { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 232 | c.state = stateDisconnected |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 233 | logger.Warnw(context.Background(), "sending-disconnect-event", log.Fields{"endpoint": c.apiEndPoint, "error": err, "curr-state": stateConnected, "new-state": c.state}) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 234 | c.events <- eventDisconnected |
| 235 | } |
| 236 | c.stateLock.Unlock() |
| 237 | } else if strings.Contains(err.Error(), connectionClosedSubstring) { |
| 238 | logger.Errorw(context.Background(), "invalid-client-connection-closed", log.Fields{"endpoint": c.apiEndPoint, "error": err}) |
| 239 | } |
| 240 | return err |
| 241 | } |
| 242 | // Update activity on success only |
| 243 | c.updateActivity(ctx) |
| 244 | return nil |
| 245 | } |
| 246 | |
| 247 | // updateActivity pushes an activity indication on the channel so that the monitoring routine does not validate |
| 248 | // the gRPC connection when the connection is being used. Note that this update is done both when the connection |
| 249 | // is alive or a connection error is returned. A separate routine takes care of doing the re-connect. |
| 250 | func (c *Client) updateActivity(ctx context.Context) { |
| 251 | c.activeChMutex.RLock() |
| 252 | defer c.activeChMutex.RUnlock() |
| 253 | if c.activeCh != nil { |
| 254 | logger.Debugw(ctx, "update-activity", log.Fields{"api-endpoint": c.apiEndPoint}) |
| 255 | c.activeCh <- struct{}{} |
| 256 | |
| 257 | // Update liveness only in connected state |
| 258 | if c.livenessCallback != nil { |
| 259 | c.stateLock.RLock() |
| 260 | if c.state == stateConnected { |
| 261 | c.livenessCallback(time.Now()) |
| 262 | } |
| 263 | c.stateLock.RUnlock() |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | func WithGrpcMonitorContext(ctx context.Context, name string) context.Context { |
| 269 | ctx = context.WithValue(ctx, grpcMonitorContextKey, name) |
| 270 | return ctx |
| 271 | } |
| 272 | |
| 273 | func isGrpcMonitorKeyPresentInContext(ctx context.Context) bool { |
| 274 | if ctx != nil { |
| 275 | _, present := ctx.Value(grpcMonitorContextKey).(string) |
| 276 | return present |
| 277 | } |
| 278 | return false |
| 279 | } |
| 280 | |
| 281 | // monitorActivity monitors the activity on the gRPC connection. If there are no activity after a specified |
| 282 | // timeout, it will send a default API request on that connection. If the connection is good then nothing |
| 283 | // happens. If it's bad this will trigger reconnection attempts. |
| 284 | func (c *Client) monitorActivity(ctx context.Context, handler SetAndTestServiceHandler) { |
| 285 | logger.Infow(ctx, "start-activity-monitor", log.Fields{"endpoint": c.apiEndPoint}) |
| 286 | |
| 287 | // Create an activity monitor channel. Unbuffered channel works well. However, we use a buffered |
| 288 | // channel here as a safeguard of having the grpc interceptor publishing too many events that can be |
| 289 | // consumed by this monitoring thread |
| 290 | c.activeChMutex.Lock() |
| 291 | c.activeCh = make(chan struct{}, 10) |
| 292 | c.activeChMutex.Unlock() |
| 293 | |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 294 | grpcMonitorCheckRunning := false |
| 295 | var grpcMonitorCheckRunningLock sync.RWMutex |
| 296 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 297 | // Interval to wait for no activity before probing the connection |
| 298 | timeout := c.monitorInterval |
| 299 | loop: |
| 300 | for { |
| 301 | timeoutTimer := time.NewTimer(timeout) |
| 302 | select { |
| 303 | |
| 304 | case <-c.activeCh: |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 305 | logger.Debugw(ctx, "endpoint-reachable", log.Fields{"endpoint": c.apiEndPoint}) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 306 | |
| 307 | // Reset timer |
| 308 | if !timeoutTimer.Stop() { |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 309 | select { |
| 310 | case <-timeoutTimer.C: |
| 311 | default: |
| 312 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | case <-ctx.Done(): |
| 316 | break loop |
| 317 | |
| 318 | case <-timeoutTimer.C: |
| 319 | // Trigger an activity check if the state is connected. If the state is not connected then there is already |
| 320 | // a backoff retry mechanism in place to retry establishing connection. |
| 321 | c.stateLock.RLock() |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 322 | grpcMonitorCheckRunningLock.RLock() |
| 323 | runCheck := (c.state == stateConnected) && !grpcMonitorCheckRunning |
| 324 | grpcMonitorCheckRunningLock.RUnlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 325 | c.stateLock.RUnlock() |
| 326 | if runCheck { |
| 327 | go func() { |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 328 | grpcMonitorCheckRunningLock.Lock() |
| 329 | if grpcMonitorCheckRunning { |
| 330 | grpcMonitorCheckRunningLock.Unlock() |
| 331 | logger.Debugw(ctx, "connection-check-already-in-progress", log.Fields{"api-endpoint": c.apiEndPoint}) |
| 332 | return |
| 333 | } |
| 334 | grpcMonitorCheckRunning = true |
| 335 | grpcMonitorCheckRunningLock.Unlock() |
| 336 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 337 | logger.Debugw(ctx, "connection-check-start", log.Fields{"api-endpoint": c.apiEndPoint}) |
| 338 | subCtx, cancel := context.WithTimeout(ctx, c.backoffMaxInterval) |
| 339 | defer cancel() |
| 340 | subCtx = WithGrpcMonitorContext(subCtx, "grpc-monitor") |
| 341 | c.connectionLock.RLock() |
| 342 | defer c.connectionLock.RUnlock() |
| 343 | if c.connection != nil { |
| 344 | response := handler(subCtx, c.connection) |
| 345 | logger.Debugw(ctx, "connection-check-response", log.Fields{"api-endpoint": c.apiEndPoint, "up": response != nil}) |
| 346 | } |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 347 | grpcMonitorCheckRunningLock.Lock() |
| 348 | grpcMonitorCheckRunning = false |
| 349 | grpcMonitorCheckRunningLock.Unlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 350 | }() |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | logger.Infow(ctx, "activity-monitor-stopping", log.Fields{"endpoint": c.apiEndPoint}) |
| 355 | } |
| 356 | |
| 357 | // Start kicks off the adapter agent by trying to connect to the adapter |
| 358 | func (c *Client) Start(ctx context.Context, handler SetAndTestServiceHandler) { |
| 359 | logger.Debugw(ctx, "Starting GRPC - Client", log.Fields{"api-endpoint": c.apiEndPoint}) |
| 360 | |
| 361 | // If the context contains a k8s probe then register services |
| 362 | p := probe.GetProbeFromContext(ctx) |
| 363 | if p != nil { |
| 364 | p.RegisterService(ctx, c.apiEndPoint) |
| 365 | } |
| 366 | |
| 367 | // Enable activity check, if required |
| 368 | if c.activityCheck { |
| 369 | go c.monitorActivity(ctx, handler) |
| 370 | } |
| 371 | |
| 372 | initialConnection := true |
| 373 | c.events <- eventConnecting |
| 374 | backoff := NewBackoff(c.backoffInitialInterval, c.backoffMaxInterval, c.backoffMaxElapsedTime) |
| 375 | attempt := 1 |
| 376 | loop: |
| 377 | for { |
| 378 | select { |
| 379 | case <-ctx.Done(): |
| 380 | logger.Debugw(ctx, "context-closing", log.Fields{"endpoint": c.apiEndPoint}) |
khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 381 | break loop |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 382 | case event := <-c.events: |
| 383 | logger.Debugw(ctx, "received-event", log.Fields{"event": event, "endpoint": c.apiEndPoint}) |
khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 384 | c.connectionLock.RLock() |
| 385 | // On a client stopped, just allow the stop event to go through |
| 386 | if c.done && event != eventStopped { |
| 387 | c.connectionLock.RUnlock() |
| 388 | logger.Debugw(ctx, "ignoring-event-on-client-stop", log.Fields{"event": event, "endpoint": c.apiEndPoint}) |
| 389 | continue |
| 390 | } |
| 391 | c.connectionLock.RUnlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 392 | switch event { |
| 393 | case eventConnecting: |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 394 | c.stateLock.Lock() |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 395 | logger.Debugw(ctx, "connection-start", log.Fields{"endpoint": c.apiEndPoint, "attempts": attempt, "curr-state": c.state}) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 396 | if c.state == stateConnected { |
| 397 | c.state = stateDisconnected |
| 398 | } |
| 399 | if c.state != stateConnecting { |
| 400 | c.state = stateConnecting |
| 401 | go func() { |
| 402 | if err := c.connectToEndpoint(ctx, handler, p); err != nil { |
| 403 | c.stateLock.Lock() |
| 404 | c.state = stateDisconnected |
| 405 | c.stateLock.Unlock() |
| 406 | logger.Errorw(ctx, "connection-failed", log.Fields{"endpoint": c.apiEndPoint, "attempt": attempt, "error": err}) |
| 407 | |
| 408 | // Retry connection after a delay |
| 409 | if err = backoff.Backoff(ctx); err != nil { |
| 410 | // Context has closed or reached maximum elapsed time, if set |
| 411 | logger.Errorw(ctx, "retry-aborted", log.Fields{"endpoint": c.apiEndPoint, "error": err}) |
| 412 | return |
| 413 | } |
| 414 | attempt += 1 |
khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 415 | c.connectionLock.RLock() |
| 416 | if !c.done { |
| 417 | c.events <- eventConnecting |
| 418 | } |
| 419 | c.connectionLock.RUnlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 420 | } else { |
| 421 | backoff.Reset() |
| 422 | } |
| 423 | }() |
| 424 | } |
| 425 | c.stateLock.Unlock() |
| 426 | |
| 427 | case eventConnected: |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 428 | attempt = 1 |
| 429 | c.stateLock.Lock() |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 430 | logger.Debugw(ctx, "endpoint-connected", log.Fields{"endpoint": c.apiEndPoint, "curr-state": c.state}) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 431 | if c.state != stateConnected { |
| 432 | c.state = stateConnected |
| 433 | if initialConnection { |
| 434 | logger.Debugw(ctx, "initial-endpoint-connection", log.Fields{"endpoint": c.apiEndPoint}) |
| 435 | initialConnection = false |
| 436 | } else { |
| 437 | logger.Debugw(ctx, "endpoint-reconnection", log.Fields{"endpoint": c.apiEndPoint}) |
| 438 | // Trigger any callback on a restart |
| 439 | go func() { |
| 440 | err := c.onRestart(log.WithSpanFromContext(context.Background(), ctx), c.apiEndPoint) |
| 441 | if err != nil { |
| 442 | logger.Errorw(ctx, "unable-to-restart-endpoint", log.Fields{"error": err, "endpoint": c.apiEndPoint}) |
| 443 | } |
| 444 | }() |
| 445 | } |
| 446 | } |
| 447 | c.stateLock.Unlock() |
| 448 | |
| 449 | case eventDisconnected: |
| 450 | if p != nil { |
| 451 | p.UpdateStatus(ctx, c.apiEndPoint, probe.ServiceStatusNotReady) |
| 452 | } |
khenaidoo | aa29096 | 2021-10-22 18:14:33 -0400 | [diff] [blame] | 453 | c.stateLock.RLock() |
| 454 | logger.Debugw(ctx, "endpoint-disconnected", log.Fields{"endpoint": c.apiEndPoint, "curr-state": c.state}) |
| 455 | c.stateLock.RUnlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 456 | |
| 457 | // Try to connect again |
| 458 | c.events <- eventConnecting |
| 459 | |
| 460 | case eventStopped: |
| 461 | logger.Debugw(ctx, "endPoint-stopped", log.Fields{"adapter": c.apiEndPoint}) |
| 462 | go func() { |
| 463 | if err := c.closeConnection(ctx, p); err != nil { |
| 464 | logger.Errorw(ctx, "endpoint-closing-connection-failed", log.Fields{"endpoint": c.apiEndPoint, "error": err}) |
| 465 | } |
| 466 | }() |
| 467 | break loop |
| 468 | case eventError: |
| 469 | logger.Errorw(ctx, "endpoint-error-event", log.Fields{"endpoint": c.apiEndPoint}) |
| 470 | default: |
| 471 | logger.Errorw(ctx, "endpoint-unknown-event", log.Fields{"endpoint": c.apiEndPoint, "error": event}) |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | logger.Infow(ctx, "endpoint-stopped", log.Fields{"endpoint": c.apiEndPoint}) |
| 476 | } |
| 477 | |
| 478 | func (c *Client) connectToEndpoint(ctx context.Context, handler SetAndTestServiceHandler, p *probe.Probe) error { |
| 479 | if p != nil { |
| 480 | p.UpdateStatus(ctx, c.apiEndPoint, probe.ServiceStatusPreparing) |
| 481 | } |
| 482 | |
| 483 | c.connectionLock.Lock() |
| 484 | defer c.connectionLock.Unlock() |
| 485 | |
| 486 | if c.connection != nil { |
| 487 | _ = c.connection.Close() |
| 488 | c.connection = nil |
| 489 | } |
| 490 | |
| 491 | c.service = nil |
| 492 | |
| 493 | // Use Interceptors to: |
| 494 | // 1. automatically inject |
| 495 | // 2. publish Open Tracing Spans by this GRPC Client |
| 496 | // 3. detect connection failure on client calls such that the reconnection process can begin |
| 497 | conn, err := grpc.Dial(c.apiEndPoint, |
| 498 | grpc.WithInsecure(), |
| 499 | grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient( |
| 500 | grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
| 501 | )), |
| 502 | grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient( |
| 503 | grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
| 504 | )), |
| 505 | grpc.WithUnaryInterceptor(c.clientInterceptor), |
| 506 | // Set keealive parameter - use default grpc values |
| 507 | grpc.WithKeepaliveParams(keepalive.ClientParameters{ |
| 508 | Time: c.monitorInterval, |
| 509 | Timeout: c.backoffMaxInterval, |
| 510 | PermitWithoutStream: true, |
| 511 | }), |
| 512 | ) |
| 513 | |
| 514 | if err == nil { |
| 515 | subCtx, cancel := context.WithTimeout(ctx, c.backoffMaxInterval) |
| 516 | defer cancel() |
| 517 | svc := handler(subCtx, conn) |
| 518 | if svc != nil { |
| 519 | c.connection = conn |
| 520 | c.service = svc |
| 521 | if p != nil { |
| 522 | p.UpdateStatus(ctx, c.apiEndPoint, probe.ServiceStatusRunning) |
| 523 | } |
| 524 | logger.Infow(ctx, "connected-to-endpoint", log.Fields{"endpoint": c.apiEndPoint}) |
| 525 | c.events <- eventConnected |
| 526 | return nil |
| 527 | } |
| 528 | } |
| 529 | logger.Warnw(ctx, "Failed to connect to endpoint", |
| 530 | log.Fields{ |
| 531 | "endpoint": c.apiEndPoint, |
| 532 | "error": err, |
| 533 | }) |
| 534 | |
| 535 | if p != nil { |
| 536 | p.UpdateStatus(ctx, c.apiEndPoint, probe.ServiceStatusFailed) |
| 537 | } |
| 538 | return fmt.Errorf("no connection to endpoint %s", c.apiEndPoint) |
| 539 | } |
| 540 | |
| 541 | func (c *Client) closeConnection(ctx context.Context, p *probe.Probe) error { |
| 542 | if p != nil { |
| 543 | p.UpdateStatus(ctx, c.apiEndPoint, probe.ServiceStatusStopped) |
| 544 | } |
| 545 | |
| 546 | c.connectionLock.Lock() |
| 547 | defer c.connectionLock.Unlock() |
| 548 | |
| 549 | if c.connection != nil { |
| 550 | err := c.connection.Close() |
| 551 | c.connection = nil |
| 552 | return err |
| 553 | } |
| 554 | |
| 555 | return nil |
| 556 | } |
| 557 | |
| 558 | func (c *Client) Stop(ctx context.Context) { |
khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 559 | c.connectionLock.Lock() |
| 560 | defer c.connectionLock.Unlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 561 | if !c.done { |
khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 562 | c.done = true |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 563 | c.events <- eventStopped |
| 564 | close(c.events) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 565 | } |
khenaidoo | fe90ac3 | 2021-11-08 18:17:32 -0500 | [diff] [blame] | 566 | logger.Infow(ctx, "client-stopped", log.Fields{"endpoint": c.apiEndPoint}) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // SetService is used for testing only |
| 570 | func (c *Client) SetService(srv interface{}) { |
| 571 | c.connectionLock.Lock() |
| 572 | defer c.connectionLock.Unlock() |
| 573 | c.service = srv |
| 574 | } |
| 575 | |
| 576 | func (c *Client) SubscribeForLiveness(callback func(timestamp time.Time)) { |
| 577 | c.livenessCallback = callback |
| 578 | } |