Skip to content

Commit

Permalink
Remove time package
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
  • Loading branch information
aknuds1 committed Dec 22, 2021
1 parent 1f49a70 commit 21ba6aa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 244 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [CHANGE] spanlogger: Take interface implementation for extracting tenant ID. #59
* [CHANGE] The `status_code` label on gRPC client metrics has changed from '200' and '500' to '2xx', '5xx', '4xx', 'cancel' or 'error'. #68
* [CHANGE] Memberlist: changed probe interval from `1s` to `5s` and probe timeout from `500ms` to `2s`. #90
* [CHANGE] time: Remove time package. #103
* [ENHANCEMENT] Add middleware package. #38
* [ENHANCEMENT] Add the ring package #45
* [ENHANCEMENT] Add limiter package. #41
Expand Down
7 changes: 3 additions & 4 deletions ring/basic_lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/services"
dstime "github.com/grafana/dskit/time"
)

type BasicLifecyclerDelegate interface {
Expand Down Expand Up @@ -186,7 +185,7 @@ func (l *BasicLifecycler) starting(ctx context.Context) error {
}

func (l *BasicLifecycler) running(ctx context.Context) error {
heartbeatTickerStop, heartbeatTickerChan := dstime.NewDisableableTicker(l.cfg.HeartbeatPeriod)
heartbeatTickerStop, heartbeatTickerChan := newDisableableTicker(l.cfg.HeartbeatPeriod)
defer heartbeatTickerStop()

for {
Expand Down Expand Up @@ -218,7 +217,7 @@ func (l *BasicLifecycler) stopping(runningError error) error {
}()

// Heartbeat while the stopping delegate function is running.
heartbeatTickerStop, heartbeatTickerChan := dstime.NewDisableableTicker(l.cfg.HeartbeatPeriod)
heartbeatTickerStop, heartbeatTickerChan := newDisableableTicker(l.cfg.HeartbeatPeriod)
defer heartbeatTickerStop()

heartbeatLoop:
Expand Down Expand Up @@ -300,7 +299,7 @@ func (l *BasicLifecycler) registerInstance(ctx context.Context) error {
}

func (l *BasicLifecycler) waitStableTokens(ctx context.Context, period time.Duration) error {
heartbeatTickerStop, heartbeatTickerChan := dstime.NewDisableableTicker(l.cfg.HeartbeatPeriod)
heartbeatTickerStop, heartbeatTickerChan := newDisableableTicker(l.cfg.HeartbeatPeriod)
defer heartbeatTickerStop()

// The first observation will occur after the specified period.
Expand Down
5 changes: 2 additions & 3 deletions ring/lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/grafana/dskit/flagext"
"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/services"
dstime "github.com/grafana/dskit/time"
)

// LifecyclerConfig is the config to build a Lifecycler.
Expand Down Expand Up @@ -403,7 +402,7 @@ func (i *Lifecycler) loop(ctx context.Context) error {
autoJoinAfter := time.After(i.cfg.JoinAfter)
var observeChan <-chan time.Time

heartbeatTickerStop, heartbeatTickerChan := dstime.NewDisableableTicker(i.cfg.HeartbeatPeriod)
heartbeatTickerStop, heartbeatTickerChan := newDisableableTicker(i.cfg.HeartbeatPeriod)
defer heartbeatTickerStop()

for {
Expand Down Expand Up @@ -480,7 +479,7 @@ func (i *Lifecycler) stopping(runningError error) error {
return nil
}

heartbeatTickerStop, heartbeatTickerChan := dstime.NewDisableableTicker(i.cfg.HeartbeatPeriod)
heartbeatTickerStop, heartbeatTickerChan := newDisableableTicker(i.cfg.HeartbeatPeriod)
defer heartbeatTickerStop()

// Mark ourselved as Leaving so no more samples are send to us.
Expand Down
14 changes: 14 additions & 0 deletions ring/ticker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ring

import "time"

// newDisableableTicker essentially wraps NewTicker but allows the ticker to be disabled by passing
// zero duration as the interval. Returns a function for stopping the ticker, and the ticker channel.
func newDisableableTicker(interval time.Duration) (func(), <-chan time.Time) {
if interval == 0 {
return func() {}, nil
}

tick := time.NewTicker(interval)
return func() { tick.Stop() }, tick.C
}
34 changes: 34 additions & 0 deletions ring/ticker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ring

import (
"testing"
"time"
)

func TestNewDisableableTicker_Enabled(t *testing.T) {
stop, ch := newDisableableTicker(10 * time.Millisecond)
defer stop()

time.Sleep(100 * time.Millisecond)

select {
case <-ch:
break
default:
t.Error("ticker should have ticked when enabled")
}
}

func TestNewDisableableTicker_Disabled(t *testing.T) {
stop, ch := newDisableableTicker(0)
defer stop()

time.Sleep(100 * time.Millisecond)

select {
case <-ch:
t.Error("ticker should not have ticked when disabled")
default:
break
}
}
96 changes: 0 additions & 96 deletions time/time.go

This file was deleted.

141 changes: 0 additions & 141 deletions time/time_test.go

This file was deleted.

0 comments on commit 21ba6aa

Please sign in to comment.