Sunday, 15 January 2012

Could one connection support multiple channels in go api for rabbitmq? -



Could one connection support multiple channels in go api for rabbitmq? -

package main import ( "fmt" "github.com/streadway/amqp" "time" ) // every connection should declare topology expect func setup(url, queue string) (*amqp.connection, *amqp.channel, error) { //setup connection conn, err := amqp.dial(url) if err != nil { homecoming nil, nil, err } //build channel in connection ch, err := conn.channel() if err != nil { homecoming nil, nil, err } //queue declare if _, err := ch.queuedeclare(queue, false, true, false, false, nil); err != nil { homecoming nil, nil, err } homecoming conn, ch, nil } func main() { //amqp url url := "amqp://guest:guest@127.0.0.1:5672"; := 1; <= 2; i++ { fmt.println("connect ", i) //two goroutine go func() { //queue name queue := fmt.sprintf("example.reconnect.%d", i) //setup channel in tcp connection _, pub, err := setup(url, queue) if err != nil { fmt.println("err publisher setup:", err) homecoming } // purge queue publisher side found initial state if _, err := pub.queuepurge(queue, false); err != nil { fmt.println("err purge:", err) homecoming } //publish msg if err := pub.publish("", queue, false, false, amqp.publishing{ body: []byte(fmt.sprintf("%d", i)), }); err != nil { fmt.println("err publish:", err) homecoming } //keep running for{ time.sleep(time.second * 20) } }() } //keep running { time.sleep(time.second * 20) } }

i thought there 1 connection between programme , mq-server,

but there 2 connection,one connection can back upwards 1 channel,why?

can't 2 goroutine share same tcp connection?

socket descriptor can share in threads of process in theory.

why 2 goroutine don't share 1 socket have own channel?

the model hand:

the real model in rabbitmq:

looking @ source library appears though can phone call conn.channel() many times , creates new stream of communication on same connection.

ok, tried it, here's working example... 1 goroutine, 1 connection, 2 channels setup receiver, send message, read receiver channel

if wanted multiple queue's bound in 1 goroutine, phone call rec.consume twice , select across queues.

package main import ( "fmt" "github.com/streadway/amqp" "os" ) func main() { conn, err := amqp.dial("amqp://localhost") e(err) defer conn.close() fmt.println("connected") rec, err := conn.channel() e(err) fmt.println("setup receiver") rq, err := rec.queuedeclare("go-test", false, false, false, false, nil) e(err) msgs, err := rec.consume(rq.name, "", true, false, false, false, nil) e(err) fmt.println("setup sender") send, err := conn.channel() e(err) sq, err := send.queuedeclare("go-test", false, false, false, false, nil) e(err) fmt.println("send message") err = send.publish("", sq.name, false, false, amqp.publishing{ contenttype: "text/plain", body: []byte("this test"), }) e(err) msg := <-msgs fmt.println("received from:", rq, "msg:", string(msg.body)) } func e(err error) { if err != nil { fmt.println(err) os.exit(1) } }

output on box:

$ go run rmq.go connected setup receiver setup sender send message received from: {go-test 0 0} msg: test

go rabbitmq amqp

No comments:

Post a Comment