Documentation
¶
Overview ¶
Package sd provides service-discovery helpers that wire together an Instancer, EndpointCache, Balancer, and Retry executor into a single callable endpoint.Endpoint.
Typical usage:
instancer := consul.NewInstancer(consulClient, logger, "my-service", true)
defer instancer.Stop()
ep := sd.NewEndpoint(instancer, factory, logger,
sd.WithMaxRetries(3),
sd.WithTimeout(500*time.Millisecond),
sd.WithInvalidateOnError(5*time.Second),
)
resp, err := ep(ctx, request)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewEndpoint ¶
func NewEndpoint( src interfaces.Instancer, factory endpoint.Factory, logger *kitlog.Logger, opts ...Option, ) endpoint.Endpoint
NewEndpoint wires together an Instancer → Endpointer → RoundRobin balancer → Retry executor and returns a single endpoint.Endpoint ready to call.
The returned endpoint automatically distributes requests across healthy instances and retries on transient failures.
func NewEndpointWithDefaults ¶
func NewEndpointWithDefaults( src interfaces.Instancer, factory endpoint.Factory, logger *kitlog.Logger, ) endpoint.Endpoint
NewEndpointWithDefaults is identical to NewEndpoint but uses sensible production defaults without requiring any options:
- MaxRetries: 3
- Timeout: 500ms
- InvalidateOnError: 5s
Use NewEndpoint when you need to customise these values.
Types ¶
type Option ¶
type Option func(*Options)
Option is a functional option for NewEndpoint.
func WithInvalidateOnError ¶
WithInvalidateOnError enables cache invalidation after a SD error. The cache is cleared once the given grace period has elapsed.
func WithMaxRetries ¶
WithMaxRetries sets the maximum retry count. 0 means retry until Timeout.
func WithTimeout ¶
WithTimeout sets the total call timeout (including retries).
type Options ¶
type Options struct {
// MaxRetries is the maximum number of retry attempts (default 3).
// Set to 0 for unlimited retries within Timeout.
MaxRetries int
// Timeout is the total time budget for one call including all retries
// (default 500ms).
Timeout time.Duration
// InvalidateOnError, when > 0, causes the endpoint cache to be cleared
// after the given duration following a service-discovery error.
InvalidateOnError time.Duration
}
Options controls the behaviour of NewEndpoint.