nats_messaging 1.6.0 → 1.6.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/nats_messaging/nats_service.rb +26 -2
- data/lib/nats_messaging.rb +9 -2
- 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: afa8e0bfe4f5c00bd91323e60c3d3e630bf1eea504495a079c667533fa98ca5a
|
|
4
|
+
data.tar.gz: 30a30e95c8c0feaabfaf01346faddedeeea6232e97f768ed58ddde810ea6285e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 862f641b1555417327a7d16c7a566d43b3ed168f5cf4403ad5732e8ef7e6eba6530fbae98a530c0a73fc615b65bda4b31dc08efec34ab437bc48cc41774062d2
|
|
7
|
+
data.tar.gz: f700b68ecae1504dc079c6702bddb4d1720c779101f0de0c9595aafc6134610fc5754c977e4b95bc48fb7348415620c860ffa074dc5dafc00841d394d265a75e
|
|
@@ -3,6 +3,7 @@ require 'msgpack'
|
|
|
3
3
|
require 'logger'
|
|
4
4
|
require 'active_support'
|
|
5
5
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
6
|
+
require 'timeout'
|
|
6
7
|
|
|
7
8
|
module NatsMessaging
|
|
8
9
|
class NatsService
|
|
@@ -17,12 +18,35 @@ module NatsMessaging
|
|
|
17
18
|
max_outstanding_pings: 2
|
|
18
19
|
}.freeze
|
|
19
20
|
|
|
21
|
+
# nats-pure usa el mismo max_reconnect_attempts tanto para el connect
|
|
22
|
+
# inicial (establish_connection!) como para la reconexión de una conexión
|
|
23
|
+
# ya establecida (attempt_reconnect): con max_reconnect_attempts: -1,
|
|
24
|
+
# can_reuse_server? siempre devuelve true y el connect inicial reintenta
|
|
25
|
+
# para siempre sin lanzar excepción, colgando el arranque del proceso host.
|
|
26
|
+
# El `connect_timeout` propio de nats-pure no sirve para acotar esto: solo
|
|
27
|
+
# limita un intento de conexión TCP/handshake individual, no la cantidad
|
|
28
|
+
# de reintentos. No hay forma nativa en nats-pure 2.5 de distinguir
|
|
29
|
+
# "primer connect" de "reconexión posterior", así que se acota el connect
|
|
30
|
+
# inicial desde afuera con Timeout.timeout. Es aceptable usar Timeout pese
|
|
31
|
+
# a su conocida rudeza (mata el hilo sin dar chance de cleanup) porque
|
|
32
|
+
# ocurre solo durante el boot, antes de que exista estado que corromper;
|
|
33
|
+
# si el timeout dispara, la instancia parcialmente construida simplemente
|
|
34
|
+
# se descarta al propagar la excepción.
|
|
35
|
+
DEFAULT_INITIAL_CONNECT_TIMEOUT = 15
|
|
36
|
+
|
|
20
37
|
attr_reader :pid
|
|
21
38
|
|
|
22
|
-
def initialize(nats_url, stream_name: nil, subjects: [], connect_options: {})
|
|
39
|
+
def initialize(nats_url, stream_name: nil, subjects: [], connect_options: {}, initial_connect_timeout: DEFAULT_INITIAL_CONNECT_TIMEOUT)
|
|
23
40
|
@pid = Process.pid
|
|
24
41
|
@subscriptions = {} # Store active subscriptions
|
|
25
|
-
|
|
42
|
+
begin
|
|
43
|
+
@nats = Timeout.timeout(initial_connect_timeout) do
|
|
44
|
+
NATS.connect(nats_url, DEFAULT_CONNECT_OPTIONS.merge(connect_options))
|
|
45
|
+
end
|
|
46
|
+
rescue Timeout::Error
|
|
47
|
+
raise NatsMessaging::InitialConnectTimeoutError,
|
|
48
|
+
"Could not connect to NATS at #{nats_url} within #{initial_connect_timeout}s"
|
|
49
|
+
end
|
|
26
50
|
register_lifecycle_callbacks
|
|
27
51
|
if stream_name
|
|
28
52
|
@js = @nats.jetstream
|
data/lib/nats_messaging.rb
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
require 'nats_messaging/nats_service'
|
|
2
2
|
|
|
3
3
|
module NatsMessaging
|
|
4
|
+
# Se lanza cuando el connect inicial no logra establecerse dentro de
|
|
5
|
+
# initial_connect_timeout segundos. No debe confundirse con las
|
|
6
|
+
# desconexiones posteriores a una conexión ya establecida, que nats-pure
|
|
7
|
+
# reintenta indefinidamente en su propio hilo sin lanzar excepciones.
|
|
8
|
+
class InitialConnectTimeoutError < StandardError; end
|
|
9
|
+
|
|
4
10
|
# Main entry point to expose the functionalities.
|
|
5
11
|
#
|
|
6
12
|
# Memoiza la instancia por PID: si el proceso actual forkeó (Puma/solid_queue)
|
|
@@ -8,13 +14,14 @@ module NatsMessaging
|
|
|
8
14
|
# socket pertenece al padre) y se descarta sin intentar cerrarla, creando una
|
|
9
15
|
# instancia nueva con los parámetros de ESTA llamada. Dentro del mismo PID la
|
|
10
16
|
# memoización ignora los parámetros de llamadas posteriores, igual que antes.
|
|
11
|
-
def self.service(nats_url, stream_name: nil, subjects: [], connect_options: {})
|
|
17
|
+
def self.service(nats_url, stream_name: nil, subjects: [], connect_options: {}, initial_connect_timeout: NatsService::DEFAULT_INITIAL_CONNECT_TIMEOUT)
|
|
12
18
|
@service = nil if @service && @service.pid != Process.pid
|
|
13
19
|
@service ||= NatsService.new(
|
|
14
20
|
nats_url,
|
|
15
21
|
stream_name: stream_name,
|
|
16
22
|
subjects: subjects,
|
|
17
|
-
connect_options: connect_options
|
|
23
|
+
connect_options: connect_options,
|
|
24
|
+
initial_connect_timeout: initial_connect_timeout
|
|
18
25
|
)
|
|
19
26
|
end
|
|
20
27
|
end
|