Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove time package #103

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [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] Remove package `math`. #104
* [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.