Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async handler support for Slack::RealTime::Config and Client #486

Merged
merged 27 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
62b9fdb
Async cache build for Slack::RealTime::Stores::Store
milestruecar Jun 27, 2023
2881802
rubocop
milestruecar Jun 27, 2023
dccc45a
instead of asyncing one thing, async ALL the things
milestruecar Jun 27, 2023
5e878ab
remove stray logging line
milestruecar Jun 27, 2023
f3c1af0
run handlers loop in a single async task
milestruecar Jun 30, 2023
69a0966
Add support for async_handlers in Slack::RealTime::Config
milestruecar Jul 6, 2023
3795520
Merge branch 'master' into async-cache-build-485
milestruecar Jul 6, 2023
2664bff
remove log line
milestruecar Jul 6, 2023
9da3f7d
remove tricky send and just run it through a case statement
milestruecar Jul 6, 2023
feb221c
add Slack::RealTime::Config::InvalidAsyncHandlersError and messaging
milestruecar Jul 7, 2023
2c97c31
shorter error message
milestruecar Jul 7, 2023
70d2507
rspec: ensure Async::Task is returned when config#async_handlers is :all
milestruecar Jul 19, 2023
293bafe
Merge branch 'master' into async-cache-build-485
milestruecar Jul 20, 2023
9770b69
add async_handlers config option to README.md
milestruecar Jul 20, 2023
8be8667
rubocop
milestruecar Jul 20, 2023
e4f877b
be more explicit with ::Async
milestruecar Jul 20, 2023
770f526
Revert "be more explicit with ::Async"
milestruecar Jul 20, 2023
1810223
Merge branch 'master' into async-cache-build-485
milestruecar Jul 24, 2023
18fa984
implement run_async in RealTime::Concurrency::Async::Socket, add test…
milestruecar Aug 3, 2023
9d0e2a8
i mean this makes a passing test i guess
milestruecar Aug 3, 2023
14a1d58
more tests
milestruecar Aug 4, 2023
10446ed
rubocop, move test so i'm not making a redundant copy
milestruecar Aug 4, 2023
6079b0f
rubocop again
milestruecar Aug 4, 2023
3c627d1
Split async client specs. (#1)
dblock Aug 7, 2023
8e3706a
Merge branch 'master' into async-cache-build-485
milestruecar Aug 7, 2023
2d174c6
Fix: handle CONCURRENCY not being set (#2)
dblock Aug 7, 2023
85e6c9b
fix merge conflict
milestruecar Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#480](https://github.com/slack-ruby-client/pulls/480): Add common message formatting utilities - [@chrisbloom7](https://github.com/chrisbloom7).
* [#481](https://github.com/slack-ruby-client/pulls/481): Update API from [slack-api-ref@7c22d0b](https://github.com/slack-ruby/slack-api-ref/commit/7c22d0b) - [@slack-ruby-ci-bot](https://github.com/apps/slack-ruby-ci-bot).
* [#488](https://github.com/slack-ruby-client/pulls/488): Update API from [slack-api-ref@a45def2](https://github.com/slack-ruby/slack-api-ref/commit/a45def2) - [@slack-ruby-ci-bot](https://github.com/apps/slack-ruby-ci-bot).
* [#486](https://github.com/slack-ruby/slack-ruby-client/pull/486): Async handler support for Slack::RealTime::Config and Client - [@milestruecar](https://github.com/milestruecar).
* Your contribution here.

### 2.1.0 (2023/03/17)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ websocket_proxy | Connect via proxy, include `:origin` and `:headers`.
start_options | Options to pass to `rtm.connect`, default is `{ request: { timeout: 180 } }`.
store_class | Local store class, default is an in-memory `Slack::RealTime::Stores::Starter`.
store_options | Options to initialize the store, default is `{}`.
async_handlers | Option to run handlers asynchronously. Valid options are `:all` or `:none`, default is `:none`
dblock marked this conversation as resolved.
Show resolved Hide resolved
logger | Optional `Logger` instance that logs RealTime requests and socket data.

Note that the RealTime client uses a Web client to obtain the WebSocket URL via [rtm.connect](https://api.slack.com/methods/rtm.connect). While `token` and `logger` options are passed down from the RealTime client, you may also configure Web client options via `Slack::Web::Client.configure` as described above.
Expand Down
16 changes: 14 additions & 2 deletions lib/slack/real_time/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,25 @@ def dispatch(event)

def run_handlers(type, data)
handlers = store.class.events[type.to_s]
handlers.each do |handler|
store.instance_exec(data, self, &handler)
case async_handlers
when :all
Async.run { handlers_loop(handlers, data) }
when :none
handlers_loop(handlers, data)
else
raise Config::InvalidAsyncHandlersError,
"Invalid value '#{async_handlers.inspect}' for config#async_handlers, must be :all or :none."
end
rescue StandardError => e
logger.error("#{self}##{__method__}") { e }
false
end

def handlers_loop(handlers, data)
handlers.each do |handler|
store.instance_exec(data, self, &handler)
end
end

def run_callbacks(type, data)
callbacks = self.callbacks[type]
Expand Down
3 changes: 3 additions & 0 deletions lib/slack/real_time/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Slack
module RealTime
module Config
class NoConcurrencyError < StandardError; end
class InvalidAsyncHandlersError < StandardError; end

extend self

Expand All @@ -15,6 +16,7 @@ class NoConcurrencyError < StandardError; end
store_class
store_options
logger
async_handlers
].freeze

attr_accessor(*Config::ATTRIBUTES - [:concurrency])
Expand All @@ -29,6 +31,7 @@ def reset
self.store_class = Slack::RealTime::Stores::Starter
self.store_options = {}
self.logger = nil
self.async_handlers = :none
end

def concurrency
Expand Down
22 changes: 18 additions & 4 deletions spec/slack/real_time/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,30 @@
end

describe '#run_handlers' do
context 'when store has no event hooks' do
before do
@events = client.store.class.events.dup
client.store.class.events.clear
end

after do
client.store.class.events.merge!(@events)
dblock marked this conversation as resolved.
Show resolved Hide resolved
end

context 'when config#async_handlers is :all' do
before do
@events = client.store.class.events.dup
client.store.class.events.clear
client.async_handlers = :all
end

after do
client.store.class.events.merge!(@events)
client.async_handlers = :none
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Store the original value of async_handlers.

end

it 'returns an Async::Task' do
expect(client.send(:run_handlers, 'example', {})).to be_a Async::Task
end
end

context 'when store has no event hooks' do
milestruecar marked this conversation as resolved.
Show resolved Hide resolved
it 'returns empty array of handlers' do
expect(client.send(:run_handlers, 'example', {})).to be_empty
end
Expand Down
Loading