unpublic consensusPerPeerState fields

This commit is contained in:
Fran Bull
2025-04-25 08:48:26 -07:00
parent 7ce80691f2
commit a1a85beef7

View File

@@ -50,7 +50,7 @@ func (ipp *ConsensusIPPool) DomainForIP(from tailcfg.NodeID, addr netip.Addr, up
} }
pm.mu.Lock() pm.mu.Lock()
defer pm.mu.Unlock() defer pm.mu.Unlock()
ww, ok := pm.AddrToDomain.Lookup(addr) ww, ok := pm.addrToDomain.Lookup(addr)
if !ok { if !ok {
log.Printf("DomainForIP: peer state doesn't recognize domain") log.Printf("DomainForIP: peer state doesn't recognize domain")
return "", false return "", false
@@ -96,7 +96,7 @@ func (ipp *ConsensusIPPool) applyMarkLastUsed(from tailcfg.NodeID, addr netip.Ad
log.Printf("applyMarkLastUsed: could not find peer state, nodeID: %s", from) log.Printf("applyMarkLastUsed: could not find peer state, nodeID: %s", from)
return nil return nil
} }
ww, ok := ps.AddrToDomain.Lookup(addr) ww, ok := ps.addrToDomain.Lookup(addr)
if !ok { if !ok {
// The peer state didn't have an entry for the IP address (possibly it expired), so there's nothing to mark. // The peer state didn't have an entry for the IP address (possibly it expired), so there's nothing to mark.
return nil return nil
@@ -110,7 +110,7 @@ func (ipp *ConsensusIPPool) applyMarkLastUsed(from tailcfg.NodeID, addr netip.Ad
return nil return nil
} }
ww.LastUsed = updatedAt ww.LastUsed = updatedAt
ps.AddrToDomain.Insert(netip.PrefixFrom(addr, addr.BitLen()), ww) ps.addrToDomain.Insert(netip.PrefixFrom(addr, addr.BitLen()), ww)
return nil return nil
} }
@@ -132,8 +132,8 @@ type whereWhen struct {
} }
type consensusPerPeerState struct { type consensusPerPeerState struct {
DomainToAddr map[string]netip.Addr domainToAddr map[string]netip.Addr
AddrToDomain *bart.Table[whereWhen] addrToDomain *bart.Table[whereWhen]
mu sync.Mutex mu sync.Mutex
} }
@@ -158,7 +158,7 @@ func (ps *consensusPerPeerState) unusedIPV4(ipset *netipx.IPSet, reuseDeadline t
continue continue
} }
for toIP.Compare(ip) != -1 { for toIP.Compare(ip) != -1 {
ww, ok := ps.AddrToDomain.Lookup(ip) ww, ok := ps.addrToDomain.Lookup(ip)
if !ok { if !ok {
return ip, false, "", nil return ip, false, "", nil
} }
@@ -266,24 +266,24 @@ func (ipp *ConsensusIPPool) executeCheckoutAddr(bs []byte) tsconsensus.CommandRe
// so that's fine). // so that's fine).
func (ipp *ConsensusIPPool) applyCheckoutAddr(nid tailcfg.NodeID, domain string, reuseDeadline, updatedAt time.Time) (netip.Addr, error) { func (ipp *ConsensusIPPool) applyCheckoutAddr(nid tailcfg.NodeID, domain string, reuseDeadline, updatedAt time.Time) (netip.Addr, error) {
pm, _ := ipp.perPeerMap.LoadOrStore(nid, &consensusPerPeerState{ pm, _ := ipp.perPeerMap.LoadOrStore(nid, &consensusPerPeerState{
AddrToDomain: &bart.Table[whereWhen]{}, addrToDomain: &bart.Table[whereWhen]{},
}) })
if existing, ok := pm.DomainToAddr[domain]; ok { if existing, ok := pm.domainToAddr[domain]; ok {
// TODO (fran) handle error case where this doesn't exist // TODO (fran) handle error case where this doesn't exist
ww, _ := pm.AddrToDomain.Lookup(existing) ww, _ := pm.addrToDomain.Lookup(existing)
ww.LastUsed = updatedAt ww.LastUsed = updatedAt
pm.AddrToDomain.Insert(netip.PrefixFrom(existing, existing.BitLen()), ww) pm.addrToDomain.Insert(netip.PrefixFrom(existing, existing.BitLen()), ww)
return existing, nil return existing, nil
} }
addr, wasInUse, previousDomain, err := pm.unusedIPV4(ipp.IPSet, reuseDeadline) addr, wasInUse, previousDomain, err := pm.unusedIPV4(ipp.IPSet, reuseDeadline)
if err != nil { if err != nil {
return netip.Addr{}, err return netip.Addr{}, err
} }
mak.Set(&pm.DomainToAddr, domain, addr) mak.Set(&pm.domainToAddr, domain, addr)
if wasInUse { if wasInUse {
delete(pm.DomainToAddr, previousDomain) delete(pm.domainToAddr, previousDomain)
} }
pm.AddrToDomain.Insert(netip.PrefixFrom(addr, addr.BitLen()), whereWhen{Domain: domain, LastUsed: updatedAt}) pm.addrToDomain.Insert(netip.PrefixFrom(addr, addr.BitLen()), whereWhen{Domain: domain, LastUsed: updatedAt})
return addr, nil return addr, nil
} }