go func() {
<-time.After(eventTime - time.Nanoseconds())
doSomething()
}
?
i'm not convinced. the code to do the delay is one line.
> Another minor point: If we have a job which should be processed 10min
> later, then your code would create a goroutine waiting for 10min and
> then process the data; while my package put all tasks in a priority
> queue and run the tasks when they are ready to run in a separate
> goroutine. OK, I know goroutines are cheap. It should not be a big
> advantage at this point.
there's also time.AfterFunc which doesn't start a new goroutine
for each job.
can you give an example that can't implemented in less lines of code
with the existing primitives?
isn't the following code simpler and easier to understand?
go func() {
<-time.After(5e9)
for i := 0; i < 5; i++ {
fmt.Printf("Hello, world\n")
<-time.After(2e9)
}
}
or for exponential backoff:
go func() {
backoff := 0.1e9
for backoff < 120e9 {
fmt.Printf("Hello, world\n")
<-time.After(backoff)
backoff *= 2
}
}
go has goroutines to deal with state, why not use them?
> Besides exponential backoff algorithm, I think I will provide more
> algorithms, which are common used to calculate delay time of works.
if there are other, less obvious, algorithms, then i agree
that a package to implement them might be useful.
but the time calculation can be orthogonal to the task
queuing mechanism, and it's potentially more useful that way.
i could imagine an interface like this:
type TimeSequence interface {
NextDelay() (int64, bool)
}
then either function above might be written as:
go func() {
for {
fmt.Printf("Hello, world\n")
delay, ok := seq.NextDelay()
if !ok {
break
}
<-time.After(delay) }
}
and exponential backoff might be factored out into code like this:
type exponentialBackoff struct {
delay int64
max int64
}
func (t *exponentialBackoff) NextDelay() (int64, bool) {
delay := t.delay
if delay >= t.max {
return 0, false
}
t.delay *= 2
return delay, true
}
func NewExponentialBackoff(initialNanoseconds, maxNanoseconds int64)
TimeSequence {
return &exponentialBackoff{initialNanoseconds, maxNanoseconds}
}
if there were sufficiently involved time calculation algorithms that
you wanted to plug and play with, then i could see that
this might be a win. TaskQueue itself makes things harder though, not
easier, i think, sorry.