lib: Replace done channel with contexts in and add names to util services (#6166)

This commit is contained in:
Simon Frei
2019-11-21 08:41:15 +01:00
committed by GitHub
parent 552ea68672
commit 90d85fd0a2
34 changed files with 240 additions and 218 deletions

View File

@@ -3,6 +3,7 @@
package client
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
@@ -32,11 +33,11 @@ func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan pr
certs: certs,
timeout: timeout,
}
c.commonClient = newCommonClient(invitations, c.serve)
c.commonClient = newCommonClient(invitations, c.serve, c.String())
return c
}
func (c *dynamicClient) serve(stop chan struct{}) error {
func (c *dynamicClient) serve(ctx context.Context) error {
uri := *c.pooladdr
// Trim off the `dynamic+` prefix
@@ -69,9 +70,9 @@ func (c *dynamicClient) serve(stop chan struct{}) error {
addrs = append(addrs, ruri.String())
}
for _, addr := range relayAddressesOrder(addrs, stop) {
for _, addr := range relayAddressesOrder(ctx, addrs) {
select {
case <-stop:
case <-ctx.Done():
l.Debugln(c, "stopping")
return nil
default:
@@ -148,7 +149,7 @@ type dynamicAnnouncement struct {
// the closest 50ms, and puts them in buckets of 50ms latency ranges. Then
// shuffles each bucket, and returns all addresses starting with the ones from
// the lowest latency bucket, ending with the highest latency buceket.
func relayAddressesOrder(input []string, stop chan struct{}) []string {
func relayAddressesOrder(ctx context.Context, input []string) []string {
buckets := make(map[int][]string)
for _, relay := range input {
@@ -162,7 +163,7 @@ func relayAddressesOrder(input []string, stop chan struct{}) []string {
buckets[id] = append(buckets[id], relay)
select {
case <-stop:
case <-ctx.Done():
return nil
default:
}