mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-14 10:10:42 -07:00
Some changes based on review
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user