Simplify IP pool and convert it to host pool

This commit is contained in:
2025-01-16 09:39:20 +02:00
parent 5357ceb04d
commit 766ee26f68
3 changed files with 25 additions and 40 deletions

View File

@@ -1,10 +1,12 @@
package gemini
package hostPool
import (
"sync"
"time"
"gemini-grc/logging"
)
var ipPool = IpAddressPool{IPs: make(map[string]int)} //nolint:gochecknoglobals
var hostPool = HostPool{hostnames: make(map[string]struct{})} //nolint:gochecknoglobals
type HostPool struct {
@@ -30,3 +32,23 @@ func (p *HostPool) Delete(key string) {
defer p.Lock.Unlock()
delete(p.hostnames, key)
}
func AddHostToHostPool(key string) {
for {
// Sleep until the host doesn't exist in pool,
// then add it.
if hostPool.Get(key) {
time.Sleep(1 * time.Second) // Avoid flood-retrying
logging.LogInfo("Waiting to add %s to pool...", key)
} else {
hostPool.Add(key)
return
}
}
}
func RemoveHostFromHostPool(key string) {
if hostPool.Get(key) {
hostPool.Delete(key)
}
}