Skip to content

Instantly share code, notes, and snippets.

@kevin-cantwell
Created March 9, 2018 01:35
Show Gist options
  • Save kevin-cantwell/06af2de922a80e244a279980eb2b6a01 to your computer and use it in GitHub Desktop.
Save kevin-cantwell/06af2de922a80e244a279980eb2b6a01 to your computer and use it in GitHub Desktop.
backplane connect test
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
"time"
)
/*
This program creates backplane backends on 100 consecutive ports from 7000 to 7099.
It then connects each backend to a backplane endpoint, provided as a command line argument.
*/
func main() {
if len(os.Args) < 2 {
fmt.Println(`
Usage:
go run main.go <endpoint>
`,
)
os.Exit(1)
}
endpoint := os.Args[1]
for i := 0; i < 100; i++ {
go listenAndServe(i)
go backplaneConnect(i, endpoint)
}
// Give the backplane agents a second to connect
time.Sleep(time.Second)
fmt.Printf("\nhttps://%s\n", endpoint)
select {}
}
func listenAndServe(i int) {
addr := fmt.Sprintf(":7%03d", i)
fmt.Println("Listening on http://localhost" + addr)
http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/health" {
w.Write([]byte("ok"))
return // Not interested in health checks
}
fmt.Printf("addr=%s,path=%s\n", addr, r.URL.Path)
w.Write([]byte("Serving backend address: " + addr))
}))
}
func backplaneConnect(i int, endpoint string) {
addr := fmt.Sprintf(":7%03d", i)
cmd := exec.Command("backplane", "connect", "endpoint="+endpoint+",release=v1", "http://localhost"+addr)
if err := cmd.Run(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment