| 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 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 25 |
"github.com/apache/qpid-proton/go/pkg/proton" |
| 26 |
"time" |
| 27 |
) |
| 28 |
|
| 29 |
// Settings associated with a link |
| 30 |
type LinkSettings interface { |
| 31 |
// Source address that messages are coming from. |
| 32 |
Source() string |
| 33 |
|
| 34 |
// Target address that messages are going to. |
| 35 |
Target() string |
| 36 |
|
| 37 |
// Name is a unique name for the link among links between the same |
| 38 |
// containers in the same direction. By default generated automatically. |
| 39 |
LinkName() string |
| 40 |
|
| 41 |
// IsSender is true if this is the sending end of the link. |
| 42 |
IsSender() bool |
| 43 |
|
| 44 |
// IsReceiver is true if this is the receiving end of the link. |
| 45 |
IsReceiver() bool |
| 46 |
|
| 47 |
// SndSettle defines when the sending end of the link settles message delivery. |
| 48 |
SndSettle() SndSettleMode |
| 49 |
|
| 50 |
// RcvSettle defines when the sending end of the link settles message delivery. |
| 51 |
RcvSettle() RcvSettleMode |
| 52 |
|
| 53 |
// Session containing the Link |
| 54 |
Session() Session |
| 55 |
|
| 56 |
// Filter for the link |
| 57 |
Filter() map[amqp.Symbol]interface{} |
| 58 |
|
| 59 |
// Advanced settings for the source |
| 60 |
SourceSettings() TerminusSettings |
| 61 |
|
| 62 |
// Advanced settings for the target |
| 63 |
TargetSettings() TerminusSettings |
| 64 |
} |
| 65 |
|
| 66 |
// LinkOption can be passed when creating a sender or receiver link to set optional configuration. |
| 67 |
type LinkOption func(*linkSettings) |
| 68 |
|
| 69 |
// Source returns a LinkOption that sets address that messages are coming from. |
| 70 |
func Source(s string) LinkOption { return func(l *linkSettings) { l.source = s } } |
| 71 |
|
| 72 |
// Target returns a LinkOption that sets address that messages are going to. |
| 73 |
func Target(s string) LinkOption { return func(l *linkSettings) { l.target = s } } |
| 74 |
|
| 75 |
// LinkName returns a LinkOption that sets the link name. |
| 76 |
func LinkName(s string) LinkOption { return func(l *linkSettings) { l.linkName = s } } |
| 77 |
|
| 78 |
// SndSettle returns a LinkOption that sets the send settle mode |
| 79 |
func SndSettle(m SndSettleMode) LinkOption { return func(l *linkSettings) { l.sndSettle = m } } |
| 80 |
|
| 81 |
// RcvSettle returns a LinkOption that sets the send settle mode |
| 82 |
func RcvSettle(m RcvSettleMode) LinkOption { return func(l *linkSettings) { l.rcvSettle = m } } |
| 83 |
|
| 84 |
// Capacity returns a LinkOption that sets the link capacity |
| 85 |
func Capacity(n int) LinkOption { return func(l *linkSettings) { l.capacity = n } } |
| 86 |
|
| 87 |
// Prefetch returns a LinkOption that sets a receivers pre-fetch flag. Not relevant for a sender. |
| 88 |
func Prefetch(p bool) LinkOption { return func(l *linkSettings) { l.prefetch = p } } |
| 89 |
|
| 90 |
// DurableSubscription returns a LinkOption that configures a Receiver as a named durable |
| 91 |
// subscription. The name overrides (and is overridden by) LinkName() so you should normally |
| 92 |
// only use one of these options. |
| 93 |
func DurableSubscription(name string) LinkOption { |
| 94 |
return func(l *linkSettings) { |
| 95 |
l.linkName = name |
| 96 |
l.sourceSettings.Durability = proton.Deliveries |
| 97 |
l.sourceSettings.Expiry = proton.ExpireNever |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// AtMostOnce returns a LinkOption that sets "fire and forget" mode, messages |
| 102 |
// are sent but no acknowledgment is received, messages can be lost if there is |
| 103 |
// a network failure. Sets SndSettleMode=SendSettled and RcvSettleMode=RcvFirst |
| 104 |
func AtMostOnce() LinkOption { |
| 105 |
return func(l *linkSettings) { |
| 106 |
SndSettle(SndSettled)(l) |
| 107 |
RcvSettle(RcvFirst)(l) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// AtLeastOnce returns a LinkOption that requests acknowledgment for every |
| 112 |
// message, acknowledgment indicates the message was definitely received. In the |
| 113 |
// event of a failure, unacknowledged messages can be re-sent but there is a |
| 114 |
// chance that the message will be received twice in this case. Sets |
| 115 |
// SndSettleMode=SndUnsettled and RcvSettleMode=RcvFirst |
| 116 |
func AtLeastOnce() LinkOption { |
| 117 |
return func(l *linkSettings) { |
| 118 |
SndSettle(SndUnsettled)(l) |
| 119 |
RcvSettle(RcvFirst)(l) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Filter returns a LinkOption that sets a filter. |
| 124 |
func Filter(m map[amqp.Symbol]interface{}) LinkOption { |
| 125 |
return func(l *linkSettings) { l.filter = m } |
| 126 |
} |
| 127 |
|
| 128 |
// SourceSettings returns a LinkOption that sets all the SourceSettings. |
| 129 |
// Note: it will override the source address set by a Source() option |
| 130 |
func SourceSettings(ts TerminusSettings) LinkOption { |
| 131 |
return func(l *linkSettings) { l.sourceSettings = ts } |
| 132 |
} |
| 133 |
|
| 134 |
// TargetSettings returns a LinkOption that sets all the TargetSettings. |
| 135 |
// Note: it will override the target address set by a Target() option |
| 136 |
func TargetSettings(ts TerminusSettings) LinkOption { |
| 137 |
return func(l *linkSettings) { l.targetSettings = ts } |
| 138 |
} |
| 139 |
|
| 140 |
// SndSettleMode defines when the sending end of the link settles message delivery. |
| 141 |
type SndSettleMode proton.SndSettleMode |
| 142 |
|
| 143 |
const ( |
| 144 |
// Messages are sent unsettled |
| 145 |
SndUnsettled = SndSettleMode(proton.SndUnsettled) |
| 146 |
// Messages are sent already settled |
| 147 |
SndSettled = SndSettleMode(proton.SndSettled) |
| 148 |
// Sender can send either unsettled or settled messages. |
| 149 |
SndMixed = SndSettleMode(proton.SndMixed) |
| 150 |
) |
| 151 |
|
| 152 |
// RcvSettleMode defines when the receiving end of the link settles message delivery. |
| 153 |
type RcvSettleMode proton.RcvSettleMode |
| 154 |
|
| 155 |
const ( |
| 156 |
// Receiver settles first. |
| 157 |
RcvFirst = RcvSettleMode(proton.RcvFirst) |
| 158 |
// Receiver waits for sender to settle before settling. |
| 159 |
RcvSecond = RcvSettleMode(proton.RcvSecond) |
| 160 |
) |
| 161 |
|
| 162 |
type linkSettings struct { |
| 163 |
source string |
| 164 |
sourceSettings TerminusSettings |
| 165 |
target string |
| 166 |
targetSettings TerminusSettings |
| 167 |
linkName string |
| 168 |
isSender bool |
| 169 |
sndSettle SndSettleMode |
| 170 |
rcvSettle RcvSettleMode |
| 171 |
capacity int |
| 172 |
prefetch bool |
| 173 |
filter map[amqp.Symbol]interface{} |
| 174 |
session *session |
| 175 |
pLink proton.Link |
| 176 |
} |
| 177 |
|
| 178 |
// Advanced AMQP settings for the source or target of a link. |
| 179 |
// Usually these can be set via a more descriptive LinkOption, e.g. DurableSubscription() |
| 180 |
// and do not need to be set/examined directly. |
| 181 |
type TerminusSettings struct { |
| 182 |
Durability proton.Durability |
| 183 |
Expiry proton.ExpiryPolicy |
| 184 |
Timeout time.Duration |
| 185 |
Dynamic bool |
| 186 |
} |
| 187 |
|
| 188 |
func makeTerminusSettings(t proton.Terminus) TerminusSettings { |
| 189 |
return TerminusSettings{ |
| 190 |
Durability: t.Durability(), |
| 191 |
Expiry: t.ExpiryPolicy(), |
| 192 |
Timeout: t.Timeout(), |
| 193 |
Dynamic: t.IsDynamic(), |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
type link struct { |
| 198 |
endpoint |
| 199 |
linkSettings |
| 200 |
} |
| 201 |
|
| 202 |
func (l *linkSettings) Source() string { return l.source } |
| 203 |
func (l *linkSettings) Target() string { return l.target } |
| 204 |
func (l *linkSettings) LinkName() string { return l.linkName } |
| 205 |
func (l *linkSettings) IsSender() bool { return l.isSender } |
| 206 |
func (l *linkSettings) IsReceiver() bool { return !l.isSender } |
| 207 |
func (l *linkSettings) SndSettle() SndSettleMode { return l.sndSettle } |
| 208 |
func (l *linkSettings) RcvSettle() RcvSettleMode { return l.rcvSettle } |
| 209 |
func (l *linkSettings) Filter() map[amqp.Symbol]interface{} { return l.filter } |
| 210 |
func (l *linkSettings) SourceSettings() TerminusSettings { return l.sourceSettings } |
| 211 |
func (l *linkSettings) TargetSettings() TerminusSettings { return l.targetSettings } |
| 212 |
|
| 213 |
func (l *link) Session() Session { return l.session } |
| 214 |
func (l *link) Connection() Connection { return l.session.Connection() } |
| 215 |
func (l *link) engine() *proton.Engine { return l.session.connection.engine } |
| 216 |
func (l *link) handler() *handler { return l.session.connection.handler } |
| 217 |
|
| 218 |
// Open a link and return the linkSettings. |
| 219 |
func makeLocalLink(sn *session, isSender bool, setting ...LinkOption) (linkSettings, error) { |
| 220 |
l := linkSettings{ |
| 221 |
isSender: isSender, |
| 222 |
capacity: 1, |
| 223 |
prefetch: false, |
| 224 |
session: sn, |
| 225 |
} |
| 226 |
for _, set := range setting { |
| 227 |
set(&l) |
| 228 |
} |
| 229 |
if l.linkName == "" { |
| 230 |
l.linkName = l.session.connection.container.nextLinkName() |
| 231 |
} |
| 232 |
if l.IsSender() { |
| 233 |
l.pLink = l.session.pSession.Sender(l.linkName) |
| 234 |
} else { |
| 235 |
l.pLink = l.session.pSession.Receiver(l.linkName) |
| 236 |
} |
| 237 |
if l.pLink.IsNil() { |
| 238 |
return l, fmt.Errorf("cannot create link %s", l.pLink) |
| 239 |
} |
| 240 |
l.pLink.Source().SetAddress(l.source) |
| 241 |
|
| 242 |
if len(l.filter) > 0 { |
| 243 |
if err := l.pLink.Source().Filter().Marshal(l.filter); err != nil { |
| 244 |
panic(err) // Shouldn't happen |
| 245 |
} |
| 246 |
} |
| 247 |
l.pLink.Source().SetDurability(l.sourceSettings.Durability) |
| 248 |
l.pLink.Source().SetExpiryPolicy(l.sourceSettings.Expiry) |
| 249 |
l.pLink.Source().SetTimeout(l.sourceSettings.Timeout) |
| 250 |
l.pLink.Source().SetDynamic(l.sourceSettings.Dynamic) |
| 251 |
|
| 252 |
l.pLink.Target().SetAddress(l.target) |
| 253 |
l.pLink.Target().SetDurability(l.targetSettings.Durability) |
| 254 |
l.pLink.Target().SetExpiryPolicy(l.targetSettings.Expiry) |
| 255 |
l.pLink.Target().SetTimeout(l.targetSettings.Timeout) |
| 256 |
l.pLink.Target().SetDynamic(l.targetSettings.Dynamic) |
| 257 |
|
| 258 |
l.pLink.SetSndSettleMode(proton.SndSettleMode(l.sndSettle)) |
| 259 |
l.pLink.SetRcvSettleMode(proton.RcvSettleMode(l.rcvSettle)) |
| 260 |
l.pLink.Open() |
| 261 |
return l, nil |
| 262 |
} |
| 263 |
|
| 264 |
func makeIncomingLinkSettings(pLink proton.Link, sn *session) linkSettings { |
| 265 |
l := linkSettings{ |
| 266 |
isSender: pLink.IsSender(), |
| 267 |
source: pLink.RemoteSource().Address(), |
| 268 |
sourceSettings: makeTerminusSettings(pLink.RemoteSource()), |
| 269 |
target: pLink.RemoteTarget().Address(), |
| 270 |
targetSettings: makeTerminusSettings(pLink.RemoteTarget()), |
| 271 |
linkName: pLink.Name(), |
| 272 |
sndSettle: SndSettleMode(pLink.RemoteSndSettleMode()), |
| 273 |
rcvSettle: RcvSettleMode(pLink.RemoteRcvSettleMode()), |
| 274 |
capacity: 1, |
| 275 |
prefetch: false, |
| 276 |
pLink: pLink, |
| 277 |
session: sn, |
| 278 |
} |
| 279 |
filter := l.pLink.RemoteSource().Filter() |
| 280 |
if !filter.Empty() { |
| 281 |
filter.Unmarshal(&l.filter) // TODO aconway 2017-06-08: ignoring errors |
| 282 |
} |
| 283 |
return l |
| 284 |
} |
| 285 |
|
| 286 |
// Not part of Link interface but use by Sender and Receiver. |
| 287 |
func (l *link) Credit() (credit int, err error) { |
| 288 |
err = l.engine().InjectWait(func() error { |
| 289 |
if l.Error() != nil { |
| 290 |
return l.Error() |
| 291 |
} |
| 292 |
credit = l.pLink.Credit() |
| 293 |
return nil |
| 294 |
}) |
| 295 |
return |
| 296 |
} |
| 297 |
|
| 298 |
// Not part of Link interface but use by Sender and Receiver. |
| 299 |
func (l *link) Capacity() int { return l.capacity } |
| 300 |
|
| 301 |
func (l *link) Close(err error) { |
| 302 |
_ = l.engine().Inject(func() { |
| 303 |
if l.Error() == nil { |
| 304 |
localClose(l.pLink, err) |
| 305 |
} |
| 306 |
}) |
| 307 |
} |