Types: rename metric.SetTTLDuration to metric.SetTTL
GetTTL returns duration. SetTTL should take duration too, not seconds. This removes the original SetTTL method which used seconds. License: MIT Signed-off-by: Hector Sanjuan <code@hector.link>
This commit is contained in:
parent
8f8e76ac9a
commit
a9d6fe3479
10
api/types.go
10
api/types.go
|
@ -612,14 +612,8 @@ type Metric struct {
|
|||
Valid bool // if the metric is not valid it will be discarded
|
||||
}
|
||||
|
||||
// SetTTL sets Metric to expire after the given seconds
|
||||
func (m *Metric) SetTTL(seconds int) {
|
||||
d := time.Duration(seconds) * time.Second
|
||||
m.SetTTLDuration(d)
|
||||
}
|
||||
|
||||
// SetTTLDuration sets Metric to expire after the given time.Duration
|
||||
func (m *Metric) SetTTLDuration(d time.Duration) {
|
||||
// SetTTL sets Metric to expire after the given time.Duration
|
||||
func (m *Metric) SetTTL(d time.Duration) {
|
||||
exp := time.Now().Add(d)
|
||||
m.Expire = exp.UnixNano()
|
||||
}
|
||||
|
|
|
@ -209,7 +209,7 @@ func TestMetric(t *testing.T) {
|
|||
t.Error("metric should be expired")
|
||||
}
|
||||
|
||||
m.SetTTL(1)
|
||||
m.SetTTL(1 * time.Second)
|
||||
if m.Expired() {
|
||||
t.Error("metric should not be expired")
|
||||
}
|
||||
|
@ -221,7 +221,7 @@ func TestMetric(t *testing.T) {
|
|||
t.Error("metric should be expired")
|
||||
}
|
||||
|
||||
m.SetTTL(30)
|
||||
m.SetTTL(30 * time.Second)
|
||||
m.Valid = true
|
||||
|
||||
if m.Discard() {
|
||||
|
|
|
@ -228,7 +228,7 @@ func (c *Cluster) pushPingMetrics() {
|
|||
Peer: c.id,
|
||||
Valid: true,
|
||||
}
|
||||
metric.SetTTLDuration(c.config.MonitorPingInterval * 2)
|
||||
metric.SetTTL(c.config.MonitorPingInterval * 2)
|
||||
c.monitor.PublishMetric(metric)
|
||||
|
||||
select {
|
||||
|
|
|
@ -94,6 +94,6 @@ func (disk *Informer) GetMetric() api.Metric {
|
|||
Valid: valid,
|
||||
}
|
||||
|
||||
m.SetTTLDuration(disk.config.MetricTTL)
|
||||
m.SetTTL(disk.config.MetricTTL)
|
||||
return m
|
||||
}
|
||||
|
|
|
@ -78,6 +78,6 @@ func (npi *Informer) GetMetric() api.Metric {
|
|||
Valid: valid,
|
||||
}
|
||||
|
||||
m.SetTTLDuration(npi.config.MetricTTL)
|
||||
m.SetTTL(npi.config.MetricTTL)
|
||||
return m
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func newMetric(n string, p peer.ID) api.Metric {
|
|||
Value: fmt.Sprintf("%d", metricCounter),
|
||||
Valid: true,
|
||||
}
|
||||
m.SetTTL(5)
|
||||
m.SetTTL(5 * time.Second)
|
||||
metricCounter++
|
||||
return m
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ func TestLogMetricConcurrent(t *testing.T) {
|
|||
Value: fmt.Sprintf("%d", time.Now().UnixNano()),
|
||||
Valid: true,
|
||||
}
|
||||
mt.SetTTLDuration(150 * time.Millisecond)
|
||||
mt.SetTTL(150 * time.Millisecond)
|
||||
pm.LogMetric(mt)
|
||||
time.Sleep(75 * time.Millisecond)
|
||||
}
|
||||
|
|
|
@ -4,9 +4,10 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
peer "github.com/libp2p/go-libp2p-peer"
|
||||
|
||||
"github.com/ipfs/ipfs-cluster/api"
|
||||
"github.com/ipfs/ipfs-cluster/test"
|
||||
peer "github.com/libp2p/go-libp2p-peer"
|
||||
)
|
||||
|
||||
func TestMetricsChecker(t *testing.T) {
|
||||
|
@ -21,7 +22,7 @@ func TestMetricsChecker(t *testing.T) {
|
|||
Value: "1",
|
||||
Valid: true,
|
||||
}
|
||||
metr.SetTTL(2)
|
||||
metr.SetTTL(2 * time.Second)
|
||||
|
||||
metrics["test"] = make(PeerMetrics)
|
||||
metrics["test"][test.TestPeerID1] = NewMetricsWindow(5, true)
|
||||
|
|
|
@ -2,6 +2,7 @@ package util
|
|||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/ipfs-cluster/api"
|
||||
)
|
||||
|
@ -24,7 +25,7 @@ func TestMetricsWindow(t *testing.T) {
|
|||
Value: "1",
|
||||
Valid: true,
|
||||
}
|
||||
metr.SetTTL(5)
|
||||
metr.SetTTL(5 * time.Second)
|
||||
|
||||
mw.Add(metr)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user