Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mirror selection logic in Closest with full-server fallback #22

Merged
merged 3 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}
}

func Remove[V comparable](collection []V, value V) []V {

Check failure on line 82 in config.go

View workflow job for this annotation

GitHub Actions / build

exported function Remove should have comment or be unexported
return lo.Filter(collection, func(item V, _ int) bool {
return item != value
})
Expand All @@ -94,30 +94,26 @@
// Load maxmind database
if r.db != nil {
err = r.db.Close()

if err != nil {
return errors.Wrap(err, "Unable to close database")
}
}

if r.asnDB != nil {
err = r.asnDB.Close()

if err != nil {
return errors.Wrap(err, "Unable to close asn database")
}
}

// db can be hot-reloaded if the file changed
r.db, err = maxminddb.Open(r.config.GeoDBPath)

if err != nil {
return errors.Wrap(err, "Unable to open database")
}

if r.config.ASNDBPath != "" {
r.asnDB, err = maxminddb.Open(r.config.ASNDBPath)

if err != nil {
return errors.Wrap(err, "Unable to open asn database")
}
Expand Down Expand Up @@ -145,21 +141,16 @@

// Create mirror map
mirrors := make(map[string][]*Server)

for _, server := range r.servers {
mirrors[server.Continent] = append(mirrors[server.Continent], server)
}

mirrors["default"] = append(mirrors["NA"], mirrors["EU"]...)

r.regionMap = mirrors

hosts := make(map[string]*Server)

for _, server := range r.servers {
hosts[server.Host] = server
}

r.hostMap = hosts

// Check top choices size
Expand All @@ -180,26 +171,20 @@
var wg sync.WaitGroup

existing := make(map[string]int)

for i, server := range r.servers {
existing[server.Host] = i
}

hosts := make(map[string]bool)

var hostsLock sync.Mutex

for _, server := range r.config.ServerList {
wg.Add(1)

var prefix string

if !strings.HasPrefix(server.Server, "http") {
prefix = "https://"
}

u, err := url.Parse(prefix + server.Server)

if err != nil {
log.WithFields(log.Fields{
"error": err,
Expand All @@ -209,42 +194,35 @@
}

i := -1

if v, exists := existing[u.Host]; exists {
i = v
}

go func(i int, server ServerConfig, u *url.URL) {
defer wg.Done()

s, err := r.addServer(server, u)

if err != nil {
log.WithError(err).Warning("Unable to add server")
return
}

hostsLock.Lock()
hosts[u.Host] = true
hostsLock.Unlock()

if _, ok := existing[u.Host]; ok {
s.Redirects = r.servers[i].Redirects

r.servers[i] = s
} else {
s.Redirects = promauto.NewCounter(prometheus.CounterOpts{
Name: "armbian_router_redirects_" + metricReplacer.Replace(u.Host),
Help: "The number of redirects for server " + u.Host,
})

r.servers = append(r.servers, s)

log.WithFields(log.Fields{
"server": u.Host,
"path": u.Path,
"latitude": s.Latitude,
"longitude": s.Longitude,
"country": s.Country,
}).Info("Added server")
}
}(i, server, u)
Expand All @@ -257,11 +235,9 @@
if _, exists := hosts[r.servers[i].Host]; exists {
continue
}

log.WithFields(log.Fields{
"server": r.servers[i].Host,
}).Info("Removed server")

r.servers = append(r.servers[:i], r.servers[i+1:]...)
}

Expand All @@ -284,33 +260,27 @@
Protocols: []string{"http", "https"},
Rules: server.Rules,
}

if len(server.Protocols) > 0 {
for _, proto := range server.Protocols {
if !lo.Contains(s.Protocols, proto) {
s.Protocols = append(s.Protocols, proto)
}
}
}

// Defaults to 10 to allow servers to be set lower for lower priority
if s.Weight == 0 {
s.Weight = 10
}

ips, err := net.LookupIP(u.Host)

if err != nil {
log.WithFields(log.Fields{
"error": err,
"server": s.Host,
}).Warning("Could not resolve address")
return nil, err
}

var city db.City
err = r.db.Lookup(ips[0], &city)

if err != nil {
log.WithFields(log.Fields{
"error": err,
Expand All @@ -319,35 +289,27 @@
}).Warning("Could not geolocate address")
return nil, err
}

s.Country = city.Country.IsoCode
if s.Continent == "" {
s.Continent = city.Continent.Code
}

if s.Latitude == 0 && s.Longitude == 0 {
s.Latitude = city.Location.Latitude
s.Longitude = city.Location.Longitude
}

return s, nil
}

func (r *Redirector) reloadMap() error {
mapFile := r.config.MapFile

if mapFile == "" {
return nil
}

log.WithField("file", mapFile).Info("Loading download map")

newMap, err := loadMapFile(mapFile)

if err != nil {
return err
}

r.dlMap = newMap

return nil
}
Loading
Loading