-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
800aa49
commit f00fb11
Showing
5 changed files
with
79 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package syslog | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress" | ||
) | ||
|
||
// RetryWriter wraps a WriteCloser and will retry writes if the first fails. | ||
type Retryer struct { | ||
retryDuration RetryDuration | ||
maxRetries int | ||
binding *URLBinding | ||
} | ||
|
||
func NewBackoffRetryer( | ||
urlBinding *URLBinding, | ||
retryDuration RetryDuration, | ||
maxRetries int, | ||
) *Retryer { | ||
return &Retryer{ | ||
retryDuration: retryDuration, | ||
maxRetries: maxRetries, | ||
binding: urlBinding, | ||
} | ||
} | ||
|
||
// Write will retry writes unitl maxRetries has been reached. | ||
func (r *Retryer) Retry(message []byte, fn func(msg []byte) error) error { | ||
logTemplate := "failed to write to %s, retrying in %s, err: %s" | ||
|
||
var err error | ||
|
||
for i := 0; i < r.maxRetries; i++ { | ||
err = fn(message) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if egress.ContextDone(r.binding.Context) { | ||
return err | ||
} | ||
|
||
sleepDuration := r.retryDuration(i) | ||
log.Printf(logTemplate, r.binding.URL.Host, sleepDuration, err) | ||
|
||
time.Sleep(sleepDuration) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters