Files
elmprodvpn/selective-vpn-api/app/resolver/precheck_finalize.go

107 lines
2.7 KiB
Go

package resolver
import (
"os"
"strings"
)
type ResolverPrecheckFinalizeInput struct {
PrecheckDue bool
PrecheckStatePath string
Now int
TimeoutRecheck ResolverTimeoutRecheckStats
LiveBatchTarget int
LiveBatchMin int
LiveBatchMax int
LiveBatchNXHeavyPct int
LiveBatchNXHeavyMin int
LiveBatchNXHeavyMax int
DNSStats DNSMetrics
LiveDeferred int
ResolvedNowDNS int
LiveP1 int
LiveP2 int
LiveP3 int
LiveNXHeavyTotal int
LiveNXHeavySkip int
ToResolveTotal int
PrecheckFileForced bool
PrecheckForcePath string
}
type ResolverPrecheckFinalizeResult struct {
NextTarget int
NextReason string
NextNXPct int
NextNXReason string
Saved bool
ForceFileConsumed bool
}
func FinalizeResolverPrecheck(in ResolverPrecheckFinalizeInput, logf func(string, ...any)) ResolverPrecheckFinalizeResult {
out := ResolverPrecheckFinalizeResult{}
if in.PrecheckDue {
nextTarget, nextReason := ComputeNextLiveBatchTarget(in.LiveBatchTarget, in.LiveBatchMin, in.LiveBatchMax, in.DNSStats, in.LiveDeferred)
nextNXPct, nextNXReason := ComputeNextLiveBatchNXHeavyPct(
in.LiveBatchNXHeavyPct,
in.LiveBatchNXHeavyMin,
in.LiveBatchNXHeavyMax,
in.DNSStats,
in.ResolvedNowDNS,
in.LiveP3,
in.LiveNXHeavyTotal,
in.LiveNXHeavySkip,
)
if logf != nil {
logf(
"resolve live-batch nxheavy: pct=%d next=%d reason=%s selected=%d total=%d skipped=%d",
in.LiveBatchNXHeavyPct,
nextNXPct,
nextNXReason,
in.LiveP3,
in.LiveNXHeavyTotal,
in.LiveNXHeavySkip,
)
}
SaveResolverPrecheckState(
in.PrecheckStatePath,
in.Now,
in.TimeoutRecheck,
ResolverLiveBatchStats{
Target: in.LiveBatchTarget,
Total: in.ToResolveTotal,
Deferred: in.LiveDeferred,
P1: in.LiveP1,
P2: in.LiveP2,
P3: in.LiveP3,
NXHeavyPct: in.LiveBatchNXHeavyPct,
NXHeavyTotal: in.LiveNXHeavyTotal,
NXHeavySkip: in.LiveNXHeavySkip,
NextTarget: nextTarget,
NextReason: nextReason,
NextNXPct: nextNXPct,
NextNXReason: nextNXReason,
DNSAttempts: in.DNSStats.Attempts,
DNSTimeout: in.DNSStats.Timeout,
DNSCoolSkips: in.DNSStats.Skipped,
},
)
out.NextTarget = nextTarget
out.NextReason = nextReason
out.NextNXPct = nextNXPct
out.NextNXReason = nextNXReason
out.Saved = true
}
if in.PrecheckFileForced && strings.TrimSpace(in.PrecheckForcePath) != "" {
_ = os.Remove(in.PrecheckForcePath)
if logf != nil {
logf("resolve precheck force-file consumed: %s", in.PrecheckForcePath)
}
out.ForceFileConsumed = true
}
return out
}