-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
client.go
68 lines (58 loc) · 2.17 KB
/
client.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
package client
import (
"context"
"errors"
"fmt"
"strings"
"time"
stnrv1 "github.com/l7mp/stunner/pkg/apis/v1"
"github.com/pion/logging"
)
var errFileTruncated = errors.New("zero-length config file")
var (
// Send pings to the CDS server with this period. Must be less than PongWait.
PingPeriod = 5 * time.Second
// Time allowed to read the next pong message from the CDS server.
PongWait = 8 * time.Second
// Time allowed to write a message to the CDS server.
WriteWait = 2 * time.Second
// Period for retrying failed CDS connections.
RetryPeriod = 1 * time.Second
)
// Client represents a generic config client. Currently supported config providers: http, ws, or
// file. Configuration obtained through the client are not validated, make sure to validate on the
// receiver side.
type Client interface {
// Load grabs a new configuration from the config client.
Load() (*stnrv1.StunnerConfig, error)
// Watch grabs new configs from a config origin (config file or CDS server) and returns
// them on the channel. The context cancels the watcher. If the origin is not available
// watch will retry.
Watch(ctx context.Context, ch chan<- *stnrv1.StunnerConfig) error
// Poll creates a one-shot config watcher without the retry mechanincs of Watch.
Poll(ctx context.Context, ch chan<- *stnrv1.StunnerConfig) error
fmt.Stringer
}
// New creates a generic config client. Origin is either a network address in the form
// "<IP>:<port>" or a proper HTTP/WS URI, in which case a CDS client is returned, or a proper file
// URL "file://<path>/<filename>" in which case a config file watcher is returned.
func New(origin string, id string, logger logging.LoggerFactory) (Client, error) {
u, err := getURI(origin)
if err != nil {
return nil, fmt.Errorf("could not parse config origin URI %q: %w", origin, err)
}
switch strings.ToLower(u.Scheme) {
case "http", "ws", "https", "wss":
client, err := NewCDSClient(u.String(), id, logger.NewLogger("cds-client"))
if err != nil {
return nil, err
}
return client, nil
default:
client, err := NewConfigFileClient(origin, id, logger.NewLogger("config-file-client"))
if err != nil {
return nil, err
}
return client, nil
}
}