Commit c68e7c72 by bergquist

avatar: avoid concurrent map writes

parent ea2f65b4
...@@ -25,6 +25,8 @@ import ( ...@@ -25,6 +25,8 @@ import (
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1" "gopkg.in/macaron.v1"
gocache "github.com/patrickmn/go-cache"
) )
var gravatarSource string var gravatarSource string
...@@ -92,7 +94,7 @@ func (this *Avatar) Update() (err error) { ...@@ -92,7 +94,7 @@ func (this *Avatar) Update() (err error) {
type CacheServer struct { type CacheServer struct {
notFound *Avatar notFound *Avatar
cache map[string]*Avatar cache *gocache.Cache
} }
func (this *CacheServer) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) { func (this *CacheServer) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) {
...@@ -110,7 +112,9 @@ func (this *CacheServer) Handler(ctx *macaron.Context) { ...@@ -110,7 +112,9 @@ func (this *CacheServer) Handler(ctx *macaron.Context) {
var avatar *Avatar var avatar *Avatar
if avatar, _ = this.cache[hash]; avatar == nil { if obj, exist := this.cache.Get(hash); exist {
avatar = obj.(*Avatar)
} else {
avatar = New(hash) avatar = New(hash)
} }
...@@ -124,7 +128,7 @@ func (this *CacheServer) Handler(ctx *macaron.Context) { ...@@ -124,7 +128,7 @@ func (this *CacheServer) Handler(ctx *macaron.Context) {
if avatar.notFound { if avatar.notFound {
avatar = this.notFound avatar = this.notFound
} else { } else {
this.cache[hash] = avatar this.cache.Add(hash, avatar, gocache.DefaultExpiration)
} }
ctx.Resp.Header().Add("Content-Type", "image/jpeg") ctx.Resp.Header().Add("Content-Type", "image/jpeg")
...@@ -146,7 +150,7 @@ func NewCacheServer() *CacheServer { ...@@ -146,7 +150,7 @@ func NewCacheServer() *CacheServer {
return &CacheServer{ return &CacheServer{
notFound: newNotFound(), notFound: newNotFound(),
cache: make(map[string]*Avatar), cache: gocache.New(time.Hour, time.Hour*2),
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment