Simplify IP pool and convert it to host pool
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user