sort in internal scoring, extract latency keys in batch for top 100

This commit is contained in:
2024-12-17 03:39:02 -06:00
parent db9bcb06dc
commit 6c00da29a7
4 changed files with 58 additions and 8 deletions
+4 -2
View File
@@ -74,6 +74,10 @@ func (l *LatencyQueue) SetHandler(handler chan<- PingResult) {
l.handlerChannel = handler
}
func (l *LatencyQueue) GetSelfIP() net.IP {
return l.ipSelf
}
func (l *LatencyQueue) RefreshIP() error {
resp, err := http.Get("https://api.ipify.org?format=text")
if err != nil {
@@ -145,8 +149,6 @@ func (l *LatencyQueue) Start(ctx context.Context) {
pinger.Interval = time.Nanosecond
pinger.Timeout = time.Millisecond * 500
l.logger.Debugw("Ping Request", "ip", ip)
// Process the request
err = pinger.Run()
if err != nil {
+13 -1
View File
@@ -3,6 +3,7 @@ package api
import (
"fmt"
"math"
"slices"
"go.uber.org/zap"
)
@@ -190,10 +191,21 @@ func ScoreOffers(offers []Offer) []ScoredOffer {
}
}
newScore := score * multiplier
// sugar.Infow("Multiplier Applied", "offer", offer.ID, "baseScore", score, "score", newScore, "multiplier", multiplier)
score = newScore
scoredOffers = append(scoredOffers, ScoredOffer{Offer: offer, Score: score, Reasons: reasons})
}
// Sort by score
slices.SortStableFunc(scoredOffers, func(a, b ScoredOffer) int {
if a.Score < b.Score {
return 1
} else if a.Score > b.Score {
return -1
} else {
return 0
}
})
return scoredOffers
}