-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsource_config.go
More file actions
72 lines (60 loc) · 1.99 KB
/
source_config.go
File metadata and controls
72 lines (60 loc) · 1.99 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
package connet
import (
"time"
"github.com/connet-dev/connet/model"
)
// SourceConfig structure represents source configuration.
type SourceConfig struct {
Endpoint model.Endpoint
Route model.RouteOption
RelayEncryptions []model.EncryptionScheme
DialTimeout time.Duration
DestinationPolicy model.LoadBalancePolicy
DestinationRetry model.LoadBalanceRetry
DestinationRetryMax int
}
// NewSourceConfig creates a source config for a given name.
func NewSourceConfig(name string) SourceConfig {
return SourceConfig{
Endpoint: model.NewEndpoint(name),
Route: model.RouteAny,
RelayEncryptions: []model.EncryptionScheme{model.NoEncryption},
DestinationPolicy: model.NoPolicy,
DestinationRetry: model.NeverRetry,
}
}
// WithRoute sets the route option for this configuration.
func (cfg SourceConfig) WithRoute(route model.RouteOption) SourceConfig {
cfg.Route = route
return cfg
}
// WithRelayEncryptions sets the relay encryptions option for this configuration.
func (cfg SourceConfig) WithRelayEncryptions(schemes ...model.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 model.LoadBalancePolicy, retry model.LoadBalanceRetry, max int) SourceConfig {
cfg.DestinationPolicy = policy
cfg.DestinationRetry = retry
cfg.DestinationRetryMax = max
switch {
case cfg.DestinationRetry == model.CountRetry && cfg.DestinationRetryMax == 0:
cfg.DestinationRetryMax = 2
case cfg.DestinationRetry == model.TimedRetry && cfg.DestinationRetryMax == 0:
cfg.DestinationRetryMax = 1000
}
return cfg
}
func (cfg SourceConfig) endpointConfig() endpointConfig {
return endpointConfig{
endpoint: cfg.Endpoint,
role: model.Source,
route: cfg.Route,
}
}