bzync-nextsql 0.1.0 → 0.1.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/lib/nextsql/client.rb +13 -5
- data/lib/nextsql/errors.rb +9 -1
- data/lib/nextsql/protocol.rb +26 -5
- data/lib/nextsql/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: c3ea6924ca4732695f486e9def584fd7d8eb6dfb90e646c54819c047ee4ff61a
|
|
4
|
+
data.tar.gz: e70bf019af638b3ba62255994dfe881a033c340ccc1f6f20732a625e3a0dd19e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8115ef2bd516eefc60bc59c5a237e71f668571cc95cdaf97cd37928eb73582552fdbd912ff6ec16c2b46256218dc89f0c853049cad0e7038aae895aa3b6dc573
|
|
7
|
+
data.tar.gz: 27caa6edbcf6b55c10e9af5f2fb59159a32d3d4f9cbfbb5bdd96f1ba744b7435ccf9a1430585219ed8a6fb5fdaaed2efdd2063de75752f5186cb2b55486962b2
|
data/lib/nextsql/client.rb
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
require "socket"
|
|
8
8
|
require "openssl"
|
|
9
|
+
require "ipaddr"
|
|
9
10
|
|
|
10
11
|
require_relative "protocol"
|
|
11
12
|
require_relative "errors"
|
|
@@ -40,7 +41,6 @@ module NextSQL
|
|
|
40
41
|
# from multiple threads/fibers — open one Connection per worker, or use
|
|
41
42
|
# +Cluster+ which pools one connection per node.
|
|
42
43
|
class Connection
|
|
43
|
-
LOOPBACK_RE = /\A127\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/.freeze
|
|
44
44
|
CONNECT_TIMEOUT = 10.0
|
|
45
45
|
|
|
46
46
|
class << self
|
|
@@ -73,9 +73,12 @@ module NextSQL
|
|
|
73
73
|
host, = split_host_port(addr, allow_bare: true)
|
|
74
74
|
host = host.strip.downcase
|
|
75
75
|
return true if host == "localhost"
|
|
76
|
-
return true if %w[::1 0:0:0:0:0:0:0:1].include?(host)
|
|
77
76
|
|
|
78
|
-
|
|
77
|
+
begin
|
|
78
|
+
IPAddr.new(host).loopback?
|
|
79
|
+
rescue IPAddr::Error
|
|
80
|
+
false
|
|
81
|
+
end
|
|
79
82
|
end
|
|
80
83
|
|
|
81
84
|
def validate_config!(cfg)
|
|
@@ -369,12 +372,17 @@ module NextSQL
|
|
|
369
372
|
|
|
370
373
|
def handshake
|
|
371
374
|
cfg = @cfg
|
|
372
|
-
write_frame(Protocol::TYPE_HELLO,
|
|
375
|
+
write_frame(Protocol::TYPE_HELLO,
|
|
376
|
+
Protocol.encode_hello(Protocol::VERSION, Protocol::FLAG_PUBLIC_ERROR_CODES, "\x00" * 8,
|
|
377
|
+
cfg.database, cfg.user, cfg.realm))
|
|
373
378
|
typ, payload = read_frame
|
|
374
379
|
raise unexpected(typ, payload) if typ != Protocol::TYPE_HELLO_OK
|
|
375
380
|
|
|
376
|
-
_version, auth_method, secret = Protocol.decode_hello_ok(payload)
|
|
381
|
+
_version, auth_method, secret, flags = Protocol.decode_hello_ok(payload)
|
|
377
382
|
@secret = secret
|
|
383
|
+
# Diagnostic only: decode_error reads the field whenever it is present,
|
|
384
|
+
# so nothing depends on this having been echoed.
|
|
385
|
+
@public_error_codes = (flags & Protocol::FLAG_PUBLIC_ERROR_CODES) != 0
|
|
378
386
|
write_frame(Protocol::TYPE_AUTH, Protocol.u16str(cfg.password))
|
|
379
387
|
typ, payload = read_frame
|
|
380
388
|
raise unexpected(typ, payload) if typ != Protocol::TYPE_AUTH_OK
|
data/lib/nextsql/errors.rb
CHANGED
|
@@ -10,8 +10,16 @@ module NextSQL
|
|
|
10
10
|
class Error < StandardError
|
|
11
11
|
attr_reader :error_code
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
# +public_code+ is the stable ERR_* name from docs/error-codes.md. It is
|
|
14
|
+
# set only on an error received over a connection that negotiated the
|
|
15
|
+
# public taxonomy, and is "" otherwise -- for a local error, or an older
|
|
16
|
+
# server. +error_code+ always carries the legacy class, so retry logic
|
|
17
|
+
# keeps reading that and nothing should require +public_code+.
|
|
18
|
+
attr_reader :public_code
|
|
19
|
+
|
|
20
|
+
def initialize(error_code, message = "", public_code = "")
|
|
14
21
|
@error_code = error_code
|
|
22
|
+
@public_code = public_code
|
|
15
23
|
super(message.empty? ? error_code : message)
|
|
16
24
|
end
|
|
17
25
|
end
|
data/lib/nextsql/protocol.rb
CHANGED
|
@@ -53,6 +53,12 @@ module NextSQL
|
|
|
53
53
|
AUTH_PASSWORD = 1
|
|
54
54
|
AUTH_PASSWORD_KEY = 2
|
|
55
55
|
FLAG_CANCEL = 1
|
|
56
|
+
# FLAG_PUBLIC_ERROR_CODES asks the server for the stable ERR_* error
|
|
57
|
+
# taxonomy (docs/error-codes.md). A server that does not implement it
|
|
58
|
+
# ignores the bit and keeps the NSQL v1 error shape, which decode_error
|
|
59
|
+
# still reads, so setting it is safe against any server version. The server
|
|
60
|
+
# echoes it in the hello-ok flags when it was accepted.
|
|
61
|
+
FLAG_PUBLIC_ERROR_CODES = 2
|
|
56
62
|
FLAG_NULL = 0x01
|
|
57
63
|
|
|
58
64
|
KIND_UUID = 1
|
|
@@ -313,9 +319,16 @@ module NextSQL
|
|
|
313
319
|
end
|
|
314
320
|
|
|
315
321
|
def decode_hello_ok(b)
|
|
316
|
-
raise ProtocolError, "bad hello-ok length" unless b.bytesize
|
|
317
|
-
|
|
318
|
-
|
|
322
|
+
raise ProtocolError, "bad hello-ok length" unless [11, 13].include?(b.bytesize)
|
|
323
|
+
|
|
324
|
+
flags = 0
|
|
325
|
+
if b.bytesize == 13
|
|
326
|
+
flags = u16(b, 11)
|
|
327
|
+
# The server omits the field when it accepted no capability, so a
|
|
328
|
+
# present-but-zero field is a second encoding of the v1 hello-ok.
|
|
329
|
+
raise ProtocolError, "empty hello-ok flags" if flags.zero?
|
|
330
|
+
end
|
|
331
|
+
[u16(b, 0), b.getbyte(2), b.byteslice(3, 8), flags]
|
|
319
332
|
end
|
|
320
333
|
|
|
321
334
|
def encode_query(sql, params)
|
|
@@ -946,8 +959,16 @@ module NextSQL
|
|
|
946
959
|
|
|
947
960
|
def decode_error(b)
|
|
948
961
|
code, off = read_u16_string(b, 0, MAX_NAME)
|
|
949
|
-
msg, = read_u16_string(b, off, MAX_NAME)
|
|
950
|
-
|
|
962
|
+
msg, off = read_u16_string(b, off, MAX_NAME)
|
|
963
|
+
# Optional trailing field, present only from a server that accepted
|
|
964
|
+
# FLAG_PUBLIC_ERROR_CODES. Its absence is normal -- an older server
|
|
965
|
+
# ignores the request bit -- so this must never be required.
|
|
966
|
+
public_code = ""
|
|
967
|
+
if off < b.bytesize
|
|
968
|
+
public_code, = read_u16_string(b, off, MAX_NAME)
|
|
969
|
+
raise ProtocolError, "empty public error code" if public_code.empty?
|
|
970
|
+
end
|
|
971
|
+
Error.new(code, msg, public_code)
|
|
951
972
|
end
|
|
952
973
|
|
|
953
974
|
def encode_set_read_consistency(mode, max_staleness_ms)
|
data/lib/nextsql/version.rb
CHANGED