Thread safety in rails with slack-api gem

380 views Asked by At

I have just started using rails. In my app I have to access Slack apis, so I am using the slack-api gem. The way to configure that is

Slack.configure do |config|
  config.token = "token"
end

I am wondering since the token is configured at class level 'Slack', would that cause any inconsistent behaviour? One request might set it to value A and before it is done, another request may set it to value B.

1

There are 1 answers

0
dB. On

While Ruby web frameworks are generally single-threaded, this is not always the case. So it likely will cause problems if the token is different across multiple requests, will be hard to reason about or become a problem in the long run.

Try the newer gem, https://github.com/dblock/slack-ruby-client which will take a token in the initializer, ie. Slack::Web::Client.new(token: 'token') or Slack::RealTime::Client.new(token: 'token'), should avoid the problem altogether.