gbn

package
v0.3.1-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 12 Imported by: 4

Documentation

Index

Constants

View Source
const (
	SYN    = 0x01
	DATA   = 0x02
	ACK    = 0x03
	NACK   = 0x04
	FIN    = 0x05
	SYNACK = 0x06

	FALSE = 0x00
	TRUE  = 0x01
)
View Source
const (
	DefaultSendTimeout = math.MaxInt64
	DefaultRecvTimeout = math.MaxInt64
)
View Source
const (
	DefaultN = 20
)
View Source
const Subsystem = "GOBN"

Variables

This section is empty.

Functions

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type BackoffWaiter

type BackoffWaiter struct {
	// contains filtered or unexported fields
}

func NewBackoffWaiter

func NewBackoffWaiter(initial, min, max time.Duration) *BackoffWaiter

func (*BackoffWaiter) Wait

func (b *BackoffWaiter) Wait()

type GoBackNConn

type GoBackNConn struct {
	// contains filtered or unexported fields
}

func NewClientConn

func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
	receiveFunc recvBytesFunc, opts ...Option) (*GoBackNConn, error)

NewClientConn creates a new bidirectional Go-Back-N client. The sendStream function must write to the underlying transport stream. The receiveStream function must read from an underlying transport stream. The resendTimeout parameter defines the duration to wait before resending data if the corresponding ACK for the data is not received.

func NewServerConn

func NewServerConn(ctx context.Context, sendFunc sendBytesFunc,
	recvFunc recvBytesFunc, opts ...Option) (*GoBackNConn, error)

NewServerConn creates a new bidirectional Go-Back-N server. The sendStream function must write to the underlying transport stream. The receiveStream function must read from an underlying transport stream. The resendTimeout parameter defines the duration to wait before resending data if the corresponding ACK for the data is not received.

func (*GoBackNConn) Close

func (g *GoBackNConn) Close() error

Close attempts to cleanly close the connection by sending a FIN message.

func (*GoBackNConn) Recv

func (g *GoBackNConn) Recv() ([]byte, error)

Recv blocks until it gets a recv with the correct sequence it was expecting.

func (*GoBackNConn) Send

func (g *GoBackNConn) Send(data []byte) error

Send blocks until an ack is received for the packet sent N packets before.

func (*GoBackNConn) SetRecvTimeout

func (g *GoBackNConn) SetRecvTimeout(timeout time.Duration)

SetRecvTimeout sets the timeout used in the Recv function.

func (*GoBackNConn) SetSendTimeout

func (g *GoBackNConn) SetSendTimeout(timeout time.Duration)

SetSendTimeout sets the timeout used in the Send function.

type IntervalAwareForceTicker

type IntervalAwareForceTicker struct {

	// Force is used to force-feed a ticks into the ticker. Useful for
	// debugging when trying to wake an event.
	Force chan time.Time
	// contains filtered or unexported fields
}

IntervalAwareForceTicker implements the Ticker interface, and provides a method of force-feeding ticks, even while paused. This is a copy of lnd's ticker.Force that is also aware when the last timed tick happened and how long approximately it takes until the next timed tick happens.

func NewIntervalAwareForceTicker

func NewIntervalAwareForceTicker(interval time.Duration) *IntervalAwareForceTicker

NewIntervalAwareForceTicker returns a IntervalAwareForceTicker ticker, used for testing and debugging. It supports the ability to force-feed events that get output by the channel returned by Ticks().

func (*IntervalAwareForceTicker) ForceTick

func (t *IntervalAwareForceTicker) ForceTick()

ForceTick force feeds an event into the ticker channel and resets the internal clock ticker causing the next clock tick to occur in the configured interval.

func (*IntervalAwareForceTicker) IsActive

func (t *IntervalAwareForceTicker) IsActive() bool

IsActive returns true if the timed ticks are currently forwarded to the Force channel.

func (*IntervalAwareForceTicker) LastTimedTick

func (t *IntervalAwareForceTicker) LastTimedTick() time.Time

LastTimedTick returns the timestamp when the last tick occurred that was fired by the underlying clock. This does not mean that the tick was necessarily also forwarded to the Force channel. If we are paused, this timestamp is still updated but no ticks are sent to the channel.

func (*IntervalAwareForceTicker) NextTickIn

func (t *IntervalAwareForceTicker) NextTickIn() time.Duration

NextTickIn returns the approximate duration until the next timed tick will occur.

func (*IntervalAwareForceTicker) Pause

func (t *IntervalAwareForceTicker) Pause()

Pause suspends the underlying ticker, such that Ticks() stops signaling at regular intervals.

