Skip to content

Commit

Permalink
Merge pull request #149 from Revolyssup/removehard
Browse files Browse the repository at this point in the history
Wait for broker to come up before connecting via NATS
  • Loading branch information
leecalcote authored Oct 7, 2022
2 parents 1a2b24f + 8e57b27 commit 7873119
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand All @@ -17,10 +19,11 @@ import (
)

var (
serviceName = "meshsync"
provider = configprovider.ViperKey
version = "Not Set"
commitsha = "Not Set"
serviceName = "meshsync"
provider = configprovider.ViperKey
version = "Not Set"
commitsha = "Not Set"
pingEndpoint = ":8222/connz"
)

func main() {
Expand Down Expand Up @@ -63,7 +66,26 @@ func main() {
log.Error(err)
os.Exit(1)
}
// Seeding done
// Make sure Broker has started before starting NATS client

for {
urls := strings.Split(config.BrokerURL, ":")
if len(urls) == 0 {
log.Error(err)
os.Exit(1)
}
pingURL := "http://" + urls[0] + pingEndpoint
resp, err := http.Get(pingURL) //remove nats port and use status port for ping
if err != nil {
log.Error(err)
continue
}
if resp.StatusCode == http.StatusOK {
break
}
log.Error(fmt.Errorf("could not ping broker at: "+pingURL, " retrying..."))
time.Sleep(1 * time.Second)
}

// Initialize Broker instance
br, err := nats.New(nats.Options{
Expand Down

0 comments on commit 7873119

Please sign in to comment.