0

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.

Sudhagar
  • 178
  • 1
  • 3
  • 15
  • Where abouts are you setting this value? Looks like it should be in your application environments config files or an initialiser, in which case it is preloaded once when your app starts. Also unless you are using jruby or rubinius you don't need to worry about thread safety with this kind of thing – tpbowden Jun 28 '15 at 19:44
  • @tpbowden It is not in initializer. It is in slack omniauth controller for now, Because the token values are different everytime, I am running config again. – Sudhagar Jun 28 '15 at 19:46
  • Rails apps are single threaded so you won't have any problems anyway unless you're using a version of ruby with real threads and manually creating them. – tpbowden Jun 28 '15 at 19:51
  • Thanks @tpbowden, guess I have to read more on this topic. – Sudhagar Jun 28 '15 at 20:09
  • @Sudhagar : You are right. This code might cause problems if you use a multithreaded environment. You might want to read this [answer](http://stackoverflow.com/a/18438902/2483313) about multithreaded Rails servers. – spickermann Jun 28 '15 at 21:49

1 Answers1

1

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.

dB.
  • 4,700
  • 2
  • 46
  • 51