ndb 1.26.0 → 1.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/lib/ndb/client.rb +12 -4
- data/lib/ndb/connection.rb +52 -7
- data/lib/ndb/errors.rb +8 -1
- data/lib/ndb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fee6c0494c812d9f98dd7f987fca0f10acdaf8a1cfa9b193a35b0c826b7fe930
|
|
4
|
+
data.tar.gz: 6b63face67e889e82ea6cc731d708b1602089268b1b165e63a28ec11e0130708
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d6be6c4448d8b43ce6478bdcd7ca862cd31de65aa1422ebbbb32d87e3a4930e8f672dd95f8b95fe613ead3d26f31b7a842d92b8b492d4f2fff1efcad2b5de2a0
|
|
7
|
+
data.tar.gz: ff942b549f3cc997f6f99681fa88e59d7628dc56a60615ff13d2141c030fb449a095e9004a0f894608042a377d7cd25ed701d60c1796ab3f5969f6918e0e221d
|
data/README.md
CHANGED
|
@@ -36,6 +36,8 @@ require "ndb"
|
|
|
36
36
|
```ruby
|
|
37
37
|
ndb = Ndb.connect("unix:///run/ndb/ndb.sock") # unix socket
|
|
38
38
|
ndb = Ndb.connect("tcp://10.0.0.5:7070", token: "secret") # TCP with a bearer token
|
|
39
|
+
ndb = Ndb.connect("tcp://:secret@10.0.0.5:7070/billion") # token and database in the URL
|
|
40
|
+
ndb = Ndb.connect("unix:///run/ndb/ndb.sock?db=billion") # a database on the unix socket
|
|
39
41
|
ndb = Ndb.connect # reads NDB_URL and NDB_TOKEN
|
|
40
42
|
|
|
41
43
|
ndb.health # => true when the daemon is ready (GET /_health)
|
|
@@ -55,7 +57,8 @@ Ndb::Client.new(
|
|
|
55
57
|
```
|
|
56
58
|
|
|
57
59
|
- The address is, in order: `url:` (the `addr` of `Ndb.connect`), `socket:`, env `NDB_URL`, then the default socket `/run/ndb/ndb.sock`.
|
|
58
|
-
- The token is `token:`,
|
|
60
|
+
- The token is, in order: `token:`, the token of the URL (`tcp://:<token>@host:port`), env `NDB_TOKEN`. It goes only in the `Authorization` header. A unix socket does not need it.
|
|
61
|
+
- The database is the path of the URL (`tcp://host:7070/billion`), or `?db=` (needed on a unix socket). None is the default database. See [Databases](https://ndb.clowk.in/databases).
|
|
59
62
|
- A bad address raises `ArgumentError` at once.
|
|
60
63
|
- `ready_timeout` is how long a call retries while the daemon is not ready (`not_ready`, or the socket is missing or refuses). It uses exponential backoff (50 ms up to 1 s) with jitter. A write is retried only when the daemon certainly did not process it. A read is also retried after a dropped connection. A timeout is never retried. When the deadline passes, the last error is raised.
|
|
61
64
|
- `health` does not wait: a daemon that is recovering raises `Ndb::NotReady`.
|
data/lib/ndb/client.rb
CHANGED
|
@@ -29,8 +29,11 @@ module Ndb
|
|
|
29
29
|
RETRY_CAP = 1.0
|
|
30
30
|
|
|
31
31
|
# The address is, in order: url:, socket:, env NDB_URL, the default
|
|
32
|
-
# socket. The token is token:,
|
|
33
|
-
#
|
|
32
|
+
# socket. The token is, in order: token:, the token of the address
|
|
33
|
+
# (tcp://:<token>@host:port), env NDB_TOKEN. It goes only in the
|
|
34
|
+
# Authorization header, and the unix socket does not need it. The path
|
|
35
|
+
# of the address names a database (tcp://host:7070/billion; ?db=billion
|
|
36
|
+
# for a unix socket); none is the default database.
|
|
34
37
|
#
|
|
35
38
|
# ready_timeout is how many seconds a call retries while the daemon is
|
|
36
39
|
# starting or recovering (not_ready, socket missing or refusing), with
|
|
@@ -40,7 +43,7 @@ module Ndb
|
|
|
40
43
|
# after the User-Agent of the gem, so the daemon can tell the apps apart.
|
|
41
44
|
def initialize(url: nil, socket: nil, token: nil, timeout: 30, pool_size: 16, ready_timeout: 10, user_agent: nil)
|
|
42
45
|
@endpoint = Endpoint.parse(url || socket || presence(ENV["NDB_URL"]) || DEFAULT_SOCKET)
|
|
43
|
-
@token = presence(token) || presence(ENV["NDB_TOKEN"])
|
|
46
|
+
@token = presence(token) || @endpoint.token || presence(ENV["NDB_TOKEN"])
|
|
44
47
|
@pool = Pool.new(@endpoint, timeout: timeout, size: pool_size)
|
|
45
48
|
@ready_timeout = ready_timeout.to_f
|
|
46
49
|
@user_agent = [USER_AGENT, presence(user_agent.to_s.strip)].compact.join(" ")
|
|
@@ -48,6 +51,9 @@ module Ndb
|
|
|
48
51
|
|
|
49
52
|
def socket = @endpoint.path
|
|
50
53
|
|
|
54
|
+
# The address only: never the token.
|
|
55
|
+
def inspect = "#<Ndb::Client #{@endpoint}>"
|
|
56
|
+
|
|
51
57
|
# A collection (or a pattern such as "logs-2026-*") with the query
|
|
52
58
|
# builder. definition is what the first call declares (PUT /{coll}).
|
|
53
59
|
def collection(name, definition = {})
|
|
@@ -487,9 +493,11 @@ module Ndb
|
|
|
487
493
|
end
|
|
488
494
|
end
|
|
489
495
|
|
|
490
|
-
# Adds the User-Agent,
|
|
496
|
+
# Adds the User-Agent, the database of the address (Ndb-Database) and
|
|
497
|
+
# the bearer token, when the client has them.
|
|
491
498
|
def authorize(headers)
|
|
492
499
|
headers = headers.merge("User-Agent" => @user_agent)
|
|
500
|
+
headers = headers.merge("Ndb-Database" => @endpoint.database) if @endpoint.database
|
|
493
501
|
|
|
494
502
|
@token ? headers.merge("Authorization" => "Bearer #{@token}") : headers
|
|
495
503
|
end
|
data/lib/ndb/connection.rb
CHANGED
|
@@ -12,10 +12,14 @@ module Ndb
|
|
|
12
12
|
# https:// URL (ndb behind a TLS proxy such as Caddy). The host name is
|
|
13
13
|
# resolved each time a connection opens, so a changed DNS record is
|
|
14
14
|
# followed after a reconnect.
|
|
15
|
+
#
|
|
16
|
+
# The token can be in the address, as in a Redis URL:
|
|
17
|
+
# tcp://:<token>@host:port. The user part is ignored.
|
|
15
18
|
class Endpoint
|
|
16
19
|
INVALID = "must be unix:///path, tcp://host:port or https://host"
|
|
20
|
+
NO_COLON = "put the token after a colon: tcp://:<token>@host:port"
|
|
17
21
|
|
|
18
|
-
attr_reader :path, :host, :port
|
|
22
|
+
attr_reader :path, :host, :port, :token, :database
|
|
19
23
|
|
|
20
24
|
# "unix:///path", "tcp://host:port", "https://host[:port]",
|
|
21
25
|
# "http://host[:port]", or a bare socket path.
|
|
@@ -25,18 +29,55 @@ module Ndb
|
|
|
25
29
|
return new(path: address) if address.start_with?("/")
|
|
26
30
|
|
|
27
31
|
uri = URI.parse(address)
|
|
32
|
+
token = token_of(uri, address)
|
|
33
|
+
database = database_of(uri, address)
|
|
28
34
|
|
|
29
35
|
case uri.scheme
|
|
30
|
-
when "unix" then new(path: uri.path)
|
|
31
|
-
when "tcp" then new(host: uri.host, port: uri.port)
|
|
32
|
-
when "https", "http" then new(host: uri.host, port: uri.port, tls: uri.scheme == "https")
|
|
33
|
-
else raise ArgumentError, "ndb address #{address.inspect} #{INVALID}"
|
|
36
|
+
when "unix" then new(path: uri.path, database: database)
|
|
37
|
+
when "tcp" then new(host: uri.host, port: uri.port, token: token, database: database)
|
|
38
|
+
when "https", "http" then new(host: uri.host, port: uri.port, tls: uri.scheme == "https", token: token, database: database)
|
|
39
|
+
else raise ArgumentError, "ndb address #{redact(address).inspect} #{INVALID}"
|
|
34
40
|
end
|
|
35
41
|
rescue URI::InvalidURIError
|
|
36
|
-
raise ArgumentError, "ndb address #{address.inspect} #{INVALID}"
|
|
42
|
+
raise ArgumentError, "ndb address #{redact(address).inspect} #{INVALID}"
|
|
37
43
|
end
|
|
38
44
|
|
|
39
|
-
|
|
45
|
+
# The password of the userinfo. A user with no password is refused: it
|
|
46
|
+
# is a token in the wrong place.
|
|
47
|
+
def self.token_of(uri, address)
|
|
48
|
+
return nil if uri.userinfo.nil?
|
|
49
|
+
|
|
50
|
+
raise ArgumentError, "ndb address #{redact(address).inspect}: #{NO_COLON}" if uri.password.nil? && !uri.user.to_s.empty?
|
|
51
|
+
|
|
52
|
+
token = URI::RFC2396_PARSER.unescape(uri.password.to_s)
|
|
53
|
+
|
|
54
|
+
token.empty? ? nil : token
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The database of the address: the path of a tcp or https address
|
|
58
|
+
# (tcp://host:7070/billion) or the query db (needed for a unix socket,
|
|
59
|
+
# whose path is the file). nil is the default database.
|
|
60
|
+
def self.database_of(uri, address)
|
|
61
|
+
query = URI.decode_www_form(uri.query.to_s).to_h["db"]
|
|
62
|
+
path = uri.scheme == "unix" ? "" : uri.path.to_s.delete_prefix("/").delete_suffix("/")
|
|
63
|
+
shown = redact(address).inspect
|
|
64
|
+
|
|
65
|
+
raise ArgumentError, "ndb address #{shown}: the path names one database: tcp://host:port/<database>" if path.include?("/")
|
|
66
|
+
raise ArgumentError, "ndb address #{shown}: the path and ?db= name two databases" if !path.empty? && query && path != query
|
|
67
|
+
|
|
68
|
+
database = path.empty? ? query : path
|
|
69
|
+
|
|
70
|
+
database.to_s.empty? ? nil : database
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# address with its userinfo hidden, for error messages.
|
|
74
|
+
def self.redact(address)
|
|
75
|
+
address.sub(%r{\A([a-z][a-z0-9+.-]*://)[^/]*@}i, '\1***@')
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private_class_method :token_of, :database_of
|
|
79
|
+
|
|
80
|
+
def initialize(path: nil, host: nil, port: nil, tls: false, token: nil, database: nil)
|
|
40
81
|
if path.to_s.empty? && (host.to_s.empty? || port.nil?)
|
|
41
82
|
raise ArgumentError, "ndb address needs a socket path or a host and port"
|
|
42
83
|
end
|
|
@@ -45,8 +86,12 @@ module Ndb
|
|
|
45
86
|
@host = host
|
|
46
87
|
@port = port
|
|
47
88
|
@tls = tls
|
|
89
|
+
@token = token
|
|
90
|
+
@database = database
|
|
48
91
|
end
|
|
49
92
|
|
|
93
|
+
def inspect = "#<Ndb::Endpoint #{self}>"
|
|
94
|
+
|
|
50
95
|
def unix? = !@path.to_s.empty?
|
|
51
96
|
|
|
52
97
|
def tls? = @tls
|
data/lib/ndb/errors.rb
CHANGED
|
@@ -140,6 +140,11 @@ module Ndb
|
|
|
140
140
|
class UnknownGroup < StreamError; end
|
|
141
141
|
class GroupExists < StreamError; end
|
|
142
142
|
|
|
143
|
+
# A database name that is not [a-z0-9][a-z0-9_-]{0,62}, or a read of a
|
|
144
|
+
# database that does not exist.
|
|
145
|
+
class InvalidDatabase < Error; end
|
|
146
|
+
class DatabaseNotFound < Error; end
|
|
147
|
+
|
|
143
148
|
# The daemon error code -> the Ruby class.
|
|
144
149
|
CODE_TO_ERROR = {
|
|
145
150
|
"unauthorized" => Unauthorized,
|
|
@@ -207,7 +212,9 @@ module Ndb
|
|
|
207
212
|
"invalid_stream_id" => InvalidStreamID,
|
|
208
213
|
"stream_id_too_small" => StreamIDTooSmall,
|
|
209
214
|
"unknown_group" => UnknownGroup,
|
|
210
|
-
"group_exists" => GroupExists
|
|
215
|
+
"group_exists" => GroupExists,
|
|
216
|
+
"invalid_database" => InvalidDatabase,
|
|
217
|
+
"database_not_found" => DatabaseNotFound
|
|
211
218
|
}.freeze
|
|
212
219
|
|
|
213
220
|
def self.error_for(code, message, status)
|
data/lib/ndb/version.rb
CHANGED