service_mesh_nats 0.8.0 → 0.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7d47cf27e0ff8d0e54d505b7c59403458afb435e8160f78412817c21eda1a34
4
- data.tar.gz: 9ea7182672a1a1647aebd0eea30ffde78ff61b7f4f6bd20573a3ec2f6da18423
3
+ metadata.gz: 45e8d3352cc574ebf97228c0e7a7fccc8e35b8aa5c51a9ad6c807039b60b7b27
4
+ data.tar.gz: 7d236c8f5f5c7a113c01be8902e7023507d94312d05f6105f34b9be701d7e460
5
5
  SHA512:
6
- metadata.gz: bdf3079c3f4abd4faf07b4918d28a781d19d20c22013f870b3a5db48bfbfb10689cc787caa09c68f132791ac7b3939832234324aa38f4fcc2ac4a20b7892049a
7
- data.tar.gz: 746c42c2edefa1b947ff86e78df780979699672dd393c85f6550b541b99b2a77cf9b9ef109b5739a7c5b6ecd4328a22c046075b2a0219be4bae31c62f4d78fba
6
+ metadata.gz: 50754880b109a293ec9ccc5f35fe4c3fb15493c2a920affeb714021bd15dca4b884cb51f4f64f73b3b602f83840b293feefac61d13e7583ac9b855c125fb9dc8
7
+ data.tar.gz: 4b4d25551c600b7de8d3db05252b8181a24565f4a2dcabf1410d81de9cf33cc316781296f694c06559d82220f64d072898233bb0890548229d0711a88445e319
data/README.md CHANGED
@@ -1,8 +1,14 @@
1
1
  # service-mesh-nats-ruby
2
2
 
3
+ This library implements the Service Mesh API Specification v0.5.0.
4
+
5
+ This library implements the Service Mesh NATS API Specification v0.3.0.
6
+
3
7
  service-mesh-nats-ruby is the Ruby implementation of the
