Skip to content

Rails example is not threadsafe #59

@kingdonb

Description

@kingdonb

https://github.com/3scale/3scale_ws_api_for_ruby#a-rails-example

The example provided in Rails Example section of the README is not thread-safe. This is not a good example because the default Rails web server today, puma, is threaded.

A better example might include this method for create_client instead:

def create_client
  @@threescale_client ||= {}
  @@threescale_client[Thread.current.name] ||=
    ThreeScale::Client.new(service_tokens: true)
end

I am not an expert but when I was hitting my API several times within the space of a second, I was getting some calls out to 3scale aborted with a response from "openresty" that looked like a header was placed into the body by accident.

It seems to me this could only be a threading issue, because it went away when I gave each worker thread his own 3scale client.

> puts body
HTTP/1.1 200 OK
Server: openresty
Date: Thu, 15 Nov 2018 14:45:56 GMT
Content-Type: application/vnd.3scale-v2.0+xml
Con

intercepted at lib/3scale/client.rb ~ line 368 instead of the expected response body:

<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Your Plan Name Here</plan></status>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions