Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another ticker example to handle ticks arriving while the operation is running #155

Open
Jonathan-Landeed opened this issue Jan 10, 2025 · 0 comments

Comments

@Jonathan-Landeed
Copy link

I was experimenting with Ticker and realized this structure could be a useful example.

	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	for range ticker.C {
		if result, err = operation(); err != nil {
			log.Println(err, "will retry...")
			select {
			case <-ctx.Done():
				break
			case <-ticker.C:
				// consume a waiting tick
				continue
			default:
				continue
			}
			break
		}
	}

It ends up being similar to Retry behavior for slow fails, but incrementing the backoff twice for each operation. It's not always useful behavior, but it addresses the "operations that take a while to fail could run in quick succession" problem.

I also experimented with a non-blocking ticker by modifying Ticker.send:

	select {
	case t.c <- tick:
	case <-t.stop:
		return nil
	default:
	}

(also mentioned in #154)
This is more intuitive, as its tick rate is independent of slow fails, but ticks that occur during a slow fail are ignored.

I can make a PR if you're interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant