-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from SkynetLabs/ivo/schedule
Implement a sweeping schedule.
- Loading branch information
Showing
14 changed files
with
509 additions
and
201 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
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,69 @@ | ||
package sweeper | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type ( | ||
// schedule defines how often, if at all, we sweep this server automatically. | ||
schedule struct { | ||
period time.Duration | ||
cancelCh chan struct{} | ||
mu sync.Mutex | ||
} | ||
) | ||
|
||
// Close cancels any running sweeper thread.Returns true if it closed a running | ||
// thread and false otherwise. | ||
func (s *schedule) Close() bool { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
if isOpen(s.cancelCh) { | ||
close(s.cancelCh) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// Update schedules a new series of sweeps to be run, using the given Sweeper. | ||
// If there are already scheduled sweeps, that schedule is cancelled (running | ||
// sweeps are not interrupted) and a new schedule is established. | ||
func (s *schedule) Update(period time.Duration, sweeper *Sweeper) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if isOpen(s.cancelCh) { | ||
close(s.cancelCh) | ||
} | ||
|
||
s.period = period | ||
s.cancelCh = make(chan struct{}) | ||
|
||
go func() { | ||
t := time.NewTicker(s.period) | ||
defer t.Stop() | ||
for { | ||
select { | ||
case <-t.C: | ||
sweeper.Sweep() | ||
case <-s.cancelCh: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// isOpen checks whether a channel is open (and not nil). | ||
// The question the function answers is "Can I close this?" | ||
func isOpen(ch chan struct{}) bool { | ||
if ch == nil { | ||
return false | ||
} | ||
select { | ||
case _, open := <-ch: | ||
return open | ||
default: | ||
return true | ||
} | ||
} |
Oops, something went wrong.