From 366d217f44dd8f49f2ed67763afda0ffafe4aeaf Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Fri, 1 Apr 2022 17:55:33 -0600 Subject: [PATCH] Some changes based on review Signed-off-by: Ivan Kozlovic --- server/client.go | 2 +- server/log.go | 2 +- server/server.go | 44 +++++++++++++++++++++----------------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/server/client.go b/server/client.go index 4dc8f7a0..bea7fea7 100644 --- a/server/client.go +++ b/server/client.go @@ -5328,7 +5328,7 @@ func (c *client) Warnf(format string, v ...interface{}) { func (c *client) RateLimitWarnf(format string, v ...interface{}) { // Do the check before adding the client info to the format... statement := fmt.Sprintf(format, v...) - if _, loaded := c.srv.rateLimitLogging.LoadOrStore(statement, time.Now().UnixNano()); loaded { + if _, loaded := c.srv.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { return } c.Warnf("%s", statement) diff --git a/server/log.go b/server/log.go index 97d8397f..4b9193c0 100644 --- a/server/log.go +++ b/server/log.go @@ -208,7 +208,7 @@ func (s *Server) Warnf(format string, v ...interface{}) { func (s *Server) RateLimitWarnf(format string, v ...interface{}) { statement := fmt.Sprintf(format, v...) - if _, loaded := s.rateLimitLogging.LoadOrStore(statement, time.Now().UnixNano()); loaded { + if _, loaded := s.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { return } s.Warnf("%s", statement) diff --git a/server/server.go b/server/server.go index eb05937d..b226db2d 100644 --- a/server/server.go +++ b/server/server.go @@ -274,7 +274,7 @@ type Server struct { // To limit logging frequency rateLimitLogging sync.Map - rateLimitLoggingCh chan int64 + rateLimitLoggingCh chan time.Duration } // For tracking JS nodes. @@ -361,19 +361,20 @@ func NewServer(opts *Options) (*Server, error) { now := time.Now().UTC() s := &Server{ - kp: kp, - configFile: opts.ConfigFile, - info: info, - prand: rand.New(rand.NewSource(time.Now().UnixNano())), - opts: opts, - done: make(chan bool, 1), - start: now, - configTime: now, - gwLeafSubs: NewSublistWithCache(), - httpBasePath: httpBasePath, - eventIds: nuid.New(), - routesToSelf: make(map[string]struct{}), - httpReqStats: make(map[string]uint64), // Used to track HTTP requests + kp: kp, + configFile: opts.ConfigFile, + info: info, + prand: rand.New(rand.NewSource(time.Now().UnixNano())), + opts: opts, + done: make(chan bool, 1), + start: now, + configTime: now, + gwLeafSubs: NewSublistWithCache(), + httpBasePath: httpBasePath, + eventIds: nuid.New(), + routesToSelf: make(map[string]struct{}), + httpReqStats: make(map[string]uint64), // Used to track HTTP requests + rateLimitLoggingCh: make(chan time.Duration, 1), } if opts.TLSRateLimit > 0 { @@ -3601,25 +3602,22 @@ func (s *Server) updateRemoteSubscription(acc *Account, sub *subscription, delta } func (s *Server) startRateLimitLogExpiration() { - s.mu.Lock() - interval := int64(time.Second) - s.rateLimitLoggingCh = make(chan int64, 1) - s.mu.Unlock() - + interval := time.Second s.startGoRoutine(func() { defer s.grWG.Done() ticker := time.NewTicker(time.Second) + defer ticker.Stop() for { select { case <-s.quitCh: return case interval = <-s.rateLimitLoggingCh: - ticker.Reset(time.Duration(interval)) + ticker.Reset(interval) case <-ticker.C: - now := time.Now().UnixNano() s.rateLimitLogging.Range(func(k, v interface{}) bool { - if start := v.(int64); now-start >= interval { + start := v.(time.Time) + if time.Since(start) >= interval { s.rateLimitLogging.Delete(k) } return true @@ -3634,7 +3632,7 @@ func (s *Server) changeRateLimitLogInterval(d time.Duration) { return } select { - case s.rateLimitLoggingCh <- int64(d): + case s.rateLimitLoggingCh <- d: default: } }