ruby-amqp/bunny — pull request #221 by devin-c (2014-06-25 23:41:09+00:00)
Title: Keep publish confirmation in sync after recovery

When a connection is recovered, the sequence counter resets on the
broker, but not the client. I think this is the correct behavior;
however, to keep things in sync the client must store a confirmation
offset after a recovery. The offset should be the sequence number of the
last message the client attempted to deliver, i.e., `@next_publish_seq_no - 1`.

To understand why we need this offset, here's an example:

1. Client sends 1 through 4 which are confirmed by the broker.
2. Client sends 5 through 8 which are _not_ confirmed by the broker (due
to temporary network failure).
3. Client recovers from network failure and stores **8** as the
confirmation offset (8 was the last sequence we attempted to send)
4. Client sends 9 and 10. These are confirmed as 1 and 2 by the broker,
we apply the offset to each to correctly confirm 9 and 10 on the client
(`1 + 8 = 9` and `2 + 8 = 10`).
5. Client sends 11 through 15 which are confirmed as 7 (with
`multiple=true`) by the broker. Client applies the offset and confirms the
range `(9..15)`. Here, nine is the value the sequence counter was at
immediately after recovery. It acts as our new base (instead of 1) for
multiple confirms. Fifteen is the delivery_tag plus the offset (`7 + 8`)