-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsource_config.go
More file actions
120 lines (103 loc) · 3.11 KB
/
Copy pathsource_config.go
File metadata and controls
120 lines (103 loc) · 3.11 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
package connet
import (
"fmt"
"time"
)
// SourceConfig structure represents source configuration.
type SourceConfig struct {
Endpoint Endpoint
Route RouteOption
RelayEncryptions []EncryptionScheme
DialTimeout time.Duration
DestinationPolicy LoadBalancePolicy
DestinationRetry LoadBalanceRetry
DestinationRetryMax int
}
// NewSourceConfig creates a source config for a given name.
func NewSourceConfig(name string) SourceConfig {
return SourceConfig{
Endpoint: NewEndpoint(name),
Route: RouteAny,
RelayEncryptions: []EncryptionScheme{NoEncryption},
DestinationPolicy: NoPolicy,
DestinationRetry: NeverRetry,
}
}
// WithRoute sets the route option for this configuration.
func (cfg SourceConfig) WithRoute(route RouteOption) SourceConfig {
cfg.Route = route
return cfg
}
// WithRelayEncryptions sets the relay encryptions option for this configuration.
func (cfg SourceConfig) WithRelayEncryptions(schemes ...EncryptionScheme) SourceConfig {
cfg.RelayEncryptions = schemes
return cfg
}
// WithDialTimeout sets the dial timeout
func (cfg SourceConfig) WithDialTimeout(timeout time.Duration) SourceConfig {
cfg.DialTimeout = timeout
return cfg
}
// WithLoadBalance sets the load balancing behavior for this source
func (cfg SourceConfig) WithLoadBalance(policy LoadBalancePolicy, retry LoadBalanceRetry, max int) SourceConfig {
cfg.DestinationPolicy = policy
cfg.DestinationRetry = retry
cfg.DestinationRetryMax = max
switch {
case cfg.DestinationRetry == CountRetry && cfg.DestinationRetryMax == 0:
cfg.DestinationRetryMax = 2
case cfg.DestinationRetry == TimedRetry && cfg.DestinationRetryMax == 0:
cfg.DestinationRetryMax = 1000
}
return cfg
}
func (cfg SourceConfig) endpointConfig() endpointConfig {
return endpointConfig{
endpoint: cfg.Endpoint,
role: RoleSource,
route: cfg.Route,
}
}
type LoadBalancePolicy struct{ string }
var (
NoPolicy = LoadBalancePolicy{}
LeastLatencyPolicy = LoadBalancePolicy{"least-latency"}
LeastConnsPolicy = LoadBalancePolicy{"least-conns"}
RoundRobinPolicy = LoadBalancePolicy{"round-robin"}
RandomPolicy = LoadBalancePolicy{"random"}
)
func ParseLBPolicy(s string) (LoadBalancePolicy, error) {
switch s {
case NoPolicy.string:
return NoPolicy, nil
case LeastLatencyPolicy.string:
return LeastLatencyPolicy, nil
case LeastConnsPolicy.string:
return LeastConnsPolicy, nil
case RoundRobinPolicy.string:
return RoundRobinPolicy, nil
case RandomPolicy.string:
return RandomPolicy, nil
}
return NoPolicy, fmt.Errorf("invalid load balance policy '%s'", s)
}
type LoadBalanceRetry struct{ string }
var (
NeverRetry = LoadBalanceRetry{}
CountRetry = LoadBalanceRetry{"count"}
TimedRetry = LoadBalanceRetry{"timed"}
AllRetry = LoadBalanceRetry{"all"}
)
func ParseLBRetry(s string) (LoadBalanceRetry, error) {
switch s {
case NeverRetry.string:
return NeverRetry, nil
case CountRetry.string:
return CountRetry, nil
case TimedRetry.string:
return TimedRetry, nil
case AllRetry.string:
return AllRetry, nil
}
return NeverRetry, fmt.Errorf("invalid load balance retry '%s'", s)
}