@@ -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
5453func (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
0 commit comments