Skip to content

Commit 718b80f

Browse files
oktalzmjuraga
authored andcommitted
BUG/MEDIUM: cluster: wait until all resources and routines are ready
wait until all resources and routines are ready before starting connection process. previously connection process could be started earlier that other resources expected
1 parent 6ea2929 commit 718b80f

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

cmd/dataplaneapi/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package main
1717

1818
import (
19+
"crypto/tls"
1920
"fmt"
21+
"net/http"
2022
"os"
2123
"path"
2224
"syscall"
25+
"time"
2326

2427
loads "github.com/go-openapi/loads"
2528
"github.com/go-openapi/runtime"
@@ -218,6 +221,28 @@ func startServer(cfg *configuration.Configuration) (reload configuration.AtomicB
218221
}()
219222

220223
server.ConfigureAPI()
224+
schema := "http"
225+
if len(server.EnabledListeners) > 0 {
226+
schema = server.EnabledListeners[0]
227+
}
228+
path := fmt.Sprintf("%s://%s:%d%s", schema, server.Host, server.Port, cfg.RuntimeData.APIBasePath)
229+
go func(path string) {
230+
tr := &http.Transport{
231+
// we just need to check if we have a response
232+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec
233+
}
234+
client := &http.Client{Transport: tr}
235+
numTrys := 0
236+
for ; numTrys < 10; numTrys++ {
237+
_, err := client.Get(path)
238+
if err == nil {
239+
cfg.Notify.ServerStarted.Notify()
240+
return
241+
}
242+
time.Sleep(500 * time.Millisecond)
243+
}
244+
log.Fatalf("check if dataplane is running failed on %s", path)
245+
}(path)
221246
if err := server.Serve(); err != nil {
222247
log.Fatalln(err)
223248
}

configuration/cluster_sync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"net/http"
3232
"os"
3333
"path"
34-
"runtime"
3534
"strconv"
3635
"strings"
3736
"time"
@@ -88,11 +87,12 @@ func (c *ClusterSync) Monitor(cfg *Configuration, cli *client_native.HAProxyClie
8887
c.certFetch = make(chan struct{}, 2)
8988
go c.fetchCert()
9089

90+
<-c.cfg.Notify.ServerStarted.Subscribe("clusterMonitor")
91+
9192
key := c.cfg.Cluster.BootstrapKey.Load()
9293
certFetched := cfg.Cluster.CertificateFetched.Load()
9394

9495
if key != "" && !certFetched {
95-
runtime.Gosched()
9696
c.cfg.Notify.BootstrapKeyChanged.Notify()
9797
}
9898
}

configuration/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ type RuntimeData struct {
134134

135135
type NotifyConfiguration struct {
136136
BootstrapKeyChanged *ChanNotify `yaml:"-"`
137+
ServerStarted *ChanNotify `yaml:"-"`
137138
CertificateRefresh *ChanNotify `yaml:"-"`
138139
Reload *ChanNotify `yaml:"-"`
139140
Shutdown *ChanNotify `yaml:"-"`
@@ -171,6 +172,7 @@ func Get() *Configuration {
171172
cfg = &Configuration{}
172173
cfg.initSignalHandler()
173174
cfg.Notify.BootstrapKeyChanged = NewChanNotify()
175+
cfg.Notify.ServerStarted = NewChanNotify()
174176
cfg.Notify.CertificateRefresh = NewChanNotify()
175177
cfg.Notify.Reload = NewChanNotify()
176178
cfg.Notify.Shutdown = NewChanNotify()
@@ -204,6 +206,7 @@ func (c *Configuration) GetStorageData() *StorageDataplaneAPIConfiguration {
204206

205207
func (c *Configuration) UnSubscribeAll() {
206208
c.Notify.BootstrapKeyChanged.UnSubscribeAll()
209+
c.Notify.ServerStarted.UnSubscribeAll()
207210
c.Notify.CertificateRefresh.UnSubscribeAll()
208211
c.Notify.Reload.UnSubscribeAll()
209212
c.Notify.Shutdown.UnSubscribeAll()

0 commit comments

Comments
 (0)