Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf156f1

Browse files
committed
call finish on unused messages; tidy retry logic
Signed-off-by: taehwoi oum <[email protected]>
1 parent 50b18a0 commit bf156f1

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

v2/protocol/http/protocol_retry.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"io"
1313
"io/ioutil"
1414
"net/http"
15-
"net/url"
1615
"time"
1716

1817
"go.uber.org/zap"
@@ -52,7 +51,7 @@ func (p *Protocol) doOnce(req *http.Request) (binding.Message, protocol.Result)
5251
}
5352

5453
func (p *Protocol) doWithRetry(ctx context.Context, params *cecontext.RetryParams, req *http.Request) (binding.Message, error) {
55-
then := time.Now()
54+
start := time.Now()
5655
retry := 0
5756
results := make([]protocol.Result, 0)
5857

@@ -79,51 +78,34 @@ func (p *Protocol) doWithRetry(ctx context.Context, params *cecontext.RetryParam
7978

8079
// Fast track common case.
8180
if protocol.IsACK(result) {
82-
return msg, NewRetriesResult(result, retry, then, results)
81+
return msg, NewRetriesResult(result, retry, start, results)
8382
}
8483

85-
// Try again?
86-
//
87-
// Make sure the error was something we should retry.
88-
89-
{
90-
var uErr *url.Error
91-
if errors.As(result, &uErr) {
92-
goto DoBackoff
84+
var httpResult *Result
85+
if errors.As(result, &httpResult) {
86+
sc := httpResult.StatusCode
87+
if !p.isRetriableFunc(sc) {
88+
cecontext.LoggerFrom(ctx).Debugw("status code not retryable, will not try again",
89+
zap.Error(httpResult),
90+
zap.Int("statusCode", sc))
91+
return msg, NewRetriesResult(result, retry, start, results)
9392
}
9493
}
9594

96-
{
97-
var httpResult *Result
98-
if errors.As(result, &httpResult) {
99-
sc := httpResult.StatusCode
100-
if p.isRetriableFunc(sc) {
101-
// retry!
102-
goto DoBackoff
103-
} else {
104-
// Permanent error
105-
cecontext.LoggerFrom(ctx).Debugw("status code not retryable, will not try again",
106-
zap.Error(httpResult),
107-
zap.Int("statusCode", sc))
108-
return msg, NewRetriesResult(result, retry, then, results)
109-
}
110-
}
111-
}
112-
113-
DoBackoff:
114-
resetBody(req, body)
115-
116-
// Wait for the correct amount of backoff time.
117-
11895
// total tries = retry + 1
119-
if err := params.Backoff(ctx, retry+1); err != nil {
96+
if err = params.Backoff(ctx, retry+1); err != nil {
12097
// do not try again.
12198
cecontext.LoggerFrom(ctx).Debugw("backoff error, will not try again", zap.Error(err))
122-
return msg, NewRetriesResult(result, retry, then, results)
99+
return msg, NewRetriesResult(result, retry, start, results)
123100
}
124101

125102
retry++
103+
resetBody(req, body)
126104
results = append(results, result)
105+
if msg != nil {
106+
// avoid leak, forget message, ignore error
107+
_ = msg.Finish(nil)
108+
}
127109
}
128110
}
129111

v2/protocol/http/protocol_retry_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
"go.uber.org/zap/zaptest"
1718

1819
"github.com/cloudevents/sdk-go/v2/binding"
1920
cecontext "github.com/cloudevents/sdk-go/v2/context"
@@ -192,6 +193,7 @@ func TestRequestWithRetries_linear(t *testing.T) {
192193
}
193194

194195
ctx := cecontext.WithTarget(context.Background(), srv.URL)
196+
ctx = cecontext.WithLogger(ctx, zaptest.NewLogger(t).Sugar())
195197
ctxWithRetries := cecontext.WithRetriesLinearBackoff(ctx, tc.delay, tc.retries)
196198

197199
dummyMsg := binding.ToMessage(&tc.event)

0 commit comments

Comments
 (0)