retransmission

package
v0.0.0-...-369a1d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 6 Imported by: 0

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

func WithRetransmissionSupport(delegate func(m net.Message)) func(m net.Message)

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

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

func WithBackoffStrategy() *BackoffStrategy

WithBackoffStrategy uses the BackoffStrategy as the retransmission strategy.

func (*BackoffStrategy) Tick

func (bos *BackoffStrategy) Tick(retransmitFn RetransmitFn) error

Tick implements the Strategy.Tick function.

type RetransmitFn

type RetransmitFn func() error

RetransmitFn represents a retransmission routine.

type StandardStrategy

type StandardStrategy struct{}

StandardStrategy is the basic retransmission strategy that triggers the retransmission routine on every tick.

func WithStandardStrategy

func WithStandardStrategy() *StandardStrategy

WithStandardStrategy uses the StandardStrategy as the retransmission strategy.

func (*StandardStrategy) Tick

func (ss *StandardStrategy) Tick(retransmitFn RetransmitFn) error

Tick implements the Strategy.Tick function.

type Strategy

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

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

func NewTicker(ticks <-chan uint64) *Ticker

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

func NewTimeTicker(ctx context.Context, duration time.Duration) *Ticker

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL