From ac695603b759edaa37714a25bb5e7de733454164 Mon Sep 17 00:00:00 2001 From: samosvalishe Date: Thu, 21 May 2026 00:43:48 +0300 Subject: [PATCH] =?UTF-8?q?fix(bondclient):=20=D1=81=D0=BB=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20Hello=20=D1=81=20=D1=80=D0=B5=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=BC=20=D1=87=D0=B8=D1=81=D0=BB=D0=BE=D0=BC=20lane=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Открыть все кандидатные стримы сначала, затем писать Hello с len(opened), чтобы bondserver.waitForInitialLanes не зависал на lane, которые никогда не открывались (закрыты/ошибка). Также документирует, почему клиентский write-retry не зацикливается как серверный: набор lane клиента фиксирован на время жизни Handle. --- internal/proxy/bondclient/bondclient.go | 37 ++++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/internal/proxy/bondclient/bondclient.go b/internal/proxy/bondclient/bondclient.go index c03031e..5fd4d56 100644 --- a/internal/proxy/bondclient/bondclient.go +++ b/internal/proxy/bondclient/bondclient.go @@ -55,9 +55,15 @@ func (h *Handler) Handle(ctx context.Context, tcpConn net.Conn, connID uint64, c ctx, cancel := context.WithCancel(ctx) defer cancel() - lanes := make([]*lane, 0, len(candidates)) - laneIDs := make([]string, 0, len(candidates)) - for i, ps := range candidates { + // Phase 1: open streams on usable sessions. Phase 2: send Hello with the + // actual lane count so bondserver.waitForInitialLanes doesn't wait on lanes + // that were never opened. + type pending struct { + ps *tcpfwd.PooledSession + stream *smux.Stream + } + opened := make([]pending, 0, len(candidates)) + for _, ps := range candidates { if ps.Sess.IsClosed() { continue } @@ -66,15 +72,22 @@ func (h *Handler) Handle(ctx context.Context, tcpConn net.Conn, connID uint64, c h.Deps.log().Errorf("[bond %d] session %d open stream error: %s", connID, ps.ID, err) continue } - if err := bondframe.WriteHello(stream, connID, uint16(i), uint16(len(candidates))); err != nil { - h.Deps.log().Errorf("[bond %d] session %d hello error: %s", connID, ps.ID, err) - _ = stream.Close() + opened = append(opened, pending{ps: ps, stream: stream}) + } + + lanes := make([]*lane, 0, len(opened)) + laneIDs := make([]string, 0, len(opened)) + laneCount := uint16(len(opened)) + for i, p := range opened { + if err := bondframe.WriteHello(p.stream, connID, uint16(i), laneCount); err != nil { + h.Deps.log().Errorf("[bond %d] session %d hello error: %s", connID, p.ps.ID, err) + _ = p.stream.Close() continue } - ps.Opened.Add(1) - ps.Active.Add(1) - lanes = append(lanes, &lane{ps: ps, stream: stream}) - laneIDs = append(laneIDs, strconv.Itoa(ps.ID)) + p.ps.Opened.Add(1) + p.ps.Active.Add(1) + lanes = append(lanes, &lane{ps: p.ps, stream: p.stream}) + laneIDs = append(laneIDs, strconv.Itoa(p.ps.ID)) } if len(lanes) == 0 { @@ -190,6 +203,10 @@ func (h *Handler) copyTCPToBond(ctx context.Context, connID uint64, tcpConn net. } } +// writeBondFrameToNextLane writes to the next live lane in round-robin order. +// Unlike bondserver.writeToNextLane (which loops waiting for new lanes to +// attach), the client's lane set is fixed for the lifetime of Handle, so once +// all lanes are dead there is nothing to wait for — fail fast. func writeBondFrameToNextLane(ctx context.Context, lanes []*lane, typ byte, seq uint64, data []byte, laneIdx *uint64) (*lane, error) { for range lanes { idx := *laneIdx % uint64(len(lanes))