-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtile_proxy.go
More file actions
202 lines (169 loc) · 4.13 KB
/
Copy pathtile_proxy.go
File metadata and controls
202 lines (169 loc) · 4.13 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package tileproxy
import (
"context"
"log"
"net/http"
"regexp"
"sync"
"time"
"github.com/flywave/go-tileproxy/setting"
)
type TileProxy struct {
m sync.RWMutex
Services map[string]*Service
globals *setting.GlobalsSetting
serviceReqRegex *regexp.Regexp
serviceCache map[string]*Service
serviceCacheMu sync.RWMutex
requestTimeout time.Duration
}
func (t *TileProxy) UpdateService(id string, d *setting.ProxyService, fac setting.CacheFactory) {
t.m.Lock()
old, exists := t.Services[id]
t.Services[id] = NewService(d, t.globals, fac)
t.m.Unlock()
if exists {
old.Clean()
}
t.serviceCacheMu.Lock()
t.serviceCache[id] = t.Services[id]
t.serviceCacheMu.Unlock()
log.Printf("service updated: %s", id)
}
func (t *TileProxy) RemoveService(id string) {
t.m.Lock()
d, ok := t.Services[id]
delete(t.Services, id)
t.m.Unlock()
t.serviceCacheMu.Lock()
delete(t.serviceCache, id)
t.serviceCacheMu.Unlock()
if ok {
d.Clean()
log.Printf("service removed: %s", id)
}
}
func (t *TileProxy) Reload(proxy []*setting.ProxyService, fac setting.CacheFactory) {
t.m.Lock()
oldServices := t.Services
t.Services = make(map[string]*Service)
for i := range proxy {
t.Services[proxy[i].Id] = NewService(proxy[i], t.globals, fac)
}
t.m.Unlock()
for _, s := range oldServices {
s.Clean()
}
t.serviceCacheMu.Lock()
t.serviceCache = make(map[string]*Service)
for id, svc := range t.Services {
t.serviceCache[id] = svc
}
t.serviceCacheMu.Unlock()
log.Printf("reloaded %d services", len(t.Services))
}
func (t *TileProxy) Shutdown(ctx context.Context) error {
t.m.Lock()
services := make([]*Service, 0, len(t.Services))
for _, s := range t.Services {
services = append(services, s)
}
t.m.Unlock()
done := make(chan struct{}, 1)
go func() {
for _, s := range services {
s.Clean()
}
done <- struct{}{}
}()
select {
case <-done:
log.Println("all services shut down")
return nil
case <-ctx.Done():
log.Println("shutdown timed out")
return ctx.Err()
}
}
func (t *TileProxy) GetServiceIDs() []string {
t.m.RLock()
defer t.m.RUnlock()
ids := make([]string, 0, len(t.Services))
for id := range t.Services {
ids = append(ids, id)
}
return ids
}
func (t *TileProxy) GetService(id string) *Service {
t.m.RLock()
defer t.m.RUnlock()
return t.Services[id]
}
func (t *TileProxy) SetRequestTimeout(timeout time.Duration) {
t.requestTimeout = timeout
}
func (t *TileProxy) parseServiceId(r *http.Request) string {
match := t.serviceReqRegex.FindStringSubmatch(r.URL.Path)
if len(match) == 0 {
return ""
}
groupNames := t.serviceReqRegex.SubexpNames()
for i, name := range groupNames {
if name != "" && i < len(match) && match[i] != "" {
if name == "service" {
return match[i]
}
}
}
return ""
}
func (s *TileProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
log.Printf("panic serving %s: %v", r.URL.Path, rec)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}()
serviceId := s.parseServiceId(r)
if serviceId == "" {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
start := time.Now()
defer func() {
log.Printf("%s %s -> service=%s duration=%v",
r.Method, r.URL.Path, serviceId, time.Since(start))
}()
s.serviceCacheMu.RLock()
d, ok := s.serviceCache[serviceId]
s.serviceCacheMu.RUnlock()
if !ok {
s.m.RLock()
d, ok = s.Services[serviceId]
s.m.RUnlock()
if ok {
s.serviceCacheMu.Lock()
s.serviceCache[serviceId] = d
s.serviceCacheMu.Unlock()
} else {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
}
if s.requestTimeout > 0 {
ctx, cancel := context.WithTimeout(r.Context(), s.requestTimeout)
defer cancel()
r = r.WithContext(ctx)
}
d.ServeHTTP(w, r)
}
var serviceReqPattern = regexp.MustCompile(`^/(?P<service>[^/]+)`)
func NewTileProxy(globals *setting.GlobalsSetting, proxys []*setting.ProxyService, fac setting.CacheFactory) *TileProxy {
proxy := &TileProxy{
globals: globals,
serviceCache: make(map[string]*Service),
serviceReqRegex: serviceReqPattern,
}
proxy.Reload(proxys, fac)
return proxy
}