-
Notifications
You must be signed in to change notification settings - Fork 69
Open
Description
I realise this is a generic question which could apply to many similar situations but I thought I'd bring it up in case anyone thinks it's useful to mention it in createsend-ruby's README.md.
In multi-threaded applications, it is possible to get into an unsafe situation when refreshing tokens, e.g.:
record = SomeModel.find(...)
cs = CreateSend::CreateSent.new(
record.access_token,
record.refresh_token
)
begin
tries = 2
cs.clients
rescue CreateSend::ExpiredOAuthToken => e
access_token, expires_in, request_token = cs.refresh_token
record.access_token = access_token
record.request_token = request_token
record.save
retry if (trues -= 1).zero?
raise e
end
record.access_tokenexpires- Thread A encounters
ExpiredOAuthToken - Thread A calls
#refresh_token - Thread B encounters
ExpiredOAuthToken - Thread B calls
#refresh_tokenbut fails becauserecord.refresh_tokenis now invalid - Thread A persists new token
- Thread A continues
I'm assuming others have encountered this possibility. Anyone have any favoured suggestions for dealing with it?
I've created a question on SO too (modified the code to make it broader than createsend-ruby) if anyone would like some points!
Metadata
Metadata
Assignees
Labels
No labels