From e677261fd567b8cd9abeba097db0cc680f0c7a33 Mon Sep 17 00:00:00 2001 From: zarazaex69 Date: Mon, 25 May 2026 04:06:26 +0300 Subject: [PATCH] refactor: improve error wrapping and extract constructor helper --- internal/protect/protect.go | 7 ++++-- internal/transport/vp8channel/transport.go | 29 +++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/internal/protect/protect.go b/internal/protect/protect.go index b3754b0..ed5d359 100644 --- a/internal/protect/protect.go +++ b/internal/protect/protect.go @@ -108,10 +108,13 @@ func (t *retryTransport) RoundTrip(req *http.Request) (*http.Response, error) { } resp, err = t.base.RoundTrip(req) if err == nil || !isRetriableError(err) { - return resp, err + if err != nil { + return resp, fmt.Errorf("round trip: %w", err) + } + return resp, nil } } - return resp, err + return resp, fmt.Errorf("round trip after %d retries: %w", maxRetries, err) } func isRetriableError(err error) bool { diff --git a/internal/transport/vp8channel/transport.go b/internal/transport/vp8channel/transport.go index c20afe8..6d16f41 100644 --- a/internal/transport/vp8channel/transport.go +++ b/internal/transport/vp8channel/transport.go @@ -165,6 +165,22 @@ func New(ctx context.Context, cfg transport.Config) (transport.Transport, error) return nil, fmt.Errorf("create local video track: %w", err) } + tr := newStreamTransport(stream, track, cfg, opts) + + if err := stream.AddTrack(track); err != nil { + return nil, fmt.Errorf("attach local video track: %w", err) + } + stream.SetTrackHandler(tr.handleRemoteTrack) + + return tr, nil +} + +func newStreamTransport( + stream *engineVideoSession, + track *webrtc.TrackLocalStaticSample, + cfg transport.Config, + opts Options, +) *streamTransport { fps := opts.FPS batchSize := opts.BatchSize if fps <= 0 { @@ -206,12 +222,7 @@ func New(ctx context.Context, cfg transport.Config) (transport.Transport, error) tr.onData = cfg.OnData } - if err := stream.AddTrack(track); err != nil { - return nil, fmt.Errorf("attach local video track: %w", err) - } - stream.SetTrackHandler(tr.handleRemoteTrack) - - return tr, nil + return tr } func (p *streamTransport) Connect(ctx context.Context) error { @@ -758,7 +769,7 @@ func (p *streamTransport) getOrCreatePeerKCP(epoch uint32) *kcpRuntime { // peerWriterPump drains a peer's outbound KCP queue and writes frames to the // shared video track. Stops when the channel is closed or transport shuts down. -func (p *streamTransport) peerWriterPump(epoch uint32, out chan []byte) { +func (p *streamTransport) peerWriterPump(_ uint32, out chan []byte) { for { select { case <-p.closeCh: @@ -782,9 +793,9 @@ func formatPeerID(epoch uint32) string { func parsePeerID(peerID string) (uint32, error) { v, err := strconv.ParseUint(peerID, 16, 32) if err != nil { - return 0, err + return 0, fmt.Errorf("parse peer ID %q: %w", peerID, err) } - return uint32(v), nil //nolint:gosec // G115: bounded by ParseUint bitSize=32 + return uint32(v), nil } func deliverKCPPayload(rt *kcpRuntime, payload []byte) {