@ -31,15 +31,15 @@ type ForEachAddrOpts struct {
}
}
// ForEachAddr returns a Probe that resolves a given hostname into all
// ForEachAddr returns a Probe that resolves a given hostname into all
// available IP addresses, and then calls a function to create a new Probe
// available IP addresses, and then calls a function to create new Probes
// every time a new IP is discovered. The Probe returned will be closed if an
// every time a new IP is discovered. The Probe s returned will be closed if an
// IP address is no longer in the DNS record for the given hostname. This can
// IP address is no longer in the DNS record for the given hostname. This can
// be used to healthcheck every IP address that a hostname resolves to.
// be used to healthcheck every IP address that a hostname resolves to.
func ForEachAddr ( host string , newProbe func ( netip . Addr ) * Probe , opts ForEachAddrOpts ) ProbeFunc {
func ForEachAddr ( host string , makeProbes func ( netip . Addr ) [ ] * Probe , opts ForEachAddrOpts ) ProbeFunc {
return makeForEachAddr ( host , newProbe , opts ) . run
return makeForEachAddr ( host , makeProbes , opts ) . run
}
}
func makeForEachAddr ( host string , newProbe func ( netip . Addr ) * Probe , opts ForEachAddrOpts ) * forEachAddrProbe {
func makeForEachAddr ( host string , makeProbes func ( netip . Addr ) [ ] * Probe , opts ForEachAddrOpts ) * forEachAddrProbe {
if opts . Logf == nil {
if opts . Logf == nil {
opts . Logf = logger . Discard
opts . Logf = logger . Discard
}
}
@ -54,9 +54,9 @@ func makeForEachAddr(host string, newProbe func(netip.Addr) *Probe, opts ForEach
logf : opts . Logf ,
logf : opts . Logf ,
host : host ,
host : host ,
networks : opts . Networks ,
networks : opts . Networks ,
newProbe: newProbe ,
makeProbes: makeProbes ,
lookupNetIP : opts . LookupNetIP ,
lookupNetIP : opts . LookupNetIP ,
probes : make ( map [ netip . Addr ] * Probe ) ,
probes : make ( map [ netip . Addr ] [ ] * Probe ) ,
}
}
}
}
@ -65,12 +65,12 @@ type forEachAddrProbe struct {
logf logger . Logf
logf logger . Logf
host string
host string
networks [ ] string
networks [ ] string
newProbe func ( netip . Addr ) * Probe
makeProbes func ( netip . Addr ) [ ] * Probe
lookupNetIP func ( context . Context , string , string ) ( [ ] netip . Addr , error )
lookupNetIP func ( context . Context , string , string ) ( [ ] netip . Addr , error )
// state
// state
mu sync . Mutex // protects following
mu sync . Mutex // protects following
probes map [ netip . Addr ] * Probe
probes map [ netip . Addr ] [ ] * Probe
}
}
// run matches the ProbeFunc signature
// run matches the ProbeFunc signature
@ -102,23 +102,25 @@ func (f *forEachAddrProbe) run(ctx context.Context) error {
}
}
// Make a new probe, and add it to 'probes'; if the
// Make a new probe, and add it to 'probes'; if the
// function returns nil , we skip it.
// function returns an empty list , we skip it.
probe := f . newProbe ( addr )
probe s := f . makeProbes ( addr )
if probe == nil {
if len ( probes ) == 0 {
continue
continue
}
}
f . logf ( "adding new probe for %v", addr )
f . logf ( "adding %d new probes for %v", len ( probes ) , addr )
f . probes [ addr ] = probe
f . probes [ addr ] = probe s
}
}
// Remove probes that we didn't see during this address resolution.
// Remove probes that we didn't see during this address resolution.
for addr , probe := range f . probes {
for addr , probe s := range f . probes {
if ! sawIPs [ addr ] {
if ! sawIPs [ addr ] {
f . logf ( "removing probe for %v", addr )
f . logf ( "removing %d probes for %v", len ( probes ) , addr )
// This IP is no longer in the DNS record. Close and remove the probe.
// This IP is no longer in the DNS record. Close and remove all probes
probe . Close ( )
for _ , probe := range probes {
probe . Close ( )
}
delete ( f . probes , addr )
delete ( f . probes , addr )
}
}
}
}