@@ -60,6 +60,7 @@ type Options struct {
60
60
UpdateInterval time.Duration
61
61
LogBufferInterval time.Duration
62
62
Connector Connector
63
+ InitConnectionCh chan struct {} // only to be used in tests
63
64
}
64
65
65
66
// New creates and starts a provisioner daemon.
@@ -84,6 +85,9 @@ func New(clientDialer Dialer, opts *Options) *Server {
84
85
mets := NewMetrics (reg )
85
86
opts .Metrics = & mets
86
87
}
88
+ if opts .InitConnectionCh == nil {
89
+ opts .InitConnectionCh = make (chan struct {})
90
+ }
87
91
88
92
ctx , ctxCancel := context .WithCancel (context .Background ())
89
93
daemon := & Server {
@@ -93,11 +97,12 @@ func New(clientDialer Dialer, opts *Options) *Server {
93
97
clientDialer : clientDialer ,
94
98
clientCh : make (chan proto.DRPCProvisionerDaemonClient ),
95
99
96
- closeContext : ctx ,
97
- closeCancel : ctxCancel ,
98
- closedCh : make (chan struct {}),
99
- shuttingDownCh : make (chan struct {}),
100
- acquireDoneCh : make (chan struct {}),
100
+ closeContext : ctx ,
101
+ closeCancel : ctxCancel ,
102
+ closedCh : make (chan struct {}),
103
+ shuttingDownCh : make (chan struct {}),
104
+ acquireDoneCh : make (chan struct {}),
105
+ initConnectionCh : opts .InitConnectionCh ,
101
106
}
102
107
103
108
daemon .wg .Add (2 )
@@ -115,6 +120,11 @@ type Server struct {
115
120
116
121
wg sync.WaitGroup
117
122
123
+ // initConnectionCh will receive when the daemon connects to coderd for the
124
+ // first time.
125
+ initConnectionCh chan struct {}
126
+ initConnectionOnce sync.Once
127
+
118
128
// mutex protects all subsequent fields
119
129
mutex sync.Mutex
120
130
// closeContext is canceled when we start closing.
@@ -231,6 +241,9 @@ connectLoop:
231
241
}
232
242
p .opts .Logger .Info (p .closeContext , "successfully connected to coderd" )
233
243
retrier .Reset ()
244
+ p .initConnectionOnce .Do (func () {
245
+ close (p .initConnectionCh )
246
+ })
234
247
235
248
// serve the client until we are closed or it disconnects
236
249
for {
0 commit comments