From 64f98bb37c46365168841d577a7e641699003e77 Mon Sep 17 00:00:00 2001 From: antanst Date: Thu, 16 Jan 2025 09:37:29 +0200 Subject: [PATCH] Add mode that prints multiple worker status in console --- {gemini => common}/workerStatus.go | 0 config/config.go | 10 ++++++++++ hostPool/hostPool.go | 32 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) rename {gemini => common}/workerStatus.go (100%) create mode 100644 hostPool/hostPool.go diff --git a/gemini/workerStatus.go b/common/workerStatus.go similarity index 100% rename from gemini/workerStatus.go rename to common/workerStatus.go diff --git a/config/config.go b/config/config.go index cc2d430..f08b09c 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ const ( EnvPanicOnUnexpectedError = "PANIC_ON_UNEXPECTED_ERROR" EnvBlacklistPath = "BLACKLIST_PATH" EnvDryRun = "DRY_RUN" + EnvPrintWorkerStatus = "PRINT_WORKER_STATUS" ) // Config holds the application configuration loaded from environment variables. @@ -30,6 +31,7 @@ type Config struct { PanicOnUnexpectedError bool // Panic on unexpected errors when visiting a URL BlacklistPath string // File that has blacklisted strings of "host:port" DryRun bool // If false, don't write to disk + PrintWorkerStatus bool // If false, don't print worker status table } var CONFIG Config //nolint:gochecknoglobals @@ -136,6 +138,14 @@ func GetConfig() *Config { config.DryRun = val return nil }, + EnvPrintWorkerStatus: func(v string) error { + val, err := parseBool(EnvPrintWorkerStatus, v) + if err != nil { + return err + } + config.PrintWorkerStatus = val + return nil + }, } // Process each environment variable diff --git a/hostPool/hostPool.go b/hostPool/hostPool.go new file mode 100644 index 0000000..317f488 --- /dev/null +++ b/hostPool/hostPool.go @@ -0,0 +1,32 @@ +package gemini + +import ( + "sync" +) + +var ipPool = IpAddressPool{IPs: make(map[string]int)} //nolint:gochecknoglobals +var hostPool = HostPool{hostnames: make(map[string]struct{})} //nolint:gochecknoglobals + +type HostPool struct { + hostnames map[string]struct{} + Lock sync.RWMutex +} + +func (p *HostPool) Add(key string) { + p.Lock.Lock() + defer p.Lock.Unlock() + p.hostnames[key] = struct{}{} +} + +func (p *HostPool) Get(key string) bool { + p.Lock.RLock() + defer p.Lock.RUnlock() + _, ok := p.hostnames[key] + return ok +} + +func (p *HostPool) Delete(key string) { + p.Lock.Lock() + defer p.Lock.Unlock() + delete(p.hostnames, key) +}