sendgrid / sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
https://sendgrid.com
MIT License
625 stars 322 forks source link

Threadsafe? #423

Closed 9mm closed 4 years ago

9mm commented 4 years ago

Hello, is this gem threadsafe? I was wanting to use it inside Sidekiq.

Thanks!

childish-sambino commented 4 years ago

No it's not. I haven't dug to see if there are more reasons why it's not, but one is that the request that's built and the http client are attributes of the client-wrapper: https://github.com/sendgrid/ruby-http-client/blob/master/lib/ruby_http_client.rb#L86

So you'll need a SendGrid::API.new instance for each thread.

9mm commented 4 years ago

@childish-sambino ah, interesting... so I'm not an expert in threadsafe code (I just switched to Sidekiq so I'm trying to learn). Can you explain why the attr_reader line isn't thread safe? I thought that makes instance variables which are threadsafe as long as .new is instantiated for each thread?

childish-sambino commented 4 years ago

What I meant is that you can't use the same SendGrid::API instance across multiple threads. But yes, if each thread uses a .new instance then it should be safe.