-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipath.go
97 lines (87 loc) · 2.54 KB
/
multipath.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
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
package chained
import (
"context"
"fmt"
"net"
"github.com/getlantern/common/config"
"github.com/getlantern/errors"
"github.com/getlantern/flashlight/v7/balancer"
"github.com/getlantern/flashlight/v7/common"
"github.com/getlantern/flashlight/v7/ops"
"github.com/getlantern/multipath"
)
type mpDialerAdapter struct {
impl proxyImpl
label string
}
func (d *mpDialerAdapter) DialContext(ctx context.Context) (net.Conn, error) {
var op *ops.Op
if value := ctx.Value("op"); value != nil {
if existing, ok := value.(*ops.Op); ok {
op = existing.Begin("dial_subflow")
}
}
if op == nil {
op = ops.Begin("dial_subflow")
}
defer op.End()
return d.impl.dialServer(op, ctx)
}
func (d *mpDialerAdapter) Label() string {
return d.label
}
type multipathImpl struct {
nopCloser
dialer multipath.Dialer
}
func (impl *multipathImpl) dialServer(op *ops.Op, ctx context.Context) (net.Conn, error) {
return impl.dialer.DialContext(context.WithValue(ctx, "op", op))
}
func (impl *multipathImpl) FormatStats() []string {
return impl.dialer.(multipath.Stats).FormatStats()
}
func CreateMPDialer(configDir, endpoint string, ss map[string]*config.ProxyConfig, uc common.UserConfig) (balancer.Dialer, error) {
if len(ss) < 1 {
return nil, errors.New("no dialers")
}
var p *proxy
var err error
var dialers []multipath.Dialer
for name, s := range ss {
if p == nil {
// Note: we pass the first server info to newProxy for the attributes shared by all paths
p, err = newProxy(endpoint, endpoint+":0", "multipath", "multipath", s, uc)
if err != nil {
return nil, err
}
}
addr, transport, _, err := extractParams(s)
if err != nil {
return nil, err
}
impl, err := createImpl(configDir, name, addr, transport, s, uc, p.reportDialCore)
if err != nil {
log.Errorf("failed to add %v to %v, continuing: %v", s.Addr, name, err)
continue
}
label := fmt.Sprintf("%-38s at %21s", fmt.Sprintf("%s(%s)", name, transport), addr)
dialers = append(dialers, &mpDialerAdapter{impl, label})
}
if len(dialers) == 0 {
return nil, errors.New("no subflow dialer")
}
p.impl = &multipathImpl{dialer: multipath.NewDialer(endpoint, dialers)}
return p, nil
}
func groupByMultipathEndpoint(proxies map[string]*config.ProxyConfig) map[string]map[string]*config.ProxyConfig {
groups := make(map[string]map[string]*config.ProxyConfig)
for name, s := range proxies {
group, exists := groups[s.MultipathEndpoint]
if !exists {
group = make(map[string]*config.ProxyConfig)
groups[s.MultipathEndpoint] = group
}
group[name] = s
}
return groups
}