client-go icon indicating copy to clipboard operation
client-go copied to clipboard

Configurable backoff strategy for websocket client

Open kevin1chun opened this issue 10 months ago • 0 comments

Is your feature request related to a problem? Please describe. I'd like to be able to inject a backoff strategy into the websocket client. The current implementation hardcodes an instance of ExponentialBackOff, which is suitable for most cases but the default interval is 500ms which is quite long when streaming real-time trades.

// Default values for ExponentialBackOff.
const (
	DefaultInitialInterval     = 500 * time.Millisecond
	DefaultRandomizationFactor = 0.5
	DefaultMultiplier          = 1.5
	DefaultMaxInterval         = 60 * time.Second
	DefaultMaxElapsedTime      = 15 * time.Minute
)

// NewExponentialBackOff creates an instance of ExponentialBackOff using default values.
func NewExponentialBackOff(opts ...ExponentialBackOffOpts) *ExponentialBackOff {
	b := &ExponentialBackOff{
		InitialInterval:     DefaultInitialInterval,
		RandomizationFactor: DefaultRandomizationFactor,
		Multiplier:          DefaultMultiplier,
		MaxInterval:         DefaultMaxInterval,
		MaxElapsedTime:      DefaultMaxElapsedTime,
		Stop:                Stop,
		Clock:               SystemClock,
	}

Describe the solution you'd like The ability to inject my own backoff.Backoff instance into the Client via Config

// Client defines a client to the Polygon WebSocket API.
type Client struct {
	apiKey string
	feed   Feed
	market Market
	url    string

	shouldClose bool
	backoff     backoff.BackOff

Describe alternatives you've considered We could parameterize the backoff strategy itself by adding parameters like initialInterval to Config but that is verbose and not future proof. I agree that this is a sane default, but being able to inject in a custom backoff strategy is ideal.

Additional context

kevin1chun avatar Apr 09 '25 02:04 kevin1chun