-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1187776
commit e7da105
Showing
7 changed files
with
581 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2024 RethinkDNS and its authors. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package core | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var errNewJob = errors.New("sched: replaced by newer job") | ||
|
||
type Job func() error | ||
|
||
type ctl struct { | ||
who context.Context | ||
cancel context.CancelCauseFunc | ||
} | ||
|
||
type Scheduler struct { | ||
ctx context.Context | ||
|
||
mu sync.Mutex | ||
jobctl map[string]*ctl | ||
} | ||
|
||
func NewScheduler(ctx context.Context) *Scheduler { | ||
return &Scheduler{ | ||
ctx: ctx, | ||
jobctl: make(map[string]*ctl), | ||
} | ||
} | ||
|
||
// At runs f at time t; accepts a Context to cancel it. | ||
func (s *Scheduler) At(id string, t time.Time, f Job) context.Context { | ||
s.mu.Lock() | ||
ctx, done := context.WithCancelCause(s.ctx) | ||
if c := s.jobctl[id]; c != nil { | ||
c.cancel(errNewJob) // dispose existing job | ||
} | ||
s.jobctl[id] = &ctl{who: ctx, cancel: done} | ||
s.mu.Unlock() | ||
|
||
Go("at."+id, func() { | ||
var cause error | ||
|
||
defer func() { | ||
done(cause) | ||
s.mu.Lock() | ||
if c := s.jobctl[id]; c != nil && c.who == ctx { | ||
delete(s.jobctl, id) | ||
} | ||
s.mu.Unlock() | ||
}() | ||
|
||
select { | ||
case <-s.ctx.Done(): | ||
cause = s.ctx.Err() | ||
return | ||
case <-time.After(time.Until(t)): | ||
cause = f() | ||
return | ||
} | ||
}) | ||
return ctx | ||
} |
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
Oops, something went wrong.