Vendor new queue-worker version

Introduces 0.4.6 of queue-worker - see upstream repo for changes.

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware) 2018-06-18 20:09:33 +01:00
parent 41bda568a7
commit 223c561706
81 changed files with 4701 additions and 1323 deletions

View File

@ -6,7 +6,6 @@ RUN curl -sL https://github.com/alexellis/license-check/releases/download/0.2.2/
WORKDIR /go/src/github.com/openfaas/faas/gateway
COPY vendor vendor
COPY handlers handlers
@ -20,13 +19,20 @@ COPY plugin plugin
COPY server.go .
# Run a gofmt and exclude all vendored code.
RUN license-check -path ./ --verbose=false "Alex Ellis" "OpenFaaS Project" \
RUN license-check -path ./ --verbose=false "Alex Ellis" "OpenFaaS Project" "OpenFaaS Authors" \
&& test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" \
&& go test $(go list ./... | grep -v integration | grep -v /vendor/ | grep -v /template/) -cover \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway .
FROM alpine:3.7
LABEL org.label-schema.license="MIT" \
org.label-schema.vcs-url="https://github.com/openfaas/faas" \
org.label-schema.vcs-type="Git" \
org.label-schema.name="openfaas/faas" \
org.label-schema.vendor="openfaas" \
org.label-schema.docker.schema-version="1.0"
RUN addgroup -S app \
&& adduser -S -g app app

49
gateway/Gopkg.lock generated
View File

