Last active
September 13, 2017 08:40
-
-
Save gorsuch/5aed15e45c90c9ad7d2d to your computer and use it in GitHub Desktop.
Example Serf client in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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