Last active
January 5, 2023 22:05
-
-
Save reetasingh/cdfb5d8139b10d0ec171abd50e99d14a to your computer and use it in GitHub Desktop.
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
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