@ -7,9 +7,19 @@
packages = ["quantile"]
revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9"
[[projects]]
name = "github.com/docker/distribution"
packages = ["uuid"]
revision = "48294d928ced5dd9b378f7fd7c6f5da3ff3f2c89"
version = "v2.6.2"
[[projects]]
name = "github.com/gogo/protobuf"
packages = ["gogoproto","proto","protoc-gen-gogo/descriptor"]
packages = [
"gogoproto",
"proto",
"protoc-gen-gogo/descriptor"
]
revision = "342cbe0a04158f6dcb03ca0079991a51a4248c02"
version = "v0.5"
@ -39,15 +49,22 @@
[[projects]]
name = "github.com/nats-io/go-nats"
packages = [".","encoders/builtin","util"]
revision = "d66cb54e6b7bdd93f0b28afc8450d84c780dfb68"
version = "v1.4.0"
packages = [
".",
"encoders/builtin",
"util"
]
revision = "062418ea1c2181f52dc0f954f6204370519a868b"
version = "v1.5.0"
[[projects]]
name = "github.com/nats-io/go-nats-streaming"
packages = [".","pb"]
revision = "6e620057a207bd61e992c1c5b6a2de7b6a4cb010"
version = "v0.3.4"
packages = [
".",
"pb"
]
revision = "e15a53f85e4932540600a16b56f6c4f65f58176f"
version = "v0.4.0"
[[projects]]
name = "github.com/nats-io/nuid"
@ -55,12 +72,11 @@
revision = "289cccf02c178dc782430d534e3c1f5b72af807f"
version = "v1.0.0"
[[projects]]
name = "github.com/openfaas/nats-queue-worker"
packages = ["handler"]
revision = "c064b78787b00d458fcb96435b0c1fff413126c9"
version = "0.4"
revision = "62e8dcfe87fc01de8b94156f3e4291370c0c84a8"
version = "0.4.6"
[[projects]]
name = "github.com/prometheus/client_golang"
@ -77,18 +93,25 @@
[[projects]]
branch = "master"
name = "github.com/prometheus/common"
packages = ["expfmt","internal/bitbucket.org/ww/goautoneg","model"]
packages = [
"expfmt",
"internal/bitbucket.org/ww/goautoneg",
"model"
]
revision = "2e54d0b93cba2fd133edc32211dcc32c06ef72ca"
[[projects]]
branch = "master"
name = "github.com/prometheus/procfs"
packages = [".","xfs"]
packages = [
".",
"xfs"
]
revision = "b15cd069a83443be3154b719d0cc9fe8117f09fb"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "6c22951557e478603b9ee6d67e3787efd5230d52b29b01ad8e136fd3e1ca1bde"
inputs-digest = "181fb3c695e3b12e0a8e967135ef717ea83d9525c7c31b92f641d7a963d26254"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -1,32 +1,21 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
ignored = ["github.com/openfaas/faas/gateway/queue"]
[[constraint]]
name = "github.com/gorilla/mux"
version = "1.6.0"
[[constraint]]
name = "github.com/openfaas/nats-queue-worker"
name = "github.com/nats-io/go-nats-streaming"
version = "0.4.0"
[[override]]
name = "github.com/nats-io/go-nats"
version = "v1.5.0"
[[constraint]]
name = "github.com/openfaas/nats-queue-worker"
version = "0.4.5"
[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
@ -34,3 +23,5 @@
[[constraint]]
branch = "master"
name = "golang.org/x/net"

View File

@ -88,7 +88,7 @@ func main() {
if config.UseNATS() {
log.Println("Async enabled: Using NATS Streaming.")
natsQueue, queueErr := natsHandler.CreateNatsQueue(*config.NATSAddress, *config.NATSPort)
natsQueue, queueErr := natsHandler.CreateNatsQueue(*config.NATSAddress, *config.NATSPort, natsHandler.DefaultNatsConfig{})
if queueErr != nil {
log.Fatalln(queueErr)
}

View File

@ -1,22 +1,26 @@
language: go
sudo: false
go:
- 1.6.4
- 1.7.4
- 1.8.x
- 1.9.x
- 1.10.x
install:
- go get -t ./...
- go get github.com/nats-io/nats-streaming-server
- go get github.com/mattn/goveralls
- go get github.com/wadey/gocovmerge
- go get honnef.co/go/staticcheck/cmd/staticcheck
script:
- go fmt ./...
- go get -u honnef.co/go/tools/cmd/megacheck
- go get -u github.com/client9/misspell/cmd/misspell
before_script:
- $(exit $(go fmt ./... | wc -l))
- go vet ./...
- misspell -error -locale US .
- megacheck ./...
script:
- go test -i -race ./...
- go test -v -race ./...
- staticcheck ./...
after_success:
- if [ "$TRAVIS_GO_VERSION" \> "1.7." ]; then ./scripts/cov.sh TRAVIS; fi
- if [[ "$TRAVIS_GO_VERSION" == 1.9.* ]]; then ./scripts/cov.sh TRAVIS; fi
env:
global:
secure: OoCemKSHHH/SkkamHLWd0qh9qgQDx4/3fGuykYuzW/gjUhLlL0ThyUXOr3HOandoh3wTU8Ntj184WU6Sjh1oXzdDAYcI/ryNQXSmJ/DyGC6ffoj4Je/Rwj3sbwpaFTl1imawL8Lv6+5Dkb2JSbbbqapjbO3BhrrNfqLuQulqrLJKVaOyS5nOByiGFYsgjf/ac7Qrr9AnHhlkWRXoR+q8GlGG7qcKtLlmG5OqxifqfgQ+pcVtyeleT6zGPI0LUyr9gWHRZtMK9nYfxXuQK2d7V+SW4NBW1jdDKBHZbeJRxZ8N8rU8Nk3ka54YHXC2PeD8EloiAr5HkALuHbIdzyy40Y3rJyHfxyY6EYBcZEy+ZCRoqkVJ4NN4R46YE588BpYhT48YHK+lptM7YxrPtf08X+Cugc206X0hk/YFqqsaaNIwMfiTPbapuHxa8S4kgT2vDn3OTI53ZTrDiLVY3ZDp+EdUO1hiYFR6cpu5el/EQN5G0iW6sI69gOv26UmGI369D3fezbYPFPHHDao8xq7s8HdYUZleDNL0oCWK1MgL2g/Irbt5Kr6JjT/tpQOiiagqeR5dlV9mAiOZFr88gg7aqwOuSqmlULWVB4qYncQ6IBoednIHtrLW6H+2RfrZU01cI6tGSrXD+VoFnQ7aZwLxLc71VyN5khYPk0gGvyQhZxk=

View 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).

View File

@ -1,20 +1,201 @@
The MIT License (MIT)
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Copyright (c) 2016 Apcera Inc.
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
1. Definitions.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"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.

View File

@ -0,0 +1,9 @@
reviewers:
- aricart
- ColinSullivan1
- derekcollison
- kozlovic
- wallyqs
approvers:
- derekcollison
- kozlovic

View File

@ -2,7 +2,7 @@
NATS Streaming is an extremely performant, lightweight reliable streaming platform powered by [NATS](https://nats.io).
[![License MIT](https://img.shields.io/npm/l/express.svg)](http://opensource.org/licenses/MIT)
[![License Apache 2](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Build Status](https://travis-ci.org/nats-io/go-nats-streaming.svg?branch=master)](http://travis-ci.org/nats-io/go-nats-streaming)
[![Coverage Status](https://coveralls.io/repos/nats-io/go-nats-streaming/badge.svg?branch=master)](https://coveralls.io/r/nats-io/go-nats-streaming?branch=master)
@ -17,9 +17,6 @@ NATS Streaming provides the following high-level feature set:
- Please raise questions/issues via the [Issue Tracker](https://github.com/nats-io/go-nats-streaming/issues).
## Known Issues
- Time- and sequence-based subscriptions are exact. Requesting a time or seqno before the earliest stored message for a subject will result in an error (in SubscriptionRequest.Error)
## Installation
```bash
@ -59,29 +56,29 @@ The options are described with examples below:
// 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))
}, StartWithLastReceived())
}, 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))
}, DeliverAllAvailable())
}, 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))
}, StartAtSequence(22))
}, 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))
}, StartAtTime(startTime))
}, 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))
}, StartAtTimeDelta(time.ParseDuration("30s")))
}, stan.StartAtTimeDelta(time.ParseDuration("30s")))
```
### Durable Subscriptions
@ -184,7 +181,7 @@ that is, the start position will take effect and delivery will start from there.
### Durable Queue Groups
As described above, for non durable queue subsribers, when the last member leaves the group,
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.
@ -220,7 +217,7 @@ The rules for non-durable queue subscribers apply to durable subscribers.
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 transfered to remaining members. See *Closing the Group* for important difference
is transferred to remaining members. See *Closing the Group* for important difference
with non-durable queue subscribers.
#### Closing the Group
@ -242,6 +239,58 @@ NATS Streaming subscriptions **do not** support wildcards.
## Advanced Usage
### 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.
@ -301,7 +350,7 @@ ah := func(nuid string, err error) {
}
for i := 1; i < 1000; i++ {
// If the server is unable to keep up with the publisher, the number of oustanding acks will eventually
// 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)
}
@ -327,24 +376,5 @@ sc.Subscribe("foo", func(m *stan.Msg) {
## License
(The MIT License)
Copyright (c) 2012-2016 Apcera Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Unless otherwise noted, the NATS source files are distributed
under the Apache Version 2.0 license found in the LICENSE file.

View File

@ -1,15 +0,0 @@
- [ ] Retry limits?
- [ ] Server Store Limits (time, msgs, byte)
- [X] Change time to deltas
- [X] Server heartbeat, release dead clients.
- [X] Require clientID for published messages, error if not registered.
- [X] Check for need of ackMap (out of order re-delivery to queue subscribers).
- [X] Redelivered Flag for Msg.
- [X] Queue Subscribers
- [X] Durable Subscribers (survive reconnect, etc)
- [X] Start Positions on Subscribers
- [X] Ack for delivered just Reply? No need on ConnectedResponse?
- [X] PublishWithReply, or option.
- [X] Data Races in Server.
- [X] Manual Ack?

View File

@ -1,3 +1,15 @@
// 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
////////////////////////////////////////////////////////////////////////////////

View File

@ -1,4 +1,15 @@
// Copyright 2015 Apcera Inc. All rights reserved.
// 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 main
@ -29,12 +40,16 @@ const (
)
func usage() {
log.Fatalf("Usage: nats-bench [-s server (%s)] [--tls] [-id CLIENT_ID] [-np NUM_PUBLISHERS] [-ns NUM_SUBSCRIBERS] [-n NUM_MSGS] [-ms MESSAGE_SIZE] [-csv csvfile] [-mpa MAX_NUMBER_OF_PUBLISHED_ACKS_INFLIGHT] [-io] [-a] <subject>\n", nats.DefaultURL)
log.Fatalf("Usage: stan-bench [-s server (%s)] [--tls] [-id CLIENT_ID] [-np NUM_PUBLISHERS] [-ns NUM_SUBSCRIBERS] [-n NUM_MSGS] [-ms MESSAGE_SIZE] [-csv csvfile] [-mpa MAX_NUMBER_OF_PUBLISHED_ACKS_INFLIGHT] [-io] [-a] <subject>\n", nats.DefaultURL)
}
var benchmark *bench.Benchmark
func main() {
var clusterID string
flag.StringVar(&clusterID, "c", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clusterID, "cluster", "test-cluster", "The NATS Streaming cluster ID")
var urls = flag.String("s", nats.DefaultURL, "The NATS server URLs (separated by comma")
var tls = flag.Bool("tls", false, "Use TLS secure sonnection")
var numPubs = flag.Int("np", DefaultNumPubs, "Number of concurrent publishers")
@ -57,7 +72,7 @@ func main() {
}
// Setup the option block
opts := nats.DefaultOptions
opts := nats.GetDefaultOptions()
opts.Servers = strings.Split(*urls, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
@ -76,7 +91,7 @@ func main() {
startwg.Add(*numSubs)
for i := 0; i < *numSubs; i++ {
subID := fmt.Sprintf("%s-sub-%d", *clientID, i)
go runSubscriber(&startwg, &donewg, opts, *numMsgs, *messageSize, *ignoreOld, subID)
go runSubscriber(&startwg, &donewg, opts, clusterID, *numMsgs, *messageSize, *ignoreOld, subID)
}
startwg.Wait()
@ -85,7 +100,7 @@ func main() {
pubCounts := bench.MsgsPerClient(*numMsgs, *numPubs)
for i := 0; i < *numPubs; i++ {
pubID := fmt.Sprintf("%s-pub-%d", *clientID, i)
go runPublisher(&startwg, &donewg, opts, pubCounts[i], *messageSize, *async, pubID, *maxPubAcks)
go runPublisher(&startwg, &donewg, opts, clusterID, pubCounts[i], *messageSize, *async, pubID, *maxPubAcks)
}
log.Printf("Starting benchmark [msgs=%d, msgsize=%d, pubs=%d, subs=%d]\n", *numMsgs, *messageSize, *numPubs, *numSubs)
@ -103,12 +118,15 @@ func main() {
}
}
func runPublisher(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs int, msgSize int, async bool, pubID string, maxPubAcksInflight int) {
func runPublisher(startwg, donewg *sync.WaitGroup, opts nats.Options, clusterID string, numMsgs int, msgSize int, async bool, pubID string, maxPubAcksInflight int) {
nc, err := opts.Connect()
if err != nil {
log.Fatalf("Publisher %s can't connect: %v\n", pubID, err)
}
snc, err := stan.Connect("test-cluster", pubID, stan.MaxPubAcksInflight(maxPubAcksInflight), stan.NatsConn(nc))
snc, err := stan.Connect(clusterID, pubID, stan.MaxPubAcksInflight(maxPubAcksInflight), stan.NatsConn(nc),
stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) {
log.Fatalf("Connection lost, reason: %v", reason)
}))
if err != nil {
log.Fatalf("Publisher %s can't connect: %v\n", pubID, err)
}
@ -128,6 +146,9 @@ func runPublisher(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs in
if async {
ch := make(chan bool)
acb := func(lguid string, err error) {
if err != nil {
log.Fatalf("Publisher %q got following error: %v", pubID, err)
}
published++
if published >= numMsgs {
ch <- true
@ -156,26 +177,31 @@ func runPublisher(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs in
donewg.Done()
}
func runSubscriber(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs int, msgSize int, ignoreOld bool, subID string) {
func runSubscriber(startwg, donewg *sync.WaitGroup, opts nats.Options, clusterID string, numMsgs int, msgSize int, ignoreOld bool, subID string) {
nc, err := opts.Connect()
if err != nil {
log.Fatalf("Subscriber %s can't connect: %v\n", subID, err)
}
snc, err := stan.Connect("test-cluster", subID, stan.NatsConn(nc))
snc, err := stan.Connect(clusterID, subID, stan.NatsConn(nc),
stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) {
log.Fatalf("Connection lost, reason: %v", reason)
}))
if err != nil {
log.Fatalf("Subscriber %s can't connect: %v\n", subID, err)
}
args := flag.Args()
subj := args[0]
ch := make(chan bool)
start := time.Now()
ch := make(chan time.Time, 2)
received := 0
mcb := func(msg *stan.Msg) {
received++
if received == 1 {
ch <- time.Now()
}
if received >= numMsgs {
ch <- true
ch <- time.Now()
}
}
@ -186,8 +212,9 @@ func runSubscriber(startwg, donewg *sync.WaitGroup, opts nats.Options, numMsgs i
}
startwg.Done()
<-ch
benchmark.AddSubSample(bench.NewSample(numMsgs, msgSize, start, time.Now(), snc.NatsConn()))
start := <-ch
end := <-ch
benchmark.AddSubSample(bench.NewSample(numMsgs, msgSize, start, end, snc.NatsConn()))
snc.Close()
nc.Close()
donewg.Done()

View File

@ -1,5 +1,15 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore
// 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 main
@ -79,7 +89,7 @@ func main() {
ch <- true
}
if async != true {
if !async {
err = sc.Publish(subj, msg)
if err != nil {
log.Fatalf("Error during publish: %v\n", err)

View File

@ -1,5 +1,15 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// +build ignore
// 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 main
@ -89,7 +99,10 @@ func main() {
usage()
}
sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(URL))
sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(URL),
stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) {
log.Fatalf("Connection lost, reason: %v", reason)
}))
if err != nil {
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, URL)
}
@ -106,9 +119,9 @@ func main() {
if startSeq != 0 {
startOpt = stan.StartAtSequence(startSeq)
} else if deliverLast == true {
} else if deliverLast {
startOpt = stan.StartWithLastReceived()
} else if deliverAll == true {
} else if deliverAll {
log.Print("subscribing with DeliverAllAvailable")
startOpt = stan.DeliverAllAvailable()
} else if startDelta != "" {
@ -138,7 +151,7 @@ func main() {
cleanupDone := make(chan bool)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
for range signalChan {
fmt.Printf("\nReceived an interrupt, unsubscribing and closing connection...\n\n")
// Do not unsubscribe a durable on exit, except if asked to.
if durable == "" || unsubscribe {

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,15 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// 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`
@ -20,6 +31,7 @@ message PubMsg {
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
}
@ -53,6 +65,10 @@ message Ack {
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
@ -63,10 +79,24 @@ message ConnectResponse {
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;

View File

@ -1,4 +1,15 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// 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 is a Go client for the NATS Streaming messaging system (https://nats.io).
package stan
@ -16,7 +27,7 @@ import (
)
// Version is the NATS Streaming Go Client version
const Version = "0.3.4"
const Version = "0.4.0"
const (
// DefaultNatsURL is the default URL the client connects to
@ -30,6 +41,10 @@ const (
// 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
@ -54,6 +69,13 @@ type Conn interface {
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")
@ -68,13 +90,20 @@ var (
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 teh GUID and any error state. No error means the
// 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 string
@ -83,6 +112,9 @@ type Options struct {
AckTimeout time.Duration
DiscoverPrefix string
MaxPubAcksInflight int
PingIterval int // In seconds
PingMaxOut int
ConnectionLostCB ConnectionLostHandler
}
// DefaultOptions are the NATS Streaming client's default options
@ -92,6 +124,8 @@ var DefaultOptions = Options{
AckTimeout: DefaultAckWait,
DiscoverPrefix: DefaultDiscoverPrefix,
MaxPubAcksInflight: DefaultMaxPubAcksInflight,
PingIterval: DefaultPingInterval,
PingMaxOut: DefaultPingMaxOut,
}
// Option is a function on the options for a connection.
@ -140,11 +174,45 @@ func NatsConn(nc *nats.Conn) Option {
}
}
// 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.PingIterval = 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
serverID 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.
@ -159,6 +227,18 @@ type conn 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
pingMu sync.Mutex
pingSub *nats.Subscription
pingTimer *time.Timer
pingBytes []byte
pingRequests string
pingInbox string
pingInterval time.Duration
pingMaxOut int
pingOut int
}
// Closure for ack contexts.
@ -169,9 +249,10 @@ type ack struct {
}
// 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}
c := conn{clientID: clientID, opts: DefaultOptions, connID: []byte(nuid.Next()), pubNUID: nuid.New()}
for _, opt := range options {
if err := opt(&c.opts); err != nil {
return nil, err
@ -181,7 +262,15 @@ func Connect(stanClusterID, clientID string, options ...Option) (Conn, error) {
c.nc = c.opts.NatsConn
// Create a NATS connection if it doesn't exist.
if c.nc == nil {
nc, err := nats.Connect(c.opts.NatsURL)
// 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
}
@ -200,9 +289,25 @@ func Connect(stanClusterID, clientID string, options ...Option) (Conn, error) {
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.
pingSub, err := c.nc.Subscribe(nats.NewInbox(), c.processPingResponse)
if err != nil {
c.Close()
return nil, err
}
// Send Request to discover the cluster
discoverSubject := c.opts.DiscoverPrefix + "." + stanClusterID
req := &pb.ConnectRequest{ClientID: clientID, HeartbeatInbox: hbInbox}
req := &pb.ConnectRequest{
ClientID: clientID,
HeartbeatInbox: hbInbox,
ConnID: c.connID,
Protocol: protocolOne,
PingInterval: int32(c.opts.PingIterval),
PingMaxOut: int32(c.opts.PingMaxOut),
}
b, _ := req.Marshal()
reply, err := c.nc.Request(discoverSubject, b, c.opts.ConnectTimeout)
if err != nil {
@ -245,18 +350,172 @@ func Connect(stanClusterID, clientID string, options ...Option) (Conn, error) {
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 protcolOne.
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.
c.pingRequests = cr.PingRequests
c.pingInbox = pingSub.Subject
// In test, it is possible that we get a negative value
// to represent milliseconds.
if testAllowMillisecInPings && cr.PingInterval < 0 {
c.pingInterval = time.Duration(cr.PingInterval*-1) * time.Millisecond
} else {
// PingInterval is otherwise assumed to be in seconds.
c.pingInterval = time.Duration(cr.PingInterval) * time.Second
}
c.pingMaxOut = int(cr.PingMaxOut)
c.pingBytes, _ = (&pb.Ping{ConnID: c.connID}).Marshal()
c.pingSub = pingSub
// Set the timer now that we are set. Use lock to create
// synchronization point.
c.pingMu.Lock()
c.pingTimer = time.AfterFunc(c.pingInterval, c.pingServer)
c.pingMu.Unlock()
}
}
if unsubPingSub {
pingSub.Unsubscribe()
}
// Attach a finalizer
runtime.SetFinalizer(&c, func(sc *conn) { sc.Close() })
return &c, nil
}
// Close a connection to the stan system.
func (sc *conn) Close() error {
if sc == nil {
return ErrBadConnection
// 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() {
sc.pingMu.Lock()
// In case the timer fired while we were stopping it.
if sc.pingTimer == nil {
sc.pingMu.Unlock()
return
}
sc.pingOut++
if sc.pingOut > sc.pingMaxOut {
sc.pingMu.Unlock()
sc.closeDueToPing(ErrMaxPings)
return
}
sc.pingTimer.Reset(sc.pingInterval)
nc := sc.nc
sc.pingMu.Unlock()
// Send the PING now. If the NATS connection is reported closed,
// we are done.
if err := nc.PublishRequest(sc.pingRequests, sc.pingInbox, sc.pingBytes); 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.
sc.pingMu.Lock()
sc.pingOut = 0
sc.pingMu.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.nc == nil {
sc.Unlock()
return
}
// Stop timer, unsubscribe, fail the pubs, etc..
sc.cleanupOnClose(err)
// No need to send Close prototol, 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. Do this under pingMu lock.
sc.pingMu.Lock()
sc.nc = nil
sc.pingMu.Unlock()
// 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) {
sc.pingMu.Lock()
if sc.pingTimer != nil {
sc.pingTimer.Stop()
sc.pingTimer = nil
}
sc.pingMu.Unlock()
// Unsubscribe only if the NATS connection is not already closed...
if !sc.nc.IsClosed() {
if sc.ackSubscription != nil {
sc.ackSubscription.Unsubscribe()
}
if sc.pingSub != nil {
sc.pingSub.Unsubscribe()
}
}
// Fail all pending pubs
for guid, pubAck := range sc.pubAckMap {
if pubAck.t != nil {
pubAck.t.Stop()
}
if pubAck.ah != nil {
pubAck.ah(guid, err)
} else if pubAck.ch != nil {
pubAck.ch <- err
}
delete(sc.pubAckMap, guid)
if len(sc.pubAckChan) > 0 {
<-sc.pubAckChan
}
}
}
// Close a connection to the stan system.
func (sc *conn) Close() error {
sc.Lock()
defer sc.Unlock()
@ -271,13 +530,15 @@ func (sc *conn) Close() error {
defer nc.Close()
}
// Signals we are closed.
sc.nc = nil
// Now close ourselves.
if sc.ackSubscription != nil {
sc.ackSubscription.Unsubscribe()
}
sc.cleanupOnClose(ErrConnectionClosed)
// Signals we are closed.
// Do this also under pingMu lock so that we don't need
// to grab sc's lock in pingServer.
sc.pingMu.Lock()
sc.nc = nil
sc.pingMu.Unlock()
req := &pb.CloseRequest{ClientID: sc.clientID}
b, _ := req.Marshal()
@ -303,7 +564,10 @@ func (sc *conn) Close() error {
// closing the wrapped NATS conn will put the NATS Streaming Conn in an invalid
// state.
func (sc *conn) NatsConn() *nats.Conn {
return sc.nc
sc.RLock()
nc := sc.nc
sc.RUnlock()
return nc
}
// Process a heartbeat from the NATS Streaming cluster
@ -322,9 +586,7 @@ func (sc *conn) processAck(m *nats.Msg) {
pa := &pb.PubAck{}
err := pa.Unmarshal(m.Data)
if err != nil {
// FIXME, make closure to have context?
fmt.Printf("Error processing unmarshal\n")
return
panic(fmt.Errorf("Error during ack unmarshal: %v", err))
}
// Remove
@ -346,7 +608,10 @@ func (sc *conn) processAck(m *nats.Msg) {
// Publish will publish to the cluster and wait for an ACK.
func (sc *conn) Publish(subject string, data []byte) error {
ch := make(chan 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
@ -370,9 +635,11 @@ func (sc *conn) publishAsync(subject string, data []byte, ah AckHandler, ch chan
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 (and we same on new object)
peGUID := nuid.Next()
pe := &pb.PubMsg{ClientID: sc.clientID, Guid: peGUID, Subject: subject, Data: data}
// 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.
@ -381,25 +648,44 @@ func (sc *conn) publishAsync(subject string, data []byte, ah AckHandler, ch chan
ackSubject := sc.ackSubject
ackTimeout := sc.opts.AckTimeout
pac := sc.pubAckChan
nc := sc.nc
sc.Unlock()
// Use the buffered channel to control the number of outstanding acks.
pac <- struct{}{}
err := sc.nc.PublishRequest(subj, ackSubject, b)
if err != nil {
sc.removeAck(peGUID)
return "", err
}
err := nc.PublishRequest(subj, ackSubject, b)
// Setup the timer for expiration.
sc.Lock()
if err != nil || sc.nc == nil {
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() {
sc.removeAck(peGUID)
if a.ah != nil {
ah(peGUID, ErrTimeout)
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 {
a.ch <- ErrTimeout
pubAck.ch <- ErrTimeout
}
})
sc.Unlock()
@ -436,7 +722,7 @@ func (sc *conn) processMsg(raw *nats.Msg) {
msg := &Msg{}
err := msg.Unmarshal(raw.Data)
if err != nil {
panic("Error processing unmarshal for msg")
panic(fmt.Errorf("Error processing unmarshal for msg: %v", err))
}
// Lookup the subscription
sc.RLock()
@ -465,12 +751,11 @@ func (sc *conn) processMsg(raw *nats.Msg) {
cb(msg)
}
// Proces auto-ack
// Process auto-ack
if !isManualAck && nc != nil {
ack := &pb.Ack{Subject: msg.Subject, Sequence: msg.Sequence}
b, _ := ack.Marshal()
if err := nc.Publish(ackSubject, b); err != nil {
// FIXME(dlc) - Async error handler? Retry?
}
nc.Publish(ackSubject, b)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,15 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// 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 is a Go client for the NATS Streaming messaging system (https://nats.io).
package stan
@ -30,6 +41,14 @@ type Msg struct {
// Subscription represents a subscription within the NATS Streaming cluster. Subscriptions
// will be rate matched and follow at-least delivery semantics.
type Subscription interface {
ClearMaxPending() error
Delivered() (int64, error)
Dropped() (int, error)
IsValid() bool
MaxPending() (int, int, error)
Pending() (int, int, error)
PendingLimits() (int, int, error)
SetPendingLimits(msgLimit, bytesLimit int) error
// 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
@ -256,12 +275,97 @@ func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...Subs
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 {
if sub == nil {
return ErrBadSubscription
}
sub.Lock()
sc := sub.sc
if sc == nil {
@ -274,10 +378,6 @@ func (sub *subscription) closeOrUnsubscribe(doClose bool) error {
sub.inboxSub = nil
sub.Unlock()
if sc == nil {
return ErrBadSubscription
}
sc.Lock()
if sc.nc == nil {
sc.Unlock()
@ -342,12 +442,8 @@ func (msg *Msg) Ack() error {
if msg == nil {
return ErrNilMsg
}
// Look up subscription
// Look up subscription (cannot be nil)
sub := msg.Sub.(*subscription)
if sub == nil {
return ErrBadSubscription
}
sub.RLock()
ackSubject := sub.ackInbox
isManualAck := sub.opts.ManualAcks
@ -355,6 +451,9 @@ func (msg *Msg) Ack() error {
sub.RUnlock()
// Check for error conditions.
if !isManualAck {
return ErrManualAck
}
if sc == nil {
return ErrBadSubscription
}
@ -365,9 +464,6 @@ func (msg *Msg) Ack() error {
if nc == nil {
return ErrBadConnection
}
if !isManualAck {
return ErrManualAck
}
// Ack here.
ack := &pb.Ack{Subject: msg.Subject, Sequence: msg.Sequence}

View File

@ -1,9 +1,9 @@
language: go
sudo: false
go:
- 1.10.x
- 1.9.x
- 1.8.x
- 1.7.x
install:
- go get -t ./...
- go get github.com/nats-io/gnatsd
@ -18,4 +18,4 @@ before_script:
- megacheck -ignore "$(cat staticcheck.ignore)" ./...
script:
- go test -i -race ./...
- if [[ "$TRAVIS_GO_VERSION" == 1.7.* ]]; then ./scripts/cov.sh TRAVIS; else go test -v -race ./...; fi
- if [[ "$TRAVIS_GO_VERSION" == 1.9.* ]]; then ./scripts/cov.sh TRAVIS; else go test -v -race ./...; fi

View File

@ -1,20 +1,201 @@
The MIT License (MIT)
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Copyright (c) 2012-2017 Apcera Inc.
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
1. Definitions.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"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.

7
gateway/vendor/github.com/nats-io/go-nats/OWNERS generated vendored Normal file
View File

@ -0,0 +1,7 @@
reviewers:
- derekcollison
- kozlovic
- wallyqs
approvers:
- derekcollison
- kozlovic

View File

@ -1,7 +1,7 @@
# NATS - Go Client
A [Go](http://golang.org) client for the [NATS messaging system](https://nats.io).
[![License MIT](https://img.shields.io/badge/License-MIT-blue.svg)](http://opensource.org/licenses/MIT)
[![License Apache 2](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Go Report Card](https://goreportcard.com/badge/github.com/nats-io/go-nats)](https://goreportcard.com/report/github.com/nats-io/go-nats) [![Build Status](https://travis-ci.org/nats-io/go-nats.svg?branch=master)](http://travis-ci.org/nats-io/go-nats) [![GoDoc](https://godoc.org/github.com/nats-io/go-nats?status.svg)](http://godoc.org/github.com/nats-io/go-nats) [![Coverage Status](https://coveralls.io/repos/nats-io/go-nats/badge.svg?branch=master)](https://coveralls.io/r/nats-io/go-nats?branch=master)
## Installation
@ -327,24 +327,5 @@ err := c.RequestWithContext(ctx, "foo", req, resp)
## License
(The MIT License)
Copyright (c) 2012-2017 Apcera Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Unless otherwise noted, the NATS source files are distributed
under the Apache Version 2.0 license found in the LICENSE file.

View File

@ -1,4 +1,15 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// 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 bench

View File

@ -1,3 +1,16 @@
// 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 bench
import (

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// 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.
// +build go1.7

View File

@ -1,4 +1,15 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-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 nats

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 nats_test
import (

View File

@ -1,4 +1,15 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-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 builtin

View File

@ -1,4 +1,15 @@
// Copyright 2013-2015 Apcera Inc. All rights reserved.
// Copyright 2013-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 builtin

View File

@ -1,4 +1,15 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-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 builtin

View File

@ -1,4 +1,15 @@
// Copyright 2015 Apcera Inc. All rights reserved.
// Copyright 2015-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 protobuf

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 nats_test
import (

View File

@ -1,4 +1,15 @@
// Copyright 2015 Apcera Inc. All rights reserved.
// Copyright 2015-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 main

View File

@ -1,4 +1,16 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// +build ignore
package main

View File

@ -1,4 +1,16 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// +build ignore
package main

View File

@ -1,4 +1,16 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// +build ignore
package main

View File

@ -1,4 +1,16 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// +build ignore
package main

View File

@ -1,4 +1,16 @@
// Copyright 2012-2016 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// +build ignore
package main

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// A Go client for the NATS messaging system (https://nats.io).
package nats
@ -11,15 +22,16 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"net/url"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/nats-io/go-nats/util"
@ -28,7 +40,7 @@ import (
// Default Constants
const (
Version = "1.3.1"
Version = "1.5.0"
DefaultURL = "nats://localhost:4222"
DefaultPort = 4222
DefaultMaxReconnect = 60
@ -351,6 +363,12 @@ type Msg struct {
Data []byte
Sub *Subscription
next *Msg
barrier *barrierInfo
}
type barrierInfo struct {
refs int64
f func()
}
// Tracks various stats received and sent on this connection,
@ -528,6 +546,14 @@ func MaxReconnects(max int) Option {
}
}
// ReconnectBufSize sets the buffer size of messages kept while busy reconnecting
func ReconnectBufSize(size int) Option {
return func(o *Options) error {
o.ReconnectBufSize = size
return nil
}
}
// Timeout is an Option to set the timeout for Dial on a connection.
func Timeout(t time.Duration) Option {
return func(o *Options) error {
@ -568,7 +594,7 @@ func DiscoveredServersHandler(cb ConnHandler) Option {
}
}
// ErrHandler is an Option to set the async error handler.
// ErrorHandler is an Option to set the async error handler.
func ErrorHandler(cb ErrHandler) Option {
return func(o *Options) error {
o.AsyncErrorCB = cb
@ -615,7 +641,7 @@ func SetCustomDialer(dialer CustomDialer) Option {
}
}
// UseOldRequestyStyle is an Option to force usage of the old Request style.
// UseOldRequestStyle is an Option to force usage of the old Request style.
func UseOldRequestStyle() Option {
return func(o *Options) error {
o.UseOldRequestStyle = true
@ -665,7 +691,7 @@ func (nc *Conn) SetClosedHandler(cb ConnHandler) {
nc.Opts.ClosedCB = cb
}
// SetErrHandler will set the async error handler.
// SetErrorHandler will set the async error handler.
func (nc *Conn) SetErrorHandler(cb ErrHandler) {
if nc == nil {
return
@ -1094,7 +1120,7 @@ func (nc *Conn) connect() error {
} else {
// Cancel out default connection refused, will trigger the
// No servers error conditional
if matched, _ := regexp.Match(`connection refused`, []byte(err.Error())); matched {
if strings.Contains(err.Error(), "connection refused") {
returnedErr = nil
}
}
@ -1229,38 +1255,40 @@ func (nc *Conn) sendConnect() error {
return err
}
// Now read the response from the server.
br := bufio.NewReaderSize(nc.conn, defaultBufSize)
line, err := br.ReadString('\n')
// We don't want to read more than we need here, otherwise
// we would need to transfer the excess read data to the readLoop.
// Since in normal situations we just are looking for a PONG\r\n,
// reading byte-by-byte here is ok.
proto, err := nc.readProto()
if err != nil {
return err
}
// If opts.Verbose is set, handle +OK
if nc.Opts.Verbose && line == okProto {
if nc.Opts.Verbose && proto == okProto {
// Read the rest now...
line, err = br.ReadString('\n')
proto, err = nc.readProto()
if err != nil {
return err
}
}
// We expect a PONG
if line != pongProto {
if proto != pongProto {
// But it could be something else, like -ERR
// Since we no longer use ReadLine(), trim the trailing "\r\n"
line = strings.TrimRight(line, "\r\n")
proto = strings.TrimRight(proto, "\r\n")
// If it's a server error...
if strings.HasPrefix(line, _ERR_OP_) {
if strings.HasPrefix(proto, _ERR_OP_) {
// Remove -ERR, trim spaces and quotes, and convert to lower case.
line = normalizeErr(line)
return errors.New("nats: " + line)
proto = normalizeErr(proto)
return errors.New("nats: " + proto)
}
// Notify that we got an unexpected protocol.
return fmt.Errorf("nats: expected '%s', got '%s'", _PONG_OP_, line)
return fmt.Errorf("nats: expected '%s', got '%s'", _PONG_OP_, proto)
}
// This is where we are truly connected.
@ -1269,6 +1297,29 @@ func (nc *Conn) sendConnect() error {
return nil
}
// reads a protocol one byte at a time.
func (nc *Conn) readProto() (string, error) {
var (
_buf = [10]byte{}
buf = _buf[:0]
b = [1]byte{}
protoEnd = byte('\n')
)
for {
if _, err := nc.conn.Read(b[:1]); err != nil {
// Do not report EOF error
if err == io.EOF {
return string(buf), nil
}
return "", err
}
buf = append(buf, b[0])
if b[0] == protoEnd {
return string(buf), nil
}
}
}
// A control protocol line.
type control struct {
op, args string
@ -1460,10 +1511,12 @@ func (nc *Conn) processOpErr(err error) {
nc.conn = nil
}
// Create a new pending buffer to underpin the bufio Writer while
// we are reconnecting.
nc.pending = &bytes.Buffer{}
nc.bw = bufio.NewWriterSize(nc.pending, nc.Opts.ReconnectBufSize)
// Reset pending buffers before reconnecting.
if nc.pending == nil {
nc.pending = new(bytes.Buffer)
}
nc.pending.Reset()
nc.bw.Reset(nc.pending)
go nc.doReconnect()
nc.mu.Unlock()
@ -1571,6 +1624,13 @@ func (nc *Conn) waitForMsgs(s *Subscription) {
if s.pHead == nil {
s.pTail = nil
}
if m.barrier != nil {
s.mu.Unlock()
if atomic.AddInt64(&m.barrier.refs, -1) == 0 {
m.barrier.f()
}
continue
}
s.pMsgs--
s.pBytes -= len(m.Data)
}
@ -1599,6 +1659,19 @@ func (nc *Conn) waitForMsgs(s *Subscription) {
break
}
}
// Check for barrier messages
s.mu.Lock()
for m := s.pHead; m != nil; m = s.pHead {
if m.barrier != nil {
s.mu.Unlock()
if atomic.AddInt64(&m.barrier.refs, -1) == 0 {
m.barrier.f()
}
s.mu.Lock()
}
s.pHead = m.next
}
s.mu.Unlock()
}
// processMsg is called by parse and will place the msg on the
@ -1812,33 +1885,68 @@ func (nc *Conn) processInfo(info string) error {
if info == _EMPTY_ {
return nil
}
if err := json.Unmarshal([]byte(info), &nc.info); err != nil {
ncInfo := serverInfo{}
if err := json.Unmarshal([]byte(info), &ncInfo); err != nil {
return err
}
// Copy content into connection's info structure.
nc.info = ncInfo
// The array could be empty/not present on initial connect,
// if advertise is disabled on that server, or servers that
// did not include themselves in the async INFO protocol.
// If empty, do not remove the implicit servers from the pool.
if len(ncInfo.ConnectURLs) == 0 {
return nil
}
// Note about pool randomization: when the pool was first created,
// it was randomized (if allowed). We keep the order the same (removing
// implicit servers that are no longer sent to us). New URLs are sent
// to us in no specific order so don't need extra randomization.
hasNew := false
// This is what we got from the server we are connected to.
urls := nc.info.ConnectURLs
if len(urls) > 0 {
added := false
// If randomization is allowed, shuffle the received array, not the
// entire pool. We want to preserve the pool's order up to this point
// (this would otherwise be problematic for the (re)connect loop).
if !nc.Opts.NoRandomize {
for i := range urls {
j := rand.Intn(i + 1)
urls[i], urls[j] = urls[j], urls[i]
}
}
// Transform that to a map for easy lookups
tmp := make(map[string]struct{}, len(urls))
for _, curl := range urls {
if _, present := nc.urls[curl]; !present {
if err := nc.addURLToPool(fmt.Sprintf("nats://%s", curl), true); err != nil {
tmp[curl] = struct{}{}
}
// Walk the pool and removed the implicit servers that are no longer in the
// given array/map
sp := nc.srvPool
for i := 0; i < len(sp); i++ {
srv := sp[i]
curl := srv.url.Host
// Check if this URL is in the INFO protocol
_, inInfo := tmp[curl]
// Remove from the temp map so that at the end we are left with only
// new (or restarted) servers that need to be added to the pool.
delete(tmp, curl)
// Keep servers that were set through Options, but also the one that
// we are currently connected to (even if it is a discovered server).
if !srv.isImplicit || srv.url == nc.url {
continue
}
added = true
if !inInfo {
// Remove from server pool. Keep current order.
copy(sp[i:], sp[i+1:])
nc.srvPool = sp[:len(sp)-1]
sp = nc.srvPool
i--
}
}
if added && !nc.initc && nc.Opts.DiscoveredServersCB != nil {
// If there are any left in the tmp map, these are new (or restarted) servers
// and need to be added to the pool.
for curl := range tmp {
// Before adding, check if this is a new (as in never seen) URL.
// This is used to figure out if we invoke the DiscoveredServersCB
if _, present := nc.urls[curl]; !present {
hasNew = true
}
nc.addURLToPool(fmt.Sprintf("nats://%s", curl), true)
}
if hasNew && !nc.initc && nc.Opts.DiscoveredServersCB != nil {
nc.ach <- func() { nc.Opts.DiscoveredServersCB(nc) }
}
}
return nil
}
@ -3006,3 +3114,51 @@ func (nc *Conn) TLSRequired() bool {
defer nc.mu.Unlock()
return nc.info.TLSRequired
}
// Barrier schedules the given function `f` to all registered asynchronous
// subscriptions.
// Only the last subscription to see this barrier will invoke the function.
// If no subscription is registered at the time of this call, `f()` is invoked
// right away.
// ErrConnectionClosed is returned if the connection is closed prior to
// the call.
func (nc *Conn) Barrier(f func()) error {
nc.mu.Lock()
if nc.isClosed() {
nc.mu.Unlock()
return ErrConnectionClosed
}
nc.subsMu.Lock()
// Need to figure out how many non chan subscriptions there are
numSubs := 0
for _, sub := range nc.subs {
if sub.typ == AsyncSubscription {
numSubs++
}
}
if numSubs == 0 {
nc.subsMu.Unlock()
nc.mu.Unlock()
f()
return nil
}
barrier := &barrierInfo{refs: int64(numSubs), f: f}
for _, sub := range nc.subs {
sub.mu.Lock()
if sub.mch == nil {
msg := &Msg{barrier: barrier}
// Push onto the async pList
if sub.pTail != nil {
sub.pTail.next = msg
} else {
sub.pHead = msg
sub.pCond.Signal()
}
sub.pTail = msg
}
sub.mu.Unlock()
}
nc.subsMu.Unlock()
nc.mu.Unlock()
return nil
}

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 nats
////////////////////////////////////////////////////////////////////////////////
@ -10,6 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"runtime"
"strings"
@ -40,7 +54,7 @@ func stackFatalf(t *testing.T, f string, args ...interface{}) {
lines = append(lines, msg)
// Generate the Stack of callers: Skip us and verify* frames.
for i := 2; true; i++ {
for i := 1; true; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
@ -51,6 +65,23 @@ func stackFatalf(t *testing.T, f string, args ...interface{}) {
t.Fatalf("%s", strings.Join(lines, "\n"))
}
func TestVersionMatchesTag(t *testing.T) {
tag := os.Getenv("TRAVIS_TAG")
if tag == "" {
t.SkipNow()
}
// We expect a tag of the form vX.Y.Z. If that's not the case,
// we need someone to have a look. So fail if first letter is not
// a `v`
if tag[0] != 'v' {
t.Fatalf("Expect tag to start with `v`, tag is: %s", tag)
}
// Strip the `v` from the tag for the version comparison.
if Version != tag[1:] {
t.Fatalf("Version (%s) does not match tag (%s)", Version, tag[1:])
}
}
////////////////////////////////////////////////////////////////////////////////
// Reconnect tests
////////////////////////////////////////////////////////////////////////////////
@ -935,7 +966,7 @@ func TestAsyncINFO(t *testing.T) {
}
}
checkPool := func(inThatOrder bool, urls ...string) {
checkPool := func(urls ...string) {
// Check both pool and urls map
if len(c.srvPool) != len(urls) {
stackFatalf(t, "Pool should have %d elements, has %d", len(urls), len(c.srvPool))
@ -943,35 +974,27 @@ func TestAsyncINFO(t *testing.T) {
if len(c.urls) != len(urls) {
stackFatalf(t, "Map should have %d elements, has %d", len(urls), len(c.urls))
}
for i, url := range urls {
if inThatOrder {
if c.srvPool[i].url.Host != url {
stackFatalf(t, "Pool should have %q at index %q, has %q", url, i, c.srvPool[i].url.Host)
}
} else {
for _, url := range urls {
if _, present := c.urls[url]; !present {
stackFatalf(t, "Pool should have %q", url)
}
}
}
}
// Now test the decoding of "connect_urls"
// No randomize for now
c.Opts.NoRandomize = true
// Reset the pool
c.setupServerPool()
// Reinitialize the parser
c.ps = &parseState{}
info = []byte("INFO {\"connect_urls\":[\"localhost:5222\"]}\r\n")
info = []byte("INFO {\"connect_urls\":[\"localhost:4222\", \"localhost:5222\"]}\r\n")
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool now should contain localhost:4222 (the default URL) and localhost:5222
checkPool(true, "localhost:4222", "localhost:5222")
checkPool("localhost:4222", "localhost:5222")
// Make sure that if client receives the same, it is not added again.
err = c.parse(info)
@ -979,84 +1002,16 @@ func TestAsyncINFO(t *testing.T) {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool should still contain localhost:4222 (the default URL) and localhost:5222
checkPool(true, "localhost:4222", "localhost:5222")
checkPool("localhost:4222", "localhost:5222")
// Receive a new URL
info = []byte("INFO {\"connect_urls\":[\"localhost:6222\"]}\r\n")
info = []byte("INFO {\"connect_urls\":[\"localhost:4222\", \"localhost:5222\", \"localhost:6222\"]}\r\n")
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool now should contain localhost:4222 (the default URL) localhost:5222 and localhost:6222
checkPool(true, "localhost:4222", "localhost:5222", "localhost:6222")
// Receive more than 1 URL at once
info = []byte("INFO {\"connect_urls\":[\"localhost:7222\", \"localhost:8222\"]}\r\n")
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool now should contain localhost:4222 (the default URL) localhost:5222, localhost:6222
// localhost:7222 and localhost:8222
checkPool(true, "localhost:4222", "localhost:5222", "localhost:6222", "localhost:7222", "localhost:8222")
// Test with pool randomization now. Note that with randominzation,
// the initial pool is randomize, then each array of urls that the
// client gets from the INFO protocol is randomized, but added to
// the end of the pool.
c.Opts.NoRandomize = false
c.setupServerPool()
info = []byte("INFO {\"connect_urls\":[\"localhost:5222\"]}\r\n")
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool now should contain localhost:4222 (the default URL) and localhost:5222
checkPool(true, "localhost:4222", "localhost:5222")
// Make sure that if client receives the same, it is not added again.
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool should still contain localhost:4222 (the default URL) and localhost:5222
checkPool(true, "localhost:4222", "localhost:5222")
// Receive a new URL
info = []byte("INFO {\"connect_urls\":[\"localhost:6222\"]}\r\n")
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool now should contain localhost:4222 (the default URL) localhost:5222 and localhost:6222
checkPool(true, "localhost:4222", "localhost:5222", "localhost:6222")
// Receive more than 1 URL at once. Add more than 2 to increase the chance of
// the array being shuffled.
info = []byte("INFO {\"connect_urls\":[\"localhost:7222\", \"localhost:8222\", " +
"\"localhost:9222\", \"localhost:10222\", \"localhost:11222\"]}\r\n")
err = c.parse(info)
if err != nil || c.ps.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.ps.state, err)
}
// Pool now should contain localhost:4222 (the default URL) localhost:5222, localhost:6222
// localhost:7222, localhost:8222, localhost:9222, localhost:10222 and localhost:11222
checkPool(false, "localhost:4222", "localhost:5222", "localhost:6222", "localhost:7222", "localhost:8222",
"localhost:9222", "localhost:10222", "localhost:11222")
// Finally, check that (part of) the pool should be randomized.
allUrls := []string{"localhost:4222", "localhost:5222", "localhost:6222", "localhost:7222", "localhost:8222",
"localhost:9222", "localhost:10222", "localhost:11222"}
same := 0
for i, url := range c.srvPool {
if url.url.Host == allUrls[i] {
same++
}
}
if same == len(allUrls) {
t.Fatal("Pool does not seem to be randomized")
}
checkPool("localhost:4222", "localhost:5222", "localhost:6222")
// Check that pool may be randomized on setup, but new URLs are always
// added at end of pool.

View File

@ -1,4 +1,15 @@
// Copyright 2013-2017 Apcera Inc. All rights reserved.
// Copyright 2013-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 nats

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// Copyright 2012-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 nats

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 test
import (

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 test
import (

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 test
import (

View File

@ -1,7 +1,22 @@
// Copyright 2012-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 test
import (
"fmt"
"math"
"net"
"regexp"
"runtime"
"strings"
@ -9,6 +24,7 @@ import (
"testing"
"time"
"github.com/nats-io/gnatsd/server"
"github.com/nats-io/gnatsd/test"
"github.com/nats-io/go-nats"
)
@ -25,6 +41,17 @@ var testServers = []string{
var servers = strings.Join(testServers, ",")
func serverVersionAtLeast(major, minor, update int) error {
var (
ma, mi, up int
)
fmt.Sscanf(server.VERSION, "%d.%d.%d", &ma, &mi, &up)
if ma > major || (ma == major && mi > minor) || (ma == major && mi == minor && up >= update) {
return nil
}
return fmt.Errorf("Server version is %v, requires %d.%d.%d+", server.VERSION, major, minor, update)
}
func TestServersOption(t *testing.T) {
opts := nats.GetDefaultOptions()
opts.NoRandomize = true
@ -604,3 +631,230 @@ func TestPingReconnect(t *testing.T) {
}
}
}
type checkPoolUpdatedDialer struct {
conn net.Conn
first, final bool
ra int
}
func (d *checkPoolUpdatedDialer) Dial(network, address string) (net.Conn, error) {
doReal := false
if d.first {
d.first = false
doReal = true
} else if d.final {
d.ra++
return nil, fmt.Errorf("On purpose")
} else {
d.ra++
if d.ra == 15 {
d.ra = 0
doReal = true
}
}
if doReal {
c, err := net.Dial(network, address)
if err != nil {
return nil, err
}
d.conn = c
return c, nil
}
return nil, fmt.Errorf("On purpose")
}
func TestServerPoolUpdatedWhenRouteGoesAway(t *testing.T) {
if err := serverVersionAtLeast(1, 0, 7); err != nil {
t.Skipf(err.Error())
}
s1Opts := test.DefaultTestOptions
s1Opts.Host = "127.0.0.1"
s1Opts.Port = 4222
s1Opts.Cluster.Host = "127.0.0.1"
s1Opts.Cluster.Port = 6222
s1Opts.Routes = server.RoutesFromStr("nats://127.0.0.1:6223,nats://127.0.0.1:6224")
s1 := test.RunServer(&s1Opts)
defer s1.Shutdown()
s1Url := "nats://127.0.0.1:4222"
s2Url := "nats://127.0.0.1:4223"
s3Url := "nats://127.0.0.1:4224"
ch := make(chan bool, 1)
chch := make(chan bool, 1)
connHandler := func(_ *nats.Conn) {
chch <- true
}
nc, err := nats.Connect(s1Url,
nats.ReconnectHandler(connHandler),
nats.DiscoveredServersHandler(func(_ *nats.Conn) {
ch <- true
}))
if err != nil {
t.Fatalf("Error on connect")
}
s2Opts := test.DefaultTestOptions
s2Opts.Host = "127.0.0.1"
s2Opts.Port = s1Opts.Port + 1
s2Opts.Cluster.Host = "127.0.0.1"
s2Opts.Cluster.Port = 6223
s2Opts.Routes = server.RoutesFromStr("nats://127.0.0.1:6222,nats://127.0.0.1:6224")
s2 := test.RunServer(&s2Opts)
defer s2.Shutdown()
// Wait to be notified
if err := Wait(ch); err != nil {
t.Fatal("New server callback was not invoked")
}
checkPool := func(expected []string) {
// Don't use discovered here, but Servers to have the full list.
// Also, there may be cases where the mesh is not formed yet,
// so try again on failure.
var (
ds []string
timeout = time.Now().Add(5 * time.Second)
)
for time.Now().Before(timeout) {
ds = nc.Servers()
if len(ds) == len(expected) {
m := make(map[string]struct{}, len(ds))
for _, url := range ds {
m[url] = struct{}{}
}
ok := true
for _, url := range expected {
if _, present := m[url]; !present {
ok = false
break
}
}
if ok {
return
}
}
time.Sleep(50 * time.Millisecond)
}
stackFatalf(t, "Expected %v, got %v", expected, ds)
}
// Verify that we now know about s2
checkPool([]string{s1Url, s2Url})
s3Opts := test.DefaultTestOptions
s3Opts.Host = "127.0.0.1"
s3Opts.Port = s2Opts.Port + 1
s3Opts.Cluster.Host = "127.0.0.1"
s3Opts.Cluster.Port = 6224
s3Opts.Routes = server.RoutesFromStr("nats://127.0.0.1:6222,nats://127.0.0.1:6223")
s3 := test.RunServer(&s3Opts)
defer s3.Shutdown()
// Wait to be notified
if err := Wait(ch); err != nil {
t.Fatal("New server callback was not invoked")
}
// Verify that we now know about s3
checkPool([]string{s1Url, s2Url, s3Url})
// Stop s1. Since this was passed to the Connect() call, this one should
// still be present.
s1.Shutdown()
// Wait for reconnect
if err := Wait(chch); err != nil {
t.Fatal("Reconnect handler not invoked")
}
checkPool([]string{s1Url, s2Url, s3Url})
// Check the server we reconnected to.
reConnectedTo := nc.ConnectedUrl()
expected := []string{s1Url}
restartS2 := false
if reConnectedTo == s2Url {
restartS2 = true
s2.Shutdown()
expected = append(expected, s3Url)
} else if reConnectedTo == s3Url {
s3.Shutdown()
expected = append(expected, s2Url)
} else {
t.Fatalf("Unexpected server client has reconnected to: %v", reConnectedTo)
}
// Wait for reconnect
if err := Wait(chch); err != nil {
t.Fatal("Reconnect handler not invoked")
}
// The implicit server that we just shutdown should have been removed from the pool
checkPool(expected)
// Restart the one that was shutdown and check that it is now back in the pool
if restartS2 {
s2 = test.RunServer(&s2Opts)
defer s2.Shutdown()
expected = append(expected, s2Url)
} else {
s3 = test.RunServer(&s3Opts)
defer s3.Shutdown()
expected = append(expected, s3Url)
}
// Since this is not a "new" server, the DiscoveredServersCB won't be invoked.
checkPool(expected)
nc.Close()
// Restart s1
s1 = test.RunServer(&s1Opts)
defer s1.Shutdown()
// We should have all 3 servers running now...
// Create a client connection with special dialer.
d := &checkPoolUpdatedDialer{first: true}
nc, err = nats.Connect(s1Url,
nats.MaxReconnects(10),
nats.ReconnectWait(15*time.Millisecond),
nats.SetCustomDialer(d),
nats.ReconnectHandler(connHandler),
nats.ClosedHandler(connHandler))
if err != nil {
t.Fatalf("Error on connect")
}
defer nc.Close()
// Make sure that we have all 3 servers in the pool (this will wait if required)
checkPool(expected)
// Cause disconnection between client and server. We are going to reconnect
// and we want to check that when we get the INFO again with the list of
// servers, we don't lose the knowledge of how many times we tried to
// reconnect.
d.conn.Close()
// Wait for client to reconnect to a server
if err := Wait(chch); err != nil {
t.Fatal("Reconnect handler not invoked")
}
// At this point, we should have tried to reconnect 5 times to each server.
// For the one we reconnected to, its max reconnect attempts should have been
// cleared, not for the other ones.
// Cause a disconnect again and ensure we won't reconnect.
d.final = true
d.conn.Close()
// Wait for Close callback to be invoked.
if err := Wait(chch); err != nil {
t.Fatal("Close handler not invoked")
}
// Since MaxReconnect is 10, after trying 5 more times on 2 of the servers,
// these should have been removed. We have still 5 more tries for the server
// we did previously reconnect to.
// So total of reconnect attempt should be: 2*5+1*10=20
if d.ra != 20 {
t.Fatalf("Should have tried to reconnect 20 more times, got %v", d.ra)
}
nc.Close()
}

View File

@ -1,4 +1,3 @@
# Simple TLS config file
port: 4443

View File

@ -1,4 +1,3 @@
# Simple TLS config file
port: 4443

View File

@ -1,3 +1,16 @@
// Copyright 2012-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 test
import (
@ -11,6 +24,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@ -701,6 +715,7 @@ func TestCallbacksOrder(t *testing.T) {
nats.ErrorHandler(ech),
nats.ReconnectWait(50*time.Millisecond),
nats.DontRandomize())
if err != nil {
t.Fatalf("Unable to connect: %v\n", err)
}
@ -1476,12 +1491,16 @@ func TestCustomFlusherTimeout(t *testing.T) {
func TestNewServers(t *testing.T) {
s1Opts := test.DefaultTestOptions
s1Opts.Host = "127.0.0.1"
s1Opts.Port = 4222
s1Opts.Cluster.Host = "localhost"
s1Opts.Cluster.Port = 6222
s1 := test.RunServer(&s1Opts)
defer s1.Shutdown()
s2Opts := test.DefaultTestOptions
s2Opts.Host = "127.0.0.1"
s2Opts.Port = 4223
s2Opts.Port = s1Opts.Port + 1
s2Opts.Cluster.Host = "localhost"
s2Opts.Cluster.Port = 6223
@ -1525,6 +1544,8 @@ func TestNewServers(t *testing.T) {
// Start a new server.
s3Opts := test.DefaultTestOptions
s1Opts.Host = "127.0.0.1"
s1Opts.Port = 4224
s3Opts.Port = s2Opts.Port + 1
s3Opts.Cluster.Host = "localhost"
s3Opts.Cluster.Port = 6224
@ -1543,3 +1564,400 @@ func TestNewServers(t *testing.T) {
t.Fatal("Did not get our callback")
}
}
func TestBarrier(t *testing.T) {
s := RunDefaultServer()
defer s.Shutdown()
nc := NewDefaultConnection(t)
defer nc.Close()
pubMsgs := int32(0)
ch := make(chan bool, 1)
sub1, err := nc.Subscribe("pub", func(_ *nats.Msg) {
atomic.AddInt32(&pubMsgs, 1)
time.Sleep(250 * time.Millisecond)
})
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
sub2, err := nc.Subscribe("close", func(_ *nats.Msg) {
// The "close" message was sent/received lat, but
// because we are dealing with different subscriptions,
// which are dispatched by different dispatchers, and
// because the "pub" subscription is delayed, this
// callback is likely to be invoked before the sub1's
// second callback is invoked. Using the Barrier call
// here will ensure that the given function will be invoked
// after the preceding messages have been dispatched.
nc.Barrier(func() {
res := atomic.LoadInt32(&pubMsgs) == 2
ch <- res
})
})
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
// Send 2 "pub" messages followed by a "close" message
for i := 0; i < 2; i++ {
if err := nc.Publish("pub", []byte("pub msg")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
}
if err := nc.Publish("close", []byte("closing")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
select {
case ok := <-ch:
if !ok {
t.Fatal("The barrier function was invoked before the second message")
}
case <-time.After(2 * time.Second):
t.Fatal("Waited for too long...")
}
// Remove all subs
sub1.Unsubscribe()
sub2.Unsubscribe()
// Barrier should be invoked in place. Since we use buffered channel
// we are ok.
nc.Barrier(func() { ch <- true })
if err := Wait(ch); err != nil {
t.Fatal("Barrier function was not invoked")
}
if _, err := nc.Subscribe("foo", func(m *nats.Msg) {
// To check that the Barrier() function works if the subscription
// is unsubscribed after the call was made, sleep a bit here.
time.Sleep(250 * time.Millisecond)
m.Sub.Unsubscribe()
}); err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
if err := nc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
// We need to Flush here to make sure that message has been received
// and posted to subscription's internal queue before calling Barrier.
if err := nc.Flush(); err != nil {
t.Fatalf("Error on flush: %v", err)
}
nc.Barrier(func() { ch <- true })
if err := Wait(ch); err != nil {
t.Fatal("Barrier function was not invoked")
}
// Test with AutoUnsubscribe now...
sub1, err = nc.Subscribe("foo", func(m *nats.Msg) {
// Since we auto-unsubscribe with 1, there should not be another
// invocation of this callback, but the Barrier should still be
// invoked.
nc.Barrier(func() { ch <- true })
})
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
sub1.AutoUnsubscribe(1)
// Send 2 messages and flush
for i := 0; i < 2; i++ {
if err := nc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
}
if err := nc.Flush(); err != nil {
t.Fatalf("Error on flush: %v", err)
}
// Check barrier was invoked
if err := Wait(ch); err != nil {
t.Fatal("Barrier function was not invoked")
}
// Check that Barrier only affects asynchronous subscriptions
sub1, err = nc.Subscribe("foo", func(m *nats.Msg) {
nc.Barrier(func() { ch <- true })
})
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
syncSub, err := nc.SubscribeSync("foo")
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
msgChan := make(chan *nats.Msg, 1)
chanSub, err := nc.ChanSubscribe("foo", msgChan)
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
if err := nc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
if err := nc.Flush(); err != nil {
t.Fatalf("Error on flush: %v", err)
}
// Check barrier was invoked even if we did not yet consume
// from the 2 other type of subscriptions
if err := Wait(ch); err != nil {
t.Fatal("Barrier function was not invoked")
}
if _, err := syncSub.NextMsg(time.Second); err != nil {
t.Fatalf("Sync sub did not receive the message")
}
select {
case <-msgChan:
case <-time.After(time.Second):
t.Fatal("Chan sub did not receive the message")
}
chanSub.Unsubscribe()
syncSub.Unsubscribe()
sub1.Unsubscribe()
atomic.StoreInt32(&pubMsgs, 0)
// Check barrier does not prevent new messages to be delivered.
sub1, err = nc.Subscribe("foo", func(_ *nats.Msg) {
if pm := atomic.AddInt32(&pubMsgs, 1); pm == 1 {
nc.Barrier(func() {
nc.Publish("foo", []byte("second"))
nc.Flush()
})
} else if pm == 2 {
ch <- true
}
})
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
if err := nc.Publish("foo", []byte("first")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
if err := Wait(ch); err != nil {
t.Fatal("Barrier function was not invoked")
}
sub1.Unsubscribe()
// Check that barrier works if called before connection
// is closed.
if _, err := nc.Subscribe("bar", func(_ *nats.Msg) {
nc.Barrier(func() { ch <- true })
nc.Close()
}); err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
if err := nc.Publish("bar", []byte("hello")); err != nil {
t.Fatalf("Error on publish: %v", err)
}
if err := nc.Flush(); err != nil {
t.Fatalf("Error on flush: %v", err)
}
if err := Wait(ch); err != nil {
t.Fatal("Barrier function was not invoked")
}
// Finally, check that if connection is closed, Barrier returns
// an error.
if err := nc.Barrier(func() { ch <- true }); err != nats.ErrConnectionClosed {
t.Fatalf("Expected error %v, got %v", nats.ErrConnectionClosed, err)
}
// Check that one can call connection methods from Barrier
// when there is no async subscriptions
nc = NewDefaultConnection(t)
defer nc.Close()
if err := nc.Barrier(func() {
ch <- nc.TLSRequired()
}); err != nil {
t.Fatalf("Error on Barrier: %v", err)
}
if err := Wait(ch); err != nil {
t.Fatal("Barrier was blocked")
}
}
func TestReceiveInfoRightAfterFirstPong(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Error on listen: %v", err)
}
tl := l.(*net.TCPListener)
defer tl.Close()
addr := tl.Addr().(*net.TCPAddr)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
c, err := tl.Accept()
if err != nil {
return
}
defer c.Close()
// Send the initial INFO
c.Write([]byte("INFO {}\r\n"))
buf := make([]byte, 0, 100)
b := make([]byte, 100)
for {
n, err := c.Read(b)
if err != nil {
return
}
buf = append(buf, b[:n]...)
if bytes.Contains(buf, []byte("PING\r\n")) {
break
}
}
// Send PONG and following INFO in one go (or at least try).
// The processing of PONG in sendConnect() should leave the
// rest for the readLoop to process.
c.Write([]byte(fmt.Sprintf("PONG\r\nINFO {\"connect_urls\":[\"127.0.0.1:%d\", \"me:1\"]}\r\n", addr.Port)))
// Wait for client to disconnect
for {
if _, err := c.Read(buf); err != nil {
return
}
}
}()
nc, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", addr.Port))
if err != nil {
t.Fatalf("Error on connect: %v", err)
}
defer nc.Close()
var (
ds []string
timeout = time.Now().Add(2 * time.Second)
ok = false
)
for time.Now().Before(timeout) {
ds = nc.DiscoveredServers()
if len(ds) == 1 && ds[0] == "nats://me:1" {
ok = true
break
}
time.Sleep(50 * time.Millisecond)
}
nc.Close()
wg.Wait()
if !ok {
t.Fatalf("Unexpected discovered servers: %v", ds)
}
}
func TestReceiveInfoWithEmptyConnectURLs(t *testing.T) {
ready := make(chan bool, 2)
ch := make(chan bool, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
ports := []int{4222, 4223}
for i := 0; i < 2; i++ {
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", ports[i]))
if err != nil {
t.Fatalf("Error on listen: %v", err)
}
tl := l.(*net.TCPListener)
defer tl.Close()
ready <- true
c, err := tl.Accept()
if err != nil {
return
}
defer c.Close()
// Send the initial INFO
c.Write([]byte(fmt.Sprintf("INFO {\"server_id\":\"server%d\"}\r\n", (i + 1))))
buf := make([]byte, 0, 100)
b := make([]byte, 100)
for {
n, err := c.Read(b)
if err != nil {
return
}
buf = append(buf, b[:n]...)
if bytes.Contains(buf, []byte("PING\r\n")) {
break
}
}
if i == 0 {
// Send PONG and following INFO in one go (or at least try).
// The processing of PONG in sendConnect() should leave the
// rest for the readLoop to process.
c.Write([]byte("PONG\r\nINFO {\"server_id\":\"server1\",\"connect_urls\":[\"127.0.0.1:4222\", \"127.0.0.1:4223\", \"127.0.0.1:4224\"]}\r\n"))
// Wait for the notication
<-ch
// Close the connection in our side and go back into accept
c.Close()
} else {
// Send no connect ULRs (as if this was an older server that could in some cases
// send an empty array)
c.Write([]byte(fmt.Sprintf("PONG\r\nINFO {\"server_id\":\"server2\"}\r\n")))
// Wait for client to disconnect
for {
if _, err := c.Read(buf); err != nil {
return
}
}
}
}
}()
// Wait for listener to be up and running
if err := Wait(ready); err != nil {
t.Fatal("Listener not ready")
}
rch := make(chan bool)
nc, err := nats.Connect("nats://127.0.0.1:4222",
nats.ReconnectWait(50*time.Millisecond),
nats.ReconnectHandler(func(_ *nats.Conn) {
rch <- true
}))
if err != nil {
t.Fatalf("Error on connect: %v", err)
}
defer nc.Close()
var (
ds []string
timeout = time.Now().Add(2 * time.Second)
ok = false
)
for time.Now().Before(timeout) {
ds = nc.DiscoveredServers()
if len(ds) == 2 {
if (ds[0] == "nats://127.0.0.1:4223" && ds[1] == "nats://127.0.0.1:4224") ||
(ds[0] == "nats://127.0.0.1:4224" && ds[1] == "nats://127.0.0.1:4223") {
ok = true
break
}
}
time.Sleep(50 * time.Millisecond)
}
if !ok {
t.Fatalf("Unexpected discovered servers: %v", ds)
}
// Make the server close our connection
ch <- true
// Wait for the reconnect
if err := Wait(rch); err != nil {
t.Fatal("Did not reconnect")
}
// Discovered servers should still contain nats://me:1
ds = nc.DiscoveredServers()
if len(ds) != 2 ||
!((ds[0] == "nats://127.0.0.1:4223" && ds[1] == "nats://127.0.0.1:4224") ||
(ds[0] == "nats://127.0.0.1:4224" && ds[1] == "nats://127.0.0.1:4223")) {
t.Fatalf("Unexpected discovered servers list: %v", ds)
}
nc.Close()
wg.Wait()
}

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// Copyright 2012-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.
// +build go1.7

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// Copyright 2012-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 test

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// Copyright 2012-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 test

View File

@ -1,10 +1,23 @@
// Copyright 2015 Apcera Inc. All rights reserved.
// Copyright 2015-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 test
import (
"errors"
"fmt"
"runtime"
"strings"
"time"
"github.com/nats-io/gnatsd/server"
@ -37,6 +50,23 @@ func WaitTime(ch chan bool, timeout time.Duration) error {
return errors.New("timeout")
}
func stackFatalf(t tLogger, f string, args ...interface{}) {
lines := make([]string, 0, 32)
msg := fmt.Sprintf(f, args...)
lines = append(lines, msg)
// Generate the Stack of callers: Skip us and verify* frames.
for i := 1; true; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
msg := fmt.Sprintf("%d - %s:%d", i, file, line)
lines = append(lines, msg)
}
t.Fatalf("%s", strings.Join(lines, "\n"))
}
////////////////////////////////////////////////////////////////////////////////
// Creating client connections
////////////////////////////////////////////////////////////////////////////////

View File

@ -1,4 +1,15 @@
// Copyright 2012-2017 Apcera Inc. All rights reserved.
// Copyright 2012-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 test

View File

@ -1,4 +1,15 @@
// Copyright 2017 Apcera Inc. All rights reserved.
// Copyright 2017-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 test

View File

@ -1,3 +1,16 @@
// Copyright 2013-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 test
import (

View File

@ -1,3 +1,16 @@
// Copyright 2015-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 test
import (

View File

@ -1,3 +1,16 @@
// Copyright 2013-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 test
import (
@ -576,6 +589,21 @@ func TestReconnectVerbose(t *testing.T) {
}
}
func TestReconnectBufSizeOption(t *testing.T) {
s := RunDefaultServer()
defer s.Shutdown()
nc, err := nats.Connect("nats://localhost:4222", nats.ReconnectBufSize(32))
if err != nil {
t.Fatalf("Should have connected ok: %v", err)
}
defer nc.Close()
if nc.Opts.ReconnectBufSize != 32 {
t.Fatalf("ReconnectBufSize should be 32 but it is %d", nc.Opts.ReconnectBufSize)
}
}
func TestReconnectBufSize(t *testing.T) {
s := RunDefaultServer()
defer s.Shutdown()

View File

@ -1,3 +1,16 @@
// Copyright 2013-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 test
import (

View File

@ -1,3 +1,16 @@
// Copyright 2017-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 nats
import (

View File

@ -1,3 +1,16 @@
// Copyright 2017-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 nats
import (

View File

@ -1,4 +1,16 @@
// Copyright 2017 Apcera Inc. All rights reserved.
// Copyright 2017-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.
// +build go1.8
package util

View File

@ -1,4 +1,16 @@
// Copyright 2016 Apcera Inc. All rights reserved.
// 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.
// +build go1.7,!go1.8
package util

View File

@ -0,0 +1,16 @@
maintainers:
- alexellis
- rgee0
- johnmccabe
- jockdarock
- ericstoekl
- austinfrey
- itscaro
- rorpage
- kenfdev
- BurtonR
features:
- dco_check
- comments

View File

@ -0,0 +1,35 @@
<!--- Provide a general summary of the issue in the Title above -->
## Expected Behaviour
<!--- If you're describing a bug, tell us what should happen -->
<!--- If you're suggesting a change/improvement, tell us how it should work -->
## Current Behaviour
<!--- If describing a bug, tell us what happens instead of the expected behavior -->
<!--- If suggesting a change/improvement, explain the difference from current behavior -->
## Possible Solution
<!--- Not obligatory, but suggest a fix/reason for the bug, -->
<!--- or ideas how to implement the addition or change -->
## Steps to Reproduce (for bugs)
<!--- Provide a link to a live example, or an unambiguous set of steps to -->
<!--- reproduce this bug. Include code to reproduce, if relevant -->
1.
2.
3.
4.
## Context
<!--- How has this issue affected you? What are you trying to accomplish? -->
<!--- Providing context helps us come up with a solution that is most useful in the real world -->
## Your Environment
<!--- Include as many relevant details about the environment you experienced the bug in -->
* Docker version `docker version` (e.g. Docker 17.0.05 ):
* Are you using Docker Swarm or Kubernetes (FaaS-netes)?
* Operating System and version (e.g. Linux, Windows, MacOS):
* Link to your project or a code example to reproduce issue:

View File

@ -0,0 +1,31 @@
<!--- Provide a general summary of your changes in the Title above -->
## Description
<!--- Describe your changes in detail -->
## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
- [ ] I have raised an issue to propose this change ([required](https://github.com/openfaas/faas/blob/master/CONTRIBUTING.md))
## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->
## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I've read the [CONTRIBUTION](https://github.com/openfaas/faas/blob/master/CONTRIBUTING.md) guide
- [ ] I have signed-off my commits with `git commit -s`
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.

View File

@ -15,3 +15,4 @@
.idea
.DS_Store
nats-queue-worker

View File

@ -4,14 +4,29 @@ language: go
go:
- 1.9.x
install:
- echo "Please don't go get"
services:
- docker
before_script:
- curl -sSL test.docker.com | sudo -E sh
addons:
apt:
packages:
- docker-ce
install:
- echo "Please don't go get"
script:
- cd queue-worker && ./build.sh
after_success:
- if [ ! -z "$TRAVIS_TAG" ] ; then
if [ -z $DOCKER_NS ] ; then
export DOCKER_NS=functions;
fi
docker tag $DOCKER_NS/queue-worker:latest-dev $DOCKER_NS/queue-worker:$TRAVIS_TAG;
echo $DOCKER_PASSWORD | docker login -u=$DOCKER_USERNAME --password-stdin;
docker push $DOCKER_NS/queue-worker:$TRAVIS_TAG;
fi

View File

@ -1,14 +1,20 @@
FROM golang:1.9.2-alpine as golang
FROM golang:1.9.7-alpine as golang
WORKDIR /go/src/github.com/openfaas/nats-queue-worker
COPY vendor vendor
COPY handler handler
COPY main.go .
RUN go test -v ./handler/
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:3.7
RUN apk add --no-cache ca-certificates
WORKDIR /root/
RUN addgroup -S app \
&& adduser -S -g app app \
&& apk add --no-cache ca-certificates
WORKDIR /home/app
EXPOSE 8080
ENV http_proxy ""
@ -16,4 +22,7 @@ ENV https_proxy ""
COPY --from=golang /go/src/github.com/openfaas/nats-queue-worker/app .
RUN chown -R app:app ./
USER app
CMD ["./app"]

View File

@ -1,14 +1,19 @@
FROM golang:1.9.2-alpine as golang
FROM golang:1.9.7-alpine as golang
WORKDIR /go/src/github.com/openfaas/nats-queue-worker
COPY vendor vendor
COPY handler handler
COPY main.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:3.7
RUN apk add --no-cache ca-certificates
WORKDIR /root/
RUN addgroup -S app \
&& adduser -S -g app app \
&& apk add --no-cache ca-certificates
WORKDIR /home/app
EXPOSE 8080
ENV http_proxy ""
@ -16,4 +21,7 @@ ENV https_proxy ""
COPY --from=golang /go/src/github.com/openfaas/nats-queue-worker/app .
RUN chown -R app:app ./
USER app
CMD ["./app"]

View File

@ -0,0 +1,50 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/gogo/protobuf"
packages = [
"gogoproto",
"proto",
"protoc-gen-gogo/descriptor"
]
revision = "1adfc126b41513cc696b209667c8656ea7aac67c"
version = "v1.0.0"
[[projects]]
name = "github.com/nats-io/go-nats"
packages = [
".",
"encoders/builtin",
"util"
]
revision = "062418ea1c2181f52dc0f954f6204370519a868b"
version = "v1.5.0"
[[projects]]
name = "github.com/nats-io/go-nats-streaming"
packages = [
".",
"pb"
]
revision = "e15a53f85e4932540600a16b56f6c4f65f58176f"
version = "v0.4.0"
[[projects]]
name = "github.com/nats-io/nuid"
packages = ["."]
revision = "289cccf02c178dc782430d534e3c1f5b72af807f"
version = "v1.0.0"
[[projects]]
name = "github.com/openfaas/faas"
packages = ["gateway/queue"]
revision = "81334141832d6c4fc9b78e6b5e17e5330eca7606"
version = "0.8.2"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "fb873f03ba109e6e479b9133d610f50d7fd6cbcdc0fe7dcf947ae70a1eade465"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
name = "github.com/nats-io/go-nats-streaming"
version = "0.4.0"
[[constraint]]
name = "github.com/openfaas/faas"
version = "0.8.2"
[prune]
go-tests = true
unused-packages = true

View File

@ -0,0 +1,9 @@
TAG?=latest
build:
docker build --build-arg http_proxy="${http_proxy}" --build-arg https_proxy="${https_proxy}" -t functions/queue-worker:$(TAG) .
push:
docker push functions/queue-worker:$(TAG)
all: build

View File

@ -6,7 +6,7 @@ This is a queue-worker to enable asynchronous processing of function requests.
* [Read more in the async guide](https://github.com/openfaas/faas/blob/master/guide/asynchronous.md)
Hub image: [functions/queue-worker:0.1.1](https://hub.docker.com/r/functions/queue-worker/)
Hub image: [functions/queue-worker](https://hub.docker.com/r/functions/queue-worker/)
License: MIT

View File

@ -9,4 +9,3 @@ fi
echo Building functions/queue-worker:$eTAG
docker build --build-arg http_proxy=$http_proxy -t functions/queue-worker:$eTAG .

View File

@ -15,15 +15,26 @@ type NatsQueue struct {
nc stan.Conn
}
type NatsConfig interface {
GetClientID() string
}
type DefaultNatsConfig struct {
}
func (DefaultNatsConfig) GetClientID() string {
val, _ := os.Hostname()
return "faas-publisher-" + val
}
// CreateNatsQueue ready for asynchronous processing
func CreateNatsQueue(address string, port int) (*NatsQueue, error) {
func CreateNatsQueue(address string, port int, clientConfig NatsConfig) (*NatsQueue, error) {
queue1 := NatsQueue{}
var err error
natsURL := fmt.Sprintf("nats://%s:%d", address, port)
log.Printf("Opening connection to %s\n", natsURL)
val, _ := os.Hostname()
clientID := "faas-publisher-" + val
clientID := clientConfig.GetClientID()
clusterID := "faas-cluster"
nc, err := stan.Connect(clusterID, clientID, stan.NatsURL(natsURL))

View File

@ -0,0 +1,19 @@
package handler
import (
"os"
"strings"
"testing"
)
func Test_GetClientID_ContainsHostname(t *testing.T) {
c := DefaultNatsConfig{}
val := c.GetClientID()
hostname, _ := os.Hostname()
if !strings.HasSuffix(val, hostname) {
t.Errorf("GetClientID should contain hostname as suffix, got: %s", val)
t.Fail()
}
}

View File

@ -98,6 +98,7 @@ func main() {
req := queue.Request{}
unmarshalErr := json.Unmarshal(msg.Data, &req)
if unmarshalErr != nil {
log.Printf("Unmarshal error: %s with data %s", unmarshalErr, msg.Data)
return
@ -115,12 +116,10 @@ func main() {
functionURL := fmt.Sprintf("http://%s%s:8080/%s", req.Function, functionSuffix, queryString)
request, err := http.NewRequest("POST", functionURL, bytes.NewReader(req.Body))
request, err := http.NewRequest(http.MethodPost, functionURL, bytes.NewReader(req.Body))
defer request.Body.Close()
for k, v := range req.Header {
request.Header[k] = v
}
copyHeaders(request.Header, &req.Header)
res, err := client.Do(request)
var status int
@ -135,7 +134,7 @@ func main() {
if req.CallbackURL != nil {
log.Printf("Callback to: %s\n", req.CallbackURL.String())
resultStatusCode, resultErr := postResult(&client, req, functionResult, status)
resultStatusCode, resultErr := postResult(&client, res, functionResult, req.CallbackURL.String())
if resultErr != nil {
log.Println(resultErr)
} else {
@ -170,7 +169,7 @@ func main() {
if req.CallbackURL != nil {
log.Printf("Callback to: %s\n", req.CallbackURL.String())
resultStatusCode, resultErr := postResult(&client, req, functionResult, res.StatusCode)
resultStatusCode, resultErr := postResult(&client, res, functionResult, req.CallbackURL.String())
if resultErr != nil {
log.Println(resultErr)
} else {
@ -238,18 +237,21 @@ func main() {
<-cleanupDone
}
func postResult(client *http.Client, req queue.Request, result []byte, statusCode int) (int, error) {
func postResult(client *http.Client, functionRes *http.Response, result []byte, callbackURL string) (int, error) {
var reader io.Reader
if result != nil {
reader = bytes.NewReader(result)
}
request, err := http.NewRequest("POST", req.CallbackURL.String(), reader)
request, err := http.NewRequest(http.MethodPost, callbackURL, reader)
copyHeaders(request.Header, &functionRes.Header)
res, err := client.Do(request)
if err != nil {
return http.StatusBadGateway, fmt.Errorf("error posting result to URL %s %s", req.CallbackURL.String(), err.Error())
return http.StatusBadGateway, fmt.Errorf("error posting result to URL %s %s", callbackURL, err.Error())
}
if request.Body != nil {
@ -262,6 +264,14 @@ func postResult(client *http.Client, req queue.Request, result []byte, statusCod
return res.StatusCode, nil
}
func copyHeaders(destination http.Header, source *http.Header) {
for k, v := range *source {
vClone := make([]string, len(v))
copy(vClone, v)
(destination)[k] = vClone
}
}
func postReport(client *http.Client, function string, statusCode int, timeTaken float64, gatewayAddress string) (int, error) {
req := AsyncReport{
FunctionName: function,
@ -271,7 +281,7 @@ func postReport(client *http.Client, function string, statusCode int, timeTaken
targetPostback := "http://" + gatewayAddress + ":8080/system/async-report"
reqBytes, _ := json.Marshal(req)
request, err := http.NewRequest("POST", targetPostback, bytes.NewReader(reqBytes))
request, err := http.NewRequest(http.MethodPost, targetPostback, bytes.NewReader(reqBytes))
defer request.Body.Close()
res, err := client.Do(request)

View File

@ -1 +0,0 @@
queue-worker

View File

@ -1,5 +0,0 @@
github.com/openfaas/faas 4cc299d4c84e7ce10c6a5117e918c5a5b4aeb2ae
github.com/nats-io/go-nats-streaming bf8654e90f5296da96eab1e85808eb5c4b7b5541
github.com/nats-io/go-nats 34c8842105ac0b69c838a9998a239d482936c466
github.com/nats-io/nuid 3cf34f9fca4e88afa9da8eabd75e3326c9941b44
github.com/gogo/protobuf dda3e8acadcc9affc16faf33fbb229db78399245