From 6f460ca7d679da3bb56ec0666b3d3a7be7997b3a Mon Sep 17 00:00:00 2001 From: Lz Date: Sun, 11 Jan 2026 16:20:41 +0800 Subject: [PATCH 1/2] fix: remove WithSchedulerInterval and use hardcoded 24h interval The scheduler interval is now fixed at 24 hours and no longer configurable. This prevents misconfiguration that could cause CPU overuse (e.g., setting interval to 0 or nanosecond values). --- config.go | 10 ---------- knownbots_test.go | 7 ------- validator.go | 5 +---- 3 files changed, 1 insertion(+), 21 deletions(-) diff --git a/config.go b/config.go index e311c6c..e9e8e38 100644 --- a/config.go +++ b/config.go @@ -1,11 +1,8 @@ package knownbots -import "time" - // Config holds the options for creating a Validator. type Config struct { Root string - Interval time.Duration FailLimit int ClassifyUA bool } @@ -20,13 +17,6 @@ func WithRoot(dir string) Option { } } -// WithSchedulerInterval sets the background scheduler interval. -func WithSchedulerInterval(interval time.Duration) Option { - return func(c *Config) { - c.Interval = interval - } -} - // WithFailLimit sets the limit for failed lookup cache. func WithFailLimit(limit int) Option { return func(c *Config) { diff --git a/knownbots_test.go b/knownbots_test.go index 15b8ac2..4a6fea3 100644 --- a/knownbots_test.go +++ b/knownbots_test.go @@ -6,7 +6,6 @@ import ( "path/filepath" "sync/atomic" "testing" - "time" ) func init() { @@ -589,12 +588,6 @@ func TestConfigOptions(t *testing.T) { t.Errorf("WithRoot failed: got %q, want %q", cfg.Root, "/custom/bots") } - // Test WithSchedulerInterval - WithSchedulerInterval(1 * time.Hour)(cfg) - if cfg.Interval != 1*time.Hour { - t.Errorf("WithSchedulerInterval failed: got %v, want %v", cfg.Interval, 1*time.Hour) - } - // Test WithFailLimit WithFailLimit(500)(cfg) if cfg.FailLimit != 500 { diff --git a/validator.go b/validator.go index db0d02f..9cac2e4 100644 --- a/validator.go +++ b/validator.go @@ -39,7 +39,6 @@ type Validator struct { bots atomic.Pointer[[]*Bot] // []*Bot, atomic for lock-free reads uaIndex atomic.Pointer[map[byte][]*Bot] // map[byte][]*Bot, byte-level index for UA lookup cancel context.CancelFunc - interval time.Duration failLimit int classifyUA bool } @@ -60,7 +59,6 @@ func (v *Validator) setBots(bots []*Bot) { func New(opts ...Option) (*Validator, error) { cfg := Config{ Root: "./bots", - Interval: SchedulerInterval, FailLimit: FailLRULimit, ClassifyUA: false, // Default: skip classifyUA for performance } @@ -89,7 +87,6 @@ func New(opts ...Option) (*Validator, error) { v := &Validator{ root: cfg.Root, cancel: cancel, - interval: cfg.Interval, failLimit: cfg.FailLimit, classifyUA: cfg.ClassifyUA, } @@ -106,7 +103,7 @@ func New(opts ...Option) (*Validator, error) { // - persistCaches: write valid cache entries to persistent storage func (v *Validator) startScheduler(ctx context.Context) { httpClient := &http.Client{Timeout: 30 * time.Second} - ticker := time.NewTicker(v.interval) + ticker := time.NewTicker(SchedulerInterval) defer ticker.Stop() // Run immediately on start From 02d1225258027aee0fc6dd803718c4c0effd5d94 Mon Sep 17 00:00:00 2001 From: Lz Date: Sun, 11 Jan 2026 16:23:26 +0800 Subject: [PATCH 2/2] docs: update README to remove WithSchedulerInterval references --- README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b65b997..c36b449 100644 --- a/README.md +++ b/README.md @@ -114,10 +114,9 @@ func BotVerificationMiddleware(v *knownbots.Validator) func(http.Handler) http.H ```go v, err := knownbots.New( - knownbots.WithRoot("./custom-bots"), // Custom bot config directory - knownbots.WithSchedulerInterval(12*time.Hour), // IP refresh frequency - knownbots.WithFailLimit(5000), // Failed lookup cache size - knownbots.WithClassifyUA(), // Enable UA classification (disabled by default) + knownbots.WithRoot("./custom-bots"), // Custom bot config directory + knownbots.WithFailLimit(5000), // Failed lookup cache size + knownbots.WithClassifyUA(), // Enable UA classification (disabled by default) ) // Disable logging to reduce console pollution (e.g., in benchmarks) @@ -325,9 +324,6 @@ func (v *Validator) Close() error // WithRoot sets custom bot directory (default: "./bots") func WithRoot(dir string) Option -// WithSchedulerInterval sets refresh interval (default: 24h) -func WithSchedulerInterval(interval time.Duration) Option - // WithFailLimit sets failed lookup cache size (default: 1000) func WithFailLimit(limit int) Option ```