mirror of
https://github.com/openfaas/faas.git
synced 2025-06-26 08:43:24 +00:00
Bump queue-worker and NATS
NATS moved its primary library to: github.com/nats-io/stan.go This commit synchronises the library in the gateway to match the queue worker. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
committed by
Alex Ellis
parent
542d7c0b83
commit
9bde25aedb
29
gateway/vendor/github.com/nats-io/stan.go/.gitignore
generated
vendored
Normal file
29
gateway/vendor/github.com/nats-io/stan.go/.gitignore
generated
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
# Eclipse stuff
|
||||
.project
|
||||
.settings/
|
||||
.idea
|
23
gateway/vendor/github.com/nats-io/stan.go/.travis.yml
generated
vendored
Normal file
23
gateway/vendor/github.com/nats-io/stan.go/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
language: go
|
||||
sudo: false
|
||||
go:
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
env:
|
||||
- GO111MODULE=off
|
||||
go_import_path: github.com/nats-io/stan.go
|
||||
install:
|
||||
- go get -t -v ./...
|
||||
- go get github.com/nats-io/nats-streaming-server
|
||||
- go get -u honnef.co/go/tools/cmd/staticcheck
|
||||
- go get -u github.com/client9/misspell/cmd/misspell
|
||||
before_script:
|
||||
- $(exit $(go fmt ./... | wc -l))
|
||||
- go vet ./...
|
||||
- find . -type f -name "*.go" | grep -v "/pb/" | xargs misspell -error -locale US
|
||||
- staticcheck ./...
|
||||
script:
|
||||
- go test -i -v ./...
|
||||
- go test -v -race ./...
|
||||
after_success:
|
||||
- if [[ "$TRAVIS_GO_VERSION" =~ 1.13 ]]; then ./scripts/cov.sh TRAVIS; fi
|
4
gateway/vendor/github.com/nats-io/stan.go/CODE-OF-CONDUCT.md
generated
vendored
Normal file
4
gateway/vendor/github.com/nats-io/stan.go/CODE-OF-CONDUCT.md
generated
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
## Community Code of Conduct
|
||||
|
||||
NATS follows the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md).
|
||||
|
3
gateway/vendor/github.com/nats-io/stan.go/GOVERNANCE.md
generated
vendored
Normal file
3
gateway/vendor/github.com/nats-io/stan.go/GOVERNANCE.md
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# NATS Streaming Governance
|
||||
|
||||
NATS Streaming is part of the NATS project and is subject to the [NATS Governance](https://github.com/nats-io/nats-general/blob/master/GOVERNANCE.md).
|
201
gateway/vendor/github.com/nats-io/stan.go/LICENSE
generated
vendored
Normal file
201
gateway/vendor/github.com/nats-io/stan.go/LICENSE
generated
vendored
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
13
gateway/vendor/github.com/nats-io/stan.go/MAINTAINERS.md
generated
vendored
Normal file
13
gateway/vendor/github.com/nats-io/stan.go/MAINTAINERS.md
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
# Maintainers
|
||||
|
||||
Maintainership is on a per project basis.
|
||||
|
||||
### Core-maintainers
|
||||
- Derek Collison <derek@nats.io> [@derekcollison](https://github.com/derekcollison)
|
||||
- Ivan Kozlovic <ivan@nats.io> [@kozlovic](https://github.com/kozlovic)
|
||||
|
||||
### Maintainers
|
||||
- Alberto Ricart <alberto@nats.io> [@aricart](https://github.com/aricart)
|
||||
- Colin Sullivan <colin@nats.io> [@ColinSullivan1](https://github.com/ColinSullivan1)
|
||||
- Waldemar Quevedo <wally@nats.io> [@wallyqs](https://github.com/wallyqs)
|
||||
- R.I. Pienaar <rip@devco.net> [@ripienaar](https://github.com/ripienaar)
|
410
gateway/vendor/github.com/nats-io/stan.go/README.md
generated
vendored
Normal file
410
gateway/vendor/github.com/nats-io/stan.go/README.md
generated
vendored
Normal file
@ -0,0 +1,410 @@
|
||||
# NATS Streaming
|
||||
|
||||
NATS Streaming is an extremely performant, lightweight reliable streaming platform powered by [NATS](https://nats.io).
|
||||
|
||||
[](https://www.apache.org/licenses/LICENSE-2.0)
|
||||
[](http://travis-ci.org/nats-io/stan.go)
|
||||
[](https://coveralls.io/r/nats-io/stan.go?branch=master)
|
||||
[](http://godoc.org/github.com/nats-io/stan.go)
|
||||
|
||||
NATS Streaming provides the following high-level feature set:
|
||||
- Log based persistence
|
||||
- At-Least-Once Delivery model, giving reliable message delivery
|
||||
- Rate matched on a per subscription basis
|
||||
- Replay/Restart
|
||||
- Last Value Semantics
|
||||
|
||||
## Notes
|
||||
|
||||
- Please raise questions/issues via the [Issue Tracker](https://github.com/nats-io/stan.go/issues).
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# Go client
|
||||
go get github.com/nats-io/stan.go/
|
||||
```
|
||||
|
||||
When using or transitioning to Go modules support:
|
||||
|
||||
```bash
|
||||
# Go client latest or explicit version
|
||||
go get github.com/nats-io/stan.go/@latest
|
||||
go get github.com/nats-io/stan.go/@v0.6.0
|
||||
```
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```go
|
||||
import stan "github.com/nats-io/stan.go"
|
||||
|
||||
sc, _ := stan.Connect(clusterID, clientID)
|
||||
|
||||
// Simple Synchronous Publisher
|
||||
sc.Publish("foo", []byte("Hello World")) // does not return until an ack has been received from NATS Streaming
|
||||
|
||||
// Simple Async Subscriber
|
||||
sub, _ := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
})
|
||||
|
||||
// Unsubscribe
|
||||
sub.Unsubscribe()
|
||||
|
||||
// Close connection
|
||||
sc.Close()
|
||||
```
|
||||
|
||||
### Subscription Start (i.e. Replay) Options
|
||||
|
||||
NATS Streaming subscriptions are similar to NATS subscriptions, but clients may start their subscription at an earlier point in the message stream, allowing them to receive messages that were published before this client registered interest.
|
||||
|
||||
The options are described with examples below:
|
||||
|
||||
```go
|
||||
|
||||
// Subscribe starting with most recently published value
|
||||
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.StartWithLastReceived())
|
||||
|
||||
// Receive all stored values in order
|
||||
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.DeliverAllAvailable())
|
||||
|
||||
// Receive messages starting at a specific sequence number
|
||||
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.StartAtSequence(22))
|
||||
|
||||
// Subscribe starting at a specific time
|
||||
var startTime time.Time
|
||||
...
|
||||
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.StartAtTime(startTime))
|
||||
|
||||
// Subscribe starting a specific amount of time in the past (e.g. 30 seconds ago)
|
||||
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.StartAtTimeDelta(time.ParseDuration("30s")))
|
||||
```
|
||||
|
||||
### Durable Subscriptions
|
||||
|
||||
Replay of messages offers great flexibility for clients wishing to begin processing at some earlier point in the data stream.
|
||||
However, some clients just need to pick up where they left off from an earlier session, without having to manually track their position in the stream of messages.
|
||||
Durable subscriptions allow clients to assign a durable name to a subscription when it is created.
|
||||
Doing this causes the NATS Streaming server to track the last acknowledged message for that clientID + durable name, so that only messages since the last acknowledged message will be delivered to the client.
|
||||
|
||||
```go
|
||||
sc, _ := stan.Connect("test-cluster", "client-123")
|
||||
|
||||
// Subscribe with durable name
|
||||
sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.DurableName("my-durable"))
|
||||
...
|
||||
// client receives message sequence 1-40
|
||||
...
|
||||
// client disconnects for an hour
|
||||
...
|
||||
// client reconnects with same clientID "client-123"
|
||||
sc, _ := stan.Connect("test-cluster", "client-123")
|
||||
|
||||
// client re-subscribes to "foo" with same durable name "my-durable"
|
||||
sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.DurableName("my-durable"))
|
||||
...
|
||||
// client receives messages 41-current
|
||||
```
|
||||
|
||||
### Queue Groups
|
||||
|
||||
All subscriptions with the same queue name (regardless of the connection
|
||||
they originate from) will form a queue group.
|
||||
Each message will be delivered to only one subscriber per queue group,
|
||||
using queuing semantics. You can have as many queue groups as you wish.
|
||||
|
||||
Normal subscribers will continue to work as expected.
|
||||
|
||||
#### Creating a Queue Group
|
||||
|
||||
A queue group is automatically created when the first queue subscriber is
|
||||
created. If the group already exists, the member is added to the group.
|
||||
|
||||
```go
|
||||
sc, _ := stan.Connect("test-cluster", "clientid")
|
||||
|
||||
// Create a queue subscriber on "foo" for group "bar"
|
||||
qsub1, _ := sc.QueueSubscribe("foo", "bar", qcb)
|
||||
|
||||
// Add a second member
|
||||
qsub2, _ := sc.QueueSubscribe("foo", "bar", qcb)
|
||||
|
||||
// Notice that you can have a regular subscriber on that subject
|
||||
sub, _ := sc.Subscribe("foo", cb)
|
||||
|
||||
// A message on "foo" will be received by sub and qsub1 or qsub2.
|
||||
```
|
||||
|
||||
#### Start Position
|
||||
|
||||
Note that once a queue group is formed, a member's start position is ignored
|
||||
when added to the group. It will start receive messages from the last
|
||||
position in the group.
|
||||
|
||||
Suppose the channel `foo` exists and there are `500` messages stored, the group
|
||||
`bar` is already created, there are two members and the last
|
||||
message sequence sent is `100`. A new member is added. Note its start position:
|
||||
|
||||
```go
|
||||
sc.QueueSubscribe("foo", "bar", qcb, stan.StartAtSequence(200))
|
||||
```
|
||||
|
||||
This will not produce an error, but the start position will be ignored. Assuming
|
||||
this member would be the one receiving the next message, it would receive message
|
||||
sequence `101`.
|
||||
|
||||
#### Leaving the Group
|
||||
|
||||
There are two ways of leaving the group: closing the subscriber's connection or
|
||||
calling `Unsubscribe`:
|
||||
|
||||
```go
|
||||
// Have qsub leave the queue group
|
||||
qsub.Unsubscribe()
|
||||
```
|
||||
|
||||
If the leaving member had un-acknowledged messages, those messages are reassigned
|
||||
to the remaining members.
|
||||
|
||||
#### Closing a Queue Group
|
||||
|
||||
There is no special API for that. Once all members have left (either calling `Unsubscribe`,
|
||||
or their connections are closed), the group is removed from the server.
|
||||
|
||||
The next call to `QueueSubscribe` with the same group name will create a brand new group,
|
||||
that is, the start position will take effect and delivery will start from there.
|
||||
|
||||
### Durable Queue Groups
|
||||
|
||||
As described above, for non durable queue subscribers, when the last member leaves the group,
|
||||
that group is removed. A durable queue group allows you to have all members leave but still
|
||||
maintain state. When a member re-joins, it starts at the last position in that group.
|
||||
|
||||
#### Creating a Durable Queue Group
|
||||
|
||||
A durable queue group is created in a similar manner as that of a standard queue group,
|
||||
except the `DurableName` option must be used to specify durability.
|
||||
|
||||
```go
|
||||
sc.QueueSubscribe("foo", "bar", qcb, stan.DurableName("dur"))
|
||||
```
|
||||
A group called `dur:bar` (the concatenation of durable name and group name) is created in
|
||||
the server. This means two things:
|
||||
|
||||
- The character `:` is not allowed for a queue subscriber's durable name.
|
||||
- Durable and non-durable queue groups with the same name can coexist.
|
||||
|
||||
```go
|
||||
// Non durable queue subscriber on group "bar"
|
||||
qsub, _ := sc.QueueSubscribe("foo", "bar", qcb)
|
||||
|
||||
// Durable queue subscriber on group "bar"
|
||||
durQsub, _ := sc.QueueSubscribe("foo", "bar", qcb, stan.DurableName("mydurablegroup"))
|
||||
|
||||
// The same message produced on "foo" would be received by both queue subscribers.
|
||||
```
|
||||
|
||||
#### Start Position
|
||||
|
||||
The rules for non-durable queue subscribers apply to durable subscribers.
|
||||
|
||||
#### Leaving the Group
|
||||
|
||||
As for non-durable queue subscribers, if a member's connection is closed, or if
|
||||
`Unsubscribe` its called, the member leaves the group. Any unacknowledged message
|
||||
is transferred to remaining members. See *Closing the Group* for important difference
|
||||
with non-durable queue subscribers.
|
||||
|
||||
#### Closing the Group
|
||||
|
||||
The *last* member calling `Unsubscribe` will close (that is destroy) the
|
||||
group. So if you want to maintain durability of the group, you should not be
|
||||
calling `Unsubscribe`.
|
||||
|
||||
So unlike for non-durable queue subscribers, it is possible to maintain a queue group
|
||||
with no member in the server. When a new member re-joins the durable queue group,
|
||||
it will resume from where the group left of, actually first receiving all unacknowledged
|
||||
messages that may have been left when the last member previously left.
|
||||
|
||||
|
||||
### Wildcard Subscriptions
|
||||
|
||||
NATS Streaming subscriptions **do not** support wildcards.
|
||||
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Connection configuration such as TLS, etc..
|
||||
|
||||
If you want more advanced configuration of the underlying NATS Connection, you will need
|
||||
to create a NATS connection and pass that connection to the `stan.Connect()` call with
|
||||
the `stan.NatsConn()` option.
|
||||
|
||||
```go
|
||||
// Create a NATS connection that you can configure the way you want
|
||||
nc, err = nats.Connect("tls://localhost:4443", nats.ClientCert("mycerts/client-cert.pem", "mycerts/client-key.pem"))
|
||||
if (err != nil)
|
||||
...
|
||||
|
||||
// Then pass it to the stan.Connect() call.
|
||||
sc, err = stan.Connect("test-cluster", "me", stan.NatsConn(nc))
|
||||
if (err != nil)
|
||||
...
|
||||
|
||||
// Note that you will be responsible for closing the NATS Connection after the streaming
|
||||
// connection has been closed.
|
||||
```
|
||||
|
||||
### Connection Status
|
||||
|
||||
The fact that the NATS Streaming server and clients are not directly connected poses a challenge when it comes to know if a client is still valid.
|
||||
When a client disconnects, the streaming server is not notified, hence the importance of calling `Close()`. The server sends heartbeats
|
||||
to the client's private inbox and if it misses a certain number of responses, it will consider the client's connection lost and remove it
|
||||
from its state.
|
||||
|
||||
Before version `0.4.0`, the client library was not sending PINGs to the streaming server to detect connection failure. This was problematic
|
||||
especially if an application was never sending data (had only subscriptions for instance). Picture the case where a client connects to a
|
||||
NATS Server which has a route to a NATS Streaming server (either connecting to a standalone NATS Server or the server it embeds). If the
|
||||
connection between the streaming server and the client's NATS Server is broken, the client's NATS connection would still be ok, yet, no
|
||||
communication with the streaming server is possible. This is why relying on `Conn.NatsConn()` to check the status is not helpful.
|
||||
|
||||
Starting version `0.4.0` of this library and server `0.10.0`, the client library will now send PINGs at regular intervals (default is 5 seconds)
|
||||
and will close the streaming connection after a certain number of PINGs have been sent without any response (default is 3). When that
|
||||
happens, a callback - if one is registered - will be invoked to notify the user that the connection is permanently lost, and the reason
|
||||
for the failure.
|
||||
|
||||
Here is how you would specify your own PING values and the callback:
|
||||
|
||||
```go
|
||||
|
||||
// Send PINGs every 10 seconds, and fail after 5 PINGs without any response.
|
||||
sc, err := stan.Connect(clusterName, clientName,
|
||||
stan.Pings(10, 5),
|
||||
stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) {
|
||||
log.Fatalf("Connection lost, reason: %v", reason)
|
||||
}))
|
||||
```
|
||||
|
||||
Note that the only way to be notified is to set the callback. If the callback is not set, PINGs are still sent and the connection
|
||||
will be closed if needed, but the application won't know if it has only subscriptions.
|
||||
|
||||
When the connection is lost, your application would have to re-create it and all subscriptions if any.
|
||||
|
||||
When no NATS connection is provided to the `Connect()` call, the library creates its own NATS connection and will now
|
||||
set the reconnect attempts to "infinite", which was not the case before. It should therefore be possible for the library to
|
||||
always reconnect, but this does not mean that the streaming connection will not be closed, even if you set a very high
|
||||
threshold for the PINGs max out value. Keep in mind that while the client is disconnected, the server is sending heartbeats to
|
||||
the clients too, and when not getting any response, it will remove that client from its state. When the communication is restored,
|
||||
the PINGs sent to the server will allow to detect this condition and report to the client that the connection is now closed.
|
||||
|
||||
Also, while a client is "disconnected" from the server, another application with connectivity to the streaming server may
|
||||
connect and uses the same client ID. The server, when detecting the duplicate client ID, will try to contact the first client
|
||||
to know if it should reject the connect request of the second client. Since the communication between the server and the
|
||||
first client is broken, the server will not get a response and therefore will replace the first client with the second one.
|
||||
|
||||
Prior to client `0.4.0` and server `0.10.0`, if the communication between the first client and server were to be restored,
|
||||
and the application would send messages, the server would accept those because the published messages client ID would be
|
||||
valid, although the client is not. With client at `0.4.0+` and server `0.10.0+`, additional information is sent with each
|
||||
message to allow the server to reject messages from a client that has been replaced by another client.
|
||||
|
||||
### Asynchronous Publishing
|
||||
|
||||
The basic publish API (`Publish(subject, payload)`) is synchronous; it does not return control to the caller until the NATS Streaming server has acknowledged receipt of the message. To accomplish this, a [NUID](https://github.com/nats-io/nuid) is generated for the message on creation, and the client library waits for a publish acknowledgement from the server with a matching NUID before it returns control to the caller, possibly with an error indicating that the operation was not successful due to some server problem or authorization error.
|
||||
|
||||
Advanced users may wish to process these publish acknowledgements manually to achieve higher publish throughput by not waiting on individual acknowledgements during the publish operation. An asynchronous publish API is provided for this purpose:
|
||||
|
||||
```go
|
||||
ackHandler := func(ackedNuid string, err error) {
|
||||
if err != nil {
|
||||
log.Printf("Warning: error publishing msg id %s: %v\n", ackedNuid, err.Error())
|
||||
} else {
|
||||
log.Printf("Received ack for msg id %s\n", ackedNuid)
|
||||
}
|
||||
}
|
||||
|
||||
nuid, err := sc.PublishAsync("foo", []byte("Hello World"), ackHandler) // returns immediately
|
||||
if err != nil {
|
||||
log.Printf("Error publishing msg %s: %v\n", nuid, err.Error())
|
||||
}
|
||||
```
|
||||
|
||||
### Message Acknowledgements and Redelivery
|
||||
|
||||
NATS Streaming offers At-Least-Once delivery semantics, meaning that once a message has been delivered to an eligible subscriber, if an acknowledgement is not received within the configured timeout interval, NATS Streaming will attempt redelivery of the message.
|
||||
This timeout interval is specified by the subscription option `AckWait`, which defaults to 30 seconds.
|
||||
|
||||
By default, messages are automatically acknowledged by the NATS Streaming client library after the subscriber's message handler is invoked. However, there may be cases in which the subscribing client wishes to accelerate or defer acknowledgement of the message.
|
||||
To do this, the client must set manual acknowledgement mode on the subscription, and invoke `Ack()` on the `Msg`. ex:
|
||||
|
||||
```go
|
||||
// Subscribe with manual ack mode, and set AckWait to 60 seconds
|
||||
aw, _ := time.ParseDuration("60s")
|
||||
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
m.Ack() // ack message before performing I/O intensive operation
|
||||
///...
|
||||
fmt.Printf("Received a message: %s\n", string(m.Data))
|
||||
}, stan.SetManualAckMode(), stan.AckWait(aw))
|
||||
```
|
||||
|
||||
## Rate limiting/matching
|
||||
|
||||
A classic problem of publish-subscribe messaging is matching the rate of message producers with the rate of message consumers.
|
||||
Message producers can often outpace the speed of the subscribers that are consuming their messages.
|
||||
This mismatch is commonly called a "fast producer/slow consumer" problem, and may result in dramatic resource utilization spikes in the underlying messaging system as it tries to buffer messages until the slow consumer(s) can catch up.
|
||||
|
||||
### Publisher rate limiting
|
||||
|
||||
NATS Streaming provides a connection option called `MaxPubAcksInflight` that effectively limits the number of unacknowledged messages that a publisher may have in-flight at any given time. When this maximum is reached, further `PublishAsync()` calls will block until the number of unacknowledged messages falls below the specified limit. ex:
|
||||
|
||||
```go
|
||||
sc, _ := stan.Connect(clusterID, clientID, MaxPubAcksInflight(25))
|
||||
|
||||
ah := func(nuid string, err error) {
|
||||
// process the ack
|
||||
...
|
||||
}
|
||||
|
||||
for i := 1; i < 1000; i++ {
|
||||
// If the server is unable to keep up with the publisher, the number of outstanding acks will eventually
|
||||
// reach the max and this call will block
|
||||
guid, _ := sc.PublishAsync("foo", []byte("Hello World"), ah)
|
||||
}
|
||||
```
|
||||
|
||||
### Subscriber rate limiting
|
||||
|
||||
Rate limiting may also be accomplished on the subscriber side, on a per-subscription basis, using a subscription option called `MaxInflight`.
|
||||
This option specifies the maximum number of outstanding acknowledgements (messages that have been delivered but not acknowledged) that NATS Streaming will allow for a given subscription.
|
||||
When this limit is reached, NATS Streaming will suspend delivery of messages to this subscription until the number of unacknowledged messages falls below the specified limit. ex:
|
||||
|
||||
```go
|
||||
// Subscribe with manual ack mode and a max in-flight limit of 25
|
||||
sc.Subscribe("foo", func(m *stan.Msg) {
|
||||
fmt.Printf("Received message #: %s\n", string(m.Data))
|
||||
...
|
||||
// Does not ack, or takes a very long time to ack
|
||||
...
|
||||
// Message delivery will suspend when the number of unacknowledged messages reaches 25
|
||||
}, stan.SetManualAckMode(), stan.MaxInflight(25))
|
||||
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Unless otherwise noted, the NATS source files are distributed
|
||||
under the Apache Version 2.0 license found in the LICENSE file.
|
7
gateway/vendor/github.com/nats-io/stan.go/go.mod
generated
vendored
Normal file
7
gateway/vendor/github.com/nats-io/stan.go/go.mod
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/nats-io/stan.go
|
||||
|
||||
require (
|
||||
github.com/gogo/protobuf v1.2.1
|
||||
github.com/nats-io/nats.go v1.9.1
|
||||
github.com/nats-io/nuid v1.0.1
|
||||
)
|
20
gateway/vendor/github.com/nats-io/stan.go/go.sum
generated
vendored
Normal file
20
gateway/vendor/github.com/nats-io/stan.go/go.sum
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/nats-io/jwt v0.3.0 h1:xdnzwFETV++jNc4W1mw//qFyJGb2ABOombmZJQS4+Qo=
|
||||
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
|
||||
github.com/nats-io/nats.go v1.9.1 h1:ik3HbLhZ0YABLto7iX80pZLPw/6dx3T+++MZJwLnMrQ=
|
||||
github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w=
|
||||
github.com/nats-io/nkeys v0.1.0 h1:qMd4+pRHgdr1nAClu+2h/2a5F2TmKcCzjCDazVgRoX4=
|
||||
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
3412
gateway/vendor/github.com/nats-io/stan.go/pb/protocol.pb.go
generated
vendored
Normal file
3412
gateway/vendor/github.com/nats-io/stan.go/pb/protocol.pb.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
146
gateway/vendor/github.com/nats-io/stan.go/pb/protocol.proto
generated
vendored
Normal file
146
gateway/vendor/github.com/nats-io/stan.go/pb/protocol.proto
generated
vendored
Normal file
@ -0,0 +1,146 @@
|
||||
// Copyright 2016-2018 The NATS Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Uses https://github.com/gogo/protobuf
|
||||
// compiled via `protoc -I=. -I=$GOPATH/src --gogofaster_out=. protocol.proto`
|
||||
|
||||
syntax = "proto3";
|
||||
package pb;
|
||||
|
||||
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
|
||||
|
||||
option (gogoproto.marshaler_all) = true;
|
||||
option (gogoproto.sizer_all) = true;
|
||||
option (gogoproto.unmarshaler_all) = true;
|
||||
option (gogoproto.goproto_getters_all) = false;
|
||||
|
||||
// How messages are delivered to the STAN cluster
|
||||
message PubMsg {
|
||||
string clientID = 1; // ClientID
|
||||
string guid = 2; // guid
|
||||
string subject = 3; // subject
|
||||
string reply = 4; // optional reply
|
||||
bytes data = 5; // payload
|
||||
bytes connID = 6; // Connection ID. For servers that know about this field, clientID can be omitted
|
||||
|
||||
bytes sha256 = 10; // optional sha256 of data
|
||||
}
|
||||
|
||||
// Used to ACK to publishers
|
||||
message PubAck {
|
||||
string guid = 1; // guid
|
||||
string error = 2; // err string, empty/omitted if no error
|
||||
}
|
||||
|
||||
// Msg struct. Sequence is assigned for global ordering by
|
||||
// the cluster after the publisher has been acknowledged.
|
||||
message MsgProto {
|
||||
uint64 sequence = 1; // globally ordered sequence number for the subject's channel
|
||||
string subject = 2; // subject
|
||||
string reply = 3; // optional reply
|
||||
bytes data = 4; // payload
|
||||
int64 timestamp = 5; // received timestamp
|
||||
bool redelivered = 6; // Flag specifying if the message is being redelivered
|
||||
uint32 redeliveryCount = 7; // Number of times the message has been redelivered (count currently not persisted)
|
||||
|
||||
uint32 CRC32 = 10; // optional IEEE CRC32
|
||||
}
|
||||
|
||||
// Ack will deliver an ack for a delivered msg.
|
||||
message Ack {
|
||||
string subject = 1; // Subject
|
||||
uint64 sequence = 2; // Sequence to acknowledge
|
||||
}
|
||||
|
||||
// Connection Request
|
||||
message ConnectRequest {
|
||||
string clientID = 1; // Client name/identifier.
|
||||
string heartbeatInbox = 2; // Inbox for server initiated heartbeats.
|
||||
int32 protocol = 3; // Protocol the client is at.
|
||||
bytes connID = 4; // Connection ID, a way to uniquely identify a connection (no connection should ever have the same)
|
||||
int32 pingInterval = 5; // Interval at which client wishes to send PINGs (expressed in seconds).
|
||||
int32 pingMaxOut = 6; // Maximum number of PINGs without a response after which the connection can be considered lost.
|
||||
}
|
||||
|
||||
// Response to a client connect
|
||||
message ConnectResponse {
|
||||
string pubPrefix = 1; // Prefix to use when publishing to this STAN cluster
|
||||
string subRequests = 2; // Subject to use for subscription requests
|
||||
string unsubRequests = 3; // Subject to use for unsubscribe requests
|
||||
string closeRequests = 4; // Subject for closing the stan connection
|
||||
string error = 5; // err string, empty/omitted if no error
|
||||
string subCloseRequests = 6; // Subject to use for subscription close requests
|
||||
string pingRequests = 7; // Subject to use for PING requests
|
||||
int32 pingInterval = 8; // Interval at which client should send PINGs (expressed in seconds).
|
||||
int32 pingMaxOut = 9; // Maximum number of PINGs without a response after which the connection can be considered lost
|
||||
int32 protocol = 10; // Protocol version the server is at
|
||||
|
||||
string publicKey = 100; // Possibly used to sign acks, etc.
|
||||
}
|
||||
|
||||
// PING from client to server
|
||||
message Ping {
|
||||
bytes connID = 1; // Connection ID
|
||||
}
|
||||
|
||||
// PING response from the server
|
||||
message PingResponse {
|
||||
string error = 1; // Error string, empty/omitted if no error
|
||||
}
|
||||
|
||||
// Enum for start position type.
|
||||
enum StartPosition {
|
||||
NewOnly = 0;
|
||||
LastReceived = 1;
|
||||
TimeDeltaStart = 2;
|
||||
SequenceStart = 3;
|
||||
First = 4;
|
||||
}
|
||||
|
||||
// Protocol for a client to subscribe
|
||||
message SubscriptionRequest {
|
||||
string clientID = 1; // ClientID
|
||||
string subject = 2; // Formal subject to subscribe to, e.g. foo.bar
|
||||
string qGroup = 3; // Optional queue group
|
||||
string inbox = 4; // Inbox subject to deliver messages on
|
||||
int32 maxInFlight = 5; // Maximum inflight messages without an ack allowed
|
||||
int32 ackWaitInSecs = 6; // Timeout for receiving an ack from the client
|
||||
string durableName = 7; // Optional durable name which survives client restarts
|
||||
StartPosition startPosition = 10; // Start position
|
||||
uint64 startSequence = 11; // Optional start sequence number
|
||||
int64 startTimeDelta = 12; // Optional start time
|
||||
}
|
||||
|
||||
// Response for SubscriptionRequest and UnsubscribeRequests
|
||||
message SubscriptionResponse {
|
||||
string ackInbox = 2; // ackInbox for sending acks
|
||||
string error = 3; // err string, empty/omitted if no error
|
||||
}
|
||||
|
||||
// Protocol for a clients to unsubscribe. Will return a SubscriptionResponse
|
||||
message UnsubscribeRequest {
|
||||
string clientID = 1; // ClientID
|
||||
string subject = 2; // subject for the subscription
|
||||
string inbox = 3; // Inbox subject to identify subscription
|
||||
string durableName = 4; // Optional durable name which survives client restarts
|
||||
}
|
||||
|
||||
// Protocol for a client to close a connection
|
||||
message CloseRequest {
|
||||
string clientID = 1; // Client name provided to Connect() requests
|
||||
}
|
||||
|
||||
// Response for CloseRequest
|
||||
message CloseResponse {
|
||||
string error = 1; // err string, empty/omitted if no error
|
||||
}
|
874
gateway/vendor/github.com/nats-io/stan.go/stan.go
generated
vendored
Normal file
874
gateway/vendor/github.com/nats-io/stan.go/stan.go
generated
vendored
Normal file
@ -0,0 +1,874 @@
|
||||
// Copyright 2016-2019 The NATS Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package stan is a Go client for the NATS Streaming messaging system (https://nats.io).
|
||||
package stan
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nuid"
|
||||
"github.com/nats-io/stan.go/pb"
|
||||
)
|
||||
|
||||
// Version is the NATS Streaming Go Client version
|
||||
const Version = "0.6.0"
|
||||
|
||||
const (
|
||||
// DefaultNatsURL is the default URL the client connects to
|
||||
DefaultNatsURL = "nats://127.0.0.1:4222"
|
||||
// DefaultConnectWait is the default timeout used for the connect operation
|
||||
DefaultConnectWait = 2 * time.Second
|
||||
// DefaultDiscoverPrefix is the prefix subject used to connect to the NATS Streaming server
|
||||
DefaultDiscoverPrefix = "_STAN.discover"
|
||||
// DefaultACKPrefix is the prefix subject used to send ACKs to the NATS Streaming server
|
||||
DefaultACKPrefix = "_STAN.acks"
|
||||
// DefaultMaxPubAcksInflight is the default maximum number of published messages
|
||||
// without outstanding ACKs from the server
|
||||
DefaultMaxPubAcksInflight = 16384
|
||||
// DefaultPingInterval is the default interval (in seconds) at which a connection sends a PING to the server
|
||||
DefaultPingInterval = 5
|
||||
// DefaultPingMaxOut is the number of PINGs without a response before the connection is considered lost.
|
||||
DefaultPingMaxOut = 3
|
||||
)
|
||||
|
||||
// Conn represents a connection to the NATS Streaming subsystem. It can Publish and
|
||||
// Subscribe to messages within the NATS Streaming cluster.
|
||||
// The connection is safe to use in multiple Go routines concurrently.
|
||||
type Conn interface {
|
||||
// Publish will publish to the cluster and wait for an ACK.
|
||||
Publish(subject string, data []byte) error
|
||||
|
||||
// PublishAsync will publish to the cluster and asynchronously process
|
||||
// the ACK or error state. It will return the GUID for the message being sent.
|
||||
PublishAsync(subject string, data []byte, ah AckHandler) (string, error)
|
||||
|
||||
// Subscribe will perform a subscription with the given options to the cluster.
|
||||
//
|
||||
// If no option is specified, DefaultSubscriptionOptions are used. The default start
|
||||
// position is to receive new messages only (messages published after the subscription is
|
||||
// registered in the cluster).
|
||||
Subscribe(subject string, cb MsgHandler, opts ...SubscriptionOption) (Subscription, error)
|
||||
|
||||
// QueueSubscribe will perform a queue subscription with the given options to the cluster.
|
||||
//
|
||||
// If no option is specified, DefaultSubscriptionOptions are used. The default start
|
||||
// position is to receive new messages only (messages published after the subscription is
|
||||
// registered in the cluster).
|
||||
QueueSubscribe(subject, qgroup string, cb MsgHandler, opts ...SubscriptionOption) (Subscription, error)
|
||||
|
||||
// Close a connection to the cluster.
|
||||
//
|
||||
// If there are active subscriptions at the time of the close, they are implicitly closed
|
||||
// (not unsubscribed) by the cluster. This means that durable subscriptions are maintained.
|
||||
//
|
||||
// The wait on asynchronous publish calls are canceled and ErrConnectionClosed will be
|
||||
// reported to the registered AckHandler. It is possible that the cluster received and
|
||||
// persisted these messages.
|
||||
//
|
||||
// If a NATS connection is provided as an option to the Connect() call, the NATS
|
||||
// connection is NOT closed when this call is invoked. This connection needs to be
|
||||
// managed by the application.
|
||||
Close() error
|
||||
|
||||
// NatsConn returns the underlying NATS conn. Use this with care. For
|
||||
// example, closing the wrapped NATS conn will put the NATS Streaming Conn
|
||||
// in an invalid state.
|
||||
NatsConn() *nats.Conn
|
||||
}
|
||||
|
||||
const (
|
||||
// Client send connID in ConnectRequest and PubMsg, and server
|
||||
// listens and responds to client PINGs. The validity of the
|
||||
// connection (based on connID) is checked on incoming PINGs.
|
||||
protocolOne = int32(1)
|
||||
)
|
||||
|
||||
// Errors
|
||||
var (
|
||||
ErrConnectReqTimeout = errors.New("stan: connect request timeout (possibly wrong cluster ID?)")
|
||||
ErrCloseReqTimeout = errors.New("stan: close request timeout")
|
||||
ErrSubReqTimeout = errors.New("stan: subscribe request timeout")
|
||||
ErrUnsubReqTimeout = errors.New("stan: unsubscribe request timeout")
|
||||
ErrConnectionClosed = errors.New("stan: connection closed")
|
||||
ErrTimeout = errors.New("stan: publish ack timeout")
|
||||
ErrBadAck = errors.New("stan: malformed ack")
|
||||
ErrBadSubscription = errors.New("stan: invalid subscription")
|
||||
ErrBadConnection = errors.New("stan: invalid connection")
|
||||
ErrManualAck = errors.New("stan: cannot manually ack in auto-ack mode")
|
||||
ErrNilMsg = errors.New("stan: nil message")
|
||||
ErrNoServerSupport = errors.New("stan: not supported by server")
|
||||
ErrMaxPings = errors.New("stan: connection lost due to PING failure")
|
||||
)
|
||||
|
||||
var testAllowMillisecInPings = false
|
||||
|
||||
// AckHandler is used for Async Publishing to provide status of the ack.
|
||||
// The func will be passed the GUID and any error state. No error means the
|
||||
// message was successfully received by NATS Streaming.
|
||||
type AckHandler func(string, error)
|
||||
|
||||
// ConnectionLostHandler is used to be notified if the Streaming connection
|
||||
// is closed due to unexpected errors.
|
||||
type ConnectionLostHandler func(Conn, error)
|
||||
|
||||
// Options can be used to a create a customized connection.
|
||||
type Options struct {
|
||||
// NatsURL is an URL (or comma separated list of URLs) to a node or nodes
|
||||
// in the cluster.
|
||||
NatsURL string
|
||||
|
||||
// NatsConn is a user provided low-level NATS connection that the streaming
|
||||
// connection will use to communicate with the cluster. When set, closing
|
||||
// the NATS streaming connection does NOT close this NATS connection.
|
||||
// It is the responsibility of the application to manage the lifetime of
|
||||
// the supplied NATS connection.
|
||||
NatsConn *nats.Conn
|
||||
|
||||
// ConnectTimeout is the timeout for the initial Connect(). This value is also
|
||||
// used for some of the internal request/replies with the cluster.
|
||||
ConnectTimeout time.Duration
|
||||
|
||||
// AckTimeout is how long to wait when a message is published for an ACK from
|
||||
// the cluster. If the library does not receive an ACK after this timeout,
|
||||
// the Publish() call (or the AckHandler) will return ErrTimeout.
|
||||
AckTimeout time.Duration
|
||||
|
||||
// DiscoverPrefix is the prefix connect requests are sent to for this cluster.
|
||||
// The default is "_STAN.discover".
|
||||
DiscoverPrefix string
|
||||
|
||||
// MaxPubAcksInflight specifies how many messages can be published without
|
||||
// getting ACKs back from the cluster before the Publish() or PublishAsync()
|
||||
// calls block.
|
||||
MaxPubAcksInflight int
|
||||
|
||||
// DEPRECATED: Please use PingInterval instead
|
||||
PingIterval int
|
||||
|
||||
// PingInterval is the interval at which client sends PINGs to the server
|
||||
// to detect the loss of a connection.
|
||||
PingInterval int
|
||||
|
||||
// PingMaxOut specifies the maximum number of PINGs without a corresponding
|
||||
// PONG before declaring the connection permanently lost.
|
||||
PingMaxOut int
|
||||
|
||||
// ConnectionLostCB specifies the handler to be invoked when the connection
|
||||
// is permanently lost.
|
||||
ConnectionLostCB ConnectionLostHandler
|
||||
}
|
||||
|
||||
// GetDefaultOptions returns default configuration options for the client.
|
||||
func GetDefaultOptions() Options {
|
||||
return Options{
|
||||
NatsURL: DefaultNatsURL,
|
||||
ConnectTimeout: DefaultConnectWait,
|
||||
AckTimeout: DefaultAckWait,
|
||||
DiscoverPrefix: DefaultDiscoverPrefix,
|
||||
MaxPubAcksInflight: DefaultMaxPubAcksInflight,
|
||||
PingInterval: DefaultPingInterval,
|
||||
PingMaxOut: DefaultPingMaxOut,
|
||||
}
|
||||
}
|
||||
|
||||
// DEPRECATED: Use GetDefaultOptions() instead.
|
||||
// DefaultOptions is not safe for use by multiple clients.
|
||||
// For details see https://github.com/nats-io/nats.go/issues/308.
|
||||
// DefaultOptions are the NATS Streaming client's default options
|
||||
var DefaultOptions = GetDefaultOptions()
|
||||
|
||||
// Option is a function on the options for a connection.
|
||||
type Option func(*Options) error
|
||||
|
||||
// NatsURL is an Option to set the URL the client should connect to.
|
||||
// The url can contain username/password semantics. e.g. nats://derek:pass@localhost:4222
|
||||
// Comma separated arrays are also supported, e.g. urlA, urlB.
|
||||
func NatsURL(u string) Option {
|
||||
return func(o *Options) error {
|
||||
o.NatsURL = u
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ConnectWait is an Option to set the timeout for establishing a connection.
|
||||
func ConnectWait(t time.Duration) Option {
|
||||
return func(o *Options) error {
|
||||
o.ConnectTimeout = t
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// PubAckWait is an Option to set the timeout for waiting for an ACK for a
|
||||
// published message.
|
||||
func PubAckWait(t time.Duration) Option {
|
||||
return func(o *Options) error {
|
||||
o.AckTimeout = t
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MaxPubAcksInflight is an Option to set the maximum number of published
|
||||
// messages without outstanding ACKs from the server.
|
||||
func MaxPubAcksInflight(max int) Option {
|
||||
return func(o *Options) error {
|
||||
o.MaxPubAcksInflight = max
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// NatsConn is an Option to set the underlying NATS connection to be used
|
||||
// by a streaming connection object. When such option is set, closing the
|
||||
// streaming connection does not close the provided NATS connection.
|
||||
func NatsConn(nc *nats.Conn) Option {
|
||||
return func(o *Options) error {
|
||||
o.NatsConn = nc
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Pings is an Option to set the ping interval and max out values.
|
||||
// The interval needs to be at least 1 and represents the number
|
||||
// of seconds.
|
||||
// The maxOut needs to be at least 2, since the count of sent PINGs
|
||||
// increase whenever a PING is sent and reset to 0 when a response
|
||||
// is received. Setting to 1 would cause the library to close the
|
||||
// connection right away.
|
||||
func Pings(interval, maxOut int) Option {
|
||||
return func(o *Options) error {
|
||||
// For tests, we may pass negative value that will be interpreted
|
||||
// by the library as milliseconds. If this test boolean is set,
|
||||
// do not check values.
|
||||
if !testAllowMillisecInPings {
|
||||
if interval < 1 || maxOut < 2 {
|
||||
return fmt.Errorf("invalid ping values: interval=%v (min>0) maxOut=%v (min=2)", interval, maxOut)
|
||||
}
|
||||
}
|
||||
o.PingInterval = interval
|
||||
o.PingMaxOut = maxOut
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// SetConnectionLostHandler is an Option to set the connection lost handler.
|
||||
// This callback will be invoked should the client permanently lose
|
||||
// contact with the server (or another client replaces it while being
|
||||
// disconnected). The callback will not be invoked on normal Conn.Close().
|
||||
func SetConnectionLostHandler(handler ConnectionLostHandler) Option {
|
||||
return func(o *Options) error {
|
||||
o.ConnectionLostCB = handler
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// A conn represents a bare connection to a stan cluster.
|
||||
type conn struct {
|
||||
sync.RWMutex
|
||||
clientID string
|
||||
connID []byte // This is a NUID that uniquely identify connections.
|
||||
pubPrefix string // Publish prefix set by stan, append our subject.
|
||||
subRequests string // Subject to send subscription requests.
|
||||
unsubRequests string // Subject to send unsubscribe requests.
|
||||
subCloseRequests string // Subject to send subscription close requests.
|
||||
closeRequests string // Subject to send close requests.
|
||||
ackSubject string // publish acks
|
||||
ackSubscription *nats.Subscription
|
||||
hbSubscription *nats.Subscription
|
||||
subMap map[string]*subscription
|
||||
pubAckMap map[string]*ack
|
||||
pubAckChan chan (struct{})
|
||||
pubAckCloseChan chan (struct{})
|
||||
opts Options
|
||||
nc *nats.Conn
|
||||
ncOwned bool // NATS Streaming created the connection, so needs to close it.
|
||||
pubNUID *nuid.NUID // NUID generator for published messages.
|
||||
connLostCB ConnectionLostHandler
|
||||
closed bool
|
||||
ping pingInfo
|
||||
}
|
||||
|
||||
// Holds all field related to the client-to-server pings
|
||||
type pingInfo struct {
|
||||
mu sync.Mutex
|
||||
sub *nats.Subscription
|
||||
timer *time.Timer
|
||||
proto []byte
|
||||
requests string
|
||||
inbox string
|
||||
interval time.Duration
|
||||
maxOut int
|
||||
out int
|
||||
}
|
||||
|
||||
// Closure for ack contexts.
|
||||
type ack struct {
|
||||
t *time.Timer
|
||||
ah AckHandler
|
||||
ch chan error
|
||||
}
|
||||
|
||||
// Connect will form a connection to the NATS Streaming subsystem.
|
||||
// Note that clientID can contain only alphanumeric and `-` or `_` characters.
|
||||
func Connect(stanClusterID, clientID string, options ...Option) (Conn, error) {
|
||||
// Process Options
|
||||
c := conn{
|
||||
clientID: clientID,
|
||||
opts: DefaultOptions,
|
||||
connID: []byte(nuid.Next()),
|
||||
pubNUID: nuid.New(),
|
||||
pubAckMap: make(map[string]*ack),
|
||||
pubAckCloseChan: make(chan struct{}),
|
||||
subMap: make(map[string]*subscription),
|
||||
}
|
||||
for _, opt := range options {
|
||||
if err := opt(&c.opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// Check if the user has provided a connection as an option
|
||||
c.nc = c.opts.NatsConn
|
||||
// Create a NATS connection if it doesn't exist.
|
||||
if c.nc == nil {
|
||||
// We will set the max reconnect attempts to -1 (infinite)
|
||||
// and the reconnect buffer to -1 to prevent any buffering
|
||||
// (which may cause a published message to be flushed on
|
||||
// reconnect while the API may have returned an error due
|
||||
// to PubAck timeout.
|
||||
nc, err := nats.Connect(c.opts.NatsURL,
|
||||
nats.Name(clientID),
|
||||
nats.MaxReconnects(-1),
|
||||
nats.ReconnectBufSize(-1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.nc = nc
|
||||
c.ncOwned = true
|
||||
} else if !c.nc.IsConnected() {
|
||||
// Bail if the custom NATS connection is disconnected
|
||||
return nil, ErrBadConnection
|
||||
}
|
||||
|
||||
// Create a heartbeat inbox
|
||||
hbInbox := nats.NewInbox()
|
||||
var err error
|
||||
if c.hbSubscription, err = c.nc.Subscribe(hbInbox, c.processHeartBeat); err != nil {
|
||||
c.failConnect(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Prepare a subscription on ping responses, even if we are not
|
||||
// going to need it, so that if that fails, it fails before initiating
|
||||
// a connection.
|
||||
p := &c.ping
|
||||
if p.sub, err = c.nc.Subscribe(nats.NewInbox(), c.processPingResponse); err != nil {
|
||||
c.failConnect(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Send Request to discover the cluster
|
||||
discoverSubject := c.opts.DiscoverPrefix + "." + stanClusterID
|
||||
req := &pb.ConnectRequest{
|
||||
ClientID: clientID,
|
||||
HeartbeatInbox: hbInbox,
|
||||
ConnID: c.connID,
|
||||
Protocol: protocolOne,
|
||||
PingInterval: int32(c.opts.PingInterval),
|
||||
PingMaxOut: int32(c.opts.PingMaxOut),
|
||||
}
|
||||
b, _ := req.Marshal()
|
||||
reply, err := c.nc.Request(discoverSubject, b, c.opts.ConnectTimeout)
|
||||
if err != nil {
|
||||
c.failConnect(err)
|
||||
if err == nats.ErrTimeout {
|
||||
return nil, ErrConnectReqTimeout
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
// Process the response, grab server pubPrefix
|
||||
cr := &pb.ConnectResponse{}
|
||||
err = cr.Unmarshal(reply.Data)
|
||||
if err != nil {
|
||||
c.failConnect(err)
|
||||
return nil, err
|
||||
}
|
||||
if cr.Error != "" {
|
||||
c.failConnect(err)
|
||||
return nil, errors.New(cr.Error)
|
||||
}
|
||||
|
||||
// Past this point, we need to call Close() on error because the server
|
||||
// has accepted our connection.
|
||||
|
||||
// Capture cluster configuration endpoints to publish and subscribe/unsubscribe.
|
||||
c.pubPrefix = cr.PubPrefix
|
||||
c.subRequests = cr.SubRequests
|
||||
c.unsubRequests = cr.UnsubRequests
|
||||
c.subCloseRequests = cr.SubCloseRequests
|
||||
c.closeRequests = cr.CloseRequests
|
||||
|
||||
// Setup the ACK subscription
|
||||
c.ackSubject = DefaultACKPrefix + "." + nuid.Next()
|
||||
if c.ackSubscription, err = c.nc.Subscribe(c.ackSubject, c.processAck); err != nil {
|
||||
c.Close()
|
||||
return nil, err
|
||||
}
|
||||
c.ackSubscription.SetPendingLimits(-1, -1)
|
||||
|
||||
c.pubAckChan = make(chan struct{}, c.opts.MaxPubAcksInflight)
|
||||
|
||||
// Capture the connection error cb
|
||||
c.connLostCB = c.opts.ConnectionLostCB
|
||||
|
||||
unsubPingSub := true
|
||||
// Do this with servers which are at least at protocolOne.
|
||||
if cr.Protocol >= protocolOne {
|
||||
// Note that in the future server may override client ping
|
||||
// interval value sent in ConnectRequest, so use the
|
||||
// value in ConnectResponse to decide if we send PINGs
|
||||
// and at what interval.
|
||||
// In tests, the interval could be negative to indicate
|
||||
// milliseconds.
|
||||
if cr.PingInterval != 0 {
|
||||
unsubPingSub = false
|
||||
|
||||
// These will be immutable.
|
||||
p.requests = cr.PingRequests
|
||||
p.inbox = p.sub.Subject
|
||||
// In test, it is possible that we get a negative value
|
||||
// to represent milliseconds.
|
||||
if testAllowMillisecInPings && cr.PingInterval < 0 {
|
||||
p.interval = time.Duration(cr.PingInterval*-1) * time.Millisecond
|
||||
} else {
|
||||
// PingInterval is otherwise assumed to be in seconds.
|
||||
p.interval = time.Duration(cr.PingInterval) * time.Second
|
||||
}
|
||||
p.maxOut = int(cr.PingMaxOut)
|
||||
p.proto, _ = (&pb.Ping{ConnID: c.connID}).Marshal()
|
||||
// Set the timer now that we are set. Use lock to create
|
||||
// synchronization point.
|
||||
p.mu.Lock()
|
||||
p.timer = time.AfterFunc(p.interval, c.pingServer)
|
||||
p.mu.Unlock()
|
||||
}
|
||||
}
|
||||
if unsubPingSub {
|
||||
p.sub.Unsubscribe()
|
||||
p.sub = nil
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// Invoked on a failed connect.
|
||||
// Perform appropriate cleanup operations but do not attempt to send
|
||||
// a close request.
|
||||
func (sc *conn) failConnect(err error) {
|
||||
sc.cleanupOnClose(err)
|
||||
if sc.nc != nil && sc.ncOwned {
|
||||
sc.nc.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Sends a PING (containing the connection's ID) to the server at intervals
|
||||
// specified by PingInterval option when connection is created.
|
||||
// Everytime a PING is sent, the number of outstanding PINGs is increased.
|
||||
// If the total number is > than the PingMaxOut option, then the connection
|
||||
// is closed, and connection error callback invoked if one was specified.
|
||||
func (sc *conn) pingServer() {
|
||||
p := &sc.ping
|
||||
p.mu.Lock()
|
||||
// In case the timer fired while we were stopping it.
|
||||
if p.timer == nil {
|
||||
p.mu.Unlock()
|
||||
return
|
||||
}
|
||||
p.out++
|
||||
if p.out > p.maxOut {
|
||||
p.mu.Unlock()
|
||||
sc.closeDueToPing(ErrMaxPings)
|
||||
return
|
||||
}
|
||||
p.timer.Reset(p.interval)
|
||||
p.mu.Unlock()
|
||||
// Send the PING now. If the NATS connection is reported closed, we are done.
|
||||
// sc.nc is immutable and never nil, even if connection is closed.
|
||||
if err := sc.nc.PublishRequest(p.requests, p.inbox, p.proto); err == nats.ErrConnectionClosed {
|
||||
sc.closeDueToPing(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Receives PING responses from the server.
|
||||
// If the response contains an error message, the connection is closed
|
||||
// and the connection error callback is invoked (if one is specified).
|
||||
// If no error, the number of ping out is reset to 0. There is no
|
||||
// decrement by one since for a given PING, the client may received
|
||||
// many responses when servers are running in channel partitioning mode.
|
||||
// Regardless, any positive response from the server ought to signal
|
||||
// that the connection is ok.
|
||||
func (sc *conn) processPingResponse(m *nats.Msg) {
|
||||
// No data means OK (we don't have to call Unmarshal)
|
||||
if len(m.Data) > 0 {
|
||||
pingResp := &pb.PingResponse{}
|
||||
if err := pingResp.Unmarshal(m.Data); err != nil {
|
||||
return
|
||||
}
|
||||
if pingResp.Error != "" {
|
||||
sc.closeDueToPing(errors.New(pingResp.Error))
|
||||
return
|
||||
}
|
||||
}
|
||||
// Do not attempt to decrement, simply reset to 0.
|
||||
p := &sc.ping
|
||||
p.mu.Lock()
|
||||
p.out = 0
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
// Closes a connection and invoke the connection error callback if one
|
||||
// was registered when the connection was created.
|
||||
func (sc *conn) closeDueToPing(err error) {
|
||||
sc.Lock()
|
||||
if sc.closed {
|
||||
sc.Unlock()
|
||||
return
|
||||
}
|
||||
// Stop timer, unsubscribe, fail the pubs, etc..
|
||||
sc.cleanupOnClose(err)
|
||||
// No need to send Close protocol, so simply close the underlying
|
||||
// NATS connection (if we own it, and if not already closed)
|
||||
if sc.ncOwned && !sc.nc.IsClosed() {
|
||||
sc.nc.Close()
|
||||
}
|
||||
// Mark this streaming connection as closed.
|
||||
sc.closed = true
|
||||
// Capture callback (even though this is immutable).
|
||||
cb := sc.connLostCB
|
||||
sc.Unlock()
|
||||
if cb != nil {
|
||||
// Execute in separate go routine.
|
||||
go cb(sc, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Do some cleanup when connection is lost or closed.
|
||||
// Connection lock is held on entry, and sc.nc is guaranteed not to be nil.
|
||||
func (sc *conn) cleanupOnClose(err error) {
|
||||
p := &sc.ping
|
||||
p.mu.Lock()
|
||||
if p.timer != nil {
|
||||
p.timer.Stop()
|
||||
p.timer = nil
|
||||
}
|
||||
p.mu.Unlock()
|
||||
|
||||
// Unsubscribe only if the NATS connection is not already closed
|
||||
// and we don't own it (otherwise connection is going to be closed
|
||||
// so no need for explicit unsubscribe).
|
||||
if !sc.ncOwned && !sc.nc.IsClosed() {
|
||||
if sc.hbSubscription != nil {
|
||||
sc.hbSubscription.Unsubscribe()
|
||||
}
|
||||
if p.sub != nil {
|
||||
p.sub.Unsubscribe()
|
||||
}
|
||||
if sc.ackSubscription != nil {
|
||||
sc.ackSubscription.Unsubscribe()
|
||||
}
|
||||
}
|
||||
|
||||
// Fail all pending pubs
|
||||
if len(sc.pubAckMap) > 0 {
|
||||
// Collect only the ones that have a timer that can be stopped.
|
||||
// All others will be handled either in publishAsync() or their
|
||||
// timer has already fired.
|
||||
acks := map[string]*ack{}
|
||||
for guid, pubAck := range sc.pubAckMap {
|
||||
if pubAck.t != nil && pubAck.t.Stop() {
|
||||
delete(sc.pubAckMap, guid)
|
||||
acks[guid] = pubAck
|
||||
}
|
||||
}
|
||||
// If we collected any, start a go routine that will do the job.
|
||||
// We can't do it in place in case user's ackHandler uses the connection.
|
||||
if len(acks) > 0 {
|
||||
go func() {
|
||||
for guid, a := range acks {
|
||||
if a.ah != nil {
|
||||
a.ah(guid, ErrConnectionClosed)
|
||||
} else if a.ch != nil {
|
||||
a.ch <- ErrConnectionClosed
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
// Prevent publish calls that have passed the connection close check but
|
||||
// not yet send to pubAckChan to be possibly blocked.
|
||||
close(sc.pubAckCloseChan)
|
||||
}
|
||||
|
||||
// Close a connection to the stan system.
|
||||
func (sc *conn) Close() error {
|
||||
sc.Lock()
|
||||
defer sc.Unlock()
|
||||
|
||||
if sc.closed {
|
||||
// We are already closed.
|
||||
return nil
|
||||
}
|
||||
// Signals we are closed.
|
||||
sc.closed = true
|
||||
|
||||
// Capture for NATS calls below.
|
||||
if sc.ncOwned {
|
||||
defer sc.nc.Close()
|
||||
}
|
||||
|
||||
// Now close ourselves.
|
||||
sc.cleanupOnClose(ErrConnectionClosed)
|
||||
|
||||
req := &pb.CloseRequest{ClientID: sc.clientID}
|
||||
b, _ := req.Marshal()
|
||||
reply, err := sc.nc.Request(sc.closeRequests, b, sc.opts.ConnectTimeout)
|
||||
if err != nil {
|
||||
if err == nats.ErrTimeout {
|
||||
return ErrCloseReqTimeout
|
||||
}
|
||||
return err
|
||||
}
|
||||
cr := &pb.CloseResponse{}
|
||||
err = cr.Unmarshal(reply.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cr.Error != "" {
|
||||
return errors.New(cr.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NatsConn returns the underlying NATS conn. Use this with care. For example,
|
||||
// closing the wrapped NATS conn will put the NATS Streaming Conn in an invalid
|
||||
// state.
|
||||
func (sc *conn) NatsConn() *nats.Conn {
|
||||
sc.RLock()
|
||||
nc := sc.nc
|
||||
if sc.closed {
|
||||
nc = nil
|
||||
}
|
||||
sc.RUnlock()
|
||||
return nc
|
||||
}
|
||||
|
||||
// Process a heartbeat from the NATS Streaming cluster
|
||||
func (sc *conn) processHeartBeat(m *nats.Msg) {
|
||||
// No payload assumed, just reply.
|
||||
// sc.nc is immutable and never nil, even if connection is closed.
|
||||
sc.nc.Publish(m.Reply, nil)
|
||||
}
|
||||
|
||||
// Process an ack from the NATS Streaming cluster
|
||||
func (sc *conn) processAck(m *nats.Msg) {
|
||||
pa := &pb.PubAck{}
|
||||
err := pa.Unmarshal(m.Data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error during ack unmarshal: %v", err))
|
||||
}
|
||||
|
||||
// Remove
|
||||
a := sc.removeAck(pa.Guid)
|
||||
if a != nil {
|
||||
// Capture error if it exists.
|
||||
if pa.Error != "" {
|
||||
err = errors.New(pa.Error)
|
||||
}
|
||||
if a.ah != nil {
|
||||
// Perform the ackHandler callback
|
||||
a.ah(pa.Guid, err)
|
||||
} else if a.ch != nil {
|
||||
// Send to channel directly
|
||||
a.ch <- err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Publish will publish to the cluster and wait for an ACK.
|
||||
func (sc *conn) Publish(subject string, data []byte) error {
|
||||
// Need to make this a buffered channel of 1 in case
|
||||
// a publish call is blocked in pubAckChan but cleanupOnClose()
|
||||
// is trying to push the error to this channel.
|
||||
ch := make(chan error, 1)
|
||||
_, err := sc.publishAsync(subject, data, nil, ch)
|
||||
if err == nil {
|
||||
err = <-ch
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// PublishAsync will publish to the cluster on pubPrefix+subject and asynchronously
|
||||
// process the ACK or error state. It will return the GUID for the message being sent.
|
||||
func (sc *conn) PublishAsync(subject string, data []byte, ah AckHandler) (string, error) {
|
||||
return sc.publishAsync(subject, data, ah, nil)
|
||||
}
|
||||
|
||||
func (sc *conn) publishAsync(subject string, data []byte, ah AckHandler, ch chan error) (string, error) {
|
||||
a := &ack{ah: ah, ch: ch}
|
||||
sc.Lock()
|
||||
if sc.closed {
|
||||
sc.Unlock()
|
||||
return "", ErrConnectionClosed
|
||||
}
|
||||
|
||||
subj := sc.pubPrefix + "." + subject
|
||||
// This is only what we need from PubMsg in the timer below,
|
||||
// so do this so that pe doesn't escape.
|
||||
peGUID := sc.pubNUID.Next()
|
||||
// We send connID regardless of server we connect to. Older server
|
||||
// will simply not decode it.
|
||||
pe := &pb.PubMsg{ClientID: sc.clientID, Guid: peGUID, Subject: subject, Data: data, ConnID: sc.connID}
|
||||
b, _ := pe.Marshal()
|
||||
|
||||
// Map ack to guid.
|
||||
sc.pubAckMap[peGUID] = a
|
||||
// snapshot
|
||||
ackSubject := sc.ackSubject
|
||||
ackTimeout := sc.opts.AckTimeout
|
||||
sc.Unlock()
|
||||
|
||||
// Use the buffered channel to control the number of outstanding acks.
|
||||
select {
|
||||
case sc.pubAckChan <- struct{}{}:
|
||||
default:
|
||||
// It seems faster to first try to send to pubAckChan and only if
|
||||
// it fails to retry with the check on pubAckCloseChan than having
|
||||
// simply only the select with the 2 cases.
|
||||
select {
|
||||
case sc.pubAckChan <- struct{}{}:
|
||||
case <-sc.pubAckCloseChan:
|
||||
return "", ErrConnectionClosed
|
||||
}
|
||||
}
|
||||
|
||||
// sc.nc is immutable and never nil once connection is created.
|
||||
err := sc.nc.PublishRequest(subj, ackSubject, b)
|
||||
|
||||
// Setup the timer for expiration.
|
||||
sc.Lock()
|
||||
if err != nil || sc.closed {
|
||||
sc.Unlock()
|
||||
// If we got and error on publish or the connection has been closed,
|
||||
// we need to return an error only if:
|
||||
// - we can remove the pubAck from the map
|
||||
// - we can't, but this is an async pub with no provided AckHandler
|
||||
removed := sc.removeAck(peGUID) != nil
|
||||
if removed || (ch == nil && ah == nil) {
|
||||
if err == nil {
|
||||
err = ErrConnectionClosed
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
// pubAck was removed from cleanupOnClose() and error will be sent
|
||||
// to appropriate go channel (ah or ch).
|
||||
return peGUID, nil
|
||||
}
|
||||
a.t = time.AfterFunc(ackTimeout, func() {
|
||||
pubAck := sc.removeAck(peGUID)
|
||||
// processAck could get here before and handle the ack.
|
||||
// If that's the case, we would get nil here and simply return.
|
||||
if pubAck == nil {
|
||||
return
|
||||
}
|
||||
if pubAck.ah != nil {
|
||||
pubAck.ah(peGUID, ErrTimeout)
|
||||
} else if a.ch != nil {
|
||||
pubAck.ch <- ErrTimeout
|
||||
}
|
||||
})
|
||||
sc.Unlock()
|
||||
|
||||
return peGUID, nil
|
||||
}
|
||||
|
||||
// removeAck removes the ack from the pubAckMap and cancels any state, e.g. timers
|
||||
func (sc *conn) removeAck(guid string) *ack {
|
||||
var t *time.Timer
|
||||
sc.Lock()
|
||||
a := sc.pubAckMap[guid]
|
||||
if a != nil {
|
||||
t = a.t
|
||||
delete(sc.pubAckMap, guid)
|
||||
}
|
||||
pac := sc.pubAckChan
|
||||
sc.Unlock()
|
||||
|
||||
// Cancel timer if needed.
|
||||
if t != nil {
|
||||
t.Stop()
|
||||
}
|
||||
|
||||
// Remove from channel to unblock PublishAsync
|
||||
if a != nil && len(pac) > 0 {
|
||||
<-pac
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// Process an msg from the NATS Streaming cluster
|
||||
func (sc *conn) processMsg(raw *nats.Msg) {
|
||||
msg := &Msg{}
|
||||
err := msg.Unmarshal(raw.Data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error processing unmarshal for msg: %v", err))
|
||||
}
|
||||
var sub *subscription
|
||||
// Lookup the subscription
|
||||
sc.RLock()
|
||||
isClosed := sc.closed
|
||||
if !isClosed {
|
||||
sub = sc.subMap[raw.Subject]
|
||||
}
|
||||
sc.RUnlock()
|
||||
|
||||
// Check if sub is no longer valid or connection has been closed.
|
||||
if sub == nil || isClosed {
|
||||
return
|
||||
}
|
||||
|
||||
// Store in msg for backlink
|
||||
msg.Sub = sub
|
||||
|
||||
sub.RLock()
|
||||
cb := sub.cb
|
||||
ackSubject := sub.ackInbox
|
||||
isManualAck := sub.opts.ManualAcks
|
||||
subsc := sub.sc // Can be nil if sub has been unsubscribed.
|
||||
sub.RUnlock()
|
||||
|
||||
// Perform the callback
|
||||
if cb != nil && subsc != nil {
|
||||
cb(msg)
|
||||
}
|
||||
|
||||
// Process auto-ack
|
||||
if !isManualAck {
|
||||
ack := &pb.Ack{Subject: msg.Subject, Sequence: msg.Sequence}
|
||||
b, _ := ack.Marshal()
|
||||
// FIXME(dlc) - Async error handler? Retry?
|
||||
// sc.nc is immutable and never nil once connection is created.
|
||||
sc.nc.Publish(ackSubject, b)
|
||||
}
|
||||
}
|
499
gateway/vendor/github.com/nats-io/stan.go/sub.go
generated
vendored
Normal file
499
gateway/vendor/github.com/nats-io/stan.go/sub.go
generated
vendored
Normal file
@ -0,0 +1,499 @@
|
||||
// Copyright 2016-2018 The NATS Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package stan
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/stan.go/pb"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultAckWait indicates how long the server should wait for an ACK before resending a message
|
||||
DefaultAckWait = 30 * time.Second
|
||||
// DefaultMaxInflight indicates how many messages with outstanding ACKs the server can send
|
||||
DefaultMaxInflight = 1024
|
||||
)
|
||||
|
||||
// Msg is the client defined message, which includes proto, then back link to subscription.
|
||||
type Msg struct {
|
||||
pb.MsgProto // MsgProto: Seq, Subject, Reply[opt], Data, Timestamp, CRC32[opt]
|
||||
Sub Subscription
|
||||
}
|
||||
|
||||
// Subscriptions and Options
|
||||
|
||||
// Subscription represents a subscription within the NATS Streaming cluster. Subscriptions
|
||||
// will be rate matched and follow at-least once delivery semantics.
|
||||
// The subscription is safe to use in multiple Go routines concurrently.
|
||||
type Subscription interface {
|
||||
// Unsubscribe removes interest in the subscription.
|
||||
// For durables, it means that the durable interest is also removed from
|
||||
// the server. Restarting a durable with the same name will not resume
|
||||
// the subscription, it will be considered a new one.
|
||||
Unsubscribe() error
|
||||
|
||||
// Close removes this subscriber from the server, but unlike Unsubscribe(),
|
||||
// the durable interest is not removed. If the client has connected to a server
|
||||
// for which this feature is not available, Close() will return a ErrNoServerSupport
|
||||
// error.
|
||||
Close() error
|
||||
|
||||
// These functions have been added for expert-users that need to get details
|
||||
// about the low level NATS Subscription used internally to receive messages
|
||||
// for this streaming subscription. They are documented in the Go client
|
||||
// library: https://godoc.org/github.com/nats-io/nats.go#Subscription.ClearMaxPending
|
||||
|
||||
// ClearMaxPending resets the maximums seen so far.
|
||||
ClearMaxPending() error
|
||||
|
||||
// Delivered returns the number of delivered messages for the internal low-level NATS subscription.
|
||||
Delivered() (int64, error)
|
||||
|
||||
// Dropped returns the number of known dropped messages for the internal low-level NATS subscription.
|
||||
// This will correspond to messages dropped by violations of PendingLimits. If the server declares
|
||||
// the connection a SlowConsumer, this number may not be valid.
|
||||
Dropped() (int, error)
|
||||
|
||||
// IsValid returns a boolean indicating whether the internal low-level NATS subscription is still active.
|
||||
// This will return false if the subscription has already been closed.
|
||||
IsValid() bool
|
||||
|
||||
// MaxPending returns the maximum number of queued messages and queued bytes seen so far for the internal
|
||||
// low-level NATS subscription.
|
||||
MaxPending() (int, int, error)
|
||||
|
||||
// Pending returns the number of queued messages and queued bytes in the client for the internal
|
||||
// low-level NATS subscription.
|
||||
Pending() (int, int, error)
|
||||
|
||||
// PendingLimits returns the current limits for the internal low-level NATS subscription. If no error is
|
||||
// returned, a negative value indicates that the given metric is not limited.
|
||||
PendingLimits() (int, int, error)
|
||||
|
||||
// SetPendingLimits sets the limits for pending msgs and bytes for the internal low-level NATS Subscription.
|
||||
// Zero is not allowed. Any negative value means that the given metric is not limited.
|
||||
SetPendingLimits(msgLimit, bytesLimit int) error
|
||||
}
|
||||
|
||||
// A subscription represents a subscription to a stan cluster.
|
||||
type subscription struct {
|
||||
sync.RWMutex
|
||||
sc *conn
|
||||
subject string
|
||||
qgroup string
|
||||
inbox string
|
||||
ackInbox string
|
||||
inboxSub *nats.Subscription
|
||||
opts SubscriptionOptions
|
||||
cb MsgHandler
|
||||
}
|
||||
|
||||
// SubscriptionOption is a function on the options for a subscription.
|
||||
type SubscriptionOption func(*SubscriptionOptions) error
|
||||
|
||||
// MsgHandler is a callback function that processes messages delivered to
|
||||
// asynchronous subscribers.
|
||||
type MsgHandler func(msg *Msg)
|
||||
|
||||
// SubscriptionOptions are used to control the Subscription's behavior.
|
||||
type SubscriptionOptions struct {
|
||||
// DurableName, if set will survive client restarts.
|
||||
DurableName string
|
||||
// Controls the number of messages the cluster will have inflight without an ACK.
|
||||
MaxInflight int
|
||||
// Controls the time the cluster will wait for an ACK for a given message.
|
||||
AckWait time.Duration
|
||||
// StartPosition enum from proto.
|
||||
StartAt pb.StartPosition
|
||||
// Optional start sequence number.
|
||||
StartSequence uint64
|
||||
// Optional start time.
|
||||
StartTime time.Time
|
||||
// Option to do Manual Acks
|
||||
ManualAcks bool
|
||||
}
|
||||
|
||||
// DefaultSubscriptionOptions are the default subscriptions' options
|
||||
var DefaultSubscriptionOptions = SubscriptionOptions{
|
||||
MaxInflight: DefaultMaxInflight,
|
||||
AckWait: DefaultAckWait,
|
||||
}
|
||||
|
||||
// MaxInflight is an Option to set the maximum number of messages the cluster will send
|
||||
// without an ACK.
|
||||
func MaxInflight(m int) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.MaxInflight = m
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// AckWait is an Option to set the timeout for waiting for an ACK from the cluster's
|
||||
// point of view for delivered messages.
|
||||
func AckWait(t time.Duration) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.AckWait = t
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// StartAt sets the desired start position for the message stream.
|
||||
func StartAt(sp pb.StartPosition) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.StartAt = sp
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// StartAtSequence sets the desired start sequence position and state.
|
||||
func StartAtSequence(seq uint64) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.StartAt = pb.StartPosition_SequenceStart
|
||||
o.StartSequence = seq
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// StartAtTime sets the desired start time position and state.
|
||||
func StartAtTime(start time.Time) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.StartAt = pb.StartPosition_TimeDeltaStart
|
||||
o.StartTime = start
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// StartAtTimeDelta sets the desired start time position and state using the delta.
|
||||
func StartAtTimeDelta(ago time.Duration) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.StartAt = pb.StartPosition_TimeDeltaStart
|
||||
o.StartTime = time.Now().Add(-ago)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// StartWithLastReceived is a helper function to set start position to last received.
|
||||
func StartWithLastReceived() SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.StartAt = pb.StartPosition_LastReceived
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// DeliverAllAvailable will deliver all messages available.
|
||||
func DeliverAllAvailable() SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.StartAt = pb.StartPosition_First
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// SetManualAckMode will allow clients to control their own acks to delivered messages.
|
||||
func SetManualAckMode() SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.ManualAcks = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// DurableName sets the DurableName for the subscriber.
|
||||
func DurableName(name string) SubscriptionOption {
|
||||
return func(o *SubscriptionOptions) error {
|
||||
o.DurableName = name
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe will perform a subscription with the given options to the NATS Streaming cluster.
|
||||
func (sc *conn) Subscribe(subject string, cb MsgHandler, options ...SubscriptionOption) (Subscription, error) {
|
||||
return sc.subscribe(subject, "", cb, options...)
|
||||
}
|
||||
|
||||
// QueueSubscribe will perform a queue subscription with the given options to the NATS Streaming cluster.
|
||||
func (sc *conn) QueueSubscribe(subject, qgroup string, cb MsgHandler, options ...SubscriptionOption) (Subscription, error) {
|
||||
return sc.subscribe(subject, qgroup, cb, options...)
|
||||
}
|
||||
|
||||
// subscribe will perform a subscription with the given options to the NATS Streaming cluster.
|
||||
func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...SubscriptionOption) (Subscription, error) {
|
||||
sub := &subscription{subject: subject, qgroup: qgroup, inbox: nats.NewInbox(), cb: cb, sc: sc, opts: DefaultSubscriptionOptions}
|
||||
for _, opt := range options {
|
||||
if err := opt(&sub.opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sc.Lock()
|
||||
if sc.closed {
|
||||
sc.Unlock()
|
||||
return nil, ErrConnectionClosed
|
||||
}
|
||||
|
||||
// Register subscription.
|
||||
sc.subMap[sub.inbox] = sub
|
||||
sc.Unlock()
|
||||
|
||||
// Hold lock throughout.
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
|
||||
// sc.nc is immutable and never nil once connection is created.
|
||||
|
||||
// Listen for actual messages.
|
||||
nsub, err := sc.nc.Subscribe(sub.inbox, sc.processMsg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nsub.SetPendingLimits(-1, -1)
|
||||
sub.inboxSub = nsub
|
||||
|
||||
// Create a subscription request
|
||||
// FIXME(dlc) add others.
|
||||
sr := &pb.SubscriptionRequest{
|
||||
ClientID: sc.clientID,
|
||||
Subject: subject,
|
||||
QGroup: qgroup,
|
||||
Inbox: sub.inbox,
|
||||
MaxInFlight: int32(sub.opts.MaxInflight),
|
||||
AckWaitInSecs: int32(sub.opts.AckWait / time.Second),
|
||||
StartPosition: sub.opts.StartAt,
|
||||
DurableName: sub.opts.DurableName,
|
||||
}
|
||||
|
||||
// Conditionals
|
||||
switch sr.StartPosition {
|
||||
case pb.StartPosition_TimeDeltaStart:
|
||||
sr.StartTimeDelta = time.Now().UnixNano() - sub.opts.StartTime.UnixNano()
|
||||
case pb.StartPosition_SequenceStart:
|
||||
sr.StartSequence = sub.opts.StartSequence
|
||||
}
|
||||
|
||||
b, _ := sr.Marshal()
|
||||
reply, err := sc.nc.Request(sc.subRequests, b, sc.opts.ConnectTimeout)
|
||||
if err != nil {
|
||||
sub.inboxSub.Unsubscribe()
|
||||
if err == nats.ErrTimeout {
|
||||
err = ErrSubReqTimeout
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
r := &pb.SubscriptionResponse{}
|
||||
if err := r.Unmarshal(reply.Data); err != nil {
|
||||
sub.inboxSub.Unsubscribe()
|
||||
return nil, err
|
||||
}
|
||||
if r.Error != "" {
|
||||
sub.inboxSub.Unsubscribe()
|
||||
return nil, errors.New(r.Error)
|
||||
}
|
||||
sub.ackInbox = r.AckInbox
|
||||
|
||||
return sub, nil
|
||||
}
|
||||
|
||||
// ClearMaxPending resets the maximums seen so far.
|
||||
func (sub *subscription) ClearMaxPending() error {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.ClearMaxPending()
|
||||
}
|
||||
|
||||
// Delivered returns the number of delivered messages for this subscription.
|
||||
func (sub *subscription) Delivered() (int64, error) {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return -1, ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.Delivered()
|
||||
}
|
||||
|
||||
// Dropped returns the number of known dropped messages for this subscription.
|
||||
// This will correspond to messages dropped by violations of PendingLimits. If
|
||||
// the server declares the connection a SlowConsumer, this number may not be
|
||||
// valid.
|
||||
func (sub *subscription) Dropped() (int, error) {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return -1, ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.Dropped()
|
||||
}
|
||||
|
||||
// IsValid returns a boolean indicating whether the subscription
|
||||
// is still active. This will return false if the subscription has
|
||||
// already been closed.
|
||||
func (sub *subscription) IsValid() bool {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return false
|
||||
}
|
||||
return sub.inboxSub.IsValid()
|
||||
}
|
||||
|
||||
// MaxPending returns the maximum number of queued messages and queued bytes seen so far.
|
||||
func (sub *subscription) MaxPending() (int, int, error) {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return -1, -1, ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.MaxPending()
|
||||
}
|
||||
|
||||
// Pending returns the number of queued messages and queued bytes in the client for this subscription.
|
||||
func (sub *subscription) Pending() (int, int, error) {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return -1, -1, ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.Pending()
|
||||
}
|
||||
|
||||
// PendingLimits returns the current limits for this subscription.
|
||||
// If no error is returned, a negative value indicates that the
|
||||
// given metric is not limited.
|
||||
func (sub *subscription) PendingLimits() (int, int, error) {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return -1, -1, ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.PendingLimits()
|
||||
}
|
||||
|
||||
// SetPendingLimits sets the limits for pending msgs and bytes for this subscription.
|
||||
// Zero is not allowed. Any negative value means that the given metric is not limited.
|
||||
func (sub *subscription) SetPendingLimits(msgLimit, bytesLimit int) error {
|
||||
sub.Lock()
|
||||
defer sub.Unlock()
|
||||
if sub.inboxSub == nil {
|
||||
return ErrBadSubscription
|
||||
}
|
||||
return sub.inboxSub.SetPendingLimits(msgLimit, bytesLimit)
|
||||
}
|
||||
|
||||
// closeOrUnsubscribe performs either close or unsubsribe based on
|
||||
// given boolean.
|
||||
func (sub *subscription) closeOrUnsubscribe(doClose bool) error {
|
||||
sub.Lock()
|
||||
sc := sub.sc
|
||||
if sc == nil {
|
||||
// Already closed.
|
||||
sub.Unlock()
|
||||
return ErrBadSubscription
|
||||
}
|
||||
sub.sc = nil
|
||||
sub.inboxSub.Unsubscribe()
|
||||
sub.inboxSub = nil
|
||||
sub.Unlock()
|
||||
|
||||
sc.Lock()
|
||||
if sc.closed {
|
||||
sc.Unlock()
|
||||
return ErrConnectionClosed
|
||||
}
|
||||
|
||||
delete(sc.subMap, sub.inbox)
|
||||
reqSubject := sc.unsubRequests
|
||||
if doClose {
|
||||
reqSubject = sc.subCloseRequests
|
||||
if reqSubject == "" {
|
||||
sc.Unlock()
|
||||
return ErrNoServerSupport
|
||||
}
|
||||
}
|
||||
sc.Unlock()
|
||||
|
||||
// sc.nc is immutable and never nil once connection is created.
|
||||
|
||||
usr := &pb.UnsubscribeRequest{
|
||||
ClientID: sc.clientID,
|
||||
Subject: sub.subject,
|
||||
Inbox: sub.ackInbox,
|
||||
}
|
||||
b, _ := usr.Marshal()
|
||||
reply, err := sc.nc.Request(reqSubject, b, sc.opts.ConnectTimeout)
|
||||
if err != nil {
|
||||
if err == nats.ErrTimeout {
|
||||
if doClose {
|
||||
return ErrCloseReqTimeout
|
||||
}
|
||||
return ErrUnsubReqTimeout
|
||||
}
|
||||
return err
|
||||
}
|
||||
r := &pb.SubscriptionResponse{}
|
||||
if err := r.Unmarshal(reply.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.Error != "" {
|
||||
return errors.New(r.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unsubscribe implements the Subscription interface
|
||||
func (sub *subscription) Unsubscribe() error {
|
||||
return sub.closeOrUnsubscribe(false)
|
||||
}
|
||||
|
||||
// Close implements the Subscription interface
|
||||
func (sub *subscription) Close() error {
|
||||
return sub.closeOrUnsubscribe(true)
|
||||
}
|
||||
|
||||
// Ack manually acknowledges a message.
|
||||
// The subscriber had to be created with SetManualAckMode() option.
|
||||
func (msg *Msg) Ack() error {
|
||||
if msg == nil {
|
||||
return ErrNilMsg
|
||||
}
|
||||
// Look up subscription (cannot be nil)
|
||||
sub := msg.Sub.(*subscription)
|
||||
sub.RLock()
|
||||
ackSubject := sub.ackInbox
|
||||
isManualAck := sub.opts.ManualAcks
|
||||
sc := sub.sc
|
||||
sub.RUnlock()
|
||||
|
||||
// Check for error conditions.
|
||||
if !isManualAck {
|
||||
return ErrManualAck
|
||||
}
|
||||
if sc == nil {
|
||||
return ErrBadSubscription
|
||||
}
|
||||
|
||||
// sc.nc is immutable and never nil once connection is created.
|
||||
|
||||
// Ack here.
|
||||
ack := &pb.Ack{Subject: msg.Subject, Sequence: msg.Sequence}
|
||||
b, _ := ack.Marshal()
|
||||
err := sc.nc.Publish(ackSubject, b)
|
||||
if err == nats.ErrConnectionClosed {
|
||||
return ErrBadConnection
|
||||
}
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user