Skip to content

Commit e903a2e

Browse files
committed
normalize runner errors
1 parent 6b1a11f commit e903a2e

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

README.MD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ thr := NewThrottlerAll( // throttles only if all children throttle
130130
)
131131
```
132132

133+
In gohalt v0.4.0 breaking change is introduced to replace all untyped errors with two major error types:
134+
- `ErrorThreshold` which defines error type that occurs if throttler reaches specified threshold.
135+
- `ErrorInternal` which defines error type that occurs if throttler internal error happens.
136+
You can find list of returning error types for all existing throttlers in throttlers table bellow or in documentation.
137+
**Note:** not every gohalt throttler must return error; some throttlers might cause different side effects like logging or call to `time.Sleep` instead.
138+
133139
## Throttlers
134140

135141
| Throttler | Definition | Description |

context_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package gohalt
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"testing"
87
"time"
98

@@ -28,18 +27,15 @@ func TestContext(t *testing.T) {
2827
}{
2928
"Context with throttler should be done on throttling": {
3029
ctx: WithThrottler(context.Background(), tmock{aerr: testerr}, ms1_0),
31-
err: fmt.Errorf("throttler error has happened %w", testerr),
30+
err: testerr,
3231
},
3332
"Context with throttler should be done on throttling after": {
3433
ctx: WithThrottler(context.Background(), NewThrottlerAfter(1), ms1_0),
35-
err: fmt.Errorf(
36-
"throttler error has happened %w",
37-
ErrorThreshold{Throttler: "after", Threshold: strpair{current: 3, threshold: 1}},
38-
),
34+
err: ErrorThreshold{Throttler: "after", Threshold: strpair{current: 3, threshold: 1}},
3935
},
4036
"Context with throttler should be done with canceled context": {
4137
ctx: WithThrottler(cctx, tmock{}, ms1_0),
42-
err: fmt.Errorf("context error has happened %w", cctx.Err()),
38+
err: cctx.Err(),
4339
},
4440
"Context with throttler should not be done after timeout": {
4541
ctx: WithThrottler(ctx, tmock{}, ms1_0),

executors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func retried(retries uint64, run Runnable) Runnable {
9696
if err == nil {
9797
return
9898
}
99-
log("retry error happened: %v", err)
99+
log("error happened in retry: %v", err)
100100
}
101101
return
102102
}

runners.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gohalt
22

33
import (
44
"context"
5-
"fmt"
65
"sync"
76
)
87

@@ -46,27 +45,27 @@ func NewRunnerSync(ctx context.Context, thr Throttler) Runner {
4645
func (r *rsync) Run(run Runnable) {
4746
select {
4847
case <-r.ctx.Done():
49-
r.report(fmt.Errorf("context error has happened %w", r.ctx.Err()))
48+
r.report(r.ctx.Err())
5049
return
5150
default:
5251
}
5352
defer func() {
5453
if err := r.thr.Release(r.ctx); err != nil {
55-
r.report(fmt.Errorf("throttler error has happened %w", err))
54+
r.report(err)
5655
}
5756
}()
5857
if err := r.thr.Acquire(r.ctx); err != nil {
59-
r.report(fmt.Errorf("throttler error has happened %w", err))
58+
r.report(err)
6059
return
6160
}
6261
select {
6362
case <-r.ctx.Done():
64-
r.report(fmt.Errorf("context error has happened %w", r.ctx.Err()))
63+
r.report(r.ctx.Err())
6564
return
6665
default:
6766
}
6867
if err := run(r.ctx); err != nil {
69-
r.report(fmt.Errorf("runnable error has happened %w", err))
68+
r.report(err)
7069
return
7170
}
7271
}
@@ -109,27 +108,27 @@ func (r *rasync) Run(run Runnable) {
109108
defer r.wg.Done()
110109
select {
111110
case <-r.ctx.Done():
112-
r.report(fmt.Errorf("context error happened %w", r.ctx.Err()))
111+
r.report(r.ctx.Err())
113112
return
114113
default:
115114
}
116115
defer func() {
117116
if err := r.thr.Release(r.ctx); err != nil {
118-
r.report(fmt.Errorf("throttler error has happened %w", err))
117+
r.report(err)
119118
}
120119
}()
121120
if err := r.thr.Acquire(r.ctx); err != nil {
122-
r.report(fmt.Errorf("throttler error has happened %w", err))
121+
r.report(err)
123122
return
124123
}
125124
select {
126125
case <-r.ctx.Done():
127-
r.report(fmt.Errorf("context error has happened %w", r.ctx.Err()))
126+
r.report(r.ctx.Err())
128127
return
129128
default:
130129
}
131130
if err := run(r.ctx); err != nil {
132-
r.report(fmt.Errorf("runnable error has happened %w", err))
131+
r.report(err)
133132
return
134133
}
135134
}()

runners_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package gohalt
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"testing"
87

98
"github.com/stretchr/testify/assert"
@@ -21,42 +20,42 @@ func TestRunners(t *testing.T) {
2120
"Runner sync should return error on throttling": {
2221
r: NewRunnerSync(context.Background(), tmock{aerr: testerr}),
2322
run: nope,
24-
err: fmt.Errorf("throttler error has happened %w", testerr),
23+
err: testerr,
2524
},
2625
"Runner sync should return error on realising error": {
2726
r: NewRunnerSync(context.Background(), tmock{rerr: testerr}),
2827
run: nope,
29-
err: fmt.Errorf("throttler error has happened %w", testerr),
28+
err: testerr,
3029
},
3130
"Runner sync should return error on runnable error": {
3231
r: NewRunnerSync(context.Background(), tmock{}),
3332
run: use(testerr),
34-
err: fmt.Errorf("runnable error has happened %w", testerr),
33+
err: testerr,
3534
},
3635
"Runner sync should return error on canceled context": {
3736
r: NewRunnerSync(cctx, tmock{}),
3837
run: nope,
39-
err: fmt.Errorf("context error has happened %w", cctx.Err()),
38+
err: cctx.Err(),
4039
},
4140
"Runner async should return error on throttling": {
4241
r: NewRunnerAsync(context.Background(), tmock{aerr: testerr}),
4342
run: nope,
44-
err: fmt.Errorf("throttler error has happened %w", testerr),
43+
err: testerr,
4544
},
4645
"Runner async should return error on realising error": {
4746
r: NewRunnerAsync(context.Background(), tmock{rerr: testerr}),
4847
run: nope,
49-
err: fmt.Errorf("throttler error has happened %w", testerr),
48+
err: testerr,
5049
},
5150
"Runner async should return error on runnable error": {
5251
r: NewRunnerAsync(context.Background(), tmock{}),
5352
run: use(testerr),
54-
err: fmt.Errorf("runnable error has happened %w", testerr),
53+
err: testerr,
5554
},
5655
"Runner async should return error on canceled context": {
5756
r: NewRunnerAsync(cctx, tmock{}),
5857
run: nope,
59-
err: fmt.Errorf("context error happened %w", cctx.Err()),
58+
err: cctx.Err(),
6059
},
6160
}
6261
for tname, tcase := range table {

0 commit comments

Comments
 (0)