NOTE: Part of the Ticker interface.

func (*IntervalAwareForceTicker) Reset

func (t *IntervalAwareForceTicker) Reset()

Reset restarts the ticker interval, causing the next clock tick to occur in the configured interval.

func (*IntervalAwareForceTicker) ResetWithInterval

func (t *IntervalAwareForceTicker) ResetWithInterval(newInterval time.Duration)

ResetWithInterval restarts the ticker with the given interval, causing the next clock tick to occur in the given interval.

func (*IntervalAwareForceTicker) Resume

func (t *IntervalAwareForceTicker) Resume()

Resume starts underlying time.Ticker and causes the ticker to begin delivering scheduled events.

NOTE: Part of the Ticker interface.

func (*IntervalAwareForceTicker) Stop

func (t *IntervalAwareForceTicker) Stop()

Stop suspends the underlying ticker, such that Ticks() stops signaling at regular intervals, and permanently frees up any resources.

NOTE: Part of the Ticker interface.

func (*IntervalAwareForceTicker) Ticks

func (t *IntervalAwareForceTicker) Ticks() <-chan time.Time

Ticks returns a receive-only channel that delivers times at the ticker's prescribed interval when active. Force-fed ticks can be delivered at any time.

NOTE: Part of the Ticker interface.

type Message

type Message interface {
	Serialize() ([]byte, error)
}

func Deserialize

func Deserialize(b []byte) (Message, error)

type Option

type Option func(conn *config)

func WithMaxSendSize

func WithMaxSendSize(size int) Option

WithMaxSendSize is used to set the maximum payload size in bytes per packet. If set and a large payload comes through then it will be split up into multiple packets with payloads no larger than the given maximum size. A size of zero will disable splitting.

func WithOnFIN

func WithOnFIN(fn func()) Option

WithOnFIN is used to set the onFIN callback that will be called once a FIN packet has been received and processed.

func WithTimeoutOptions

func WithTimeoutOptions(opts ...TimeoutOptions) Option

WithTimeoutOptions is used to set the different timeout options that will be used within gbn package.

type PacketACK

type PacketACK struct {
	Seq uint8
}

func (*PacketACK) Serialize

func (m *PacketACK) Serialize() ([]byte, error)

type PacketData

type PacketData struct {
	Seq        uint8
	FinalChunk bool
	IsPing     bool
	Payload    []byte
}

func (*PacketData) Serialize

func (m *PacketData) Serialize() ([]byte, error)

type PacketFIN

type PacketFIN struct {
}

func (*PacketFIN) Serialize

func (m *PacketFIN) Serialize() ([]byte, error)

type PacketNACK

type PacketNACK struct {
	Seq uint8
}

func (*PacketNACK) Serialize

func (m *PacketNACK) Serialize() ([]byte, error)

type PacketSYN

type PacketSYN struct {
	N uint8
}

func (*PacketSYN) Serialize

func (m *PacketSYN) Serialize() ([]byte, error)

type PacketSYNACK

type PacketSYNACK struct{}

func (*PacketSYNACK) Serialize

func (m *PacketSYNACK) Serialize() ([]byte, error)

type TimeoutBooster

type TimeoutBooster struct {
	// contains filtered or unexported fields
}

TimeoutBooster is used to boost a timeout by a given percentage value. The timeout will be boosted by the percentage value of the boostPercent any time the Boost function is called, and is cumulative.

func NewTimeoutBooster

func NewTimeoutBooster(originalTimeout time.Duration, boostPercent float32,
	withBoostFrequencyLimit bool) *TimeoutBooster

NewTimeoutBooster creates a new timeout booster. The originalTimeout defines the base timeout value that is boosted. The timeout will be boosted by the percentage value of the boostPercent any time the Boost function is called. Finally if the withBoostFrequencyLimit is set, then there is a cap to how often the timeout can be boosted, which is the duration of the original timeout.

func (*TimeoutBooster) Boost

func (b *TimeoutBooster) Boost()

Boost boosts the timeout by the boost percent. If the withBoostFrequencyLimit is set, then the boost will only be applied if the duration of the original timeout has passed since the last boost that had any affect was applied.

func (*TimeoutBooster) GetCurrentTimeout

func (b *TimeoutBooster) GetCurrentTimeout() time.Duration

GetCurrentTimeout returns the value of the timeout, with the boost applied.

func (*TimeoutBooster) Reset

func (b *TimeoutBooster) Reset(newTimeout time.Duration)

