diff --git a/go.mod b/go.mod index ad4ffd3..d631085 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/golang/protobuf v1.4.3 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/joho/godotenv v1.3.0 + github.com/stieneee/tickerct v0.0.0-20210420020607-d1b092aa40e9 // indirect golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 // indirect google.golang.org/protobuf v1.25.0 // indirect diff --git a/go.sum b/go.sum index a7b2e37..2e6259b 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stieneee/tickerct v0.0.0-20210420020607-d1b092aa40e9 h1:0l2H6Oj6JHMmkqm9xaBMQA5MOGhPT+Nn/thlTUcD9Iw= +github.com/stieneee/tickerct v0.0.0-20210420020607-d1b092aa40e9/go.mod h1:54+oZlabriEpT52rPAjAeEWUFgYqv325LrS3hNxHGFE= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/tickerct.go b/tickerct.go deleted file mode 100644 index 7ada854..0000000 --- a/tickerct.go +++ /dev/null @@ -1,81 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "sync" - "time" -) - -// A Ticker holds a channel that delivers ``ticks'' of a clockinter -// at intervals. -type TickerCT struct { - sync.Mutex - C <-chan time.Time // The channel on which the ticks are delivered. - c chan<- time.Time // internal use - r *time.Timer // internal timer - d time.Duration // the set duration - last time.Time // the last time the ticker ticked - stop bool // mark the ticker as stopped -} - -// NewTickerCT returns a new Ticker containing a channel that will send -// the time on the channel after each tick. The period of the ticks is -// specified by the duration argument. The ticker queue ticks. -// The duration d must be greater than zero; if not, NewTickerCT will -// panic. Stop the ticker to release associated resources. -func NewTickerCT(d time.Duration) *TickerCT { - if d <= 0 { - panic(errors.New("non-positive interval for NewTickerCT")) - } - - // Give the channel a large buffer to allow clients to catchup - c := make(chan time.Time, 100) - - t := &TickerCT{ - C: c, - c: c, - d: d, - last: time.Now(), - stop: false, - } - - t.Lock() - t.r = time.AfterFunc(d, func() { t.tick() }) - t.Unlock() - - return t -} - -func (t *TickerCT) tick() { - t.Lock() - if t.stop { - fmt.Println("stopped") - return - } - - now := time.Now() - t.c <- now - - current := t.last.Add(t.d) - target := current.Add(t.d) - - d := target.Sub(now) - - // if d.Microseconds() < 1 { - // d = time.Duration(time.Microsecond) - // } - // delta := now.Sub(current) - // fmt.Println("delta", delta, d) - - t.r.Reset(d) - t.last = current - t.Unlock() -} - -func (t *TickerCT) Stop() { - t.stop = true - if t.r != nil { - t.r.Stop() - } -} diff --git a/timing_test.go b/timing_test.go index 781138d..8592d90 100644 --- a/timing_test.go +++ b/timing_test.go @@ -8,6 +8,8 @@ import ( "sync" "testing" "time" + + "github.com/stieneee/tickerct" ) const testCount int64 = 10000 @@ -76,7 +78,7 @@ func testTickerCT(wg *sync.WaitGroup) { now := time.Now() start := now // start the ticker - t := NewTickerCT(interval) + t := tickerct.NewTickerCT(interval) var i int64 for i = 0; i < testCount; i++ { if i+1 < testCount {