Skip to content

Commit

Permalink
Make the connection retries non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
radito3 committed Apr 18, 2024
1 parent 85aedfb commit 3d200b3
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cmd/remote-work-processor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ Loop:
case <-connAttemptChan:
err := grpcClient.InitSession(rootCtx, rwpMetadata.SessionID())
if err != nil {
signalRetry(&connAttempts, connAttemptChan, err)
signalRetry(rootCtx, &connAttempts, connAttemptChan, err)
}
default:
operation, err := grpcClient.ReceiveMsg()
if err != nil {
signalRetry(&connAttempts, connAttemptChan, err)
signalRetry(rootCtx, &connAttempts, connAttemptChan, err)
continue
}
if operation == nil {
Expand All @@ -119,15 +119,15 @@ Loop:

msg, err := processor.Process(rootCtx)
if err != nil {
signalRetry(&connAttempts, connAttemptChan, fmt.Errorf("error processing operation: %v", err))
signalRetry(rootCtx, &connAttempts, connAttemptChan, fmt.Errorf("error processing operation: %v", err))
continue
}
if msg == nil {
continue
}

if err = grpcClient.Send(msg); err != nil {
signalRetry(&connAttempts, connAttemptChan, err)
signalRetry(rootCtx, &connAttempts, connAttemptChan, err)
}
}
}
Expand Down Expand Up @@ -158,10 +158,20 @@ func getKubeConfig() *rest.Config {
return config
}

func signalRetry(attempts *uint, retryChan chan<- struct{}, err error) {
// TODO: this can be made more "enterprise":
//
// make the retry interval configurable via cmd option
// add a new option to specify whether the retry strategy is
// - "fixed" (retry on regular interval)
// - "exponential" (each subsequent retry is after a longer period of time)
func signalRetry(ctx context.Context, attempts *uint, retryChan chan<- struct{}, err error) {
log.Println(err)
log.Println("retrying after 10 seconds...")
time.Sleep(10 * time.Second)
select {
case <-ctx.Done():
return
case <-time.After(10 * time.Second):
}
retryChan <- struct{}{}
*attempts++
}

0 comments on commit 3d200b3

Please sign in to comment.