| 1 |
# Qpid Go packages for AMQP |
| 2 |
|
| 3 |
These packages provide [Go](https://golang.org) support for sending and receiving |
| 4 |
AMQP messages in client or server applications. Reference documentation is |
| 5 |
available at: <https://godoc.org/github.com/apache/qpid-proton> |
| 6 |
|
| 7 |
They require the |
| 8 |
[proton-C library and header files](http://qpid.apache.org/proton) to be |
| 9 |
installed. On many platforms it is available pre-packaged, for example on |
| 10 |
Fedora |
| 11 |
|
| 12 |
dnf install qpid-proton-c-devel |
| 13 |
|
| 14 |
If you built proton from source, you can set environment variables to find the |
| 15 |
built libraries and headers as follows: |
| 16 |
|
| 17 |
source <build-directory>/config.sh |
| 18 |
|
| 19 |
If you have installed the library and headers in non-standard directories, then |
| 20 |
add them to the following environment variables: |
| 21 |
|
| 22 |
LD_LIBRARY_PATH # directory containing the library |
| 23 |
LIBRARY_PATH # directory containing the library |
| 24 |
C_INCLUDE_PATH # directory containing the proton/ subdirectory with header files |
| 25 |
|
| 26 |
There are 3 packages: |
| 27 |
|
| 28 |
[amqp](http://godoc.org/github.com/apache/qpid-proton/go/pkg/amqp) provides functions |
| 29 |
to convert AMQP messages and data types to and from Go data types. Used by both |
| 30 |
the proton and electron packages to manage AMQP data. |
| 31 |
|
| 32 |
[electron](http://godoc.org/github.com/apache/qpid-proton/go/pkg/electron) is a |
| 33 |
simple, concurrent-safe API for sending and receiving messages. It can be used |
| 34 |
with goroutines and channels to build concurrent AMQP clients and servers. |
| 35 |
|
| 36 |
[proton](http://godoc.org/github.com/apache/qpid-proton/go/pkg/proton) is an |
| 37 |
event-driven, concurrent-unsafe package that closely follows the proton C |
| 38 |
API. Most Go programmers will find the |
| 39 |
[electron](http://godoc.org/github.com/apache/qpid-proton/go/pkg/electron) package easier to use. |
| 40 |
|
| 41 |
See the [examples](https://github.com/apache/qpid-proton/blob/master/go/examples/README.md) |
| 42 |
to help you get started. |
| 43 |
|
| 44 |
Feedback is encouraged at: |
| 45 |
|
| 46 |
- Email <[email protected]> |
| 47 |
- Create issues <https://issues.apache.org/jira/browse/PROTON>, attach patches to an issue. |
| 48 |
|
| 49 |
### Why two APIs? |
| 50 |
|
| 51 |
The `proton` API is a direct mapping of the proton C library into Go. It is |
| 52 |
usable but not very natural for a Go programmer because it takes an |
| 53 |
*event-driven* approach and has no built-in support for concurrent |
| 54 |
use. `electron` uses `proton` internally but provides a more Go-like API that is |
| 55 |
safe to use from multiple concurrent goroutines. |
| 56 |
|
| 57 |
Go encourages programs to be structured as concurrent *goroutines* that |
| 58 |
communicate via *channels*. Go literature distinguishes between: |
| 59 |
|
| 60 |
- *concurrency*: "keeping track of things that could be done in parallel" |
| 61 |
- *parallelism*: "actually doing things in parallel on multiple CPUs or cores" |
| 62 |
|
| 63 |
A Go program expresses concurrency by starting goroutines for potentially |
| 64 |
concurrent tasks. The Go runtime schedules the activity of goroutines onto a |
| 65 |
small number (possibly one) of actual parallel executions. |
| 66 |
|
| 67 |
Even with no hardware parallelism, goroutine concurrency lets the Go runtime |
| 68 |
order unpredictable events like file descriptors being readable/writable, |
| 69 |
channels having data, timers firing etc. Go automatically takes care of |
| 70 |
switching out goroutines that block or sleep so it is normal to write code in |
| 71 |
terms of blocking calls. |
| 72 |
|
| 73 |
By contrast, event-driven programming is based on polling mechanisms like |
| 74 |
`select`, `poll` or `epoll`. These also dispatch unpredictably ordered events to |
| 75 |
a single thread or a small thread pool. However this requires a different style |
| 76 |
of programming: "event-driven" or "reactive" programming. Go developers call it |
| 77 |
"inside-out" programming. In an event-driven program blocking is a big problem |
| 78 |
as it consumes a scarce thread of execution, so actions that take time to |
| 79 |
complete have to be re-structured in terms of multiple events. |
| 80 |
|
| 81 |
The promise of Go is that you can express your program in concurrent, sequential |
| 82 |
terms and the Go runtime will turn it inside-out for you. You can start |
| 83 |
goroutines for all concurrent activities. They can loop forever or block for as |
| 84 |
long as they need waiting for timers, IO or any unpredictable event. Go will |
| 85 |
interleave and schedule them efficiently onto the available parallel hardware. |
| 86 |
|
| 87 |
For example: in the `electron` API, you can send a message and wait for it to be |
| 88 |
acknowledged in a single function. All the information about the message, why |
| 89 |
you sent it, and what to do when it is acknowledged can be held in local |
| 90 |
variables, all the code is in a simple sequence. Other goroutines in your |
| 91 |
program can be sending and receiving messages concurrently, they are not |
| 92 |
blocked. |
| 93 |
|
| 94 |
In the `proton` API, an event handler that sends a message must return |
| 95 |
*immediately*, it cannot block the event loop to wait for |
| 96 |
acknowledgement. Acknowledgement is a separate event, so the code for handling |
| 97 |
it is in a different event handler. Context information about the message has to |
| 98 |
be stored in some non-local variable that both functions can find. This makes |
| 99 |
the code harder to follow. |
| 100 |
|
| 101 |
The `proton` API is important because it is the foundation for the `electron` |
| 102 |
API, and may be useful for programs that need to be close to the original C |
| 103 |
library for some reason. However the `electron` API hides the event-driven |
| 104 |
details behind simple, sequential, concurrent-safe methods that can be called |
| 105 |
from arbitrary goroutines. Under the covers, data is passed through channels to |
| 106 |
dedicated `proton` goroutines so user goroutines can work concurrently with the |
| 107 |
proton event-loop. |
| 108 |
|
| 109 |
## New to Go? |
| 110 |
|
| 111 |
If you are new to Go then these are a good place to start: |
| 112 |
|
| 113 |
- [A Tour of Go](http://tour.golang.org) |
| 114 |
- [Effective Go](http://golang.org/doc/effective_go.html) |
| 115 |
|
| 116 |
Then look at the tools and docs at <http://golang.org> as you need them. |