Skip to content

Commit b715538

Browse files
committed
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 e732817 commit b715538

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

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"
@@ -223,6 +226,28 @@ func startServer(cfg *configuration.Configuration) (reload configuration.AtomicB
223226
}()
224227

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

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
@@ -113,6 +113,7 @@ type RuntimeData struct {
113113

114114
type NotifyConfiguration struct {
115115
BootstrapKeyChanged *ChanNotify `yaml:"-"`
116+
ServerStarted *ChanNotify `yaml:"-"`
116117
CertificateRefresh *ChanNotify `yaml:"-"`
117118
Reload *ChanNotify `yaml:"-"`
118119
Shutdown *ChanNotify `yaml:"-"`
@@ -152,6 +153,7 @@ func Get() *Configuration {
152153
cfg = &Configuration{}
153154
cfg.initSignalHandler()
154155
cfg.Notify.BootstrapKeyChanged = NewChanNotify()
156+
cfg.Notify.ServerStarted = NewChanNotify()
155157
cfg.Notify.CertificateRefresh = NewChanNotify()
156158
cfg.Notify.Reload = NewChanNotify()
157159
cfg.Notify.Shutdown = NewChanNotify()
@@ -177,6 +179,7 @@ func (c *Configuration) GetStorageData() *StorageDataplaneAPIConfiguration {
177179

178180
func (c *Configuration) UnSubscribeAll() {
179181
c.Notify.BootstrapKeyChanged.UnSubscribeAll()
182+
c.Notify.ServerStarted.UnSubscribeAll()
180183
c.Notify.CertificateRefresh.UnSubscribeAll()
181184
c.Notify.Reload.UnSubscribeAll()
182185
c.Notify.Shutdown.UnSubscribeAll()

0 commit comments

Comments
 (0)