package irohimport ()// DNSProvenance is the provenance string for [DNSAddressLookup] items.constDNSProvenance = "dns"// dnsStaggerMs are the delays, in milliseconds, after which additional DNS// lookups are issued while earlier ones are still in flight. Each query has its// own 3s timeout, so a lookup aborts after at most 6s.//// iroh/src/address_lookup/dns.rs DNS_STAGGERING_MS.var dnsStaggerMs = []int{200, 300, 600, 1000, 2000, 3000}// DNSAddressLookup resolves endpoint addressing information from DNS. It queries// TXT records under "_iroh.<z32-endpoint-id>.<origin>" using the endpoint's DNS// resolver, where <origin> is the discovery origin domain.//// The zero value is not usable; create one with [NewDNSAddressLookup] or// [N0DNSAddressLookup].//// It is the Go analog of iroh's DNSAddressLookup.typeDNSAddressLookupstruct { origin string resolver *dns.Resolver}// NewDNSAddressLookup returns a DNSAddressLookup querying origin (for example// [dns.N0DNSEndpointOriginProd]) using resolver. If resolver is nil, a default// [dns.Resolver] backed by the system DNS configuration is used.func ( string, *dns.Resolver) *DNSAddressLookup {if == nil { = &dns.Resolver{} }return &DNSAddressLookup{origin: , resolver: }}// N0DNSAddressLookup returns a DNSAddressLookup using the number0 production// discovery origin ([dns.N0DNSEndpointOriginProd]).func ( *dns.Resolver) *DNSAddressLookup {returnNewDNSAddressLookup(dns.N0DNSEndpointOriginProd, )}// Resolve looks up id in DNS, issuing staggered concurrent queries and yielding// the first successful result or an error.func ( *DNSAddressLookup) ( context.Context, key.EndpointID) iter.Seq2[Item, error] {returnfunc( func(Item, error) bool) { , := .lookupStaggered(, )if != nil {if .Err() == nil { (Item{}, lookupErr(DNSProvenance, )) }return }if .Err() == nil { (NewItem(, DNSProvenance, nil), nil) } }}// lookupStaggered issues a first DNS lookup immediately and additional ones// after each delay in [dnsStaggerMs] while earlier attempts are still in// flight, returning the first success or the last error once all attempts fail.func ( *DNSAddressLookup) ( context.Context, key.EndpointID) (dns.EndpointInfo, error) { , := context.WithCancel()defer ()typestruct {dns.EndpointInfoerror } := len(dnsStaggerMs) + 1 := make(chan , ) := func() {gofunc() { , := .resolver.LookupEndpointByID(, , .origin) <- {: , : } }() } () := make([]*time.Timer, len(dnsStaggerMs))for , := rangednsStaggerMs { [] = time.AfterFunc(time.Duration()*time.Millisecond, ) }deferfunc() {for , := range { .Stop() } }()varerrorfor := 0; < ; ++ {select {case := <-:if . == nil {return ., nil } = .case<-.Done():returndns.EndpointInfo{}, .Err() } }returndns.EndpointInfo{}, }
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.