Skip to content

Configurable backoff strategy for websocket client #528

@kevin1chun

Description

@kevin1chun

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions