-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
161 lines (144 loc) · 4.19 KB
/
main.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
161
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
_ "net/http/pprof"
"time"
"golang.org/x/net/publicsuffix"
"github.com/facebookgo/grace/gracehttp"
"github.com/golang/groupcache"
"gopkg.in/yaml.v1"
)
type Config struct {
// Fetch timeout
Timeout int64 `yaml:"timeout"`
// Enable Keep-Alive on fetch
KeepAlive bool `yaml:"keep_alive"`
HTML struct {
CacheSize int64 `yaml:"cache_size"`
MaxItemSize int64 `yaml:"max_item_size"`
}
Image struct {
CacheSize int64 `yaml:"cache_size"`
MaxItemSize int64 `yaml:"max_item_size"`
}
Dimension struct {
CacheSize int64 `yaml:"cache_size"`
}
}
var (
flagConfigFile = flag.String("config", "ggfetch.yml", "Config file to use.")
flagBind = flag.String("bind", "localhost", "Address to bind on. Special value ec2 will use the local ipv4 address and localhost instead.")
flagPort = flag.Int("port", 9001, "Port to listen on.")
flagListenLocal = flag.Bool("listenlocal", false, "Listen to 127.0.0.1 in addition to the bind address.")
flagMaster = flag.String("master", "", "Master server to get config from.")
)
// http client
func getHTTPClient(c *Config) *http.Client {
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
check(err)
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: !c.KeepAlive,
},
Jar: jar,
Timeout: time.Duration(c.Timeout) * time.Second,
}
}
func main() {
flag.Parse()
// Special case for ec2 binding
if *flagBind == "ec2" {
resp, err := http.Get("http://169.254.169.254/latest/meta-data/local-ipv4/")
if err != nil {
panic(err)
}
content, err := ioutil.ReadAll(resp.Body)
check(err)
resp.Body.Close()
*flagBind = string(content)
*flagListenLocal = true
}
me := fmt.Sprintf("%s:%d", *flagBind, *flagPort)
var config Config
if *flagMaster == "" {
bytes, err := ioutil.ReadFile(*flagConfigFile)
check(err)
yaml.Unmarshal(bytes, &config)
*flagMaster = me
} else {
log.Println("Getting config from master:", *flagMaster)
resp, err := http.Get(fmt.Sprintf("http://%s/config", *flagMaster))
check(err)
check(json.NewDecoder(resp.Body).Decode(&config))
resp.Body.Close()
}
log.Printf("Config loaded: %#v", config)
// Setup GGFetch
defaultHTTPClient := getHTTPClient(&config)
ggfetch := new(GGFetchHandler)
ggfetch.Register("html", HTMLFetcher{
MaxItemSize: config.HTML.MaxItemSize << 10,
Client: defaultHTTPClient,
}, config.HTML.CacheSize<<20)
ggfetch.Register("image", ImageFetcher{
MaxItemSize: config.Image.MaxItemSize << 10,
Client: defaultHTTPClient,
}, config.Image.CacheSize<<20)
ggfetch.Register("dimension", DimensionFetcher{
Client: defaultHTTPClient,
}, config.Dimension.CacheSize<<20)
// Fetchers
http.Handle("/", ggfetch)
http.HandleFunc("/config", func(response http.ResponseWriter, request *http.Request) {
json.NewEncoder(response).Encode(config)
})
http.HandleFunc("/stats", func(response http.ResponseWriter, request *http.Request) {
var stats struct {
Caches map[string]groupcache.CacheStats
}
stats.Caches = make(map[string]groupcache.CacheStats)
for name, handler := range ggfetch.methods {
stats.Caches[name] = handler.Group.CacheStats(groupcache.MainCache)
stats.Caches[name+"_hot"] = handler.Group.CacheStats(groupcache.HotCache)
}
json.NewEncoder(response).Encode(stats)
})
// Peers
peers := NewPeersPool("http://" + me)
peersManager := new(PeersManager)
http.Handle("/ping", peersManager)
go peersManager.Heartbeat(fmt.Sprintf("http://%s/ping?peer=%s", *flagMaster, me), peers.Set)
var servers []*http.Server
servers = append(servers, &http.Server{
Addr: me,
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 60 * time.Second,
})
if *flagListenLocal {
servers = append(servers, &http.Server{
Addr: fmt.Sprintf("localhost:%d", *flagPort),
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 60 * time.Second,
})
}
for _, server := range servers {
server.SetKeepAlivesEnabled(false)
}
check(gracehttp.Serve(servers...))
}
func check(err error) {
if err != nil {
panic(err)
}
}