@@ -19,17 +19,6 @@ type upstreamWithStats struct {
1919
2020 // queryDuration is the duration of the successful DNS lookup.
2121 queryDuration time.Duration
22-
23- // isFallback indicates whether the upstream is a fallback upstream.
24- isFallback bool
25- }
26-
27- // newUpstreamWithStats returns a new initialized *upstreamWithStats.
28- func newUpstreamWithStats (upstream upstream.Upstream , isFallback bool ) (u * upstreamWithStats ) {
29- return & upstreamWithStats {
30- upstream : upstream ,
31- isFallback : isFallback ,
32- }
3322}
3423
3524// type check
@@ -57,14 +46,10 @@ func (u *upstreamWithStats) Close() (err error) {
5746
5847// upstreamsWithStats takes a list of upstreams, wraps each upstream with
5948// [upstreamWithStats] to gather statistics, and returns the wrapped upstreams.
60- func upstreamsWithStats (
61- upstreams []upstream.Upstream ,
62- isFallback bool ,
63- ) (wrapped []upstream.Upstream ) {
49+ func upstreamsWithStats (upstreams []upstream.Upstream ) (wrapped []upstream.Upstream ) {
6450 wrapped = make ([]upstream.Upstream , 0 , len (upstreams ))
6551 for _ , u := range upstreams {
66- w := newUpstreamWithStats (u , isFallback )
67- wrapped = append (wrapped , w )
52+ wrapped = append (wrapped , & upstreamWithStats {upstream : u })
6853 }
6954
7055 return wrapped
@@ -130,53 +115,39 @@ func collectQueryStats(
130115 wrapped , ok = resolver .(* upstreamWithStats )
131116 if ! ok {
132117 // Should never happen.
133- err := fmt .Errorf ("unexpected type %T" , resolver )
134- panic (err )
118+ panic (fmt .Errorf ("unexpected type %T" , resolver ))
135119 }
136120
137121 unwrapped = wrapped .upstream
138122 }
139123
124+ // The DNS query was not resolved.
140125 if wrapped == nil {
141- return unwrapped , & QueryStatistics {
142- main : collectUpstreamStats (upstreams ),
143- fallback : collectUpstreamStats (fallbacks ),
126+ return nil , & QueryStatistics {
127+ main : collectUpstreamStats (upstreams ... ),
128+ fallback : collectUpstreamStats (fallbacks ... ),
144129 }
145130 }
146131
147- if mode == UpstreamModeFastestAddr && ! wrapped .isFallback {
132+ // The DNS query was successfully resolved by main resolver and the upstream
133+ // mode is [UpstreamModeFastestAddr].
134+ if mode == UpstreamModeFastestAddr && len (fallbacks ) == 0 {
148135 return unwrapped , & QueryStatistics {
149- main : collectUpstreamStats (upstreams ),
136+ main : collectUpstreamStats (upstreams ... ),
150137 }
151138 }
152139
153- return unwrapped , collectResolverQueryStats (upstreams , wrapped )
154- }
155-
156- // collectResolverQueryStats gathers the statistics from an upstream DNS
157- // resolver that successfully resolved the request. If resolver is the fallback
158- // DNS resolver, it also gathers the statistics for the upstream DNS resolvers.
159- // resolver must be not nil.
160- func collectResolverQueryStats (
161- upstreams []upstream.Upstream ,
162- resolver * upstreamWithStats ,
163- ) (stats * QueryStatistics ) {
164- dur , err := resolver .queryDuration , resolver .err
165- s := & UpstreamStatistics {
166- Address : resolver .upstream .Address (),
167- Error : err ,
168- QueryDuration : dur ,
169- }
170-
171- if resolver .isFallback {
172- return & QueryStatistics {
173- main : collectUpstreamStats (upstreams ),
174- fallback : []* UpstreamStatistics {s },
140+ // The DNS query was resolved by fallback resolver.
141+ if len (fallbacks ) > 0 {
142+ return unwrapped , & QueryStatistics {
143+ main : collectUpstreamStats (upstreams ... ),
144+ fallback : collectUpstreamStats (wrapped ),
175145 }
176146 }
177147
178- return & QueryStatistics {
179- main : []* UpstreamStatistics {s },
148+ // The DNS query was successfully resolved by main resolver.
149+ return unwrapped , & QueryStatistics {
150+ main : collectUpstreamStats (wrapped ),
180151 }
181152}
182153
@@ -200,22 +171,20 @@ type UpstreamStatistics struct {
200171
201172// collectUpstreamStats gathers the upstream statistics from the list of wrapped
202173// upstreams. upstreams must be of type *upstreamWithStats.
203- func collectUpstreamStats (upstreams [] upstream.Upstream ) (stats []* UpstreamStatistics ) {
174+ func collectUpstreamStats (upstreams ... upstream.Upstream ) (stats []* UpstreamStatistics ) {
204175 stats = make ([]* UpstreamStatistics , 0 , len (upstreams ))
205176
206177 for _ , u := range upstreams {
207178 w , ok := u .(* upstreamWithStats )
208179 if ! ok {
209180 // Should never happen.
210- err := fmt .Errorf ("unexpected type %T" , u )
211- panic (err )
181+ panic (fmt .Errorf ("unexpected type %T" , u ))
212182 }
213183
214- dur , err := w .queryDuration , w .err
215184 stats = append (stats , & UpstreamStatistics {
216- Error : err ,
185+ Error : w . err ,
217186 Address : w .Address (),
218- QueryDuration : dur ,
187+ QueryDuration : w . queryDuration ,
219188 })
220189 }
221190
0 commit comments