forked from pierrre/imageserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
groupcache.go
160 lines (147 loc) · 4.31 KB
/
groupcache.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Package advanced provides a groupcache example.
//
// Run:
// go run groupcache.go -http=:8081 -groupcache-peers=:8082,:8083
// go run groupcache.go -http=:8082 -groupcache-peers=:8081,:8083
// go run groupcache.go -http=:8083 -groupcache-peers=:8081,:8082
//
// Open http://localhost:8081/medium.jpg?width=100
//
// Stats are available on http://localhost:8081/stats
package main
import (
"crypto/sha256"
"encoding/json"
"flag"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/disintegration/gift"
"github.com/golang/groupcache"
"github.com/cognusion/imageserver"
imageserver_cache "github.com/cognusion/imageserver/cache"
imageserver_cache_groupcache "github.com/cognusion/imageserver/cache/groupcache"
imageserver_http "github.com/cognusion/imageserver/http"
imageserver_http_gift "github.com/cognusion/imageserver/http/gift"
imageserver_http_image "github.com/cognusion/imageserver/http/image"
imageserver_image "github.com/cognusion/imageserver/image"
_ "github.com/cognusion/imageserver/image/gif"
imageserver_image_gift "github.com/cognusion/imageserver/image/gift"
_ "github.com/cognusion/imageserver/image/jpeg"
_ "github.com/cognusion/imageserver/image/png"
imageserver_testdata "github.com/cognusion/imageserver/testdata"
)
const (
groupcacheName = "imageserver"
)
var (
flagHTTP = ":8080"
flagGroupcache = int64(128 * (1 << 20))
flagGroupcachePeers string
)
func main() {
parseFlags()
startHTTPServer()
}
func parseFlags() {
flag.StringVar(&flagHTTP, "http", flagHTTP, "HTTP")
flag.Int64Var(&flagGroupcache, "groupcache", flagGroupcache, "Groupcache")
flag.StringVar(&flagGroupcachePeers, "groupcache-peers", flagGroupcachePeers, "Groupcache peers")
flag.Parse()
}
func startHTTPServer() {
http.Handle("/", http.StripPrefix("/", newImageHTTPHandler()))
http.Handle("/favicon.ico", http.NotFoundHandler())
initGroupcacheHTTPPool() // it automatically registers itself to "/_groupcache"
http.HandleFunc("/stats", groupcacheStatsHTTPHandler)
err := http.ListenAndServe(flagHTTP, nil)
if err != nil {
panic(err)
}
}
func newImageHTTPHandler() http.Handler {
return &imageserver_http.Handler{
Parser: imageserver_http.ListParser([]imageserver_http.Parser{
&imageserver_http.SourcePathParser{},
&imageserver_http_gift.ResizeParser{},
&imageserver_http_image.FormatParser{},
&imageserver_http_image.QualityParser{},
}),
Server: newServer(),
}
}
func newServer() imageserver.Server {
srv := imageserver_testdata.Server
srv = newServerImage(srv)
srv = newServerGroupcache(srv)
return srv
}
func newServerImage(srv imageserver.Server) imageserver.Server {
return &imageserver.HandlerServer{
Server: srv,
Handler: &imageserver_image.Handler{
Processor: &imageserver_image_gift.ResizeProcessor{
DefaultResampling: gift.LanczosResampling,
},
},
}
}
func newServerGroupcache(srv imageserver.Server) imageserver.Server {
if flagGroupcache <= 0 {
return srv
}
return imageserver_cache_groupcache.NewServer(
srv,
imageserver_cache.NewParamsHashKeyGenerator(sha256.New),
groupcacheName,
flagGroupcache,
)
}
func initGroupcacheHTTPPool() {
self := (&url.URL{Scheme: "http", Host: flagHTTP}).String()
var peers []string
peers = append(peers, self)
for _, p := range strings.Split(flagGroupcachePeers, ",") {
if p == "" {
continue
}
peer := (&url.URL{Scheme: "http", Host: p}).String()
peers = append(peers, peer)
}
pool := groupcache.NewHTTPPool(self)
pool.Context = imageserver_cache_groupcache.HTTPPoolContext
pool.Transport = imageserver_cache_groupcache.NewHTTPPoolTransport(nil)
pool.Set(peers...)
}
func groupcacheStatsHTTPHandler(w http.ResponseWriter, req *http.Request) {
gp := groupcache.GetGroup(groupcacheName)
if gp == nil {
http.Error(w, fmt.Sprintf("group %s not found", groupcacheName), http.StatusServiceUnavailable)
return
}
type cachesStats struct {
Main groupcache.CacheStats
Hot groupcache.CacheStats
}
type stats struct {
Group groupcache.Stats
Caches cachesStats
}
data, err := json.MarshalIndent(
stats{
Group: gp.Stats,
Caches: cachesStats{
Main: gp.CacheStats(groupcache.MainCache),
Hot: gp.CacheStats(groupcache.HotCache),
},
},
"",
" ",
)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
_, _ = w.Write(data)
}