Skip to content

Commit

Permalink
Use a custom error type instead of string matching
Browse files Browse the repository at this point in the history
  • Loading branch information
mjh1 committed Sep 1, 2023
1 parent 72ef55c commit 6f28920
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions server/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var submitMultiSession = func(ctx context.Context, sess *BroadcastSession, seg *
nonce uint64, calcPerceptualHash bool, resc chan *SubmitResult) {
go submitSegment(ctx, sess, seg, segPar, nonce, calcPerceptualHash, resc)
}
var maxTranscodeAttempts = errors.New("hit max transcode attempts")

type BroadcastConfig struct {
maxPrice *big.Rat
Expand Down Expand Up @@ -986,7 +987,7 @@ func processSegment(ctx context.Context, cxn *rtmpConnection, seg *stream.HLSSeg
}()
}
if len(attempts) == MaxAttempts && err != nil {
err = fmt.Errorf("Hit max transcode attempts: %w", err)
err = fmt.Errorf("%w: %w", maxTranscodeAttempts, err)
if monitor.Enabled {
monitor.SegmentTranscodeFailed(ctx, monitor.SegmentTranscodeErrorMaxAttempts, nonce, seg.SeqNo, err, true)
}
Expand Down Expand Up @@ -1612,7 +1613,7 @@ func isNonRetryableError(err error) bool {
return true
}
}
if strings.HasPrefix(err.Error(), "Hit max transcode attempts:") {
if errors.Is(err, maxTranscodeAttempts) {
return true
}
return false
Expand Down
3 changes: 2 additions & 1 deletion server/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,8 @@ func TestProcessSegment_MaxAttempts(t *testing.T) {
transcodeCalls = 0
_, err = processSegment(context.Background(), cxn, seg, nil)
assert.NotNil(err)
assert.Equal("Hit max transcode attempts: UnknownResponse", err.Error())
assert.True(errors.Is(err, maxTranscodeAttempts))
assert.Equal("hit max transcode attempts: UnknownResponse", err.Error())
assert.Equal(1, transcodeCalls, "Segment submission calls did not match")
assert.Len(bsm.trustedPool.sessMap, 1)

Expand Down

0 comments on commit 6f28920

Please sign in to comment.