Skip to content

Commit

Permalink
Merge pull request #164 from jovandeginste/retry-osm-geo
Browse files Browse the repository at this point in the history
Retry geocoding request to openstreetmap
  • Loading branch information
jovandeginste committed Jun 4, 2024
2 parents 44bb39c + 3c1cd75 commit fb0b77d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/database/workouts_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"slices"
"time"

"github.com/codingsince1985/geo-golang"
geo "github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/openstreetmap"
"github.com/jovandeginste/workout-tracker/pkg/templatehelpers"
"github.com/labstack/gommon/log"
"github.com/tkrajina/gpxgo/gpx"
"github.com/westphae/geomag/pkg/egm96"
"gorm.io/gorm"
Expand Down Expand Up @@ -173,12 +174,23 @@ func (m *MapCenter) Address() *geo.Address {

geocoder := openstreetmap.Geocoder()

address, err := geocoder.ReverseGeocode(m.Lat, m.Lng)
if err != nil {
return nil
for i := range 5 {
address, err := geocoder.ReverseGeocode(m.Lat, m.Lng)
switch err {
case nil:
return address
case geo.ErrTimeout:
log.Warnf("OpenStreetMap geocoding timeout, retrying in %d seconds", i)
time.Sleep(time.Duration(i) * time.Second)
default:
log.Errorf("OpenStreetMap geocoding error: %v", err)
return nil
}
}

return address
log.Warnf("Too many OpenStreetMap geocoding timeouts, returning nil")

return nil
}

// allGPXPoints returns the first track segment's points
Expand Down

0 comments on commit fb0b77d

Please sign in to comment.