go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/breaker/action.go (about)

     1  package breaker
     2  
     3  import "context"
     4  
     5  // Action is a type that can be used in the breaker.
     6  type Action[A, B any] interface {
     7  	Call(context.Context, A) (B, error)
     8  }
     9  
    10  var (
    11  	_ Action[int, string] = (*ActionFunc[int, string])(nil)
    12  )
    13  
    14  // ActionFunc is a function that implements action.
    15  type ActionFunc[A, B any] func(context.Context, A) (B, error)
    16  
    17  // Action implements actioner for the function.
    18  func (af ActionFunc[A, B]) Call(ctx context.Context, args A) (B, error) {
    19  	return af(ctx, args)
    20  }
    21  
    22  var (
    23  	_ Action[int, string] = (*NoopAction[int, string])(nil)
    24  )
    25  
    26  // NoopAction is an actioner type that does nothing.
    27  type NoopAction[A, B any] struct{}
    28  
    29  // Action implements actioner
    30  func (n NoopAction[A, B]) Call(_ context.Context, _ A) (output B, err error) {
    31  	return
    32  }