@@ -46,6 +46,7 @@ const (
46
46
)
47
47
48
48
var (
49
+ errNoQuestion = errors .New ("no question" )
49
50
errCacheResponseEmpty = errors .New ("empty cache response" )
50
51
errCacheResponseMismatch = errors .New ("cache response mismatch" )
51
52
)
@@ -259,6 +260,10 @@ func (cb *cache) put(response []byte, s *Summary) (ok bool) {
259
260
}
260
261
261
262
func asResponse (q * dns.Msg , v * cres , fresh bool ) (r []byte , s * Summary , err error ) {
263
+ if q == nil {
264
+ err = errNoQuestion
265
+ return
266
+ }
262
267
a := v .ans
263
268
if a == nil {
264
269
err = errCacheResponseEmpty
@@ -294,37 +299,34 @@ func (t *ctransport) Type() string {
294
299
return t .Transport .Type ()
295
300
}
296
301
297
- type anssummary struct {
298
- ans []byte
299
- s * Summary
300
- }
301
-
302
302
func (t * ctransport ) fetch (network string , q []byte , msg * dns.Msg , summary * Summary , cb * cache , key string ) (r []byte , err error ) {
303
303
sendRequest := func (async bool ) ([]byte , error ) {
304
- var s * Summary
304
+ var finalsumm * Summary
305
305
if async {
306
- s = new (Summary )
306
+ finalsumm = new (Summary )
307
307
} else {
308
- s = summary
308
+ finalsumm = summary
309
309
}
310
310
311
- s .ID = t .Transport .ID ()
312
- s .Type = t .Transport .Type ()
311
+ finalsumm .ID = t .Transport .ID ()
312
+ finalsumm .Type = t .Transport .Type ()
313
313
314
314
rv := t .reqbarrier .Do (key , func () (any , error ) {
315
- ans , err := t .Transport .Query (network , q , s )
316
- cb .put (ans , s )
317
- return & anssummary {ans , s }, err
315
+ ans , err := t .Transport .Query (network , q , finalsumm )
316
+ cb .put (ans , finalsumm )
317
+ return & cres {ans : xdns . AsMsg ( ans ) , s : finalsumm }, err
318
318
})
319
319
320
- asmm , ok := rv .Val .(* anssummary )
320
+ cachedres , ok := rv .Val .(* cres )
321
321
if ! ok {
322
322
return nil , errCacheResponseMismatch
323
323
}
324
- // fill summary regardless of rv.Err
325
- asmm .s .FillInto (s ) // asmm.s may be equal to s
326
324
327
- return asmm .ans , rv .Err
325
+ finalres , origsumm , finalerr := asResponse (msg , cachedres , true )
326
+ // fill summary regardless of errors
327
+ origsumm .FillInto (finalsumm ) // origsumm may be equal to s
328
+
329
+ return finalres , errors .Join (rv .Err , finalerr )
328
330
}
329
331
330
332
// check if underlying transport can connect fine, if not treat cache
0 commit comments