Search code examples
goaws-lambdacircuit-breaker

Circuit Breaker for AWS Lambda


Is it possible to wrap a HTTP request in the lambda handler with a Circuit-Breaker Library like gobreaker, I'm trying something like the below but not seeing the count go over 1. Maybe it's not possible with AWS lambda? I've tested both locally with SAM and deployed on AWS.

func handler(ctx context.Context, request events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {

  resp, errBreaker := cb.Execute(func() (interface{}, error) {
    return sendHttpRequestThatFails(request, ctx)
    })

    if errBreaker != nil {
       log.Fatal("Error Breaker:", errBreaker)
    }

    return resp.(events.ALBTargetGroupResponse), nil
}


func main() {
    //global config added to global circuit breaker variable
    var st gobreaker.Settings
    st.Name = "HTTP POST Breaker"
    st.Timeout = 2
    st.OnStateChange = func(name string, from gobreaker.State, to gobreaker.State) {
       // do smth when circuit breaker trips.
       log.Info("tripped breaker name : "+name+" from "+from.String() + " to "+to.String())
    }
    st.ReadyToTrip = func(counts gobreaker.Counts) bool {
       log.Info("counts ,",counts )
       return counts.TotalFailures >= 2 // I never see this count go over 1
    }

   cb = gobreaker.NewCircuitBreaker(st)
   nrlambda.Start(handler, app)
 }

Solution

  • I'm answering my own question here because I found what was causing the counts to stay at 1 and not increment, the log.fatal line of code was killing the execution of the Lambda function and clearing all the go breaker counts and states. Remove this and the function finishes and is viable instance to be kept warm by AWS.