| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package electron |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 27 |
"github.com/apache/qpid-proton/go/pkg/proton" |
| 28 |
) |
| 29 |
|
| 30 |
// Receiver is a Link that receives messages. |
| 31 |
// |
| 32 |
type Receiver interface { |
| 33 |
Endpoint |
| 34 |
LinkSettings |
| 35 |
|
| 36 |
// Receive blocks until a message is available or until the Receiver is closed |
| 37 |
// and has no more buffered messages. |
| 38 |
Receive() (ReceivedMessage, error) |
| 39 |
|
| 40 |
// ReceiveTimeout is like Receive but gives up after timeout, see Timeout. |
| 41 |
// |
| 42 |
// Note that that if Prefetch is false, after a Timeout the credit issued by |
| 43 |
// Receive remains on the link. It will be used by the next call to Receive. |
| 44 |
ReceiveTimeout(timeout time.Duration) (ReceivedMessage, error) |
| 45 |
|
| 46 |
// Prefetch==true means the Receiver will automatically issue credit to the |
| 47 |
// remote sender to keep its buffer as full as possible, i.e. it will |
| 48 |
// "pre-fetch" messages independently of the application calling |
| 49 |
// Receive(). This gives good throughput for applications that handle a |
| 50 |
// continuous stream of messages. Larger capacity may improve throughput, the |
| 51 |
// optimal value depends on the characteristics of your application. |
| 52 |
// |
| 53 |
// Prefetch==false means the Receiver will issue only issue credit when you |
| 54 |
// call Receive(), and will only issue enough credit to satisfy the calls |
| 55 |
// actually made. This gives lower throughput but will not fetch any messages |
| 56 |
// in advance. It is good for synchronous applications that need to evaluate |
| 57 |
// each message before deciding whether to receive another. The |
| 58 |
// request-response pattern is a typical example. If you make concurrent |
| 59 |
// calls to Receive with pre-fetch disabled, you can improve performance by |
| 60 |
// setting the capacity close to the expected number of concurrent calls. |
| 61 |
// |
| 62 |
Prefetch() bool |
| 63 |
|
| 64 |
// Capacity is the size (number of messages) of the local message buffer |
| 65 |
// These are messages received but not yet returned to the application by a call to Receive() |
| 66 |
Capacity() int |
| 67 |
} |
| 68 |
|
| 69 |
// Receiver implementation |
| 70 |
type receiver struct { |
| 71 |
link |
| 72 |
buffer chan ReceivedMessage |
| 73 |
callers int |
| 74 |
} |
| 75 |
|
| 76 |
func (r *receiver) Capacity() int { return cap(r.buffer) } |
| 77 |
func (r *receiver) Prefetch() bool { return r.prefetch } |
| 78 |
|
| 79 |
// Call in proton goroutine |
| 80 |
func newReceiver(ls linkSettings) *receiver { |
| 81 |
r := &receiver{link: link{linkSettings: ls}} |
| 82 |
r.endpoint.init(r.link.pLink.String()) |
| 83 |
if r.capacity < 1 { |
| 84 |
r.capacity = 1 |
| 85 |
} |
| 86 |
r.buffer = make(chan ReceivedMessage, r.capacity) |
| 87 |
r.handler().addLink(r.pLink, r) |
| 88 |
r.link.pLink.Open() |
| 89 |
if r.prefetch { |
| 90 |
r.flow(r.maxFlow()) |
| 91 |
} |
| 92 |
return r |
| 93 |
} |
| 94 |
|
| 95 |
// Call in proton goroutine. Max additional credit we can request. |
| 96 |
func (r *receiver) maxFlow() int { return cap(r.buffer) - len(r.buffer) - r.pLink.Credit() } |
| 97 |
|
| 98 |
func (r *receiver) flow(credit int) { |
| 99 |
if credit > 0 { |
| 100 |
r.pLink.Flow(credit) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Inject flow check per-caller call when prefetch is off. |
| 105 |
// Called with inc=1 at start of call, inc = -1 at end |
| 106 |
func (r *receiver) caller(inc int) { |
| 107 |
_ = r.engine().Inject(func() { |
| 108 |
r.callers += inc |
| 109 |
need := r.callers - (len(r.buffer) + r.pLink.Credit()) |
| 110 |
max := r.maxFlow() |
| 111 |
if need > max { |
| 112 |
need = max |
| 113 |
} |
| 114 |
r.flow(need) |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
// Inject flow top-up if prefetch is enabled |
| 119 |
func (r *receiver) flowTopUp() { |
| 120 |
if r.prefetch { |
| 121 |
_ = r.engine().Inject(func() { r.flow(r.maxFlow()) }) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func (r *receiver) Receive() (rm ReceivedMessage, err error) { |
| 126 |
return r.ReceiveTimeout(Forever) |
| 127 |
} |
| 128 |
|
| 129 |
func (r *receiver) ReceiveTimeout(timeout time.Duration) (rm ReceivedMessage, err error) { |
| 130 |
if r.buffer == nil { |
| 131 |
panic(fmt.Errorf("Receiver is not open: %s", r)) |
| 132 |
} |
| 133 |
if !r.prefetch { // Per-caller flow control |
| 134 |
select { // Check for immediate availability, avoid caller() inject |
| 135 |
case rm2, ok := <-r.buffer: |
| 136 |
if ok { |
| 137 |
rm = rm2 |
| 138 |
} else { |
| 139 |
err = r.Error() |
| 140 |
} |
| 141 |
return |
| 142 |
default: // Not immediately available, inject caller() counts |
| 143 |
r.caller(+1) |
| 144 |
defer r.caller(-1) |
| 145 |
} |
| 146 |
} |
| 147 |
rmi, err := timedReceive(r.buffer, timeout) |
| 148 |
switch err { |
| 149 |
case nil: |
| 150 |
r.flowTopUp() |
| 151 |
rm = rmi.(ReceivedMessage) |
| 152 |
case Closed: |
| 153 |
err = r.Error() |
| 154 |
} |
| 155 |
return |
| 156 |
} |
| 157 |
|
| 158 |
// Called in proton goroutine on MMessage event. |
| 159 |
func (r *receiver) message(delivery proton.Delivery) { |
| 160 |
if r.pLink.State().RemoteClosed() { |
| 161 |
localClose(r.pLink, r.pLink.RemoteCondition().Error()) |
| 162 |
return |
| 163 |
} |
| 164 |
if delivery.HasMessage() { |
| 165 |
bytes, err := delivery.MessageBytes() |
| 166 |
var m amqp.Message |
| 167 |
if err == nil { |
| 168 |
m = amqp.NewMessage() |
| 169 |
err = r.session.connection.mc.Decode(m, bytes) |
| 170 |
} |
| 171 |
if err != nil { |
| 172 |
localClose(r.pLink, err) |
| 173 |
return |
| 174 |
} |
| 175 |
r.pLink.Advance() |
| 176 |
if r.pLink.Credit() < 0 { |
| 177 |
localClose(r.pLink, fmt.Errorf("received message in excess of credit limit")) |
| 178 |
} else { |
| 179 |
// We never issue more credit than cap(buffer) so this will not block. |
| 180 |
r.buffer <- ReceivedMessage{m, delivery, r} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func (r *receiver) closed(err error) error { |
| 186 |
e := r.link.closed(err) |
| 187 |
if r.buffer != nil { |
| 188 |
close(r.buffer) |
| 189 |
} |
| 190 |
return e |
| 191 |
} |
| 192 |
|
| 193 |
// ReceivedMessage contains an amqp.Message and allows the message to be acknowledged. |
| 194 |
type ReceivedMessage struct { |
| 195 |
// Message is the received message. |
| 196 |
Message amqp.Message |
| 197 |
|
| 198 |
pDelivery proton.Delivery |
| 199 |
receiver Receiver |
| 200 |
} |
| 201 |
|
| 202 |
// Acknowledge a ReceivedMessage with the given delivery status. |
| 203 |
func (rm *ReceivedMessage) acknowledge(status uint64) error { |
| 204 |
return rm.receiver.(*receiver).engine().Inject(func() { |
| 205 |
// Deliveries are valid as long as the connection is, unless settled. |
| 206 |
rm.pDelivery.SettleAs(uint64(status)) |
| 207 |
}) |
| 208 |
} |
| 209 |
|
| 210 |
// Accept tells the sender that we take responsibility for processing the message. |
| 211 |
func (rm *ReceivedMessage) Accept() error { return rm.acknowledge(proton.Accepted) } |
| 212 |
|
| 213 |
// Reject tells the sender we consider the message invalid and unusable. |
| 214 |
func (rm *ReceivedMessage) Reject() error { return rm.acknowledge(proton.Rejected) } |
| 215 |
|
| 216 |
// Release tells the sender we will not process the message but some other |
| 217 |
// receiver might. |
| 218 |
func (rm *ReceivedMessage) Release() error { return rm.acknowledge(proton.Released) } |
| 219 |
|
| 220 |
// IncomingReceiver is sent on the Connection.Incoming() channel when there is |
| 221 |
// an incoming request to open a receiver link. |
| 222 |
type IncomingReceiver struct { |
| 223 |
incoming |
| 224 |
linkSettings |
| 225 |
} |
| 226 |
|
| 227 |
func newIncomingReceiver(sn *session, pLink proton.Link) *IncomingReceiver { |
| 228 |
return &IncomingReceiver{ |
| 229 |
incoming: makeIncoming(pLink), |
| 230 |
linkSettings: makeIncomingLinkSettings(pLink, sn), |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// SetCapacity sets the capacity of the incoming receiver, call before Accept() |
| 235 |
func (in *IncomingReceiver) SetCapacity(capacity int) { in.capacity = capacity } |
| 236 |
|
| 237 |
// SetPrefetch sets the pre-fetch mode of the incoming receiver, call before Accept() |
| 238 |
func (in *IncomingReceiver) SetPrefetch(prefetch bool) { in.prefetch = prefetch } |
| 239 |
|
| 240 |
// Accept accepts an incoming receiver endpoint |
| 241 |
func (in *IncomingReceiver) Accept() Endpoint { |
| 242 |
return in.accept(func() Endpoint { return newReceiver(in.linkSettings) }) |
| 243 |
} |