fix(publicip): do not retry on too many requests
This commit is contained in:
@@ -2,6 +2,7 @@ package publicip
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -94,11 +95,18 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
result, exit := l.fetchIPData(runCtx)
|
result, err := l.fetchIPData(runCtx)
|
||||||
if exit {
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastFetch = l.timeNow()
|
||||||
|
timerIsReadyToReset = l.updateTimer(*l.settings.Period, lastFetch, timer, timerIsReadyToReset)
|
||||||
|
|
||||||
|
if errors.Is(err, ipinfo.ErrTooManyRequests) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
message := "Public IP address is " + result.IP.String()
|
message := "Public IP address is " + result.IP.String()
|
||||||
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
|
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
|
||||||
l.logger.Info(message)
|
l.logger.Info(message)
|
||||||
@@ -108,37 +116,34 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
|
|||||||
l.ipDataMutex.Unlock()
|
l.ipDataMutex.Unlock()
|
||||||
|
|
||||||
filepath := *l.settings.IPFilepath
|
filepath := *l.settings.IPFilepath
|
||||||
err := persistPublicIP(filepath, result.IP.String(), l.puid, l.pgid)
|
err = persistPublicIP(filepath, result.IP.String(), l.puid, l.pgid)
|
||||||
if err != nil { // non critical error, which can be fixed with settings updates.
|
if err != nil { // non critical error, which can be fixed with settings updates.
|
||||||
l.logger.Error(err.Error())
|
l.logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
lastFetch = l.timeNow()
|
|
||||||
timerIsReadyToReset = l.updateTimer(*l.settings.Period, lastFetch, timer, timerIsReadyToReset)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loop) fetchIPData(ctx context.Context) (result ipinfo.Response, exit bool) {
|
func (l *Loop) fetchIPData(ctx context.Context) (result ipinfo.Response, err error) {
|
||||||
// keep retrying since settings updates won't change the
|
// keep retrying since settings updates won't change the
|
||||||
// behavior of the following code.
|
// behavior of the following code.
|
||||||
const defaultBackoffTime = 5 * time.Second
|
const defaultBackoffTime = 5 * time.Second
|
||||||
backoffTime := defaultBackoffTime
|
backoffTime := defaultBackoffTime
|
||||||
for {
|
for {
|
||||||
var err error
|
|
||||||
result, err = l.fetcher.FetchInfo(ctx, netip.Addr{})
|
result, err = l.fetcher.FetchInfo(ctx, netip.Addr{})
|
||||||
if err == nil {
|
switch {
|
||||||
return result, false
|
case err == nil:
|
||||||
}
|
return result, nil
|
||||||
|
case ctx.Err() != nil:
|
||||||
exit = ctx.Err() != nil
|
return result, err
|
||||||
if exit {
|
case errors.Is(err, ipinfo.ErrTooManyRequests):
|
||||||
return result, true
|
l.logger.Warn(err.Error() + "; not retrying.")
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
l.logger.Error(fmt.Sprintf("%s - retrying in %s", err, backoffTime))
|
l.logger.Error(fmt.Sprintf("%s - retrying in %s", err, backoffTime))
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return result, true
|
return result, ctx.Err()
|
||||||
case <-time.After(backoffTime):
|
case <-time.After(backoffTime):
|
||||||
}
|
}
|
||||||
const backoffTimeMultipler = 2
|
const backoffTimeMultipler = 2
|
||||||
|
|||||||
Reference in New Issue
Block a user