gale.net.channel module

This file contains the hand-rolled reliability layer used by gale.net’s RELIABLE_ORDERED and RELIABLE_UNORDERED channels: an outgoing side that tracks unacked packets and retransmits them, and an incoming side that deduplicates packets and (for the ordered channel) buffers out-of-order ones until the gap is filled.

UNRELIABLE traffic does not go through either of these: it is just sent once, with no tracking at all.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.net.channel.ReliableSender[source]

Bases: object

Tracks packets sent on one (peer, channel) pair that are waiting to be acked, and decides when they should be retransmitted.

is_overflowing()[source]
Returns:

Whether too many packets are in flight, unacked, on this channel. The caller should treat this peer as unreachable.

Return type:

bool

next()[source]
Returns:

The next sequence number to use for a new packet, advancing the internal counter.

Return type:

int

track(sequence, payload, now)[source]

Register a just-sent packet so it can be retransmitted if it is not acked in time.

Parameters:
  • sequence (int) – The packet’s sequence number.

  • payload (bytes) – The serialized message bytes (without the header).

  • now (float) – The current time, in seconds.

Return type:

None

acknowledge(ack, ack_bitfield)[source]

Drop every pending packet the peer has confirmed receiving, either directly (sequence == ack) or through the bitfield (sequence in the ACK_WINDOW packets right before ack).

Parameters:
  • ack (int) – The highest sequence number the peer says it received.

  • ack_bitfield (int) – Bit i set means sequence (ack - 1 - i) was also received.

Return type:

None

due_for_retransmit(now, rtt)[source]
Parameters:
  • now (float) – The current time, in seconds.

  • rtt (float) – The peer’s current smoothed round-trip time, in seconds, used to size the retransmit timeout.

Returns:

A tuple (packets, gave_up). packets is the list of (sequence, payload) pairs that should be resent now. gave_up is True if some packet exceeded MAX_RETRANSMIT_ATTEMPTS, meaning the caller should disconnect this peer.

Return type:

Tuple[List[Tuple[int, bytes]], bool]

class gale.net.channel.ReliableReceiver(ordered)[source]

Bases: object

Tracks packets received on one (peer, channel) pair: deduplicates them and, for the ordered channel, holds out-of-order packets back until the ones before them arrive.

Parameters:

ordered (bool)

receive(sequence, payload)[source]
Parameters:
  • sequence (int) – The received packet’s sequence number.

  • payload (bytes) – The received packet’s payload bytes.

Returns:

The list of payloads (possibly empty, possibly more than one) that should now be delivered to on_message callbacks, in delivery order.

Return type:

List[bytes]

build_ack()[source]
Returns:

A tuple (ack, ack_bitfield) describing what has been received so far, to be sent back to the peer.

Return type:

Tuple[int, int]