Skip to content

Instantly share code, notes, and snippets.

@reetasingh
Last active January 5, 2023 22:05
Show Gist options
  • Save reetasingh/cdfb5d8139b10d0ec171abd50e99d14a to your computer and use it in GitHub Desktop.
Save reetasingh/cdfb5d8139b10d0ec171abd50e99d14a to your computer and use it in GitHub Desktop.
type ReaderRetryDecorator struct {
reader IReader
}
func (d ReaderRetryDecorator) ReadWithRetry(ctx context.Context, retryAttempts int) error {
var err error
for i := 0; i < retryAttempts; i++ {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err := d.reader.Read()
if err == nil {
return nil
}
}
return err
}
func main() {
retryReader := ReaderRetryDecorator{Reader{}}
retryReader.ReadWithRetry(context.Background(), 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment