Debian Codegraph @

pubsub/rabbitpubsub/amqp.go

raw
1 // Copyright 2018 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package rabbitpubsub
16
17 // Interfaces for the AMQP protocol, and adapters for the real amqp client.
18 // Fake implementations of the interfaces are in fake_test.go
19
20 import (
21 "context"
22
23 amqp "github.com/rabbitmq/amqp091-go"
24 )
25
26 // Values we use for the amqp client.
27 // See https://www.rabbitmq.com/amqp-0-9-1-reference.html.
28 const (
29 // Many methods of the amqp client take a "no-wait" parameter, which
30 // if true causes the client to return without waiting for a server
31 // response. We always want to wait.
32 wait = false
33
34 // If the message can't be enqueued, return it to the sender rather than silently
35 // dropping it.
36 mandatory = true
37
38 // If there are no waiting consumers, enqueue the message instead of dropping it.
39 immediate = false
40 )
41
42 // See https://pkg.go.dev/github.com/rabbitmq/amqp091-go#Connection for the documentation of these methods.
43 type amqpConnection interface {
44 Channel() (amqpChannel, error)
45 Close() error
46 }
47
48 // See https://pkg.go.dev/github.com/rabbitmq/amqp091-go#Channel for the documentation of these methods.
49 type amqpChannel interface {
50 Publish(exchange, routingKey string, msg amqp.Publishing) error
51 PublishWithContext(ctx context.Context, exchange, routingKey string, msg amqp.Publishing) error
52 Consume(queue, consumer string) (<-chan amqp.Delivery, error)
53 Ack(tag uint64) error
54 Nack(tag uint64) error
55 Cancel(consumer string) error
56 Close() error
57 NotifyPublish(chan amqp.Confirmation) chan amqp.Confirmation
58 NotifyReturn(chan amqp.Return) chan amqp.Return
59 NotifyClose(chan *amqp.Error) chan *amqp.Error
60 ExchangeDeclare(string) error
61 QueueDeclareAndBind(qname, ename string) error
62 ExchangeDelete(string) error
63 QueueDelete(qname string) error
64 Qos(prefetchCount, prefetchSize int, global bool) error
65 }
66
67 // connection adapts an *amqp.Connection to the amqpConnection interface.
68 type connection struct {
69 conn *amqp.Connection
70 }
71
72 // Channel creates a new channel. We always want the channel in confirm mode (where
73 // confirmations are delivered for each publish), so we do that here as well.
74 func (c *connection) Channel() (amqpChannel, error) {
75 ch, err := c.conn.Channel()
76 if err != nil {
77 return nil, err
78 }
79 if err := ch.Confirm(wait); err != nil {
80 return nil, err
81 }
82
83 return &channel{ch}, nil
84 }
85
86 func (c *connection) Close() error {
87 return c.conn.Close()
88 }
89
90 // channel adapts an *amqp.Channel to the amqpChannel interface.
91 type channel struct {
92 ch *amqp.Channel
93 }
94
95 func (ch *channel) Publish(exchange, routingKey string, msg amqp.Publishing) error {
96 return ch.PublishWithContext(context.Background(), exchange, routingKey, msg)
97 }
98
99 func (ch *channel) PublishWithContext(ctx context.Context, exchange, routingKey string, msg amqp.Publishing) error {
100 return ch.ch.PublishWithContext(ctx, exchange, routingKey, mandatory, immediate, msg)
101 }
102
103 func (ch *channel) Consume(queue, consumer string) (<-chan amqp.Delivery, error) {
104 return ch.ch.Consume(queue, consumer,
105 false, // autoAck
106 false, // exclusive
107 false, // noLocal
108 wait,
109 nil) // args
110 }
111
112 func (ch *channel) Ack(tag uint64) error {
113 return ch.ch.Ack(tag, false) // multiple=false: acking only this ID
114 }
115
116 func (ch *channel) Nack(tag uint64) error {
117 return ch.ch.Nack(tag, false, true) // multiple=false: acking only this ID, requeue: true to redeliver
118 }
119
120 func (ch *channel) Confirm() error {
121 return ch.ch.Confirm(wait)
122 }
123
124 func (ch *channel) Cancel(consumer string) error {
125 return ch.ch.Cancel(consumer, wait)
126 }
127
128 func (ch *channel) Close() error {
129 return ch.ch.Close()
130 }
131
132 func (ch *channel) NotifyPublish(c chan amqp.Confirmation) chan amqp.Confirmation {
133 return ch.ch.NotifyPublish(c)
134 }
135
136 func (ch *channel) NotifyReturn(c chan amqp.Return) chan amqp.Return {
137 return ch.ch.NotifyReturn(c)
138 }
139
140 func (ch *channel) NotifyClose(c chan *amqp.Error) chan *amqp.Error {
141 return ch.ch.NotifyClose(c)
142 }
143
144 func (ch *channel) ExchangeDeclare(name string) error {
145 return ch.ch.ExchangeDeclare(name,
146 amqp.ExchangeFanout, // kind
147 false, // durable
148 false, // delete when unused
149 false, // internal
150 wait,
151 nil) // args
152 }
153
154 // QueueDeclareAndBind declares a queue and binds it to an exchange.
155 func (ch *channel) QueueDeclareAndBind(queueName, exchangeName string) error {
156 q, err := ch.ch.QueueDeclare(queueName,
157 false, // durable
158 false, // delete when unused
159 false, // exclusive
160 wait,
161 nil) // args
162 if err != nil {
163 return err
164 }
165 return ch.ch.QueueBind(q.Name, q.Name, exchangeName, wait, nil)
166 }
167
168 func (ch *channel) ExchangeDelete(name string) error {
169 return ch.ch.ExchangeDelete(name, false, false)
170 }
171
172 func (ch *channel) QueueDelete(qname string) error {
173 _, err := ch.ch.QueueDelete(qname, false, false, false)
174 return err
175 }
176
177 func (ch *channel) Qos(prefetchCount, prefetchSize int, global bool) error {
178 return ch.ch.Qos(prefetchCount, prefetchSize, global)
179 }