Skip to content

time: create ticker with instant first tick - rebooted #41885

@benma

Description

@benma

I have the same issue as #17601, but the issue was closed and locked with this reason:

This doesn't seem to rise to the level of new API. You don't have to "copy the section of code underneath the first <-t.C", you just have to put the code in its own function and call that function once at the start and once after each timeout interval. That seems clear enough.

However, "call the function once at the start" does not work when you are not in control of when the function is called.

Consider this clumsy implementation of a rate limiting in rateLimited, vs. the much more concise and elegant one in rateLimitedImmediate:

package main

import (
	"fmt"
	"sync"
	"time"
)

var interval = time.Second

type rateLimited struct {
	rateLimiter <-chan time.Time
	f           func() string
}

func (r *rateLimited) do() string {
	<-r.rateLimiter
	return r.f()
}

type rateLimitedImmediate struct {
	mu          sync.Mutex
	rateLimiter <-chan time.Time
	f           func() string
}

func (r *rateLimitedImmediate) do() string {
	r.mu.Lock()
	defer r.mu.Unlock()
	defer func() { r.rateLimiter = time.After(interval) }()
	<-r.rateLimiter
	return r.f()
}

func main() {
	r := &rateLimited{
		rateLimiter: time.Tick(interval),
		f:           func() string { return "result" },
	}

	fmt.Println(r.do())
	fmt.Println(r.do())
	fmt.Println(r.do())

	r2 := &rateLimitedImmediate{
		rateLimiter: time.After(0), // start immediately
		f:           func() string { return "result" },
	}

	fmt.Println(r2.do())
	fmt.Println(r2.do())
	fmt.Println(r2.do())
}

Just that the nice implementation in rateLimited cannot start immediately.

I am not in control of calling the function myself, the library client does that.

So I second the need for a stdlib solution like proposed in the original issue, and hope the issue can be re-opened (or discussed here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions