Documentation ¶
Overview ¶
Package retransmission implements a simple retransmission mechanism for network messages based on their sequence number. Retransmitting message several times for the lifetime of the given phase helps to improve message delivery rate for senders and receivers who are not perfectly synced on time.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ScheduleRetransmissions ¶
func ScheduleRetransmissions( ctx context.Context, logger log.StandardLogger, ticker *Ticker, retransmit RetransmitFn, strategy Strategy, )
ScheduleRetransmissions uses the given Strategy to decide whether to call the provided RetransmitFn for every new tick received from the provided Ticker for the entire lifetime of the Context. The RetransmitFn function has to guarantee that every call from this function sends a message with the same sequence number.
func WithRetransmissionSupport ¶
WithRetransmissionSupport takes the standard network message handler and enhances it with functionality allowing to handle retransmissions. The returned handler filters out retransmissions and calls the delegate handler only if the received message is not a retransmission or if it is a retransmission but it has not been seen by the original handler yet. The returned handler is thread-safe.
Retransmissions are identified by sender transport ID and message sequence number. Two messages with the same sender ID and sequence number are considered the same. Handler can not be reused between channels if sequence number of message is local for channel.
Types ¶
type BackoffStrategy ¶ added in v1.21.0
type BackoffStrategy struct {
// contains filtered or unexported fields
}
BackoffStrategy is a retransmission strategy that triggers the retransmission routine with an exponentially increasing delay. That is, the delay between first and second retransmission is 1 tick, between second and third is 2 ticks, between third and fourth is 4 ticks and so on. Graphically, the schedule looks as follows: R _ R _ _ R _ _ _ _ R _ _ _ _ _ _ _ _ R
func WithBackoffStrategy ¶ added in v1.21.0
func WithBackoffStrategy() *BackoffStrategy
WithBackoffStrategy uses the BackoffStrategy as the retransmission strategy.
func (*BackoffStrategy) Tick ¶ added in v1.21.0
func (bos *BackoffStrategy) Tick(retransmitFn RetransmitFn) error
Tick implements the Strategy.Tick function.
type RetransmitFn ¶ added in v1.21.0
type RetransmitFn func() error
RetransmitFn represents a retransmission routine.
type StandardStrategy ¶ added in v1.21.0
type StandardStrategy struct{}
StandardStrategy is the basic retransmission strategy that triggers the retransmission routine on every tick.
func WithStandardStrategy ¶ added in v1.21.0
func WithStandardStrategy() *StandardStrategy
WithStandardStrategy uses the StandardStrategy as the retransmission strategy.
func (*StandardStrategy) Tick ¶ added in v1.21.0
func (ss *StandardStrategy) Tick(retransmitFn RetransmitFn) error
Tick implements the Strategy.Tick function.
type Strategy ¶ added in v1.21.0
type Strategy interface { // Tick asks the strategy to run the provided retransmission routine. // The strategy uses their internal state and logic to decide whether to // call the retransmission function or not. Tick(retransmitFn RetransmitFn) error }
Strategy represents a specific retransmission strategy.
func WithStrategy ¶ added in v1.21.0
func WithStrategy(strategy net.RetransmissionStrategy) Strategy
WithStrategy is a strategy factory function that returns the requested strategy instance.
type Ticker ¶
type Ticker struct {
// contains filtered or unexported fields
}
Ticker controls the frequency of retransmissions.
func NewTicker ¶
NewTicker creates and starts a new Ticker for the provided channel. For each item read from the channel, new tick is triggered. All handlers are unregistered and ticker is stopped when the provided channel gets closed.
func NewTimeTicker ¶
NewTimeTicker is a convenience function allowing to create time-based retransmission.Ticker for the provided duration. When the provided context is done, all handlers are unregistered and retransmission.Ticker is stopped.