From e48ade4a9ec25605b80ef6cb3f9b3e4be453de04 Mon Sep 17 00:00:00 2001 From: Michael Baird Date: Tue, 17 Oct 2017 13:49:35 -0500 Subject: [PATCH] Set the diagnostics interval and circuit breaker timeout at Open() --- diagnostics/diagnostics.go | 25 ++++++++++++++----------- diagnostics/diagnostics_test.go | 2 ++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/diagnostics/diagnostics.go b/diagnostics/diagnostics.go index 761e7adca..35d37f6cc 100644 --- a/diagnostics/diagnostics.go +++ b/diagnostics/diagnostics.go @@ -18,10 +18,9 @@ import ( // TODO: unique Cluster ID -// Default interval to sync diagnostics metrics. +// Default version check URL. const ( - DefaultDiagnosticsInterval = 1 * time.Hour - DefaultVersionCheckURL = "https://diagnostics.pilosa.com/v0/version" + DefaultVersionCheckURL = "https://diagnostics.pilosa.com/v0/version" ) type versionResponse struct { @@ -51,8 +50,6 @@ type Diagnostics struct { // New returns a pointer to a new Diagnostics Client given an addr in the format "hostname:port". func New(host string) *Diagnostics { - var st gobreaker.Settings - st.Timeout = DefaultDiagnosticsInterval * 2 return &Diagnostics{ closing: make(chan struct{}), @@ -62,9 +59,7 @@ func New(host string) *Diagnostics { start: time.Now(), client: http.DefaultClient, metrics: make(map[string]interface{}), - interval: DefaultDiagnosticsInterval, logOutput: ioutil.Discard, - cb: gobreaker.NewCircuitBreaker(st), } } @@ -74,6 +69,11 @@ func (d *Diagnostics) SetVersion(v string) { d.Set("Version", v) } +// SetInterval of the diagnostic go routine and match with the circuit breaker timeout. +func (d *Diagnostics) SetInterval(i time.Duration) { + d.interval = i +} + // schedule start the diagnostics service ticker. func (d *Diagnostics) schedule() { ticker := time.NewTicker(d.interval) @@ -117,10 +117,13 @@ func (d *Diagnostics) Flush() error { return err } -// Open starts the diagnostics metric go routine. +// Open configures the circuit breaker used by the HTTP client. func (d *Diagnostics) Open() { - d.wg.Add(1) - go func() { defer d.wg.Done(); d.schedule() }() + var st gobreaker.Settings + if d.interval > 0 { + st.Timeout = d.interval * 2 + } + d.cb = gobreaker.NewCircuitBreaker(st) } // Close notify goroutine to stop. @@ -169,7 +172,7 @@ func (d *Diagnostics) CompareVersion(value string) error { return nil } -// Encode metrics maps into the json message format +// Encode metrics maps into the json message format. func (d *Diagnostics) Encode() ([]byte, error) { return json.Marshal(d.metrics) } diff --git a/diagnostics/diagnostics_test.go b/diagnostics/diagnostics_test.go index d780a276f..81d6b7cf6 100644 --- a/diagnostics/diagnostics_test.go +++ b/diagnostics/diagnostics_test.go @@ -21,6 +21,7 @@ func TestDiagnosticsClient(t *testing.T) { // Create a new client. d := diagnostics.New(server.URL) d.SetLogger(ioutil.Discard) + d.Open() defer d.Close() d.Set("gg", 10) @@ -69,6 +70,7 @@ func TestDiagnosticsVersion_Parse(t *testing.T) { func TestDiagnosticsVersion_Compare(t *testing.T) { d := diagnostics.New("localhost:10101") + d.Open() defer d.Close() version := "0.1.1"