pubnub 4.0.27 → 4.0.28
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.
Potentially problematic release.
This version of pubnub might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.pubnub.yml +8 -1
- data/.rubocop.yml +9 -3
- data/.travis.yml +6 -19
- data/CHANGELOG.md +86 -82
- data/Gemfile +7 -5
- data/Gemfile.lock +155 -157
- data/README.md +34 -34
- data/VERSION +1 -1
- data/gemfiles/celluloid_017.gemfile +4 -4
- data/gemfiles/celluloid_018.gemfile +4 -4
- data/gemfiles/celluloid_master.gemfile +4 -4
- data/gemfiles/concurrent-ruby-1.0.5.gem +0 -0
- data/gemfiles/concurrent-ruby-edge-0.3.1.gem +0 -0
- data/lib/pubnub.rb +3 -3
- data/lib/pubnub/client.rb +19 -16
- data/lib/pubnub/client/events.rb +5 -5
- data/lib/pubnub/client/getters_setters.rb +11 -12
- data/lib/pubnub/client/paged_history.rb +11 -9
- data/lib/pubnub/configuration.rb +1 -2
- data/lib/pubnub/constants.rb +47 -47
- data/lib/pubnub/crypto.rb +2 -2
- data/lib/pubnub/envelope.rb +7 -5
- data/lib/pubnub/error.rb +22 -15
- data/lib/pubnub/event.rb +24 -25
- data/lib/pubnub/event/formatter.rb +8 -5
- data/lib/pubnub/event/signature.rb +3 -3
- data/lib/pubnub/events/audit.rb +1 -1
- data/lib/pubnub/events/channel_registration.rb +10 -15
- data/lib/pubnub/events/delete_messages.rb +9 -9
- data/lib/pubnub/events/grant.rb +3 -3
- data/lib/pubnub/events/heartbeat.rb +4 -8
- data/lib/pubnub/events/here_now.rb +4 -8
- data/lib/pubnub/events/history.rb +10 -10
- data/lib/pubnub/events/leave.rb +2 -2
- data/lib/pubnub/events/presence.rb +1 -1
- data/lib/pubnub/events/publish.rb +3 -5
- data/lib/pubnub/events/revoke.rb +3 -3
- data/lib/pubnub/events/set_state.rb +3 -3
- data/lib/pubnub/events/state.rb +4 -6
- data/lib/pubnub/events/subscribe.rb +1 -1
- data/lib/pubnub/events/time.rb +2 -2
- data/lib/pubnub/events/where_now.rb +6 -6
- data/lib/pubnub/format.rb +73 -0
- data/lib/pubnub/formatter.rb +23 -21
- data/lib/pubnub/heart.rb +4 -4
- data/lib/pubnub/origin_manager.rb +3 -5
- data/lib/pubnub/pam.rb +5 -5
- data/lib/pubnub/schemas/envelope_schema.rb +1 -1
- data/lib/pubnub/subscribe_callback.rb +3 -3
- data/lib/pubnub/subscribe_event.rb +6 -10
- data/lib/pubnub/subscribe_event/formatter.rb +82 -80
- data/lib/pubnub/subscribe_event/heartbeat.rb +1 -1
- data/lib/pubnub/subscriber.rb +31 -32
- data/lib/pubnub/telemetry.rb +26 -27
- data/lib/pubnub/validators/audit.rb +3 -2
- data/lib/pubnub/validators/channel_registration.rb +6 -4
- data/lib/pubnub/validators/client.rb +1 -0
- data/lib/pubnub/validators/common_validator.rb +37 -31
- data/lib/pubnub/validators/delete.rb +5 -4
- data/lib/pubnub/validators/grant.rb +3 -2
- data/lib/pubnub/validators/history.rb +3 -2
- data/lib/pubnub/validators/leave.rb +3 -2
- data/lib/pubnub/validators/presence.rb +3 -2
- data/lib/pubnub/validators/publish.rb +15 -10
- data/lib/pubnub/validators/set_state.rb +0 -2
- data/lib/pubnub/validators/state.rb +3 -2
- data/lib/pubnub/version.rb +1 -1
- data/pubnub.gemspec +19 -18
- data/spec/examples/audit_examples_spec.rb +1268 -1418
- data/spec/examples/channel_registration_examples_spec.rb +655 -757
- data/spec/examples/delete_messages_examples_spec.rb +19 -21
- data/spec/examples/grant_examples_spec.rb +34028 -37920
- data/spec/examples/heartbeat_examples_spec.rb +583 -731
- data/spec/examples/here_now_examples_spec.rb +493 -569
- data/spec/examples/history_examples_spec.rb +979 -1127
- data/spec/examples/leave_examples_spec.rb +439 -551
- data/spec/examples/presence_examples_spec.rb +686 -830
- data/spec/examples/publish_examples_spec.rb +27223 -31547
- data/spec/examples/publish_with_ttl_spec.rb +11 -14
- data/spec/examples/revoke_examples_spec.rb +68048 -75828
- data/spec/examples/set_state_examples_spec.rb +295 -371
- data/spec/examples/state_examples_spec.rb +169 -197
- data/spec/examples/status_request_message_count_exceeded_spec.rb +16 -19
- data/spec/examples/subscribe_examples_spec.rb +43843 -52035
- data/spec/examples/time_examples_spec.rb +82 -98
- data/spec/examples/where_now_examples_spec.rb +88 -104
- data/spec/lib/client_spec.rb +99 -117
- data/spec/lib/connection_callback_spec.rb +27 -33
- data/spec/lib/custom_retries_examples_spec.rb +8 -11
- data/spec/lib/event_spec.rb +35 -42
- data/spec/lib/events/audit_spec.rb +19 -25
- data/spec/lib/events/channel_registration_spec.rb +17 -23
- data/spec/lib/events/delete_messages_spec.rb +14 -20
- data/spec/lib/events/grant_spec.rb +15 -21
- data/spec/lib/events/heartbeat_spec.rb +21 -27
- data/spec/lib/events/here_now_spec.rb +14 -20
- data/spec/lib/events/history_spec.rb +14 -20
- data/spec/lib/events/leave_spec.rb +14 -20
- data/spec/lib/events/presence_delta_spec.rb +15 -16
- data/spec/lib/events/presence_spec.rb +36 -45
- data/spec/lib/events/publish_spec.rb +33 -40
- data/spec/lib/events/revoke_spec.rb +16 -22
- data/spec/lib/events/state_spec.rb +16 -22
- data/spec/lib/events/subscribe_spec.rb +75 -85
- data/spec/lib/events/time_spec.rb +12 -18
- data/spec/lib/events/timeout_handling_spec.rb +8 -14
- data/spec/lib/events/where_now_spec.rb +14 -20
- data/spec/lib/keep_alive_spec.rb +4 -8
- data/spec/lib/multiple_ciphers_spec.rb +56 -60
- data/spec/lib/pubnub_spec.rb +10 -13
- data/spec/lib/signatures_spec.rb +118 -120
- data/spec/lib/ssl_error_spec.rb +14 -18
- data/spec/lib/super_admin_spec.rb +58 -66
- data/spec/lib/uuid_spec.rb +3 -4
- data/spec/spec_expectations.rb +3 -3
- data/spec/spec_helper.rb +29 -46
- metadata +52 -22
- data/lib/pubnub/message.rb +0 -43
@@ -39,7 +39,7 @@ module Pubnub
|
|
39
39
|
|
40
40
|
required(:config).schema(ConfigSchema)
|
41
41
|
|
42
|
-
rule(category_value: [
|
42
|
+
rule(category_value: %i[error category]) do |error, category|
|
43
43
|
error.true?.then(category.included_in?(Pubnub::Constants::STATUS_CATEGORY_ERRORS))
|
44
44
|
error.false?.then(category.included_in?(Pubnub::Constants::STATUS_CATEGORY_SUCCESSES))
|
45
45
|
end
|
@@ -6,8 +6,8 @@ module Pubnub
|
|
6
6
|
|
7
7
|
def initialize(options)
|
8
8
|
@callbacks = {
|
9
|
-
message:
|
10
|
-
status:
|
9
|
+
message: options[:message],
|
10
|
+
status: options[:status],
|
11
11
|
presence: options[:presence]
|
12
12
|
}
|
13
13
|
validate!
|
@@ -18,7 +18,7 @@ module Pubnub
|
|
18
18
|
def validate!
|
19
19
|
# Check if each callback responds to call and accept one parameter.
|
20
20
|
@callbacks.each do |type, callback|
|
21
|
-
|
21
|
+
raise "#{type} callback is not valid in SubscribeCallback." unless (callback.respond_to?(:call) && callback.arity == 1) || callback.nil?
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -37,11 +37,9 @@ module Pubnub
|
|
37
37
|
|
38
38
|
envelopes = finalize_event(fire_callbacks(handle(response, uri)))
|
39
39
|
|
40
|
-
|
40
|
+
Concurrent::Future.execute { fire } unless @http_sync
|
41
41
|
|
42
42
|
envelopes
|
43
|
-
ensure
|
44
|
-
terminate if !@stay_alive && @http_sync
|
45
43
|
end
|
46
44
|
|
47
45
|
def leave_all
|
@@ -77,7 +75,7 @@ module Pubnub
|
|
77
75
|
request: uri)
|
78
76
|
end
|
79
77
|
req
|
80
|
-
rescue => error
|
78
|
+
rescue StandardError => error
|
81
79
|
Pubnub.logger.warn('Pubnub') { "Connection lost! Reason: #{error}" }
|
82
80
|
@app.subscriber.announce_status(announcement_type: Pubnub::Constants::TIMEOUT_ANNOUNCEMENT,
|
83
81
|
event: @event,
|
@@ -138,24 +136,22 @@ module Pubnub
|
|
138
136
|
end
|
139
137
|
|
140
138
|
def add_filter_to_params(params)
|
141
|
-
params
|
139
|
+
params['filter-expr'] = encode_parameter("(#{@app.subscribe_filter})", false) if @app.subscribe_filter
|
142
140
|
params
|
143
141
|
end
|
144
142
|
|
145
143
|
def add_timetoken_to_params(params)
|
146
|
-
params
|
144
|
+
params[:t] = encode_parameter(r: @app.region_code, t: @app.timetoken)
|
147
145
|
params
|
148
146
|
end
|
149
147
|
|
150
148
|
def add_group_to_params(params)
|
151
|
-
params
|
149
|
+
params['channel-group'] = @group.join(',') unless @group.empty?
|
152
150
|
params
|
153
151
|
end
|
154
152
|
|
155
153
|
def add_state_to_params(params)
|
156
|
-
params.merge
|
157
|
-
state: encode_parameter(@app.env[:state][@origin][:channel].merge(@app.env[:state][@origin][:group]))
|
158
|
-
) unless @app.empty_state?
|
154
|
+
params[:state] = encode_parameter(@app.env[:state][@origin][:channel].merge(@app.env[:state][@origin][:group])) unless @app.empty_state?
|
159
155
|
params
|
160
156
|
end
|
161
157
|
end
|
@@ -37,7 +37,7 @@ module Pubnub
|
|
37
37
|
cipher_key = compute_cipher_key(message.reject { |k, _v| k == :payload })
|
38
38
|
crypto = Pubnub::Crypto.new(cipher_key)
|
39
39
|
JSON.parse(crypto.decrypt(message[:payload]), quirks_mode: true)
|
40
|
-
rescue
|
40
|
+
rescue StandardError
|
41
41
|
message[:payload]
|
42
42
|
end
|
43
43
|
|
@@ -47,87 +47,89 @@ module Pubnub
|
|
47
47
|
messages = expand_messages_keys(parsed_response['m'])
|
48
48
|
|
49
49
|
# STATUS
|
50
|
-
if messages.empty?
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
code: req_res_objects[:response].code,
|
58
|
-
client_request: req_res_objects[:request],
|
59
|
-
server_response: req_res_objects[:response],
|
60
|
-
data: nil,
|
61
|
-
category: Pubnub::Constants::STATUS_ACK,
|
62
|
-
error: false,
|
63
|
-
auto_retried: true,
|
64
|
-
|
65
|
-
current_timetoken: timetoken['t'].to_i,
|
66
|
-
last_timetoken: @app.env[:timetoken].to_i,
|
67
|
-
subscribed_channels: @app.subscribed_channels,
|
68
|
-
subscribed_channel_groups: @app.subscribed_groups,
|
69
|
-
|
70
|
-
config: get_config
|
71
|
-
|
72
|
-
},
|
73
|
-
result: {
|
74
|
-
code: req_res_objects[:response].code,
|
75
|
-
operation: get_operation,
|
76
|
-
client_request: req_res_objects[:request],
|
77
|
-
server_response: req_res_objects[:response],
|
78
|
-
|
79
|
-
data: nil
|
80
|
-
}
|
81
|
-
)
|
82
|
-
]
|
83
|
-
else # RESULT
|
84
|
-
envelopes = messages.map do |message|
|
85
|
-
Pubnub::Envelope.new(
|
86
|
-
event: @event,
|
87
|
-
event_options: @given_options,
|
88
|
-
timetoken: expand_timetoken(timetoken),
|
89
|
-
status: {
|
90
|
-
code: req_res_objects[:response].code,
|
91
|
-
client_request: req_res_objects[:request],
|
92
|
-
server_response: req_res_objects[:response],
|
93
|
-
data: message,
|
94
|
-
category: Pubnub::Constants::STATUS_ACK,
|
95
|
-
error: false,
|
96
|
-
auto_retried: true,
|
97
|
-
|
98
|
-
current_timetoken: timetoken['t'].to_i,
|
99
|
-
last_timetoken: @app.env[:timetoken].to_i,
|
100
|
-
subscribed_channels: @app.subscribed_channels,
|
101
|
-
subscribed_channel_groups: @app.subscribed_groups,
|
102
|
-
|
103
|
-
config: get_config
|
104
|
-
|
105
|
-
},
|
106
|
-
result: {
|
107
|
-
code: req_res_objects[:response].code,
|
108
|
-
operation: get_operation(message),
|
109
|
-
client_request: req_res_objects[:request],
|
110
|
-
server_response: req_res_objects[:response],
|
111
|
-
|
112
|
-
data: {
|
113
|
-
message: decipher_payload(message),
|
114
|
-
subscribed_channel: message[:subscription_match] || message[:channel],
|
115
|
-
actual_channel: message[:channel],
|
116
|
-
publish_time_object: message[:publish_timetoken],
|
117
|
-
message_meta_data: message[:user_meta_data],
|
118
|
-
presence_event: get_presence_event(message),
|
119
|
-
presence: get_presence_data(message)
|
120
|
-
}
|
121
|
-
}
|
122
|
-
)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
50
|
+
envelopes = if messages.empty?
|
51
|
+
[plain_envelope(req_res_objects, timetoken)]
|
52
|
+
else # RESULT
|
53
|
+
messages.map do |message|
|
54
|
+
encrypted_envelope(req_res_objects, message, timetoken)
|
55
|
+
end
|
56
|
+
end
|
126
57
|
validate_envelopes(envelopes)
|
127
|
-
|
128
58
|
envelopes
|
129
59
|
end
|
130
60
|
|
61
|
+
def plain_envelope(req_res_objects, timetoken)
|
62
|
+
Pubnub::Envelope.new(
|
63
|
+
event: @event,
|
64
|
+
event_options: @given_options,
|
65
|
+
timetoken: expand_timetoken(timetoken),
|
66
|
+
status: {
|
67
|
+
code: req_res_objects[:response].code,
|
68
|
+
client_request: req_res_objects[:request],
|
69
|
+
server_response: req_res_objects[:response],
|
70
|
+
data: nil,
|
71
|
+
category: Pubnub::Constants::STATUS_ACK,
|
72
|
+
error: false,
|
73
|
+
auto_retried: true,
|
74
|
+
|
75
|
+
current_timetoken: timetoken['t'].to_i,
|
76
|
+
last_timetoken: @app.env[:timetoken].to_i,
|
77
|
+
subscribed_channels: @app.subscribed_channels,
|
78
|
+
subscribed_channel_groups: @app.subscribed_groups,
|
79
|
+
|
80
|
+
config: get_config
|
81
|
+
},
|
82
|
+
result: {
|
83
|
+
code: req_res_objects[:response].code,
|
84
|
+
operation: get_operation,
|
85
|
+
client_request: req_res_objects[:request],
|
86
|
+
server_response: req_res_objects[:response],
|
87
|
+
|
88
|
+
data: nil
|
89
|
+
}
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
def encrypted_envelope(req_res_objects, message, timetoken)
|
94
|
+
Pubnub::Envelope.new(
|
95
|
+
event: @event,
|
96
|
+
event_options: @given_options,
|
97
|
+
timetoken: expand_timetoken(timetoken),
|
98
|
+
status: {
|
99
|
+
code: req_res_objects[:response].code,
|
100
|
+
client_request: req_res_objects[:request],
|
101
|
+
server_response: req_res_objects[:response],
|
102
|
+
data: message,
|
103
|
+
category: Pubnub::Constants::STATUS_ACK,
|
104
|
+
error: false,
|
105
|
+
auto_retried: true,
|
106
|
+
|
107
|
+
current_timetoken: timetoken['t'].to_i,
|
108
|
+
last_timetoken: @app.env[:timetoken].to_i,
|
109
|
+
subscribed_channels: @app.subscribed_channels,
|
110
|
+
subscribed_channel_groups: @app.subscribed_groups,
|
111
|
+
|
112
|
+
config: get_config
|
113
|
+
},
|
114
|
+
result: {
|
115
|
+
code: req_res_objects[:response].code,
|
116
|
+
operation: get_operation(message),
|
117
|
+
client_request: req_res_objects[:request],
|
118
|
+
server_response: req_res_objects[:response],
|
119
|
+
|
120
|
+
data: {
|
121
|
+
message: decipher_payload(message),
|
122
|
+
subscribed_channel: message[:subscription_match] || message[:channel],
|
123
|
+
actual_channel: message[:channel],
|
124
|
+
publish_time_object: message[:publish_timetoken],
|
125
|
+
message_meta_data: message[:user_meta_data],
|
126
|
+
presence_event: get_presence_event(message),
|
127
|
+
presence: get_presence_data(message)
|
128
|
+
}
|
129
|
+
}
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
131
133
|
def validate_envelopes(envelopes)
|
132
134
|
# return if envelopes.size == 1 && envelopes.first.timetoken_update?
|
133
135
|
|
@@ -141,7 +143,7 @@ module Pubnub
|
|
141
143
|
|
142
144
|
if (results_validation + statuses_validation).map(&:failure?).index(true)
|
143
145
|
Pubnub.logger.error('Pubnub::SubscribeEvent::Formatter') { 'Invalid formatted envelope.' }
|
144
|
-
|
146
|
+
raise Exception, 'Invalid formatted envelope.'
|
145
147
|
end
|
146
148
|
end
|
147
149
|
|
@@ -180,7 +182,7 @@ module Pubnub
|
|
180
182
|
def get_presence_event(message)
|
181
183
|
return nil unless get_operation(message) == Pubnub::Constants::OPERATION_PRESENCE
|
182
184
|
message[:payload]['action']
|
183
|
-
rescue
|
185
|
+
rescue StandardError
|
184
186
|
nil
|
185
187
|
end
|
186
188
|
|
data/lib/pubnub/subscriber.rb
CHANGED
@@ -16,11 +16,11 @@ module Pubnub
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def add_listener(options)
|
19
|
-
name
|
19
|
+
name = (options[:name] || UUID.generate).to_sym
|
20
20
|
callback = options[:callback]
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
raise 'Invalid listener.' unless callback.is_a?(SubscribeCallback)
|
23
|
+
raise 'Listener with such name already exists.' if @listeners.key?(name)
|
24
24
|
|
25
25
|
@listeners[name] = callback
|
26
26
|
end
|
@@ -29,7 +29,7 @@ module Pubnub
|
|
29
29
|
name = options[:name]
|
30
30
|
callback = options[:callback]
|
31
31
|
|
32
|
-
|
32
|
+
raise 'You have to specify name _or_ listener object.' if name && callback
|
33
33
|
|
34
34
|
@listeners.delete_if { |k, _v| k == name.to_sym } if name
|
35
35
|
|
@@ -83,17 +83,17 @@ module Pubnub
|
|
83
83
|
|
84
84
|
def announce_status(options)
|
85
85
|
announcement_type = options[:announcement_type]
|
86
|
-
event
|
87
|
-
given_options
|
88
|
-
request
|
89
|
-
_response
|
86
|
+
event = options[:event]
|
87
|
+
given_options = options[:given_options]
|
88
|
+
request = options[:request]
|
89
|
+
_response = options[:response]
|
90
90
|
|
91
91
|
case announcement_type
|
92
92
|
when Pubnub::Constants::TIMEOUT_ANNOUNCEMENT
|
93
93
|
envelope = ErrorEnvelope.new(
|
94
|
-
event:
|
94
|
+
event: event,
|
95
95
|
event_options: given_options,
|
96
|
-
timetoken:
|
96
|
+
timetoken: nil,
|
97
97
|
status: {
|
98
98
|
code: nil,
|
99
99
|
client_request: request,
|
@@ -103,9 +103,9 @@ module Pubnub
|
|
103
103
|
error: true,
|
104
104
|
auto_retried: true,
|
105
105
|
|
106
|
-
current_timetoken:
|
107
|
-
last_timetoken:
|
108
|
-
subscribed_channels:
|
106
|
+
current_timetoken: @app.env[:timetoken].to_i,
|
107
|
+
last_timetoken: @app.env[:timetoken].to_i,
|
108
|
+
subscribed_channels: @app.subscribed_channels,
|
109
109
|
subscribed_channel_groups: @app.subscribed_groups,
|
110
110
|
|
111
111
|
config: get_config
|
@@ -122,9 +122,9 @@ module Pubnub
|
|
122
122
|
)
|
123
123
|
when Pubnub::Constants::RECONNECTED_ANNOUNCEMENT
|
124
124
|
envelope = ErrorEnvelope.new(
|
125
|
-
event:
|
125
|
+
event: event,
|
126
126
|
event_options: given_options,
|
127
|
-
timetoken:
|
127
|
+
timetoken: nil,
|
128
128
|
status: {
|
129
129
|
code: nil,
|
130
130
|
client_request: request,
|
@@ -134,9 +134,9 @@ module Pubnub
|
|
134
134
|
error: false,
|
135
135
|
auto_retried: true,
|
136
136
|
|
137
|
-
current_timetoken:
|
138
|
-
last_timetoken:
|
139
|
-
subscribed_channels:
|
137
|
+
current_timetoken: @app.env[:timetoken].to_i,
|
138
|
+
last_timetoken: @app.env[:timetoken].to_i,
|
139
|
+
subscribed_channels: @app.subscribed_channels,
|
140
140
|
subscribed_channel_groups: @app.subscribed_groups,
|
141
141
|
|
142
142
|
config: get_config
|
@@ -153,9 +153,9 @@ module Pubnub
|
|
153
153
|
)
|
154
154
|
when Pubnub::Constants::STATUS_REQUEST_MESSAGE_COUNT_EXCEEDED
|
155
155
|
envelope = Pubnub::Envelope.new(
|
156
|
-
event:
|
156
|
+
event: event,
|
157
157
|
event_options: given_options,
|
158
|
-
timetoken:
|
158
|
+
timetoken: nil,
|
159
159
|
status: {
|
160
160
|
code: nil,
|
161
161
|
client_request: request,
|
@@ -165,9 +165,9 @@ module Pubnub
|
|
165
165
|
error: false,
|
166
166
|
auto_retried: true,
|
167
167
|
|
168
|
-
current_timetoken:
|
169
|
-
last_timetoken:
|
170
|
-
subscribed_channels:
|
168
|
+
current_timetoken: @app.env[:timetoken].to_i,
|
169
|
+
last_timetoken: @app.env[:timetoken].to_i,
|
170
|
+
subscribed_channels: @app.subscribed_channels,
|
171
171
|
subscribed_channel_groups: @app.subscribed_groups,
|
172
172
|
|
173
173
|
config: get_config
|
@@ -253,7 +253,7 @@ module Pubnub
|
|
253
253
|
|
254
254
|
def build_subscription
|
255
255
|
@current_subscription = Subscribe.new({ ssl: @ssl }, @app)
|
256
|
-
@current_subscription_id = @current_subscription.
|
256
|
+
@current_subscription_id = @current_subscription.object_id
|
257
257
|
@current_subscription.build(callbacks: @callbacks,
|
258
258
|
channels: @channels,
|
259
259
|
groups: @groups,
|
@@ -263,30 +263,29 @@ module Pubnub
|
|
263
263
|
def remove_current_subscription
|
264
264
|
return if @current_subscription_id.nil?
|
265
265
|
Pubnub.logger.debug('Pubnub::Subscriber') { 'Removing current subscription' }
|
266
|
-
@current_subscription
|
267
|
-
@current_subscription = nil
|
266
|
+
@current_subscription = nil
|
268
267
|
@current_subscription_id = nil
|
269
268
|
@app.timetoken = 0
|
270
269
|
end
|
271
270
|
|
272
271
|
def start_subscription
|
273
272
|
Pubnub.logger.debug('Pubnub::Subscriber') { 'Starting subscription' }
|
274
|
-
|
275
|
-
@current_subscription.
|
273
|
+
raise 'Cannot start subscription without builded @current_subscription' if @current_subscription.nil?
|
274
|
+
@current_subscription.async.fire
|
276
275
|
end
|
277
276
|
|
278
277
|
def secure_call(cb, arg)
|
279
278
|
cb.call arg
|
280
|
-
rescue => error
|
279
|
+
rescue StandardError => error
|
281
280
|
Pubnub.logger.error('Pubnub::Subscriber') { "Error while calling callback #{error.inspect}" }
|
282
281
|
end
|
283
282
|
|
284
283
|
def get_config
|
285
284
|
{
|
286
|
-
tls:
|
287
|
-
uuid:
|
285
|
+
tls: @app.env[:ssl],
|
286
|
+
uuid: @app.env[:uuid],
|
288
287
|
auth_key: @app.env[:auth_key],
|
289
|
-
origin:
|
288
|
+
origin: @app.current_origin
|
290
289
|
}
|
291
290
|
end
|
292
291
|
end
|
data/lib/pubnub/telemetry.rb
CHANGED
@@ -1,43 +1,42 @@
|
|
1
1
|
module Pubnub
|
2
|
+
# Thread-safe async telemetry
|
2
3
|
class Telemetry
|
3
|
-
include
|
4
|
+
include Concurrent::Async
|
4
5
|
|
5
6
|
def initialize
|
6
|
-
@
|
7
|
-
Pubnub.logger.debug('Pubnub::Telementry') { 'Initialized Telemetry'}
|
7
|
+
@recorded_history = Concurrent::Map.new { default_telemetry }
|
8
|
+
Pubnub.logger.debug('Pubnub::Telementry') { 'Initialized Telemetry' }
|
8
9
|
end
|
9
10
|
|
10
11
|
def record_request(telemetry_type, time_start, time_end)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
Pubnub.logger.error('Pubnub::Telemetry'){ "Failed to record request #{error}\n#{error.backtrace.join("\n")}" }
|
12
|
+
@recorded_history.compute(telemetry_type) do |telemetry|
|
13
|
+
telemetry ||= default_telemetry
|
14
|
+
telemetry[:sum] += (time_end - time_start)
|
15
|
+
telemetry[:counter] += 1
|
16
|
+
telemetry
|
17
17
|
end
|
18
|
+
rescue StandardError => error
|
19
|
+
Pubnub.logger.error('Pubnub::Telemetry') { "Failed to record request #{error}\n#{error.backtrace.join("\n")}" }
|
18
20
|
end
|
19
21
|
|
20
22
|
def fetch_average(telemetry_type)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
average =
|
26
|
-
|
27
|
-
Pubnub.logger.debug('Pubnub::Telemetry'){ "Current average: #{average}" }
|
28
|
-
average
|
29
|
-
rescue => error
|
30
|
-
Pubnub.logger.error('Pubnub::Telemetry'){ "Failed to fetch average #{error}\n#{error.backtrace.join("\n")}" }
|
23
|
+
Pubnub.logger.debug('Pubnub::Telemetry') { "Fetching telemetry for #{telemetry_type}" }
|
24
|
+
return false if @recorded_history[telemetry_type][:counter].zero?
|
25
|
+
average = 0
|
26
|
+
@recorded_history.compute(telemetry_type) do |telemetry|
|
27
|
+
average = telemetry[:sum].to_f / telemetry[:counter].to_f
|
28
|
+
default_telemetry
|
31
29
|
end
|
30
|
+
Pubnub.logger.debug('Pubnub::Telemetry') { "Current average: #{average}" }
|
31
|
+
average
|
32
|
+
rescue StandardError => error
|
33
|
+
Pubnub.logger.error('Pubnub::Telemetry') { "Failed to fetch average #{error}\n#{error.backtrace.join("\n")}" }
|
32
34
|
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
rescue => error
|
39
|
-
Pubnub.logger.error('Pubnub::Telemetry'){ "Failed to clear telemetry #{error}\n#{error.backtrace.join("\n")}" }
|
40
|
-
end
|
36
|
+
private
|
37
|
+
|
38
|
+
def default_telemetry
|
39
|
+
{ sum: 0, counter: 0 }
|
41
40
|
end
|
42
41
|
end
|
43
|
-
end
|
42
|
+
end
|