From 90df9fa1bf3d707c89d04654525aebb9cca26a07 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 12 Dec 2017 16:43:19 -0500 Subject: [PATCH] p2p/trust: remove extra channels --- p2p/trust/metric.go | 27 ++++----------------------- p2p/trust/metric_test.go | 14 +++++++++----- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/p2p/trust/metric.go b/p2p/trust/metric.go index d40ffa54f..bf6ddb5e2 100644 --- a/p2p/trust/metric.go +++ b/p2p/trust/metric.go @@ -77,12 +77,6 @@ type TrustMetric struct { // While true, history data is not modified paused bool - // Signal channel for stopping the trust metric go-routine - stop chan struct{} - - // Signal channel fired when the metric go-routine has stopped - done chan struct{} - // Used during testing in order to control the passing of time intervals testTicker MetricTicker } @@ -109,10 +103,6 @@ func NewMetricWithConfig(tmc TrustMetricConfig) *TrustMetric { tm.historyMaxSize = intervalToHistoryOffset(tm.maxIntervals) + 1 // This metric has a perfect history so far tm.historyValue = 1.0 - // Setup the go-routine stop channel - tm.stop = make(chan struct{}, 1) - // Setup the go-routine done channel - tm.done = make(chan struct{}, 1) tm.BaseService = *cmn.NewBaseService(nil, "TrustMetric", tm) return tm @@ -128,14 +118,8 @@ func (tm *TrustMetric) OnStart() error { } // OnStop implements Service -// Stop tells the metric to stop recording data over time intervals -// This method also blocks until the metric has completely stopped -func (tm *TrustMetric) OnStop() { - tm.BaseService.OnStop() - - tm.stop <- struct{}{} - <-tm.done -} +// Nothing to do since the goroutine shuts down by itself via BaseService.Quit +func (tm *TrustMetric) OnStop() {} // Returns a snapshot of the trust metric history data func (tm *TrustMetric) HistoryJSON() MetricHistoryJSON { @@ -293,9 +277,8 @@ func (tm *TrustMetric) Copy() *TrustMetric { good: tm.good, bad: tm.bad, paused: tm.paused, - stop: make(chan struct{}, 1), - done: make(chan struct{}, 1), } + } /* Private methods */ @@ -315,13 +298,11 @@ loop: select { case <-tick: tm.NextTimeInterval() - case <-tm.stop: + case <-tm.Quit: // Stop all further tracking for this metric break loop } } - // Send the done signal - tm.done <- struct{}{} } // Wakes the trust metric up if it is currently paused diff --git a/p2p/trust/metric_test.go b/p2p/trust/metric_test.go index 1fae7917f..00219a19e 100644 --- a/p2p/trust/metric_test.go +++ b/p2p/trust/metric_test.go @@ -42,6 +42,7 @@ func TestTrustMetricConfig(t *testing.T) { assert.Equal(t, dc.ProportionalWeight, tm.proportionalWeight) assert.Equal(t, dc.IntegralWeight, tm.integralWeight) tm.Stop() + tm.Wait() config.ProportionalWeight = 0.3 config.IntegralWeight = 0.7 @@ -52,6 +53,7 @@ func TestTrustMetricConfig(t *testing.T) { assert.Equal(t, config.ProportionalWeight, tm.proportionalWeight) assert.Equal(t, config.IntegralWeight, tm.integralWeight) tm.Stop() + tm.Wait() } func TestTrustMetricStopPause(t *testing.T) { @@ -62,8 +64,8 @@ func TestTrustMetricStopPause(t *testing.T) { tm.SetTicker(tt) tm.Start() // Allow some time intervals to pass and pause - tm.NextTimeInterval() - tm.NextTimeInterval() + tt.NextTick() + tt.NextTick() tm.Pause() first := tm.Copy().numIntervals @@ -75,12 +77,14 @@ func TestTrustMetricStopPause(t *testing.T) { // Get the trust metric activated again tm.GoodEvents(5) // Allow some time intervals to pass and stop - tm.NextTimeInterval() - tm.NextTimeInterval() + tt.NextTick() + tt.NextTick() tm.Stop() + tm.Wait() second := tm.Copy().numIntervals - // Allow more intervals to pass and check that the number of intervals match + // Allow more intervals to pass while the metric is stopped + // and check that the number of intervals match tm.NextTimeInterval() tm.NextTimeInterval() assert.Equal(t, second+2, tm.Copy().numIntervals)