refactor(acl): default not to notify allowed and skip when total is 0

This commit is contained in:
yusing
2025-10-10 20:20:09 +08:00
parent 7a1841e9a5
commit fbabb7b7fb

View File

@@ -28,8 +28,9 @@ type Config struct {
Log *accesslog.ACLLoggerConfig `json:"log"` Log *accesslog.ACLLoggerConfig `json:"log"`
Notify struct { Notify struct {
To []string `json:"to"` // list of notification providers To []string `json:"to"` // list of notification providers
Interval time.Duration `json:"interval"` // interval between notifications Interval time.Duration `json:"interval"` // interval between notifications
IncludeAllowed *bool `json:"include_allowed"` // default: false
} `json:"notify"` } `json:"notify"`
config config
@@ -52,7 +53,8 @@ type config struct {
logger *accesslog.AccessLogger logger *accesslog.AccessLogger
// will never tick if Notify.To is empty // will never tick if Notify.To is empty
notifyTicker *time.Ticker notifyTicker *time.Ticker
notifyAllowed bool
// will be nil if both Log and Notify.To are empty // will be nil if both Log and Notify.To are empty
logNotifyCh chan ipLog logNotifyCh chan ipLog
@@ -130,6 +132,12 @@ func (c *Config) Validate() gperr.Error {
} else { } else {
c.notifyTicker = time.NewTicker(time.Duration(math.MaxInt64)) // never tick c.notifyTicker = time.NewTicker(time.Duration(math.MaxInt64)) // never tick
} }
if c.Notify.IncludeAllowed != nil {
c.notifyAllowed = *c.Notify.IncludeAllowed
} else {
c.notifyAllowed = false
}
return nil return nil
} }
@@ -196,13 +204,19 @@ func (c *Config) logNotifyLoop(parent task.Parent) {
} }
if c.needNotify() { if c.needNotify() {
if log.allowed { if log.allowed {
c.allowCounts[log.info.Str]++ if c.notifyAllowed {
c.allowCounts[log.info.Str]++
}
} else { } else {
c.blockedCounts[log.info.Str]++ c.blockedCounts[log.info.Str]++
} }
} }
case <-c.notifyTicker.C: // will never tick when notify is disabled case <-c.notifyTicker.C: // will never tick when notify is disabled
fieldsBody := make(notif.FieldsBody, 0, len(c.allowCounts)+len(c.blockedCounts)) total := len(c.allowCounts) + len(c.blockedCounts)
if total == 0 {
continue
}
fieldsBody := make(notif.FieldsBody, 0, total)
for ip, count := range c.allowCounts { for ip, count := range c.allowCounts {
fieldsBody = append(fieldsBody, notif.LogField{ fieldsBody = append(fieldsBody, notif.LogField{
Name: ip, Name: ip,