| 1 |
# Go examples |
| 2 |
|
| 3 |
## Electron examples |
| 4 |
|
| 5 |
[electron](http://godoc.org/github.com/apache/qpid-proton/go/pkg/electron) is a |
| 6 |
simple API for writing concurrent AMQP clients and servers. |
| 7 |
|
| 8 |
- [receive.go](electron/receive.go) receive from many connections concurrently. |
| 9 |
- [send.go](electron/send.go) send to many connections concurrently. |
| 10 |
- [broker.go](electron/broker.go) a simple broker using the electron API |
| 11 |
|
| 12 |
## Proton examples |
| 13 |
|
| 14 |
[proton](http://godoc.org/github.com/apache/qpid-proton/go/pkg/proton) is an |
| 15 |
event-driven, concurrent-unsafe Go wrapper for the proton-C library. The |
| 16 |
[electron](http://godoc.org/github.com/apache/qpid-proton/go/pkg/electron) package provides a more |
| 17 |
Go-friendly concurrent API built on top of proton. |
| 18 |
|
| 19 |
- [broker.go](proton/broker.go) a simple broker using the proton API |
| 20 |
|
| 21 |
See [A Tale of Two Brokers](#a-tale-of-two-brokers) for a comparison of the two APIs. |
| 22 |
|
| 23 |
## Using the Go packages |
| 24 |
|
| 25 |
If you have the proton-C library and headers installed you can get the latest go |
| 26 |
packages with |
| 27 |
|
| 28 |
go get github.com/apache/qpid-proton/go/pkg/electron |
| 29 |
|
| 30 |
If Proton-C is installed in a non-standard place (other than /usr or /usr/local) |
| 31 |
you should set these environment variables before `go get`: |
| 32 |
|
| 33 |
export CGO_LDFLAGS="-L/<my-proton>/lib[64]" |
| 34 |
export CGO_CFLAGS="-I/<my-proton>/include" |
| 35 |
go get github.com/apache/qpid-proton |
| 36 |
|
| 37 |
If you have a proton build you don't need to `go get`, you can set your GOPATH |
| 38 |
to use the binding from the checkout with: |
| 39 |
|
| 40 |
source <path-to-proton>/config.sh |
| 41 |
|
| 42 |
Once you are set up, the go tools will work as normal. You can see documentation |
| 43 |
in your web browser at `localhost:6060` by running: |
| 44 |
|
| 45 |
godoc -http=:6060 |
| 46 |
|
| 47 |
## Running the examples |
| 48 |
|
| 49 |
You can run the examples directly from source like this: |
| 50 |
|
| 51 |
go run <program>.go |
| 52 |
|
| 53 |
This is a little slow (a couple of seconds) as it compiles the program and runs it in one step. |
| 54 |
You can compile the program first and then run the executable to avoid the delay: |
| 55 |
|
| 56 |
go build <program>.go |
| 57 |
./<program> |
| 58 |
|
| 59 |
All the examples take a `-h` flag to show usage information, and the comments in |
| 60 |
the example source have more details. |
| 61 |
|
| 62 |
First start the broker on the default AMQP port 5672 (the optional `-debug` flag |
| 63 |
will print extra information about what the broker is doing) |
| 64 |
|
| 65 |
go run broker.go -addr=:5672 -debug # Use a different port if 5672 is not available. |
| 66 |
|
| 67 |
Send messages concurrently to queues "foo" and "bar", 10 messages to each queue: |
| 68 |
|
| 69 |
go run send.go -count 10 amqp://localhost:5672/foo amqp://localhost:5672/bar |
| 70 |
|
| 71 |
Receive messages concurrently from "foo" and "bar". Note -count 20 for 10 messages each on 2 queues: |
| 72 |
|
| 73 |
go run receive.go -count 20 amqp://localhost:5672/foo amqp://localhost:5672/bar |
| 74 |
|
| 75 |
The broker and clients use the standard AMQP port (5672) on the local host by |
| 76 |
default, to use a different address use the `-addr host:port` flag. |
| 77 |
|
| 78 |
If you have other Proton examples available you can try communicating between |
| 79 |
programs in in different languages. For example use the python broker with Go |
| 80 |
clients: |
| 81 |
|
| 82 |
python ../python/broker.py |
| 83 |
go run send.go -count 10 localhost:/foo localhost:/bar |
| 84 |
|
| 85 |
Or use the Go broker and the python clients: |
| 86 |
|
| 87 |
go run broker.go -debug |
| 88 |
python ../python/simple_send.py |
| 89 |
python ../python/simple_recv.py |
| 90 |
|
| 91 |
|
| 92 |
## A tale of two brokers. |
| 93 |
|
| 94 |
The [proton](http://godoc.org/github.com/apache/qpid-proton/go/pkg/proton) and |
| 95 |
[electron](http://godoc.org/github.com/apache/qpid-proton/go/pkg/electron) packages provide two |
| 96 |
different APIs for building AMQP applications. For most applications, |
| 97 |
[electron](http://godoc.org/github.com/apache/qpid-proton/go/pkg/electron) is easier to use. |
| 98 |
|
| 99 |
The examples [proton/broker.go](proton/broker.go) and |
| 100 |
[electron/broker.go](electron/broker.go) implement the same simple broker |
| 101 |
functionality using each of the two APIs. They both handle multiple connections |
| 102 |
concurrently and store messages on bounded queues implemented by Go channels. |
| 103 |
|
| 104 |
However the [electron/broker.go](electron/broker.go) is less than half as long as the |
| 105 |
[proton/broker.go](proton/broker.go) illustrating why it is better suited for most Go |
| 106 |
applications. |
| 107 |
|
| 108 |
[proton/broker.go](proton/broker.go) implements an event-driven loop per connection that reacts |
| 109 |
to events like 'incoming link', 'incoming message' and 'sender has credit'. It |
| 110 |
uses channels to exchange data between the event-loop goroutine for each |
| 111 |
connection and shared queues that are accessible to all connections. Sending |
| 112 |
messages is particularly tricky, the broker must monitor the queue for available |
| 113 |
messages and the sender link for available credit. |
| 114 |
|
| 115 |
|
| 116 |
[electron/broker.go](electron/broker.go) does not need any "upside-down" |
| 117 |
event-driven code, it is implemented as straightforward loops. The broker is a |
| 118 |
loop listening for connections. Each connection is a loop accepting for incoming |
| 119 |
sender or receiver links. Each receiving link is a loop that receives a message |
| 120 |
and pushes it to a queue. Each sending link is a loop that pops a message from |
| 121 |
a queue and sends it. |
| 122 |
|
| 123 |
Queue bounds and credit manage themselves: popping from a queue blocks till |
| 124 |
there is a message, sending blocks until there is credit, receiving blocks till |
| 125 |
something is received and pushing onto a queue blocks until there is |
| 126 |
space. There's no need for code that monitors the state of multiple queues and |
| 127 |
links. Each loop has one simple job to do, and the Go run-time schedules them |
| 128 |
efficiently. |
| 129 |
|