| 1 |
// Command pubsub is an example of a fanout exchange with dynamic reliable |
| 2 |
// membership, reading from stdin, writing to stdout. |
| 3 |
// |
| 4 |
// This example shows how to implement reconnect logic independent from a |
| 5 |
// publish/subscribe loop with bridges to application types. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"bufio" |
| 11 |
"crypto/sha1" |
| 12 |
"flag" |
| 13 |
"fmt" |
| 14 |
"io" |
| 15 |
"log" |
| 16 |
"os" |
| 17 |
|
| 18 |
"github.com/streadway/amqp" |
| 19 |
"golang.org/x/net/context" |
| 20 |
) |
| 21 |
|
| 22 |
var url = flag.String("url", "amqp:///", "AMQP url for both the publisher and subscriber") |
| 23 |
|
| 24 |
// exchange binds the publishers to the subscribers |
| 25 |
const exchange = "pubsub" |
| 26 |
|
| 27 |
// message is the application type for a message. This can contain identity, |
| 28 |
// or a reference to the recevier chan for further demuxing. |
| 29 |
type message []byte |
| 30 |
|
| 31 |
// session composes an amqp.Connection with an amqp.Channel |
| 32 |
type session struct { |
| 33 |
*amqp.Connection |
| 34 |
*amqp.Channel |
| 35 |
} |
| 36 |
|
| 37 |
// Close tears the connection down, taking the channel with it. |
| 38 |
func (s session) Close() error { |
| 39 |
if s.Connection == nil { |
| 40 |
return nil |
| 41 |
} |
| 42 |
return s.Connection.Close() |
| 43 |
} |
| 44 |
|
| 45 |
// redial continually connects to the URL, exiting the program when no longer possible |
| 46 |
func redial(ctx context.Context, url string) chan chan session { |
| 47 |
sessions := make(chan chan session) |
| 48 |
|
| 49 |
go func() { |
| 50 |
sess := make(chan session) |
| 51 |
defer close(sessions) |
| 52 |
|
| 53 |
for { |
| 54 |
select { |
| 55 |
case sessions <- sess: |
| 56 |
case <-ctx.Done(): |
| 57 |
log.Println("shutting down session factory") |
| 58 |
return |
| 59 |
} |
| 60 |
|
| 61 |
conn, err := amqp.Dial(url) |
| 62 |
if err != nil { |
| 63 |
log.Fatalf("cannot (re)dial: %v: %q", err, url) |
| 64 |
} |
| 65 |
|
| 66 |
ch, err := conn.Channel() |
| 67 |
if err != nil { |
| 68 |
log.Fatalf("cannot create channel: %v", err) |
| 69 |
} |
| 70 |
|
| 71 |
if err := ch.ExchangeDeclare(exchange, "fanout", false, true, false, false, nil); err != nil { |
| 72 |
log.Fatalf("cannot declare fanout exchange: %v", err) |
| 73 |
} |
| 74 |
|
| 75 |
select { |
| 76 |
case sess <- session{conn, ch}: |
| 77 |
case <-ctx.Done(): |
| 78 |
log.Println("shutting down new session") |
| 79 |
return |
| 80 |
} |
| 81 |
} |
| 82 |
}() |
| 83 |
|
| 84 |
return sessions |
| 85 |
} |
| 86 |
|
| 87 |
// publish publishes messages to a reconnecting session to a fanout exchange. |
| 88 |
// It receives from the application specific source of messages. |
| 89 |
func publish(sessions chan chan session, messages <-chan message) { |
| 90 |
for session := range sessions { |
| 91 |
var ( |
| 92 |
running bool |
| 93 |
reading = messages |
| 94 |
pending = make(chan message, 1) |
| 95 |
confirm = make(chan amqp.Confirmation, 1) |
| 96 |
) |
| 97 |
|
| 98 |
pub := <-session |
| 99 |
|
| 100 |
// publisher confirms for this channel/connection |
| 101 |
if err := pub.Confirm(false); err != nil { |
| 102 |
log.Printf("publisher confirms not supported") |
| 103 |
close(confirm) // confirms not supported, simulate by always nacking |
| 104 |
} else { |
| 105 |
pub.NotifyPublish(confirm) |
| 106 |
} |
| 107 |
|
| 108 |
log.Printf("publishing...") |
| 109 |
|
| 110 |
Publish: |
| 111 |
for { |
| 112 |
var body message |
| 113 |
select { |
| 114 |
case confirmed, ok := <-confirm: |
| 115 |
if !ok { |
| 116 |
break Publish |
| 117 |
} |
| 118 |
if !confirmed.Ack { |
| 119 |
log.Printf("nack message %d, body: %q", confirmed.DeliveryTag, string(body)) |
| 120 |
} |
| 121 |
reading = messages |
| 122 |
|
| 123 |
case body = <-pending: |
| 124 |
routingKey := "ignored for fanout exchanges, application dependent for other exchanges" |
| 125 |
err := pub.Publish(exchange, routingKey, false, false, amqp.Publishing{ |
| 126 |
Body: body, |
| 127 |
}) |
| 128 |
// Retry failed delivery on the next session |
| 129 |
if err != nil { |
| 130 |
pending <- body |
| 131 |
pub.Close() |
| 132 |
break Publish |
| 133 |
} |
| 134 |
|
| 135 |
case body, running = <-reading: |
| 136 |
// all messages consumed |
| 137 |
if !running { |
| 138 |
return |
| 139 |
} |
| 140 |
// work on pending delivery until ack'd |
| 141 |
pending <- body |
| 142 |
reading = nil |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// identity returns the same host/process unique string for the lifetime of |
| 149 |
// this process so that subscriber reconnections reuse the same queue name. |
| 150 |
func identity() string { |
| 151 |
hostname, err := os.Hostname() |
| 152 |
h := sha1.New() |
| 153 |
fmt.Fprint(h, hostname) |
| 154 |
fmt.Fprint(h, err) |
| 155 |
fmt.Fprint(h, os.Getpid()) |
| 156 |
return fmt.Sprintf("%x", h.Sum(nil)) |
| 157 |
} |
| 158 |
|
| 159 |
// subscribe consumes deliveries from an exclusive queue from a fanout exchange and sends to the application specific messages chan. |
| 160 |
func subscribe(sessions chan chan session, messages chan<- message) { |
| 161 |
queue := identity() |
| 162 |
|
| 163 |
for session := range sessions { |
| 164 |
sub := <-session |
| 165 |
|
| 166 |
if _, err := sub.QueueDeclare(queue, false, true, true, false, nil); err != nil { |
| 167 |
log.Printf("cannot consume from exclusive queue: %q, %v", queue, err) |
| 168 |
return |
| 169 |
} |
| 170 |
|
| 171 |
routingKey := "application specific routing key for fancy toplogies" |
| 172 |
if err := sub.QueueBind(queue, routingKey, exchange, false, nil); err != nil { |
| 173 |
log.Printf("cannot consume without a binding to exchange: %q, %v", exchange, err) |
| 174 |
return |
| 175 |
} |
| 176 |
|
| 177 |
deliveries, err := sub.Consume(queue, "", false, true, false, false, nil) |
| 178 |
if err != nil { |
| 179 |
log.Printf("cannot consume from: %q, %v", queue, err) |
| 180 |
return |
| 181 |
} |
| 182 |
|
| 183 |
log.Printf("subscribed...") |
| 184 |
|
| 185 |
for msg := range deliveries { |
| 186 |
messages <- message(msg.Body) |
| 187 |
sub.Ack(msg.DeliveryTag, false) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// read is this application's translation to the message format, scanning from |
| 193 |
// stdin. |
| 194 |
func read(r io.Reader) <-chan message { |
| 195 |
lines := make(chan message) |
| 196 |
go func() { |
| 197 |
defer close(lines) |
| 198 |
scan := bufio.NewScanner(r) |
| 199 |
for scan.Scan() { |
| 200 |
lines <- message(scan.Bytes()) |
| 201 |
} |
| 202 |
}() |
| 203 |
return lines |
| 204 |
} |
| 205 |
|
| 206 |
// write is this application's subscriber of application messages, printing to |
| 207 |
// stdout. |
| 208 |
func write(w io.Writer) chan<- message { |
| 209 |
lines := make(chan message) |
| 210 |
go func() { |
| 211 |
for line := range lines { |
| 212 |
fmt.Fprintln(w, string(line)) |
| 213 |
} |
| 214 |
}() |
| 215 |
return lines |
| 216 |
} |
| 217 |
|
| 218 |
func main() { |
| 219 |
flag.Parse() |
| 220 |
|
| 221 |
ctx, done := context.WithCancel(context.Background()) |
| 222 |
|
| 223 |
go func() { |
| 224 |
publish(redial(ctx, *url), read(os.Stdin)) |
| 225 |
done() |
| 226 |
}() |
| 227 |
|
| 228 |
go func() { |
| 229 |
subscribe(redial(ctx, *url), write(os.Stdout)) |
| 230 |
done() |
| 231 |
}() |
| 232 |
|
| 233 |
<-ctx.Done() |
| 234 |
} |