Skip to content

Instantly share code, notes, and snippets.

@gorsuch
Last active September 13, 2017 08:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorsuch/5aed15e45c90c9ad7d2d to your computer and use it in GitHub Desktop.
Save gorsuch/5aed15e45c90c9ad7d2d to your computer and use it in GitHub Desktop.
Example Serf client in go
package main
import (
"fmt"
"strconv"
"time"
"github.com/hashicorp/serf/client"
)
// handler for user events
func handler(c chan map[string]interface{}) {
for {
data := <-c
fmt.Printf("%v\n", data)
}
}
func main() {
conf := client.Config{Addr:"localhost:7373"}
// connect our client
client, err := client.ClientFromConfig(&conf)
if err != nil {
panic(nil)
}
// consume user events in another goroutine
ch := make(chan map[string]interface{})
client.Stream("user", ch)
go handler(ch)
// continually send user events
i := 0
for {
err := client.UserEvent("test", []byte(strconv.Itoa(i)), false)
if err != nil {
panic(err)
}
time.Sleep(100 * time.Millisecond)
i++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment