1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
package fixchain
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"sync/atomic"
"time"
)
type lockedCache struct {
m map[string][]byte
sync.RWMutex
}
func (c *lockedCache) get(str string) ([]byte, bool) {
c.RLock()
defer c.RUnlock()
b, ok := c.m[str]
return b, ok
}
func (c *lockedCache) set(str string, b []byte) {
c.Lock()
defer c.Unlock()
c.m[str] = b
}
func newLockedCache() *lockedCache {
return &lockedCache{m: make(map[string][]byte)}
}
type urlCache struct {
client *http.Client
cache *lockedCache
hit uint32
miss uint32
errors uint32
badStatus uint32
readFail uint32
}
func (u *urlCache) getURL(url string) ([]byte, error) {
r, ok := u.cache.get(url)
if ok {
atomic.AddUint32(&u.hit, 1)
return r, nil
}
c, err := u.client.Get(url)
if err != nil {
atomic.AddUint32(&u.errors, 1)
return nil, err
}
defer c.Body.Close()
// TODO(katjoyce): Add caching of permanent errors.
if c.StatusCode != 200 {
atomic.AddUint32(&u.badStatus, 1)
return nil, fmt.Errorf("can't deal with status %d", c.StatusCode)
}
r, err = ioutil.ReadAll(c.Body)
if err != nil {
atomic.AddUint32(&u.readFail, 1)
return nil, err
}
atomic.AddUint32(&u.miss, 1)
u.cache.set(url, r)
return r, nil
}
func newURLCache(c *http.Client, logStats bool) *urlCache {
u := &urlCache{cache: newLockedCache(), client: c}
if logStats {
t := time.NewTicker(time.Second)
go func() {
for _ = range t.C {
log.Printf("url cache: %d hits, %d misses, %d errors, "+
"%d bad status, %d read fail, %d cached", u.hit,
u.miss, u.errors, u.badStatus, u.readFail,
len(u.cache.m))
}
}()
}
return u
}
|