just fetch periodically

This commit is contained in:
Stephen McQuay 2018-04-09 20:30:52 -07:00
parent 1a0f3b26f8
commit ae974083aa
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
1 changed files with 40 additions and 24 deletions

64
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"context"
"flag" "flag"
"fmt" "fmt"
"image" "image"
@ -12,45 +13,55 @@ import (
"os" "os"
"sync" "sync"
"time" "time"
"github.com/pkg/errors"
) )
var port = flag.Int("port", 8000, "port") var port = flag.Int("port", 8000, "port")
var freq = flag.Duration("d", 15*time.Second, "refresh interval")
type Now struct { type Now struct {
lastUpdated time.Time sync.RWMutex
cacheWindow time.Duration
buffer *bytes.Buffer cache *bytes.Buffer
lock sync.RWMutex
} }
func NewNow(d time.Duration) *Now { func NewNow(ctx context.Context, d time.Duration) *Now {
return &Now{ n := &Now{
cacheWindow: d, cache: &bytes.Buffer{},
buffer: &bytes.Buffer{},
} }
n.Update()
go func() {
t := time.NewTicker(d)
for {
select {
case <-t.C:
n.Update()
case <-ctx.Done():
return
}
}
}()
return n
} }
func (n *Now) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (n *Now) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Printf("%+v", req.RemoteAddr) n.RLock()
if time.Now().After(n.lastUpdated.Add(n.cacheWindow)) { w.Write(n.cache.Bytes())
log.Println("updating image") n.RUnlock()
n.Update()
n.lastUpdated = time.Now()
}
n.lock.RLock()
w.Write(n.buffer.Bytes())
n.lock.RUnlock()
} }
func (n *Now) Update() { func (n *Now) Update() error {
resp, err := http.Get("http://imgs.xkcd.com/comics/now.png") resp, err := http.Get("http://imgs.xkcd.com/comics/now.png")
if err != nil { if err != nil {
log.Fatal(err) return errors.Wrap(err, "get")
} }
defer resp.Body.Close() defer resp.Body.Close()
src, err := png.Decode(resp.Body) src, err := png.Decode(resp.Body)
if err != nil { if err != nil {
log.Fatal(err) return errors.Wrap(err, "png decode")
} }
bounds := src.Bounds() bounds := src.Bounds()
dest := image.NewNRGBA(bounds) dest := image.NewNRGBA(bounds)
@ -64,9 +75,12 @@ func (n *Now) Update() {
} }
} }
} }
n.lock.Lock()
png.Encode(n.buffer, dest) n.Lock()
n.lock.Unlock() png.Encode(n.cache, dest)
n.Unlock()
return nil
} }
func main() { func main() {
@ -79,7 +93,9 @@ func main() {
log.Printf("serving on: http://%s:%d/", hostname, *port) log.Printf("serving on: http://%s:%d/", hostname, *port)
addr := fmt.Sprintf(":%d", *port) addr := fmt.Sprintf(":%d", *port)
http.Handle("/", NewNow(15*time.Minute)) n := NewNow(context.Background(), *freq)
http.Handle("/", n)
err = http.ListenAndServe(addr, nil) err = http.ListenAndServe(addr, nil)
if err != nil { if err != nil {