More AARP tweaks

This commit is contained in:
Josh Deprez 2024-04-13 15:18:33 +10:00
parent 1a83e4d543
commit e91dfcc3d9
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw

45
aarp.go
View file

@ -190,44 +190,34 @@ func (a *AARPMachine) Resolve(ctx context.Context, ddpAddr ddp.Addr) (ethernet.A
return result, nil return result, nil
} }
ctx, cancel := context.WithTimeout(ctx, aarpRequestTimeout) if winner {
defer cancel() if err := a.request(ddpAddr); err != nil {
return ethernet.Addr{}, err
if !winner {
// some other goroutine is running the request
for {
select {
case <-ctx.Done():
return ethernet.Addr{}, ctx.Err()
case <-waitCh:
result, waitCh, _ = a.lookupOrWait(ddpAddr)
if waitCh == nil {
return result, nil
}
}
} }
} }
// I am the winner! I get to send the request packets and run the ticker
if err := a.request(ddpAddr); err != nil {
return ethernet.Addr{}, err
}
ticker := time.NewTicker(aarpRequestRetransmit) ticker := time.NewTicker(aarpRequestRetransmit)
defer ticker.Stop() defer ticker.Stop()
ctx, cancel := context.WithTimeout(ctx, aarpRequestTimeout)
defer cancel()
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
a.requestingStopped(ddpAddr)
return ethernet.Addr{}, ctx.Err() return ethernet.Addr{}, ctx.Err()
case <-waitCh: case <-waitCh:
result, waitCh, _ = a.lookupOrWait(ddpAddr) result, waitCh, winner = a.lookupOrWait(ddpAddr)
if waitCh == nil { if waitCh == nil {
return result, nil return result, nil
} }
case <-ticker.C: case <-ticker.C:
if !winner {
continue
}
if err := a.request(ddpAddr); err != nil { if err := a.request(ddpAddr); err != nil {
return ethernet.Addr{}, err return ethernet.Addr{}, err
} }
@ -362,3 +352,16 @@ func (t *addressMappingTable) lookupOrWait(ddpAddr ddp.Addr) (ethernet.Addr, <-c
} }
return ent.hwAddr, nil, false return ent.hwAddr, nil, false
} }
func (t *addressMappingTable) requestingStopped(ddpAddr ddp.Addr) {
t.mu.Lock()
defer t.mu.Unlock()
if t.table == nil {
return
}
ent := t.table[ddpAddr]
if ent == nil {
return
}
ent.requesting = false
}