Simplify IP pool and convert it to host pool
This commit is contained in:
@@ -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()
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gemini-grc/common"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
gourl "net/url"
|
gourl "net/url"
|
||||||
@@ -14,6 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gemini-grc/common"
|
||||||
"gemini-grc/config"
|
"gemini-grc/config"
|
||||||
"gemini-grc/logging"
|
"gemini-grc/logging"
|
||||||
"github.com/guregu/null/v5"
|
"github.com/guregu/null/v5"
|
||||||
@@ -28,20 +28,11 @@ type PageData struct {
|
|||||||
Data []byte
|
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) {
|
func getHostIPAddresses(hostname string) ([]string, error) {
|
||||||
addrs, err := net.LookupHost(hostname)
|
addrs, err := net.LookupHost(hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w:%w", common.ErrNetworkDNS, err)
|
return nil, fmt.Errorf("%w:%w", common.ErrNetworkDNS, err)
|
||||||
}
|
}
|
||||||
IPPool.Lock.RLock()
|
|
||||||
defer func() {
|
|
||||||
IPPool.Lock.RUnlock()
|
|
||||||
}()
|
|
||||||
return addrs, nil
|
return addrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package gemini
|
package hostPool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"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
|
var hostPool = HostPool{hostnames: make(map[string]struct{})} //nolint:gochecknoglobals
|
||||||
|
|
||||||
type HostPool struct {
|
type HostPool struct {
|
||||||
@@ -30,3 +32,23 @@ func (p *HostPool) Delete(key string) {
|
|||||||
defer p.Lock.Unlock()
|
defer p.Lock.Unlock()
|
||||||
delete(p.hostnames, key)
|
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