github.com/blend/go-sdk@v1.20220411.3/breaker/option.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package breaker
     9  
    10  import (
    11  	"time"
    12  )
    13  
    14  // Option is a mutator for a breaker.
    15  type Option func(*Breaker)
    16  
    17  // OptOpenFailureThreshold sets the OpenFailureThreshold.
    18  func OptOpenFailureThreshold(openFailureThreshold int64) Option {
    19  	return func(b *Breaker) {
    20  		b.OpenFailureThreshold = openFailureThreshold
    21  	}
    22  }
    23  
    24  // OptHalfOpenMaxActions sets the HalfOpenMaxActions.
    25  func OptHalfOpenMaxActions(maxActions int64) Option {
    26  	return func(b *Breaker) {
    27  		b.HalfOpenMaxActions = maxActions
    28  	}
    29  }
    30  
    31  // OptClosedExpiryInterval sets the ClosedExpiryInterval.
    32  func OptClosedExpiryInterval(interval time.Duration) Option {
    33  	return func(b *Breaker) {
    34  		b.ClosedExpiryInterval = interval
    35  	}
    36  }
    37  
    38  // OptOpenExpiryInterval sets the OpenExpiryInterval.
    39  func OptOpenExpiryInterval(interval time.Duration) Option {
    40  	return func(b *Breaker) {
    41  		b.OpenExpiryInterval = interval
    42  	}
    43  }
    44  
    45  // OptConfig sets the breaker based on a config.
    46  func OptConfig(cfg Config) Option {
    47  	return func(b *Breaker) {
    48  		b.HalfOpenMaxActions = cfg.HalfOpenMaxActions
    49  		b.ClosedExpiryInterval = cfg.ClosedExpiryInterval
    50  		b.OpenExpiryInterval = cfg.OpenExpiryInterval
    51  	}
    52  }
    53  
    54  // OptOpenAction sets the open action on the breaker.
    55  //
    56  // The "Open" action is called when the breaker opens,
    57  // that is, when it no longer allows calls.
    58  func OptOpenAction(action Actioner) Option {
    59  	return func(b *Breaker) {
    60  		b.OpenAction = action
    61  	}
    62  }
    63  
    64  // OptOnStateChange sets the OnStateChange handler on the breaker.
    65  func OptOnStateChange(handler OnStateChangeHandler) Option {
    66  	return func(b *Breaker) {
    67  		b.OnStateChange = handler
    68  	}
    69  }
    70  
    71  // OptShouldOpenProvider sets the ShouldCloseProvider provider on the breaker.
    72  func OptShouldOpenProvider(provider ShouldOpenProvider) Option {
    73  	return func(b *Breaker) {
    74  		b.ShouldOpenProvider = provider
    75  	}
    76  }
    77  
    78  // OptNowProvider sets the now provider on the breaker.
    79  func OptNowProvider(provider NowProvider) Option {
    80  	return func(b *Breaker) {
    81  		b.NowProvider = provider
    82  	}
    83  }