/
atcclient_dial.go
150 lines (126 loc) · 2.95 KB
/
atcclient_dial.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package atcclient
import (
"context"
"fmt"
"io/fs"
"net"
"sync"
"time"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/chronos-tachyon/roxy/internal/misc"
"github.com/chronos-tachyon/roxy/lib/roxyutil"
"github.com/chronos-tachyon/roxy/proto/roxy_v0"
)
// Dial returns a gRPC ClientConn connected directly to the given ATC tower.
//
// The caller should _not_ call Close() on it. It is owned by the ATCClient
// and will be re-used until the ATCClient itself is Close()'d.
func (c *ATCClient) Dial(ctx context.Context, addr *net.TCPAddr) (*grpc.ClientConn, roxy_v0.AirTrafficControlClient, error) {
roxyutil.AssertNotNil(&ctx)
roxyutil.AssertNotNil(&addr)
c.wg.Add(1)
defer c.wg.Done()
addr = misc.CanonicalizeTCPAddr(addr)
// begin critical section 1
// {{{
c.mu.Lock()
if c.closed {
c.mu.Unlock()
return nil, nil, fs.ErrClosed
}
cd := c.connMap[addr]
if cd == nil {
cd = new(connData)
cd.cv = sync.NewCond(&c.mu)
c.connMap[addr] = cd
} else {
cd.Wait()
if cd.cc != nil {
cc := cd.cc
atc := cd.atc
c.mu.Unlock()
log.Logger.Trace().
Str("type", "ATCClient").
Str("method", "Dial").
Stringer("addr", addr).
Str("cc", fmt.Sprintf("%p", cc)).
Msg("Call (positive cache hit)")
return cc, atc, nil
}
if time.Now().Before(cd.timeout) {
err := cd.err
c.mu.Unlock()
log.Logger.Trace().
Str("type", "ATCClient").
Str("method", "Dial").
Stringer("addr", addr).
Err(err).
Msg("Call (negative cache hit)")
return nil, nil, err
}
cd.ready = false
cd.cc = nil
cd.atc = nil
cd.err = nil
cd.timeout = time.Time{}
}
c.mu.Unlock()
// end critical section 1
// }}}
dialOpts := make([]grpc.DialOption, 3)
if c.tc == nil {
dialOpts[0] = grpc.WithTransportCredentials(insecure.NewCredentials())
} else {
tc := c.tc.Clone()
if tc.ServerName == "" {
tc.ServerName = addr.IP.String()
}
dialOpts[0] = grpc.WithTransportCredentials(credentials.NewTLS(tc))
}
dialOpts[1] = grpc.WithBlock()
dialOpts[2] = grpc.FailOnNonTempDialError(true)
log.Logger.Trace().
Str("type", "ATCClient").
Str("method", "Dial").
Stringer("addr", addr).
Msg("Call")
cc, err := grpc.DialContext(ctx, addr.String(), dialOpts...)
if err != nil {
log.Logger.Trace().
Str("type", "ATCClient").
Str("method", "Dial").
Err(err).
Msg("Error")
}
// begin critical section 2
// {{{
c.mu.Lock()
if err == nil && c.closed {
cc2 := cc
defer func() {
_ = cc2.Close()
}()
cc = nil
err = fs.ErrClosed
}
var atc roxy_v0.AirTrafficControlClient
if err == nil {
atc = roxy_v0.NewAirTrafficControlClient(cc)
cd.cc = cc
cd.atc = atc
cd.retries = 0
} else {
cd.err = err
cd.timeout = time.Now().Add(gBackoff.Backoff(int(cd.retries)))
cd.retries++
}
cd.ready = true
cd.cv.Broadcast()
c.mu.Unlock()
// end critical section 2
// }}}
return cc, atc, err
}