Add TTL cache for stops index.

This commit is contained in:
Ray Miller 2020-04-21 08:45:05 +01:00
parent 3caaf59506
commit 507d20810d
5 changed files with 118 additions and 59 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -17,27 +18,32 @@ import (
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
if len(os.Args) != 2 { stopNames := flag.String("stops", "", "Source for refreshment stops")
log.Fatal("Usage: %s GPX_FILE_OR_DIRECTORY") flag.Parse()
if flag.NArg() != 1 {
log.Fatal("Usage: %s [--stops=ctccambridge|cyclingmaps] GPX_FILE_OR_DIRECTORY")
} }
inFile := os.Args[1] inFile := flag.Arg(0)
info, err := os.Stat(inFile) info, err := os.Stat(inFile)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// TODO add --stops flag to select stops database var stops *rtreego.Rtree
stopsIndex, err := cafes.FetchCtcCamIndex() if *stopNames != "" {
if err != nil { var err error
log.Fatal(err) stops, err = cafes.New().Get(*stopNames)
if err != nil {
log.Fatal(err)
}
} }
gs, err := placenames.NewGPXSummarizer() gs, err := placenames.NewGPXSummarizer()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if info.IsDir() { if info.IsDir() {
err = summarizeDirectory(gs, stopsIndex, inFile) err = summarizeDirectory(gs, stops, inFile)
} else { } else {
err = summarizeSingleFile(gs, stopsIndex, inFile) err = summarizeSingleFile(gs, stops, inFile)
} }
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -26,33 +27,13 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
gpxSummarizer = gs gpxSummarizer = gs
if err = loadStops(); err != nil {
log.Fatal(err)
}
http.HandleFunc("/rwgps", rwgpsHandler) http.HandleFunc("/rwgps", rwgpsHandler)
log.Printf("Listening for requests on %s", listenAddr) log.Printf("Listening for requests on %s", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, nil)) log.Fatal(http.ListenAndServe(listenAddr, nil))
} }
func loadStops() error {
var err error
log.Println("Fetching CTC Cambridge cafe stops")
stops["ctccam"], err = cafes.FetchCtcCamIndex()
if err != nil {
return err
}
log.Printf("Loaded %d ctccam stops", stops["ctccam"].Size())
log.Println("Fetching cyclingmaps.net cafe stops")
stops["cyclingmapsnet"], err = cafes.FetchCyclingMapsIndex()
if err != nil {
return err
}
log.Printf("Loaded %d cyclingmapsnet stops", stops["cyclingmapsnet"].Size())
return nil
}
var gpxSummarizer *placenames.GPXSummarizer var gpxSummarizer *placenames.GPXSummarizer
var stops = make(map[string]*rtreego.Rtree) var stops = cafes.New()
func rwgpsHandler(w http.ResponseWriter, r *http.Request) { func rwgpsHandler(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query() q := r.URL.Query()
@ -72,10 +53,15 @@ func rwgpsHandler(w http.ResponseWriter, r *http.Request) {
} }
var stopsIndex *rtreego.Rtree var stopsIndex *rtreego.Rtree
if stopsName != "" { if stopsName != "" {
stopsIndex = stops[stopsName] var err error
if stopsIndex == nil { stopsIndex, err = stops.Get(stopsName)
log.Printf("Invalid stops: %s", stopsName) if err != nil {
http.Error(w, fmt.Sprintf("Invalid stops: %s", stopsName), http.StatusBadRequest) log.Println(err)
if errors.Is(err, cafes.ErrInvalidStops) {
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return return
} }
} }

87
pkg/cafes/common.go Normal file
View file

@ -0,0 +1,87 @@
package cafes
import (
"errors"
"fmt"
"sync"
"time"
"github.com/dhconnelly/rtreego"
)
type RefreshmentStop struct {
Name string
Url string
Easting float64
Northing float64
}
func (s *RefreshmentStop) Bounds() *rtreego.Rect {
p := rtreego.Point{s.Easting, s.Northing}
return p.ToRect(100)
}
func (s *RefreshmentStop) Contains(p rtreego.Point) bool {
if len(p) != 2 {
panic("Expected a 2-dimensional point")
}
bounds := s.Bounds()
for i := 0; i < 2; i++ {
if p[i] < bounds.PointCoord(i) || p[i] > bounds.PointCoord(i)+bounds.LengthsCoord(i) {
return false
}
}
return true
}
// TTL cache based on "9.7 Example: Concurrent Non-Blocking Cache" from
// "The Go Programming Language", Alan A. A. Dovovan and Brian W. Kernighan
type result struct {
value *rtreego.Rtree
err error
}
type entry struct {
res result
expires time.Time
ready chan struct{} // closed when res is ready
}
type Cache struct {
mu sync.Mutex
entries map[string]*entry
}
func New() *Cache {
return &Cache{entries: make(map[string]*entry)}
}
func (c *Cache) Get(k string) (*rtreego.Rtree, error) {
c.mu.Lock()
e := c.entries[k]
if e == nil || e.expires.Before(time.Now()) {
e = &entry{ready: make(chan struct{}), expires: time.Now().Add(4 * time.Hour)}
c.entries[k] = e
c.mu.Unlock()
e.res.value, e.res.err = FetchStops(k)
close(e.ready)
} else {
c.mu.Unlock()
<-e.ready
}
return e.res.value, e.res.err
}
var ErrInvalidStops = errors.New("invalid stops")
func FetchStops(k string) (*rtreego.Rtree, error) {
switch k {
case "ctccambridge":
return FetchCtcCamIndex()
case "cyclingmaps":
return FetchCyclingMapsIndex()
default:
return nil, fmt.Errorf("%w: %s", ErrInvalidStops, k)
}
}

View file

@ -4,6 +4,7 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"github.com/dhconnelly/rtreego" "github.com/dhconnelly/rtreego"
@ -23,31 +24,6 @@ type Waypoints struct {
Waypoints []Waypoint `xml:"wpt"` Waypoints []Waypoint `xml:"wpt"`
} }
type RefreshmentStop struct {
Name string
Url string
Easting float64
Northing float64
}
func (s *RefreshmentStop) Bounds() *rtreego.Rect {
p := rtreego.Point{s.Easting, s.Northing}
return p.ToRect(100)
}
func (s *RefreshmentStop) Contains(p rtreego.Point) bool {
if len(p) != 2 {
panic("Expected a 2-dimensional point")
}
bounds := s.Bounds()
for i := 0; i < 2; i++ {
if p[i] < bounds.PointCoord(i) || p[i] > bounds.PointCoord(i)+bounds.LengthsCoord(i) {
return false
}
}
return true
}
func BuildCtcCamIndex(r io.Reader) (*rtreego.Rtree, error) { func BuildCtcCamIndex(r io.Reader) (*rtreego.Rtree, error) {
dec := xml.NewDecoder(r) dec := xml.NewDecoder(r)
var wpt Waypoints var wpt Waypoints
@ -77,6 +53,7 @@ func BuildCtcCamIndex(r io.Reader) (*rtreego.Rtree, error) {
} }
func FetchCtcCamIndex() (*rtreego.Rtree, error) { func FetchCtcCamIndex() (*rtreego.Rtree, error) {
log.Printf("Fetching %s", ctcCamWaypointsUrl)
res, err := http.Get(ctcCamWaypointsUrl) res, err := http.Get(ctcCamWaypointsUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting %s: %v", ctcCamWaypointsUrl, err) return nil, fmt.Errorf("error getting %s: %v", ctcCamWaypointsUrl, err)
@ -89,5 +66,6 @@ func FetchCtcCamIndex() (*rtreego.Rtree, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("error building CTC Cambridge stops index: %v", err) return nil, fmt.Errorf("error building CTC Cambridge stops index: %v", err)
} }
log.Printf("Loaded %d CTC Cambridge stops", index.Size())
return index, nil return index, nil
} }

View file

@ -54,6 +54,7 @@ func BuildCyclingMapsIndex(r io.Reader) (*rtreego.Rtree, error) {
} }
func FetchCyclingMapsIndex() (*rtreego.Rtree, error) { func FetchCyclingMapsIndex() (*rtreego.Rtree, error) {
log.Printf("Fetching %s", cyclingMapsCafesUrl)
res, err := http.Get(cyclingMapsCafesUrl) res, err := http.Get(cyclingMapsCafesUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting %s: %v", cyclingMapsCafesUrl, err) return nil, fmt.Errorf("error getting %s: %v", cyclingMapsCafesUrl, err)
@ -66,5 +67,6 @@ func FetchCyclingMapsIndex() (*rtreego.Rtree, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("error building cyclingmaps.net cafe stops index: %v", err) return nil, fmt.Errorf("error building cyclingmaps.net cafe stops index: %v", err)
} }
log.Printf("Loaded %d cyclingmaps.net stops", index.Size())
return index, nil return index, nil
} }