From 766ee26f68b63d100d54ff42c9a4705585850319 Mon Sep 17 00:00:00 2001 From: antanst Date: Thu, 16 Jan 2025 09:39:20 +0200 Subject: [PATCH] Simplify IP pool and convert it to host pool --- gemini/connectionPool.go | 28 ---------------------------- gemini/network.go | 11 +---------- hostPool/hostPool.go | 26 ++++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 40 deletions(-) delete mode 100644 gemini/connectionPool.go diff --git a/gemini/connectionPool.go b/gemini/connectionPool.go deleted file mode 100644 index 72149c7..0000000 --- a/gemini/connectionPool.go +++ /dev/null @@ -1,28 +0,0 @@ -package gemini - -import ( - "gemini-grc/logging" -) - -var IPPool = IpAddressPool{IPs: make(map[string]int)} - -func AddIPsToPool(ips []string) { - IPPool.Lock.Lock() - for _, ip := range ips { - logging.LogDebug("Adding %s to pool", ip) - IPPool.IPs[ip] = 1 - } - IPPool.Lock.Unlock() -} - -func RemoveIPsFromPool(IPs []string) { - IPPool.Lock.Lock() - for _, ip := range IPs { - _, ok := IPPool.IPs[ip] - if ok { - logging.LogDebug("Removing %s from pool", ip) - delete(IPPool.IPs, ip) - } - } - IPPool.Lock.Unlock() -} diff --git a/gemini/network.go b/gemini/network.go index 841d26c..a9cbb77 100644 --- a/gemini/network.go +++ b/gemini/network.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "errors" "fmt" - "gemini-grc/common" "io" "net" gourl "net/url" @@ -14,6 +13,7 @@ import ( "strings" "time" + "gemini-grc/common" "gemini-grc/config" "gemini-grc/logging" "github.com/guregu/null/v5" @@ -28,20 +28,11 @@ type PageData struct { Data []byte } -// Resolve the URL hostname and -// check if we already have an open -// connection to this host. -// If we can connect, return a list -// of the resolved IPs. func getHostIPAddresses(hostname string) ([]string, error) { addrs, err := net.LookupHost(hostname) if err != nil { return nil, fmt.Errorf("%w:%w", common.ErrNetworkDNS, err) } - IPPool.Lock.RLock() - defer func() { - IPPool.Lock.RUnlock() - }() return addrs, nil } diff --git a/hostPool/hostPool.go b/hostPool/hostPool.go index 317f488..5e7f191 100644 --- a/hostPool/hostPool.go +++ b/hostPool/hostPool.go @@ -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) + } +}