Skip to content

Instantly share code, notes, and snippets.

@ryanuber
Created October 24, 2014 16:13
Show Gist options
  • Select an option

  • Save ryanuber/5a09ec057e30f835b22b to your computer and use it in GitHub Desktop.

Select an option

Save ryanuber/5a09ec057e30f835b22b to your computer and use it in GitHub Desktop.
AMQP behavior
package main
import (
"github.com/armon/relay"
"log"
"time"
)
func main() {
conf := &relay.Config{Addr: "localhost"}
conn, err := relay.New(conf)
if err != nil {
log.Printf(err.Error())
return
}
defer conn.Close()
pub, err := conn.Publisher("imagesets")
if err != nil {
log.Printf(err.Error())
return
}
defer pub.Close()
doneCh := make(chan struct{})
go consume(conn, "imagesets1", doneCh)
go consume(conn, "imagesets2", doneCh)
go consume(conn, "imagesets3", doneCh)
publish(conn, "imagesets1", "hello1")
publish(conn, "imagesets2", "hello2")
publish(conn, "imagesets3", "hello3")
go consume(conn, "imagesets1", doneCh)
go consume(conn, "imagesets2", doneCh)
go consume(conn, "imagesets3", doneCh)
publish(conn, "imagesets1", "hello4")
publish(conn, "imagesets2", "hello5")
publish(conn, "imagesets3", "hello6")
go consume(conn, "imagesets1", doneCh)
go consume(conn, "imagesets2", doneCh)
go consume(conn, "imagesets3", doneCh)
publish(conn, "imagesets1", "hello7")
publish(conn, "imagesets2", "hello8")
publish(conn, "imagesets3", "hello9")
done := 0
for {
select {
case <-doneCh:
done++
}
if done == 9 {
break
}
}
}
func consume(conn *relay.Relay, queue string, doneCh chan<- struct{}) {
cons, err := conn.Consumer(queue)
if err != nil {
log.Printf(err.Error())
return
}
var msg string
if err := cons.ConsumeTimeout(&msg, 1*time.Second); err != nil {
log.Printf(err.Error())
} else {
log.Printf("received: %s", msg)
}
doneCh <- struct{}{}
}
func publish(conn *relay.Relay, queue, msg string) {
pub, err := conn.Publisher(queue)
if err != nil {
log.Printf(err.Error())
return
}
defer pub.Close()
if err := pub.Publish(msg); err != nil {
log.Printf(err.Error())
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment