Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
```
Expand Down
10 changes: 0 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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) {
Expand Down
7 changes: 0 additions & 7 deletions knownbots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"sync/atomic"
"testing"
"time"
)

func init() {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
}
Expand All @@ -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
Expand Down