mirror of
https://github.com/openfaas/faas.git
synced 2025-06-23 15:23:29 +00:00
Sync async_nats work with master
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
21
gateway/vendor/github.com/nats-io/nuid/LICENSE
generated
vendored
Normal file
21
gateway/vendor/github.com/nats-io/nuid/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
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.
|
66
gateway/vendor/github.com/nats-io/nuid/README.md
generated
vendored
Normal file
66
gateway/vendor/github.com/nats-io/nuid/README.md
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
# NUID
|
||||
|
||||
[](http://opensource.org/licenses/MIT)
|
||||
[](http://goreportcard.com/report/nats-io/nuid)
|
||||
[](http://travis-ci.org/nats-io/nuid)
|
||||
[](https://github.com/nats-io/nuid/releases/tag/v1.0.0)
|
||||
[](http://godoc.org/github.com/nats-io/nuid)
|
||||
[](https://coveralls.io/github/nats-io/nuid?branch=master)
|
||||
|
||||
A highly performant unique identifier generator.
|
||||
|
||||
## Installation
|
||||
|
||||
Use the `go` command:
|
||||
|
||||
$ go get github.com/nats-io/nuid
|
||||
|
||||
## Basic Usage
|
||||
```go
|
||||
|
||||
// Utilize the global locked instance
|
||||
nuid := nuid.Next()
|
||||
|
||||
// Create an instance, these are not locked.
|
||||
n := nuid.New()
|
||||
nuid = n.Next()
|
||||
|
||||
// Generate a new crypto/rand seeded prefix.
|
||||
// Generally not needed, happens automatically.
|
||||
n.RandomizePrefix()
|
||||
```
|
||||
|
||||
## Performance
|
||||
NUID needs to be very fast to generate and be truly unique, all while being entropy pool friendly.
|
||||
NUID uses 12 bytes of crypto generated data (entropy draining), and 10 bytes of pseudo-random
|
||||
sequential data that increments with a pseudo-random increment.
|
||||
|
||||
Total length of a NUID string is 22 bytes of base 62 ascii text, so 62^22 or
|
||||
2707803647802660400290261537185326956544 possibilities.
|
||||
|
||||
NUID can generate identifiers as fast as 60ns, or ~16 million per second. There is an associated
|
||||
benchmark you can use to test performance on your own hardware.
|
||||
|
||||
## License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 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.
|
124
gateway/vendor/github.com/nats-io/nuid/nuid.go
generated
vendored
Normal file
124
gateway/vendor/github.com/nats-io/nuid/nuid.go
generated
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
// Copyright 2016 Apcera Inc. All rights reserved.
|
||||
|
||||
// A unique identifier generator that is high performance, very fast, and tries to be entropy pool friendly.
|
||||
package nuid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
prand "math/rand"
|
||||
)
|
||||
|
||||
// NUID needs to be very fast to generate and truly unique, all while being entropy pool friendly.
|
||||
// We will use 12 bytes of crypto generated data (entropy draining), and 10 bytes of sequential data
|
||||
// that is started at a pseudo random number and increments with a pseudo-random increment.
|
||||
// Total is 22 bytes of base 62 ascii text :)
|
||||
|
||||
// Version of the library
|
||||
const Version = "1.0.0"
|
||||
|
||||
const (
|
||||
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
base = 62
|
||||
preLen = 12
|
||||
seqLen = 10
|
||||
maxSeq = int64(839299365868340224) // base^seqLen == 62^10
|
||||
minInc = int64(33)
|
||||
maxInc = int64(333)
|
||||
totalLen = preLen + seqLen
|
||||
)
|
||||
|
||||
type NUID struct {
|
||||
pre []byte
|
||||
seq int64
|
||||
inc int64
|
||||
}
|
||||
|
||||
type lockedNUID struct {
|
||||
sync.Mutex
|
||||
*NUID
|
||||
}
|
||||
|
||||
// Global NUID
|
||||
var globalNUID *lockedNUID
|
||||
|
||||
// Seed sequential random with crypto or math/random and current time
|
||||
// and generate crypto prefix.
|
||||
func init() {
|
||||
r, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
|
||||
if err != nil {
|
||||
prand.Seed(time.Now().UnixNano())
|
||||
} else {
|
||||
prand.Seed(r.Int64())
|
||||
}
|
||||
globalNUID = &lockedNUID{NUID: New()}
|
||||
globalNUID.RandomizePrefix()
|
||||
}
|
||||
|
||||
// New will generate a new NUID and properly initialize the prefix, sequential start, and sequential increment.
|
||||
func New() *NUID {
|
||||
n := &NUID{
|
||||
seq: prand.Int63n(maxSeq),
|
||||
inc: minInc + prand.Int63n(maxInc-minInc),
|
||||
pre: make([]byte, preLen),
|
||||
}
|
||||
n.RandomizePrefix()
|
||||
return n
|
||||
}
|
||||
|
||||
// Generate the next NUID string from the global locked NUID instance.
|
||||
func Next() string {
|
||||
globalNUID.Lock()
|
||||
nuid := globalNUID.Next()
|
||||
globalNUID.Unlock()
|
||||
return nuid
|
||||
}
|
||||
|
||||
// Generate the next NUID string.
|
||||
func (n *NUID) Next() string {
|
||||
// Increment and capture.
|
||||
n.seq += n.inc
|
||||
if n.seq >= maxSeq {
|
||||
n.RandomizePrefix()
|
||||
n.resetSequential()
|
||||
}
|
||||
seq := n.seq
|
||||
|
||||
// Copy prefix
|
||||
var b [totalLen]byte
|
||||
bs := b[:preLen]
|
||||
copy(bs, n.pre)
|
||||
|
||||
// copy in the seq in base36.
|
||||
for i, l := len(b), seq; i > preLen; l /= base {
|
||||
i -= 1
|
||||
b[i] = digits[l%base]
|
||||
}
|
||||
return string(b[:])
|
||||
}
|
||||
|
||||
// Resets the sequential portion of the NUID.
|
||||
func (n *NUID) resetSequential() {
|
||||
n.seq = prand.Int63n(maxSeq)
|
||||
n.inc = minInc + prand.Int63n(maxInc-minInc)
|
||||
}
|
||||
|
||||
// Generate a new prefix from crypto/rand.
|
||||
// This call *can* drain entropy and will be called automatically when we exhaust the sequential range.
|
||||
// Will panic if it gets an error from rand.Int()
|
||||
func (n *NUID) RandomizePrefix() {
|
||||
var cb [preLen]byte
|
||||
cbs := cb[:]
|
||||
if nb, err := rand.Read(cbs); nb != preLen || err != nil {
|
||||
panic(fmt.Sprintf("nuid: failed generating crypto random number: %v\n", err))
|
||||
}
|
||||
|
||||
for i := 0; i < preLen; i++ {
|
||||
n.pre[i] = digits[int(cbs[i])%base]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user