@@ -14,6 +14,7 @@ import (
14
14
"strconv"
15
15
"strings"
16
16
"sync"
17
+ "sync/atomic"
17
18
"time"
18
19
19
20
"github.com/daeuniverse/dae/common/consts"
@@ -82,6 +83,11 @@ type DnsController struct {
82
83
dnsForwarderCache map [dnsForwarderKey ]DnsForwarder
83
84
}
84
85
86
+ type handlingState struct {
87
+ mu sync.Mutex
88
+ ref uint32
89
+ }
90
+
85
91
func parseIpVersionPreference (prefer int ) (uint16 , error ) {
86
92
switch prefer := IpVersionPrefer (prefer ); prefer {
87
93
case IpVersionPrefer_No :
@@ -453,11 +459,17 @@ func (c *DnsController) handle_(
453
459
}
454
460
455
461
// No parallel for the same lookup.
456
- _mu , _ := c .handling .LoadOrStore (cacheKey , new (sync.Mutex ))
457
- mu := _mu .(* sync.Mutex )
458
- mu .Lock ()
459
- defer mu .Unlock ()
460
- defer c .handling .Delete (cacheKey )
462
+ handlingState_ , _ := c .handling .LoadOrStore (cacheKey , new (handlingState ))
463
+ handlingState := handlingState_ .(* handlingState )
464
+ atomic .AddUint32 (& handlingState .ref , 1 )
465
+ handlingState .mu .Lock ()
466
+ defer func () {
467
+ handlingState .mu .Unlock ()
468
+ atomic .AddUint32 (& handlingState .ref , ^ uint32 (0 ))
469
+ if atomic .LoadUint32 (& handlingState .ref ) == 0 {
470
+ c .handling .Delete (cacheKey )
471
+ }
472
+ }()
461
473
462
474
if resp := c .LookupDnsRespCache_ (dnsMessage , cacheKey , false ); resp != nil {
463
475
// Send cache to client directly.
0 commit comments