From 6f05ea91a390d802dfb7f3e917546a813f891cee Mon Sep 17 00:00:00 2001 From: Ryan Slade Date: Tue, 17 Oct 2017 20:38:24 +0200 Subject: [PATCH] Replace time.Now().Sub(x) with time.Since(x) --- agent/catalog_endpoint_test.go | 2 +- agent/consul/acl_replication.go | 2 +- agent/consul/acl_replication_test.go | 4 ++-- agent/consul/autopilot_test.go | 2 +- agent/consul/catalog_endpoint_test.go | 4 ++-- agent/consul/client.go | 2 +- agent/consul/fsm.go | 2 +- agent/consul/kvs_endpoint_test.go | 2 +- agent/consul/rpc.go | 4 ++-- agent/dns.go | 6 +++--- agent/dns_test.go | 2 +- agent/http.go | 2 +- agent/remote_exec_test.go | 4 ++-- api/api.go | 2 +- api/lock.go | 2 +- api/lock_test.go | 2 +- api/semaphore.go | 2 +- api/semaphore_test.go | 2 +- command/exec.go | 2 +- 19 files changed, 25 insertions(+), 25 deletions(-) diff --git a/agent/catalog_endpoint_test.go b/agent/catalog_endpoint_test.go index 49fd01d7a..048800d44 100644 --- a/agent/catalog_endpoint_test.go +++ b/agent/catalog_endpoint_test.go @@ -325,7 +325,7 @@ func TestCatalogNodes_Blocking(t *testing.T) { } // Should block for a while - if d := time.Now().Sub(start); d < 50*time.Millisecond { + if d := time.Since(start); d < 50*time.Millisecond { errch <- fmt.Errorf("too fast: %v", d) } diff --git a/agent/consul/acl_replication.go b/agent/consul/acl_replication.go index 729b34da6..3a1579cb0 100644 --- a/agent/consul/acl_replication.go +++ b/agent/consul/acl_replication.go @@ -192,7 +192,7 @@ func (s *Server) updateLocalACLs(changes structs.ACLRequests) error { // Do a smooth rate limit to wait out the min time allowed for // each op. If this op took longer than the min, then the sleep // time will be negative and we will just move on. - elapsed := time.Now().Sub(start) + elapsed := time.Since(start) time.Sleep(minTimePerOp - elapsed) } return nil diff --git a/agent/consul/acl_replication_test.go b/agent/consul/acl_replication_test.go index ea62fa0a0..0ffc8d00e 100644 --- a/agent/consul/acl_replication_test.go +++ b/agent/consul/acl_replication_test.go @@ -250,7 +250,7 @@ func TestACLReplication_updateLocalACLs_RateLimit(t *testing.T) { if err := s1.updateLocalACLs(changes); err != nil { t.Fatalf("err: %v", err) } - if dur := time.Now().Sub(start); dur < time.Second { + if dur := time.Since(start); dur < time.Second { t.Fatalf("too slow: %9.6f", dur.Seconds()) } @@ -268,7 +268,7 @@ func TestACLReplication_updateLocalACLs_RateLimit(t *testing.T) { if err := s1.updateLocalACLs(changes); err != nil { t.Fatalf("err: %v", err) } - if dur := time.Now().Sub(start); dur < 2*time.Second { + if dur := time.Since(start); dur < 2*time.Second { t.Fatalf("too fast: %9.6f", dur.Seconds()) } } diff --git a/agent/consul/autopilot_test.go b/agent/consul/autopilot_test.go index f9c37add0..e52e97d7c 100644 --- a/agent/consul/autopilot_test.go +++ b/agent/consul/autopilot_test.go @@ -231,7 +231,7 @@ func TestAutopilot_PromoteNonVoter(t *testing.T) { if !health.Healthy { r.Fatalf("bad: %v", health) } - if time.Now().Sub(health.StableSince) < s1.config.AutopilotConfig.ServerStabilizationTime { + if time.Since(health.StableSince) < s1.config.AutopilotConfig.ServerStabilizationTime { r.Fatal("stable period not elapsed") } }) diff --git a/agent/consul/catalog_endpoint_test.go b/agent/consul/catalog_endpoint_test.go index 0a31100ab..dd2d49608 100644 --- a/agent/consul/catalog_endpoint_test.go +++ b/agent/consul/catalog_endpoint_test.go @@ -1226,7 +1226,7 @@ func TestCatalog_ListServices_Blocking(t *testing.T) { } // Should block at least 100ms - if time.Now().Sub(start) < 100*time.Millisecond { + if time.Since(start) < 100*time.Millisecond { t.Fatalf("too fast") } @@ -1273,7 +1273,7 @@ func TestCatalog_ListServices_Timeout(t *testing.T) { } // Should block at least 100ms - if time.Now().Sub(start) < 100*time.Millisecond { + if time.Since(start) < 100*time.Millisecond { t.Fatalf("too fast") } diff --git a/agent/consul/client.go b/agent/consul/client.go index 2e87b9ce3..466780727 100644 --- a/agent/consul/client.go +++ b/agent/consul/client.go @@ -270,7 +270,7 @@ TRY: } // We can wait a bit and retry! - if time.Now().Sub(firstCheck) < c.config.RPCHoldTimeout { + if time.Since(firstCheck) < c.config.RPCHoldTimeout { jitter := lib.RandomStagger(c.config.RPCHoldTimeout / jitterFraction) select { case <-time.After(jitter): diff --git a/agent/consul/fsm.go b/agent/consul/fsm.go index 4ccaffebb..0d272546f 100644 --- a/agent/consul/fsm.go +++ b/agent/consul/fsm.go @@ -365,7 +365,7 @@ func (c *consulFSM) applyAutopilotUpdate(buf []byte, index uint64) interface{} { func (c *consulFSM) Snapshot() (raft.FSMSnapshot, error) { defer func(start time.Time) { - c.logger.Printf("[INFO] consul.fsm: snapshot created in %v", time.Now().Sub(start)) + c.logger.Printf("[INFO] consul.fsm: snapshot created in %v", time.Since(start)) }(time.Now()) return &consulSnapshot{c.state.Snapshot()}, nil diff --git a/agent/consul/kvs_endpoint_test.go b/agent/consul/kvs_endpoint_test.go index ee2a7ea2d..38b4e33ba 100644 --- a/agent/consul/kvs_endpoint_test.go +++ b/agent/consul/kvs_endpoint_test.go @@ -363,7 +363,7 @@ func TestKVSEndpoint_List_Blocking(t *testing.T) { } // Should block at least 100ms - if time.Now().Sub(start) < 100*time.Millisecond { + if time.Since(start) < 100*time.Millisecond { t.Fatalf("too fast") } diff --git a/agent/consul/rpc.go b/agent/consul/rpc.go index 9feed0199..927fe4f1a 100644 --- a/agent/consul/rpc.go +++ b/agent/consul/rpc.go @@ -244,7 +244,7 @@ RETRY: if firstCheck.IsZero() { firstCheck = time.Now() } - if time.Now().Sub(firstCheck) < s.config.RPCHoldTimeout { + if time.Since(firstCheck) < s.config.RPCHoldTimeout { jitter := lib.RandomStagger(s.config.RPCHoldTimeout / jitterFraction) select { case <-time.After(jitter): @@ -443,7 +443,7 @@ func (s *Server) setQueryMeta(m *structs.QueryMeta) { m.LastContact = 0 m.KnownLeader = true } else { - m.LastContact = time.Now().Sub(s.raft.LastContact()) + m.LastContact = time.Since(s.raft.LastContact()) m.KnownLeader = (s.raft.Leader() != "") } } diff --git a/agent/dns.go b/agent/dns.go index cd8964031..e3dabebe7 100644 --- a/agent/dns.go +++ b/agent/dns.go @@ -159,7 +159,7 @@ func (d *DNSServer) handlePtr(resp dns.ResponseWriter, req *dns.Msg) { metrics.MeasureSinceWithLabels([]string{"dns", "ptr_query"}, s, []metrics.Label{{Name: "node", Value: d.agent.config.NodeName}}) d.logger.Printf("[DEBUG] dns: request for %v (%v) from client %s (%s)", - q, time.Now().Sub(s), resp.RemoteAddr().String(), + q, time.Since(s), resp.RemoteAddr().String(), resp.RemoteAddr().Network()) }(time.Now()) @@ -231,7 +231,7 @@ func (d *DNSServer) handleQuery(resp dns.ResponseWriter, req *dns.Msg) { metrics.MeasureSinceWithLabels([]string{"dns", "domain_query"}, s, []metrics.Label{{Name: "node", Value: d.agent.config.NodeName}}) d.logger.Printf("[DEBUG] dns: request for %v (%v) from client %s (%s)", - q, time.Now().Sub(s), resp.RemoteAddr().String(), + q, time.Since(s), resp.RemoteAddr().String(), resp.RemoteAddr().Network()) }(time.Now()) @@ -1061,7 +1061,7 @@ func (d *DNSServer) handleRecurse(resp dns.ResponseWriter, req *dns.Msg) { network := "udp" defer func(s time.Time) { d.logger.Printf("[DEBUG] dns: request for %v (%s) (%v) from client %s (%s)", - q, network, time.Now().Sub(s), resp.RemoteAddr().String(), + q, network, time.Since(s), resp.RemoteAddr().String(), resp.RemoteAddr().Network()) }(time.Now()) diff --git a/agent/dns_test.go b/agent/dns_test.go index 368e9f0df..776314a4e 100644 --- a/agent/dns_test.go +++ b/agent/dns_test.go @@ -2208,7 +2208,7 @@ func TestDNS_RecursorTimeout(t *testing.T) { start := time.Now() in, _, err := c.Exchange(m, a.DNSAddr()) - duration := time.Now().Sub(start) + duration := time.Since(start) if err != nil { t.Fatalf("err: %v", err) diff --git a/agent/http.go b/agent/http.go index e54c8d202..10189517d 100644 --- a/agent/http.go +++ b/agent/http.go @@ -274,7 +274,7 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque // Invoke the handler start := time.Now() defer func() { - s.agent.logger.Printf("[DEBUG] http: Request %s %v (%v) from=%s", req.Method, logURL, time.Now().Sub(start), req.RemoteAddr) + s.agent.logger.Printf("[DEBUG] http: Request %s %v (%v) from=%s", req.Method, logURL, time.Since(start), req.RemoteAddr) }() obj, err := handler(resp, req) if err != nil { diff --git a/agent/remote_exec_test.go b/agent/remote_exec_test.go index fd586fa41..7a164f130 100644 --- a/agent/remote_exec_test.go +++ b/agent/remote_exec_test.go @@ -46,7 +46,7 @@ func TestRexecWriter(t *testing.T) { if len(b) != 4 { t.Fatalf("Bad: %v", b) } - if time.Now().Sub(start) < writer.BufIdle { + if time.Since(start) < writer.BufIdle { t.Fatalf("too early") } case <-time.After(2 * writer.BufIdle): @@ -66,7 +66,7 @@ func TestRexecWriter(t *testing.T) { if len(b) != 12 { t.Fatalf("Bad: %v", b) } - if time.Now().Sub(start) < writer.BufIdle { + if time.Since(start) < writer.BufIdle { t.Fatalf("too early") } case <-time.After(2 * writer.BufIdle): diff --git a/api/api.go b/api/api.go index 87cd3c5a3..97a524b5e 100644 --- a/api/api.go +++ b/api/api.go @@ -661,7 +661,7 @@ func (c *Client) doRequest(r *request) (time.Duration, *http.Response, error) { } start := time.Now() resp, err := c.config.HttpClient.Do(req) - diff := time.Now().Sub(start) + diff := time.Since(start) return diff, resp, err } diff --git a/api/lock.go b/api/lock.go index d3187113f..41f72e7d2 100644 --- a/api/lock.go +++ b/api/lock.go @@ -180,7 +180,7 @@ WAIT: // Handle the one-shot mode. if l.opts.LockTryOnce && attempts > 0 { - elapsed := time.Now().Sub(start) + elapsed := time.Since(start) if elapsed > qOpts.WaitTime { return nil, nil } diff --git a/api/lock_test.go b/api/lock_test.go index 560209874..81b47e934 100644 --- a/api/lock_test.go +++ b/api/lock_test.go @@ -541,7 +541,7 @@ func TestAPI_LockOneShot(t *testing.T) { if ch != nil { t.Fatalf("should not be leader") } - diff := time.Now().Sub(start) + diff := time.Since(start) if diff < contender.opts.LockWaitTime || diff > 2*contender.opts.LockWaitTime { t.Fatalf("time out of bounds: %9.6f", diff.Seconds()) } diff --git a/api/semaphore.go b/api/semaphore.go index d848dfd0b..d0c574177 100644 --- a/api/semaphore.go +++ b/api/semaphore.go @@ -198,7 +198,7 @@ WAIT: // Handle the one-shot mode. if s.opts.SemaphoreTryOnce && attempts > 0 { - elapsed := time.Now().Sub(start) + elapsed := time.Since(start) if elapsed > qOpts.WaitTime { return nil, nil } diff --git a/api/semaphore_test.go b/api/semaphore_test.go index 598be024c..bf710c6ee 100644 --- a/api/semaphore_test.go +++ b/api/semaphore_test.go @@ -499,7 +499,7 @@ func TestAPI_SemaphoreOneShot(t *testing.T) { if ch != nil { t.Fatalf("should not have acquired the semaphore") } - diff := time.Now().Sub(start) + diff := time.Since(start) if diff < contender.opts.SemaphoreWaitTime { t.Fatalf("time out of bounds: %9.6f", diff.Seconds()) } diff --git a/command/exec.go b/command/exec.go index 450736d3d..ec04027bd 100644 --- a/command/exec.go +++ b/command/exec.go @@ -324,7 +324,7 @@ OUTER: c.UI.Info(fmt.Sprintf("%d / %d node(s) completed / acknowledged", exitCount, ackCount)) if c.conf.verbose { c.UI.Info(fmt.Sprintf("Completed in %0.2f seconds", - float64(time.Now().Sub(start))/float64(time.Second))) + float64(time.Since(start))/float64(time.Second))) } if exitCount < ackCount { badExit++