refactor: improve error wrapping and extract constructor helper

This commit is contained in:
zarazaex69
2026-05-25 04:06:26 +03:00
parent d1c9c691dd
commit e677261fd5
2 changed files with 25 additions and 11 deletions

View File

@@ -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 {

View File

@@ -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) {