Reset removes the current applied boost, and sets the original timeout to the passed timeout. It also restarts the frequency limit timeout if the withBoostFrequencyLimit was set to true when initializing the TimeoutBooster.

type TimeoutManager

type TimeoutManager struct {
	// contains filtered or unexported fields
}

TimeoutManager manages the different timeouts used by the gbn package.

func NewTimeOutManager

func NewTimeOutManager(logger btclog.Logger,
	timeoutOpts ...TimeoutOptions) *TimeoutManager

NewTimeOutManager creates a new timeout manager.

func (*TimeoutManager) GetFinSendTimeout

func (m *TimeoutManager) GetFinSendTimeout() time.Duration

GetFinSendTimeout returns the fin send timeout.

func (*TimeoutManager) GetHandshakeTimeout

func (m *TimeoutManager) GetHandshakeTimeout() time.Duration

GetHandshakeTimeout returns the handshake timeout.

func (*TimeoutManager) GetPingTime

func (m *TimeoutManager) GetPingTime() time.Duration

GetPingTime returns the ping time, representing at which frequency we will send pings to the counterparty if we've received no packet.

func (*TimeoutManager) GetPongTime

func (m *TimeoutManager) GetPongTime() time.Duration

GetPongTime returns the pong timeout, representing how long we will wait for the expect a pong response after we've sent a ping. If no response is received within the time limit, we will close the connection.

func (*TimeoutManager) GetRecvTimeout

func (m *TimeoutManager) GetRecvTimeout() time.Duration

GetRecvTimeout returns the recv timeout.

func (*TimeoutManager) GetResendTimeout

func (m *TimeoutManager) GetResendTimeout() time.Duration

GetResendTimeout returns the current resend timeout.

func (*TimeoutManager) GetSendTimeout

func (m *TimeoutManager) GetSendTimeout() time.Duration

GetSendTimeout returns the send timeout.

func (*TimeoutManager) Received

func (m *TimeoutManager) Received(msg Message)

Received should be called when a message is received by the connection.

func (*TimeoutManager) Sent

func (m *TimeoutManager) Sent(msg Message, resent bool)

Sent should be called when a message is sent by the connection. The resent parameter should be set to true if the message is a resent message.

func (*TimeoutManager) SetRecvTimeout

func (m *TimeoutManager) SetRecvTimeout(timeout time.Duration)

SetRecvTimeout sets the receive timeout.

func (*TimeoutManager) SetSendTimeout

func (m *TimeoutManager) SetSendTimeout(timeout time.Duration)

SetSendTimeout sets the send timeout.

type TimeoutOptions

type TimeoutOptions func(manager *TimeoutManager)

TimeoutOptions can be used to modify the default timeout values used within the TimeoutManager.

func WithBoostPercent

func WithBoostPercent(boostPercent float32) TimeoutOptions

WithBoostPercent is used to set the boost percent that the timeout manager will use to boost the resend timeout & handshake timeout every time a resend is required due to not receiving a response within the current timeout.

func WithHandshakeTimeout

func WithHandshakeTimeout(timeout time.Duration) TimeoutOptions

WithHandshakeTimeout is used to set the timeout used during the handshake. If the timeout is reached without response from the peer then the handshake will be aborted and restarted.

func WithKeepalivePing

func WithKeepalivePing(ping, pong time.Duration) TimeoutOptions

WithKeepalivePing is used to send a ping packet if no packets have been received from the other side for the given duration. This helps keep the connection alive and also ensures that the connection is closed if the other side does not respond to the ping in a timely manner. After the ping the connection will be closed if the other side does not respond within time duration.

func WithResendMultiplier

func WithResendMultiplier(multiplier int) TimeoutOptions

WithResendMultiplier is used to set the resend multiplier. This is the multiplier we use when dynamically setting the resend timeout, based on how long it took for other party to respond. Note that when setting the resend timeout manually with the WithStaticResendTimeout option, this option will have no effect. Note that the passed multiplier must be greater than zero or this option will have no effect.

func WithStaticResendTimeout

func WithStaticResendTimeout(timeout time.Duration) TimeoutOptions

WithStaticResendTimeout is used to set a static resend timeout. This is the time to wait for ACKs before resending the queue.

func WithTimeoutUpdateFrequency

func WithTimeoutUpdateFrequency(frequency int) TimeoutOptions

WithTimeoutUpdateFrequency is used to set the frequency of how many corresponding responses we need to receive until updating the resend timeout. Note that when setting the resend timeout manually with the WithTimeout option, this option will have no effect. Also note that the passed frequency must be greater than zero or this option will have no effect.

Jump to

Keyboard shortcuts

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