4
8
  [Service Mesh API Specification](https://github.com/Paymentbox-com/service-mesh-api)
5
- over NATS. It implements the Ruby contract in the `service_mesh` gem, from
9
+ over NATS, following the
10
+ [Service Mesh NATS API Specification](https://github.com/Paymentbox-com/service-mesh-nats-api).
11
+ It implements the Ruby contract in the `service_mesh` gem, from
6
12
  [service-mesh-ruby](https://github.com/Paymentbox-com/service-mesh-ruby), on
7
13
  top of the `nats-pure` client.
8
14
 
@@ -75,26 +75,48 @@ module ServiceMeshNats
75
75
  # Sends +message+ to a route target and returns the reply.
76
76
  #
77
77
  # Settings are read from +options+, then the target's metadata, then the
78
- # client's config. REQUEST_TIMEOUT_KEY bounds the call. STREAM_KEY raises
79
- # BadConfig. Expiry raises NATS::Timeout, and no receiver raises
78
+ # client's config. The request's deadline is REQUEST_TIMEOUT_KEY from
79
+ # now, or +deadline+, a Time, when that comes first. STREAM_KEY, or a
80
+ # REQUEST_TIMEOUT_KEY no more than DEADLINE_OFFSET_KEY, raises
81
+ # InvalidRequest. Expiry raises NATS::Timeout, and no receiver raises
80
82
  # NATS::IO::NoRespondersError.
81
83
  #
82
- # The request carries ServiceMesh::TIMEOUT_KEY, set to the request timeout
83
- # in whole milliseconds. ServiceMesh::MESSAGE_ID_KEY is copied to
84
+ # A request whose time left is no more than DEADLINE_OFFSET_KEY, because
85
+ # +deadline+ leaves too little, raises NATS::Timeout without being sent,
86
+ # because its handler's deadline would fall before the request reached
87
+ # it. This happens when a handler passes its own deadline down a chain of
88
+ # requests, after work done before the call, and across retries under one
89
+ # deadline.
90
+ #
91
+ # The request carries ServiceMesh::TIMEOUT_KEY, set to the time left in
92
+ # whole milliseconds, and ServiceMesh::DEADLINE_OFFSET_KEY, set to
93
+ # DEADLINE_OFFSET_KEY. ServiceMesh::MESSAGE_ID_KEY is copied to
84
94
  # Nats-Msg-Id.
85
- def request(message, options = {})
95
+ def request(message, options = {}, deadline: nil)
86
96
  Target.check_kind!(message.target, :route, "request")
87
97
  subject = Target.subject(message.target)
88
98
  settings = Target.merge_settings(message.target, options)
89
99
  unless settings[STREAM_KEY].to_s.empty?
90
- raise BadConfig, "#{STREAM_KEY}=#{settings[STREAM_KEY].inspect} applies to topic targets only"
100
+ raise InvalidRequest, "#{STREAM_KEY}=#{settings[STREAM_KEY].inspect} applies to topic targets only"
91
101
  end
92
102
  timeout = Config.duration(settings, REQUEST_TIMEOUT_KEY, @settings.request_timeout)
103
+ offset = Config.offset(settings, DEADLINE_OFFSET_KEY, @settings.deadline_offset)
104
+ if timeout <= offset
105
+ raise InvalidRequest, "#{REQUEST_TIMEOUT_KEY}=#{ms(timeout)} is no more than #{DEADLINE_OFFSET_KEY}=#{ms(offset)}"
106
+ end
93
107
  nats_connection = connection
94
108
 
95
- msg = nats_msg(subject, message)
96
- msg.header = (msg.header || {}).merge(ServiceMesh::TIMEOUT_KEY => (timeout * 1000).floor.to_s)
97
- reply = nats_connection.request_msg(msg, timeout: timeout)
109
+ wait = timeout
110
+ wait = (deadline - Time.now).clamp(0, timeout) if deadline
111
+ outgoing = message.with(metadata: message.metadata.merge(
112
+ ServiceMesh::TIMEOUT_KEY => ms(wait).to_s,
113
+ ServiceMesh::DEADLINE_OFFSET_KEY => ms(offset).to_s
114
+ ))
115
+ if outgoing.expired?
116
+ raise NATS::Timeout, "#{ms(wait)}ms left is no more than #{DEADLINE_OFFSET_KEY}=#{ms(offset)}"
117
+ end
118
+
119
+ reply = nats_connection.request_msg(nats_msg(subject, outgoing), timeout: wait)
98
120
  header = reply.header || {}
99
121
  raise HandlerError.new(header[ServiceMesh::HANDLER_ERROR_KEY].to_s) if header.key?(ServiceMesh::HANDLER_ERROR_KEY)
100
122
 
@@ -130,6 +152,11 @@ module ServiceMeshNats
130
152
 
131
153
  private
132
154
 
155
+ # +seconds+ in whole milliseconds, rounded down.
156
+ def ms(seconds)
157
+ (seconds * 1000).floor
158
+ end
159
+
133
160
  # The NATS message for +message+, with its metadata as the header.
134
161
  def nats_msg(subject, message)
135
162
  NATS::Msg.new(subject: subject, data: message.payload, header: Header.outgoing_header(message.metadata))
@@ -4,11 +4,18 @@ require "etc"
4
4
 
5
5
  module ServiceMeshNats
6
6
  # Configuration keys this transport reads beyond ServiceMesh::DEPLOYMENT_GROUP_KEY.
7
- # Every value is a string. Durations are seconds, such as "5" or "0.25".
7
+ # Every value is a string. Durations are whole numbers of milliseconds, such
8
+ # as "5000" or "250".
9
+ # The server URL, or a comma-separated list of them.
8
10
  URL_KEY = "url"
9
11
  NAME_KEY = "name"
10
12
  CONNECT_TIMEOUT_KEY = "connect_timeout"
11
13
  REQUEST_TIMEOUT_KEY = "request_timeout"
14
+ # How much earlier than the caller's deadline the handler's deadline falls,
15
+ # in whole milliseconds, or "0". Sent on each request as
16
+ # ServiceMesh::DEADLINE_OFFSET_KEY. Default "0". It must be less than
17
+ # REQUEST_TIMEOUT_KEY. It is also a request setting.
18
+ DEADLINE_OFFSET_KEY = "deadline_offset"
12
19
  CONCURRENCY_KEY = "concurrency"
13
20
  # The most times one JetStream message is delivered, as a positive integer.
14
21
  # In a runtime's config it is the default for subscribers that do not set
@@ -21,39 +28,40 @@ module ServiceMeshNats
21
28
  # sets STREAM_KEY.
22
29
  #
23
30
  # The stream name. A Subscriber consumes from a consumer on it, and a
24
- # publish waits for its ack. On an Endpoint or a request it is BadConfig.
31
+ # publish waits for its ack. On an Endpoint it is BadConfig, and on a
32
+ # request it is InvalidRequest.
25
33
  STREAM_KEY = "stream"
26
34
  # "true" or "false". "false" leaves a named consumer group's consumer to be
27
35
  # created outside the runtime.
28
36
  CREATE_CONSUMER_KEY = "create_consumer"
29
37
  # "double", "explicit", or "none".
30
38
  ACK_POLICY_KEY = "ack_policy"
31
- # Seconds the server waits for an ack before it sends a message again.
39
+ # Milliseconds the server waits for an ack before it sends a message again.
32
40
  # Ignored when BACKOFF_KEY is set.
33
41
  ACK_WAIT_KEY = "ack_wait"
34
- # Comma-separated seconds, one for each delivery in turn, that set how long
35
- # the server waits for an ack before it sends the message again. Deliveries
36
- # past the end of the list use its last entry. The server sets the
37
- # consumer's ack wait to the first entry. MAX_DELIVER_KEY must exceed its
38
- # length.
42
+ # Comma-separated milliseconds, one for each delivery in turn, that set how
43
+ # long the server waits for an ack before it sends the message again.
44
+ # Deliveries past the end of the list use its last entry. The server sets
45
+ # the consumer's ack wait to the first entry. MAX_DELIVER_KEY must exceed
46
+ # its length.
39
47
  BACKOFF_KEY = "backoff"
40
- # Seconds the server waits before it sends a failed message again.
48
+ # Milliseconds the server waits before it sends a failed message again.
41
49
  NAK_DELAY_KEY = "nak_delay"
42
50
  # The most unacked messages the consumer allows at once.
43
51
  MAX_ACK_PENDING_KEY = "max_ack_pending"
44
52
  # "all" or "new", where a newly created consumer starts.
45
53
  DELIVER_POLICY_KEY = "deliver_policy"
46
- # Seconds a consumer outlives its last puller.
54
+ # Milliseconds a consumer outlives its last puller.
47
55
  CONSUMER_INACTIVE_THRESHOLD_KEY = "consumer_inactive_threshold"
48
- # Seconds the server keeps a pull open while it has nothing to send. At
49
- # least 1. Also a runtime config key, where it is the default for
56
+ # Milliseconds the server keeps a pull open while it has nothing to send. At
57
+ # least 1000. Also a runtime config key, where it is the default for
50
58
  # subscribers that do not set it.
51
59
  PULL_EXPIRY_KEY = "pull_expiry"
52
- # Seconds between the server's heartbeats on an open pull that has nothing
53
- # to send. From 0.5 to 30, and no more than half of PULL_EXPIRY_KEY. Two
54
- # missed heartbeats make the subscriber pull again. Also a
55
- # runtime config key, where it is the default for subscribers that do not
56
- # set it.
60
+ # Milliseconds between the server's heartbeats on an open pull that has
61
+ # nothing to send. From 500 to 30000, and no more than half of
62
+ # PULL_EXPIRY_KEY. Two missed heartbeats make the subscriber pull again.
63
+ # Also a runtime config key, where it is the default for subscribers that
64
+ # do not set it.
57
65
  PULL_HEARTBEAT_KEY = "pull_heartbeat"
58
66
 
59
67
  # Metadata keys the runtime adds to each JetStream delivery it hands to a
@@ -75,15 +83,31 @@ module ServiceMeshNats
75
83
 
76
84
  # Value parsers shared by the settings types.
77
85
  module Config
78
- # Reads +key+ as a positive number of seconds, or returns +fallback+ when
79
- # absent.
86
+ # The format of every duration value: a whole number of milliseconds
87
+ # above 0, with no sign, unit, or leading zero.
88
+ POSITIVE_MILLISECONDS = /\A[1-9][0-9]*\z/
89
+
90
+ # Reads +key+ as a positive whole number of milliseconds and returns it in
91
+ # seconds, as a Rational, or returns +fallback+ when absent.
80
92
  def self.duration(hash, key, fallback)
81
93
  return fallback unless hash.key?(key)
82
94
 
83
- value = Float(hash[key], exception: false)
84
- raise BadConfig, "#{key}=#{hash[key].inspect} must be a positive number of seconds" if value.nil? || value <= 0
95
+ value = hash[key].to_s
96
+ unless POSITIVE_MILLISECONDS.match?(value)
97
+ raise BadConfig, "#{key}=#{hash[key].inspect} must be a positive whole number of milliseconds such as \"5000\""
98
+ end
85
99
 
86
- value
100
+ Rational(value.to_i, 1000)
101
+ end
102
+
103
+ # Reads +key+ as "0" or a positive whole number of milliseconds and
104
+ # returns it in seconds, or returns +fallback+ when absent.
105
+ def self.offset(hash, key, fallback)
106
+ return fallback unless hash.key?(key)
107
+ return 0 if hash[key].to_s == "0"
108
+ return Rational(hash[key].to_s.to_i, 1000) if POSITIVE_MILLISECONDS.match?(hash[key].to_s)
109
+
110
+ raise BadConfig, "#{key}=#{hash[key].inspect} must be a whole number of milliseconds such as \"0\" or \"50\""
87
111
  end
88
112
 
89
113
  def self.positive_integer(hash, key, fallback)
@@ -104,36 +128,44 @@ module ServiceMeshNats
104
128
  raise BadConfig, "#{key}=#{hash[key].inspect} must be one of #{allowed.map(&:inspect).join(", ")}"
105
129
  end
106
130
 
107
- # Reads +key+ as a comma-separated list of positive numbers of seconds,
108
- # or returns nil when absent.
131
+ # Reads +key+ as a comma-separated list of positive whole numbers of
132
+ # milliseconds and returns them in seconds, as Rationals, or returns nil
133
+ # when absent.
109
134
  def self.durations(hash, key)
110
135
  return nil unless hash.key?(key)
111
136
 
112
137
  hash[key].to_s.split(",", -1).map do |part|
113
- value = Float(part.strip, exception: false)
114
- if value.nil? || value <= 0
115
- raise BadConfig, "#{key}=#{hash[key].inspect} must be a comma-separated list of positive numbers of seconds, such as \"1,5\""
138
+ value = part.strip
139
+ unless POSITIVE_MILLISECONDS.match?(value)
140
+ raise BadConfig, "#{key}=#{hash[key].inspect} must be a comma-separated list of positive whole numbers of milliseconds, such as \"1000,5000\""
116
141
  end
117
142
 
118
- value
143
+ Rational(value.to_i, 1000)
119
144
  end
120
145
  end
121
146
  end
122
147
 
123
148
  # The keys a Client reads. Any other key is ignored.
124
- ClientSettings = Data.define(:url, :name, :connect_timeout, :request_timeout) do
149
+ ClientSettings = Data.define(:url, :name, :connect_timeout, :request_timeout, :deadline_offset) do
125
150
  def self.parse(config)
126
151
  config = config.to_h
152
+ request_timeout = Config.duration(config, REQUEST_TIMEOUT_KEY, DEFAULT_REQUEST_TIMEOUT)
153
+ deadline_offset = Config.offset(config, DEADLINE_OFFSET_KEY, 0)
154
+ if deadline_offset >= request_timeout
155
+ raise BadConfig, "#{DEADLINE_OFFSET_KEY}=#{config[DEADLINE_OFFSET_KEY].inspect} must be less than " \
156
+ "#{REQUEST_TIMEOUT_KEY}=#{(request_timeout * 1000).to_i.to_s.inspect}"
157
+ end
127
158
  new(
128
159
  url: config.fetch(URL_KEY, DEFAULT_URL).then { |url| url.to_s.empty? ? DEFAULT_URL : url },
129
160
  name: config[NAME_KEY],
130
161
  connect_timeout: Config.duration(config, CONNECT_TIMEOUT_KEY, DEFAULT_CONNECT_TIMEOUT),
131
- request_timeout: Config.duration(config, REQUEST_TIMEOUT_KEY, DEFAULT_REQUEST_TIMEOUT)
162
+ request_timeout: request_timeout,
163
+ deadline_offset: deadline_offset
132
164
  )
133
165
  end
134
166
 
135
167
  def connect_options
136
- options = {servers: [url], connect_timeout: connect_timeout}
168
+ options = {servers: url.split(",").map(&:strip), connect_timeout: connect_timeout}
137
169
  options[:name] = name if name && !name.empty?
138
170
  options
139
171
  end
@@ -7,6 +7,10 @@ module ServiceMeshNats
7
7
 
8
8
  class BadConfig < Error; end
9
9
 
10
+ # Raised by Client#request when the request's settings do not allow it to
11
+ # be sent.
12
+ class InvalidRequest < Error; end
13
+
10
14
  class Closed < Error
11
15
  def initialize(msg = "client has been closed") = super
12
16
  end
@@ -102,13 +102,15 @@ module ServiceMeshNats
102
102
  # Raises BadConfig unless the pull settings are within nats.go's limits.
103
103
  # The runtime calls it at start, where nats.go checks them.
104
104
  def check_pull_limits!
105
- raise BadConfig, "#{PULL_EXPIRY_KEY}=#{pull_expiry} must be at least 1 second" if pull_expiry < 1
105
+ expiry = (pull_expiry * 1000).to_i
106
+ heartbeat = (pull_heartbeat * 1000).to_i
107
+ raise BadConfig, "#{PULL_EXPIRY_KEY}=#{expiry} must be at least 1000" if pull_expiry < 1
106
108
  unless pull_heartbeat.between?(0.5, 30)
107
- raise BadConfig, "#{PULL_HEARTBEAT_KEY}=#{pull_heartbeat} must be from 0.5 to 30 seconds"
109
+ raise BadConfig, "#{PULL_HEARTBEAT_KEY}=#{heartbeat} must be from 500 to 30000"
108
110
  end
109
111
  return if pull_heartbeat <= pull_expiry / 2
110
112
 
111
- raise BadConfig, "#{PULL_HEARTBEAT_KEY}=#{pull_heartbeat} must be no more than half of #{PULL_EXPIRY_KEY}=#{pull_expiry}"
113
+ raise BadConfig, "#{PULL_HEARTBEAT_KEY}=#{heartbeat} must be no more than half of #{PULL_EXPIRY_KEY}=#{expiry}"
112
114
  end
113
115
  end
114
116
  end
@@ -365,7 +365,7 @@ module ServiceMeshNats
365
365
  def missed_heartbeats
366
366
  return unless @connection.connected?
367
367
 
368
- reason = "no message or heartbeat for #{2 * @settings.pull_heartbeat} seconds"
368
+ reason = "no message or heartbeat for #{(2 * @settings.pull_heartbeat * 1000).to_i}ms"
369
369
  return replace_consumer(reason) if @settings.ordered?
370
370
 
371
371
  consume_error(reason)
@@ -46,12 +46,15 @@ module ServiceMeshNats
46
46
  end
47
47
 
48
48
  # Sets ServiceMesh::DEADLINE_KEY on a request that carries
49
- # ServiceMesh::TIMEOUT_KEY, to its arrival time plus the timeout. A timeout
50
- # that is not a whole number of milliseconds is ignored.
49
+ # ServiceMesh::TIMEOUT_KEY, to its arrival time plus the timeout, minus
50
+ # ServiceMesh::DEADLINE_OFFSET_KEY. A timeout or offset that is not a
51
+ # whole number of milliseconds is ignored.
51
52
  def add_deadline(metadata, arrival)
52
53
  milliseconds = Integer(metadata[ServiceMesh::TIMEOUT_KEY].to_s, 10, exception: false)
53
54
  return if milliseconds.nil? || milliseconds.negative?
54
55
 
56
+ offset = Integer(metadata[ServiceMesh::DEADLINE_OFFSET_KEY].to_s, 10, exception: false)
57
+ milliseconds -= offset if offset && !offset.negative?
55
58
  metadata[ServiceMesh::DEADLINE_KEY] = (arrival + Rational(milliseconds, 1000)).utc.iso8601(9)
56
59
  end
57
60
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ServiceMeshNats
4
- VERSION = "0.8.0"
4
+ VERSION = "0.9.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_mesh_nats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryant Morrill
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0.7'
34
+ version: '0.9'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0.7'
41
+ version: '0.9'
42
42
  description:
43
43
  email:
44
44
  - bryantreadmorrill@gmail.com