Skip to content

Commit 9e19f95

Browse files
committed
2026-05-28 11:58:38
1 parent 40f522b commit 9e19f95

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

protocol/etch/etch.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
)
1616

1717
// Protocol etch is a reliable, ordered, stream-oriented protocol implemented on top of udp. We call it rudp. It is
18-
// Shaped after tcp: data is sent as a stream of bytes, segments may arrive out of order or be lost, and the protocol
19-
// Is responsible for reassembly, retransmission, flow control and congestion control.
18+
// shaped after tcp: data is sent as a stream of bytes, segments may arrive out of order or be lost, and the protocol
19+
// is responsible for reassembly, retransmission, flow control and congestion control.
2020
//
2121
// The packet format is fixed 16-byte header followed by an optional payload:
2222
//
@@ -44,10 +44,10 @@ import (
4444
// - Msg : Data, may be empty. Only carries when cmd is ACK; SYN and FIN do not carry payload.
4545
//
4646
// Reliability is provided by sequence numbers, cumulative acknowledgements, a sliding window of inflight segments,
47-
// Timeout based retransmission with an rfc6298 style rto estimator, and a fast retransmit triggered by three
48-
// Duplicate acknowledgements. Congestion control follows the classic tcp-tahoe scheme of slow start and congestion
49-
// Avoidance with aimd on loss. Both the congestion window and the peer advertised window are measured in bytes; the
50-
// Sender is allowed to inject up to min(cwnd, rwnd) bytes into the network at any time.
47+
// timeout based retransmission with an rfc6298 style rto estimator, and a fast retransmit triggered by three
48+
// duplicate acknowledgements. Congestion control follows the classic tcp-tahoe scheme of slow start and congestion
49+
// avoidance with aimd on loss. Both the congestion window and the peer advertised window are measured in bytes; the
50+
// sender is allowed to inject up to min(cwnd, rwnd) bytes into the network at any time.
5151

5252
// Conf is acting as package level configuration.
5353
var Conf = struct {
@@ -244,7 +244,7 @@ func (r *Rtt) Update(sample time.Duration) {
244244
}
245245

246246
// Stream is a reliable bidirectional byte stream over the udp link. It implements io.ReadWriteCloser. All internal
247-
// State is guarded by mu, and progress is announced via cnd.
247+
// state is guarded by mu, and progress is announced via cnd.
248248
type Stream struct {
249249
addr net.Addr
250250
lnk io.ReadWriteCloser
@@ -333,7 +333,7 @@ func (c *Stream) sendable() uint32 {
333333
}
334334

335335
// Emit pushes a packet to the link. It must be called with mu held; the link write itself happens under the lock to
336-
// Preserve send ordering relative to state updates.
336+
// preserve send ordering relative to state updates.
337337
func (c *Stream) emit(p Packet) error {
338338
p.ack = c.rcvNxt
339339
p.win = c.rcvWnd()
@@ -354,11 +354,11 @@ func (c *Stream) fail(err error) {
354354
// ============================================================================
355355

356356
// Flush moves bytes from sndBuf into the inflight queue as long as the window permits, then sends any pending pure
357-
// Ack. Must be called with mu held.
357+
// ack. Must be called with mu held.
358358
func (c *Stream) flush() {
359359
// Data may be drained from sndBuf in any connected state until our own fin has been emitted. This matters during
360-
// Half-close: app code typically calls Write followed immediately by Close, which transitions us into fin_wait or
361-
// Last_ack while bytes are still queued. We must continue pushing those bytes before the fin segment.
360+
// half-close: app code typically calls Write followed immediately by Close, which transitions us into fin_wait or
361+
// last_ack while bytes are still queued. We must continue pushing those bytes before the fin segment.
362362
for !c.sndFinDone && (c.sid == stateEstablished || c.sid == stateFinWait || c.sid == stateCloseWait || c.sid == stateLastAck) {
363363
win := c.sendable()
364364
if win == 0 || len(c.sndBuf) == 0 {
@@ -402,7 +402,7 @@ func (c *Stream) flush() {
402402
}
403403
c.sndFinSeq = c.sndNxt
404404
c.sndFinDone = true
405-
c.sndNxt += 1
405+
c.sndNxt++
406406
c.inflight = append(c.inflight, seg)
407407
if err := c.emit(Packet{cmd: seg.cmd, seq: seg.seq}); err != nil {
408408
c.fail(err)
@@ -449,7 +449,7 @@ func (c *Stream) retransmit(now time.Time) {
449449
}
450450

451451
// Ack processes a cumulative acknowledgement. It removes acked segments from the inflight queue, updates the rtt
452-
// Estimator and the congestion window. Must be called with mu held.
452+
// estimator and the congestion window. Must be called with mu held.
453453
func (c *Stream) ack(ackNum uint32, win uint32) {
454454
c.rwnd = win
455455
if SeqGe(c.sndUna, ackNum) && ackNum != c.sndUna {
@@ -517,7 +517,7 @@ func (c *Stream) ack(ackNum uint32, win uint32) {
517517
// Recv path
518518
// ============================================================================
519519

520-
// Deliver appends an in-order data slice to the recv buffer and reno to drain rcvOoo. Must be called with mu held.
520+
// Deliver appends an in-order data slice to the recv buffer and tries to drain rcvOoo. Must be called with mu held.
521521
func (c *Stream) deliver(data []byte) {
522522
c.rcvBuf = append(c.rcvBuf, data...)
523523
c.rcvNxt += uint32(len(data))
@@ -626,7 +626,7 @@ func (c *Stream) intake(seq uint32, data []byte) {
626626
}
627627

628628
// IntakeFin records a fin segment, possibly via the out-of-order map until preceding data arrives. Must be called
629-
// With mu held.
629+
// with mu held.
630630
func (c *Stream) intakeFin(seq uint32) {
631631
if SeqLt(seq, c.rcvNxt) {
632632
return
@@ -757,9 +757,9 @@ func (c *Stream) Write(p []byte) (int, error) {
757757
}
758758

759759
// Close implements io.Closer. It performs an orderly half-close: any bytes already accepted by Write are pushed to
760-
// The peer first, then a fin is sent and acknowledged. The call blocks for at most HandshakeTimeout before forcibly
761-
// Tearing down the link. After Close returns the connection lingers briefly so that any incoming peer fin can still
762-
// Be acknowledged.
760+
// the peer first, then a fin is sent and acknowledged. The call blocks for at most HandshakeTimeout before forcibly
761+
// tearing down the link. After Close returns the connection lingers briefly so that any incoming peer fin can still
762+
// be acknowledged.
763763
func (c *Stream) Close() error {
764764
c.mu.Lock()
765765
if c.sid == stateDead || c.wer.Get() != nil {
@@ -797,7 +797,7 @@ func (c *Stream) Close() error {
797797
c.wer.Put(io.ErrClosedPipe)
798798
c.mu.Unlock()
799799
// Linger so we can acknowledge a peer fin that arrives just after our own fin was acked. This avoids stranding the
800-
// Peer in fin_wait until its idle deadline expires.
800+
// peer in fin_wait until its idle deadline expires.
801801
go func() {
802802
t := time.NewTimer(time.Second)
803803
defer t.Stop()
@@ -835,13 +835,12 @@ func (c *Stream) dialHandshake() error {
835835
// Send syn synchronously and wait for syn-ack with exponential backoff.
836836
rto := Conf.RTONew
837837
deadline := time.Now().Add(Conf.HandshakeTimeout)
838-
for try := range Conf.HandshakeRereno {
839-
_ = try
838+
for range Conf.HandshakeRereno {
840839
if _, err := c.lnk.Write(PacketEncode(Packet{cmd: cmdSyn, seq: 0, win: uint32(Conf.RecvBufSize)})); err != nil {
841840
return err
842841
}
843842
// Read with a timeout managed by the udp socket. dialLink uses a connected udp socket; SetReadDeadline is
844-
// Needed. We poke into the concrete type to set the deadline.
843+
// needed. We poke into the concrete type to set the deadline.
845844
dl, ok := c.lnk.(*ConnCli)
846845
if !ok {
847846
return errors.New("daze: invalid dial link")
@@ -951,7 +950,7 @@ func (l *Listener) detach(key string) {
951950
}
952951

953952
// Demux reads udp packets and routes them to the matching connection. New peers that send a syn cause a fresh Stream
954-
// To be created and pushed into the accept queue.
953+
// to be created and pushed into the accept queue.
955954
func (l *Listener) demux() {
956955
buf := make([]byte, 65536)
957956
for {

0 commit comments

Comments
 (0)