don't sleep unnecessarily

This commit is contained in:
gutmet 2023-04-05 22:52:31 +02:00
parent ce5102caf7
commit 2c18a1a57c

View File

@ -55,12 +55,15 @@ func (l *Location) cachedAddress() (Address, bool) {
return val, ok return val, ok
} }
func (l *Location) fetchAddress() Address { func (l *Location) fetchAddress(shallWait bool) (addr Address, nextShallWait bool) {
if l == nil { if l == nil {
return Address{} return addr, false
} }
if cachedAddress, hasKey := l.cachedAddress(); hasKey { if cachedAddress, hasKey := l.cachedAddress(); hasKey {
return cachedAddress return cachedAddress, false
}
if shallWait {
time.Sleep(sleepBetweenLookup)
} }
content, err := NominatimRequest(l) content, err := NominatimRequest(l)
if err == nil && len(content) > 0 { if err == nil && len(content) > 0 {
@ -69,10 +72,10 @@ func (l *Location) fetchAddress() Address {
if err == nil { if err == nil {
address := d.Address.simpleAddress() address := d.Address.simpleAddress()
knownAddresses[l.String()] = address knownAddresses[l.String()] = address
return address return address, true
} }
} }
return Address{Error: errors.New("fetchAddress: " + err.Error())} return Address{Error: errors.New("fetchAddress: " + err.Error())}, true
} }
func (l *Location) String() string { func (l *Location) String() string {
@ -84,11 +87,9 @@ func (l *Location) String() string {
func fetchAddresses(locations []Location) map[Location]Address { func fetchAddresses(locations []Location) map[Location]Address {
addresses := make(map[Location]Address) addresses := make(map[Location]Address)
for i, location := range locations { nextShallWait := false
addresses[location] = location.fetchAddress() for _, location := range locations {
if i < len(locations)-1 { addresses[location], nextShallWait = location.fetchAddress(nextShallWait)
time.Sleep(sleepBetweenLookup) // TODO: unnötig bei gecacheten
}
} }
return addresses return addresses
} }