gogoroutineexponential-backoff

Backoff handling with goroutines


A program sends data to API using N concurrent workers as goroutines that consume data from a channel (Producer/Consumer pattern). API signals it can't handle more using HTTP status codes and demands a back-off.

  1. How do I block all workers until back-off interval has passed?
  2. Where to I put those requests that failed for retry?

Any links/pointers to this presumably already solved problem are much appreciated!


Solution

  • You can use select to control calling API

    for _, k := range data {
        select {
            case <- backoff:
                time.Sleep(backoffDuration)
        default:
            // Call API
            // Check http status code and trigger backoff channel
            backoff <- 1
        }
    }
    

    Here is set-up:

    1. Pass same backoff channel to all go routine
    2. Setting backoffDuration. This is tricky as all go routines should be able to set this value and all others should be able to read it. One method can be using closure

    Once these two are set-up you can control API calls by manipulating backoff channel and backoffDuration to control for how long routine will pause working.

    Disclaimer: This is just pseudo-code.

    You can check Hashicorp's library here. Looks like it will solve your problem