~egtann/lanhttp

ref: 967092d4f2df lanhttp/lanhttp.go -rw-r--r-- 5.9 KiB
967092d4 — Evan Tann add isc license 7 months ago
                                                                                
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package lanhttp

import (
	"context"
	"encoding/json"
	"fmt"
	"math/rand"
	"net"
	"net/http"
	"net/url"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/hashicorp/go-cleanhttp"
)

type Client struct {
	client HTTPClient
	log    *logger
	stop   chan struct{}

	// backends that are currently live
	backends Routes

	// mu protects backends from concurrent access
	mu sync.RWMutex
}

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

// Logger is the public logging interface. We wrap this with our own logger to
// provide some more control.
type Logger interface{ Printf(string, ...interface{}) }

type Routes map[string][]string

type logger struct {
	l  Logger
	mu sync.RWMutex
}

func (l *logger) Printf(s string, vs ...interface{}) {
	// By default don't log
	if l.l == nil {
		return
	}

	// If we have a logger, then lock it to ensure we don't write while
	// it's being replaced. In practice we only log on errors so this
	// should have a negligible impact
	l.mu.RLock()
	defer l.mu.RUnlock()

	l.l.Printf(s, vs...)
}

func NewClient(client HTTPClient) *Client {
	return &Client{
		log:      &logger{},
		client:   client,
		backends: Routes{},
		stop:     make(chan struct{}),
	}
}

func DefaultClient(timeout time.Duration) *Client {
	cc := cleanhttp.DefaultClient()
	cc.Timeout = timeout
	return NewClient(cc)
}

// WithLogger replaces the logger of a client in a threadsafe way. This can be
// used for instance to load up the internal LAN clients immediately, then
// update the logger with new settings later in the program, e.g. after
// environment variables are loaded and you try to connect to internal
// services.
func (c *Client) WithLogger(lg Logger) *Client {
	c.log.mu.Lock()
	defer c.log.mu.Unlock()

	c.log = &logger{l: lg}
	return c
}

// changeRoutes in the client for internal servers. This can be called
// periodically based on healthchecks from an external service such as a
// reverse proxy. Unless you are manually updating your routes, you should use
// StartUpdating and StopUpdating instead.
func (c *Client) changeRoutes(new Routes) {
	// Check if routes have changed. Most of the time they have not, so we
	// don't need the write lock.
	if changed := diff(new, c.Routes()); !changed {
		return
	}
	c.mu.Lock()
	defer c.mu.Unlock()

	c.backends = new
}

func (c *Client) first(urls []string, timeout time.Duration) Routes {
	// Share a single context among all requests, so they're all canceled
	// or time out together
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	ch := make(chan map[string][]string, len(urls))
	update := func(uri string) {
		req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
		if err != nil {
			c.log.Printf("%s: new request: %s", uri, err)
			return
		}
		resp, err := c.client.Do(req)
		if err != nil {
			c.log.Printf("%s: do: %s", uri, err)
			return
		}
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			c.log.Printf("%s: bad status code: %d", uri, resp.StatusCode)
			return
		}
		routes := map[string][]string{}
		if err := json.NewDecoder(resp.Body).Decode(&routes); err != nil {
			c.log.Printf("%s: decode: %s", uri, err)
			return
		}
		ch <- routes
	}
	for _, uri := range urls {
		go update(uri)
	}
	select {
	case routes := <-ch:
		return routes
	case <-ctx.Done():
		// Default to keeping our existing routes, so a slowdown from
		// the reverse proxy doesn't cause an outage
		return c.Routes()
	}
}

func (c *Client) WithRoutes(routes Routes) *Client {
	c.mu.Lock()
	defer c.mu.Unlock()

	c.backends = routes
	return c
}

// StartUpdating live backends with an initial, synchronous update before
// continuing. Try all URLs simultaneously and use results from the first
// reply. Note that even when this fails, we still allow the code to
// continue... Just don't expect internal IPs to route until the servers come
// online.
func (c *Client) StartUpdating(urls []string, every time.Duration) {
	c.changeRoutes(c.first(urls, every))
	go func() {
		for {
			select {
			case <-time.After(every):
				c.changeRoutes(c.first(urls, every))
			case <-c.stop:
				return
			}
		}
	}()
}

func (c *Client) StopUpdating() {
	// Send if listening, otherwise do nothing
	select {
	case c.stop <- struct{}{}:
	default:
	}
}

func (c *Client) Do(req *http.Request) (*http.Response, error) {
	req.URL = c.ResolveHost(req.URL)
	return c.client.Do(req)
}

// ResolveHost from a URL to a specific IP if internal, otherwise return the
// URL unmodified.
func (c *Client) ResolveHost(uri *url.URL) *url.URL {
	host, port, err := net.SplitHostPort(uri.Host)
	if err != nil {
		host = uri.Host
		port = ""
	}
	if !strings.HasSuffix(host, ".internal") {
		return uri
	}
	ip := c.getIP(host)
	if ip == "" {
		return uri
	}
	if port == "" {
		uri.Host = ip
	} else {
		uri.Host = fmt.Sprintf("%s:%s", ip, port)
	}
	return uri
}

func (c *Client) getIP(host string) string {
	c.mu.RLock()
	defer c.mu.RUnlock()

	ips, ok := c.backends[host]
	if !ok {
		return ""
	}
	if len(ips) == 0 {
		return ""
	}
	return ips[rand.Intn(len(ips))]
}

// Routes returns a copy of all live backend IPs.
func (c *Client) Routes() Routes {
	c.mu.RLock()
	defer c.mu.RUnlock()

	r := make(Routes, len(c.backends))
	for host, ips := range c.backends {
		r[host] = append([]string{}, ips...)
	}
	return r
}

func diff(a, b Routes) bool {
	// Exit quickly if lengths are different
	if len(a) != len(b) {
		return true
	}

	// Iterate through every key in a and determine if all IPs match
	for key := range a {
		if (a[key] == nil) != (b[key] == nil) {
			return true
		}
		if len(a[key]) != len(b[key]) {
			return true
		}

		// Sort the live backends to get better performance when
		// diffing them
		sort.Slice(a[key], func(i, j int) bool {
			return a[key][i] < a[key][j]
		})
		sort.Slice(b[key], func(i, j int) bool {
			return b[key][i] < b[key][j]
		})

		// Compare two and exit on the first different string
		for i, ip := range a[key] {
			if b[key][i] != ip {
				return true
			}
		}
	}
	return false
}