~egtann/lanhttp

91e1dc1a175a5fa0ec668565085b9e666f019ef0 — Evan Tann 1 year, 2 months ago efa94c6
improve ctx logic
1 files changed, 6 insertions(+), 4 deletions(-)

M lanhttp.go
M lanhttp.go => lanhttp.go +6 -4
@@ 106,11 106,13 @@ func (c *Client) UpdateRoutes(new Routes) {
}

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 Routes)
	update := func(uri string) {
		ctx, cancel := context.WithTimeout(context.Background(), timeout)
		defer cancel()

		req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
		if err != nil {
			c.log.Printf("%s: new request: %s", uri, err)


@@ 139,7 141,7 @@ func (c *Client) first(urls []string, timeout time.Duration) Routes {
	select {
	case routes := <-ch:
		return routes
	case <-time.After(timeout):
	case <-ctx.Done():
		// Default to keeping our existing routes, so a slowdown from
		// the reverse proxy doesn't cause an outage
		return c.Routes()