Simplify host pool

This commit is contained in:
2025-02-26 10:35:11 +02:00
parent ca008b0796
commit 2a041fec7c

View File

@@ -11,44 +11,39 @@ var hostPool = HostPool{hostnames: make(map[string]struct{})} //nolint:gocheckno
type HostPool struct { type HostPool struct {
hostnames map[string]struct{} hostnames map[string]struct{}
Lock sync.RWMutex lock sync.RWMutex
} }
func (p *HostPool) Add(key string) { //func (p *HostPool) add(key string) {
p.Lock.Lock() // p.lock.Lock()
defer p.Lock.Unlock() // defer p.lock.Unlock()
p.hostnames[key] = struct{}{} // p.hostnames[key] = struct{}{}
} //}
//
//func (p *HostPool) has(key string) bool {
// p.lock.RLock()
// defer p.lock.RUnlock()
// _, ok := p.hostnames[key]
// return ok
//}
func (p *HostPool) Get(key string) bool { func RemoveHostFromPool(key string) {
p.Lock.RLock() hostPool.lock.Lock()
defer p.Lock.RUnlock() defer hostPool.lock.Unlock()
_, ok := p.hostnames[key] delete(hostPool.hostnames, key)
return ok
}
func (p *HostPool) Delete(key string) {
p.Lock.Lock()
defer p.Lock.Unlock()
delete(p.hostnames, key)
} }
func AddHostToHostPool(key string) { func AddHostToHostPool(key string) {
for { for {
// Sleep until the host doesn't exist in pool, hostPool.lock.Lock()
// then add it. _, exists := hostPool.hostnames[key]
if hostPool.Get(key) { if !exists {
time.Sleep(1 * time.Second) // Avoid flood-retrying hostPool.hostnames[key] = struct{}{}
logging.LogInfo("Waiting to add %s to pool...", key) hostPool.lock.Unlock()
} else {
hostPool.Add(key)
return return
} }
} hostPool.lock.Unlock()
} time.Sleep(1 * time.Second)
logging.LogInfo("Waiting to add %s to pool...", key)
func RemoveHostFromHostPool(key string) {
if hostPool.Get(key) {
hostPool.Delete(key)
} }
} }