Skip to content

Instantly share code, notes, and snippets.

@alittlebrighter
Created March 30, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alittlebrighter/c1c124b0c5f70e11e7103c44baefcb3d to your computer and use it in GitHub Desktop.
Save alittlebrighter/c1c124b0c5f70e11e7103c44baefcb3d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"os/exec"
"strconv"
"strings"
"sync"
"github.com/nats-io/go-nats"
)
func main() {
var ourIP net.IP
var network *net.IPNet
ifaces, _ := net.Interfaces()
for _, i := range ifaces {
fmt.Println("interface:", i.Name)
addrs, _ := i.Addrs()
iface:
for _, a := range addrs {
ip, ipnet, _ := net.ParseCIDR(a.String())
if strings.HasPrefix(ipnet.IP.String(), "192.168.") {
network = ipnet
ourIP = ip
break iface
}
}
}
lastSep := strings.LastIndex(network.IP.String(), ".")
lastOct, _ := strconv.Atoi(network.IP.String()[lastSep+1:])
routes := []string{"-cluster", "nats://0.0.0.0:5222", "-routes"}
rLock := sync.Mutex{}
wg := sync.WaitGroup{}
for i := lastOct + 1; i < 50; i++ {
address := fmt.Sprintf(network.IP.String()[:lastSep+1]+"%d", i)
if address == ourIP.String() {
fmt.Println("skipping our IP", ourIP.String())
continue
}
wg.Add(1)
go func() {
fmt.Println("Dialing", address)
_, err := nats.Connect("nats://" + address + ":4222")
if err == nil {
rLock.Lock()
routes = append(routes, "nats://"+address+":5222")
rLock.Unlock()
}
wg.Done()
}()
}
wg.Wait()
fmt.Println("Starting gnatsd and joining cluster through", routes)
cmd := exec.Command("gnatsd", routes...)
cmd.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment