Skip to content
/ repeat Public

A small but powerful library for cyclic or retries operations.

License

Notifications You must be signed in to change notification settings

fschnko/repeat

Repository files navigation

repeat

GoDoc Build Status Coverage Status Go Report Card


REPEAT is a small library for cyclic or retries operations. Regardless of size, this is a very powerful library. It helps make the code more readable and elegant.

Usage:

go get github.com/fschnko/repeat

For example, a code like this:

	const (
		delay   = 5 * time.Second
		attempts = 20
	)

	ticker := time.NewTicker(delay)
	cancel := make(chan struct{})
	for i := 0; i < attempts; i++ {
		select {
		case <-ticker.C:
			err := callback()
			if err != nil {
				// handle error
			}
		case <-cancel:
			ticker.Stop()
			break
		}
	}

can be written as follows:

	const (
		delay   = 5 * time.Second
		attempts = 20
	)

	ctx, cancel := context.WithCancel(context.Background())
	err := repeat.Do(ctx, callback,
		repeat.WithAttempts(attempts),
		repeat.WithDelay(delay))
	if err != nil {
		// handle error
	}

Examples

Simple repeat (5 times as default)

	err := repeat.Do(context.Background(), callback)
	if err != nil {
		// handle error
	}

Simple repeat

	const attempts = 10
	err := repeat.Do(context.Background(), callback,
		repeat.WithAttempts(attempts),
	)
	if err != nil {
		// handle error
	}

Repeat with context timeout

const (
		attempts = 1000
		timeout = 10 * time.Second
	)

	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	err := repeat.Do(ctx, callback,
		repeat.WithAttempts(attempts),
	)
	cancel()
	if err != nil {
		// handle error
	}

Repeat with delay

const (
		attempts = 100
		delay = 10 * time.Second
	)

	err := repeat.Do(context.Background(), callback,
		repeat.WithAttempts(attempts),
		repeat.WithDelay(delay),
	)
	if err != nil {
		// handle error
	}

About

A small but powerful library for cyclic or retries operations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages