Skip to content

Instantly share code, notes, and snippets.

@afshin
Last active April 11, 2016 06:49
Show Gist options
  • Save afshin/0242be8726c37144407a713f8d941815 to your computer and use it in GitHub Desktop.
Save afshin/0242be8726c37144407a713f8d941815 to your computer and use it in GitHub Desktop.
gyre leave test
package main
import (
"flag"
"fmt"
"github.com/zeromq/gyre"
"os"
"time"
)
const (
port = 5670
group = "GYRETEST-v0"
)
func joinGroup(seconds int) {
node, err := newNode()
if err != nil {
fmt.Printf("error creating node: %s\n", err.Error())
return
}
fmt.Printf("this is node %s\n", node.Name())
if seconds > 0 {
fmt.Printf("this node (%s) will leave group %s in %d seconds\n",
node.Name(), group, seconds)
time.AfterFunc(time.Duration(seconds)*time.Second, func() {
if err := node.Leave(group); err != nil {
os.Exit(1)
} else {
os.Exit(0)
}
})
}
for {
event := <-node.Events()
switch event.Type() {
case gyre.EventEnter:
fmt.Printf("[%s enter] %s\n", group, event.Name())
case gyre.EventExit:
fmt.Printf("[%s exit] %s\n", group, event.Name())
case gyre.EventJoin:
fmt.Printf("[%s join] %s\n", group, event.Name())
case gyre.EventLeave:
fmt.Printf("[%s leave] %s\n", group, event.Name())
}
}
}
func newNode() (*gyre.Gyre, error) {
node, err := gyre.New()
if err != nil {
return nil, err
}
if err = node.SetPort(port); err != nil {
return nil, err
}
if err = node.Start(); err != nil {
return nil, err
}
if err = node.Join(group); err != nil {
node.Stop()
return nil, err
}
return node, nil
}
func main() {
seconds := flag.Int("seconds", 0, "seconds after joining before leaving")
flag.Parse()
joinGroup(*seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment