Skip to content

Store connection in Thread local or instance instead of class #255

Closed
@wgrrrr

Description

@wgrrrr

I am writing a multi-tenant front-end application which queries a back-end service. The API is authenticated via HTTP basic, which I have implemented in json_api_client using a Faraday middleware:

self.connection do |connection|
  connection.use Faraday::Request::BasicAuthentication, api_key, api_secret
end

The problem is that this connection is stored as a class variable and is not thread safe. I considered just calling self.connection(true) to rebuild it on every request, but I believe race conditions will emerge. Ultimately, the design is the problem. I'd love to be able to store a connection as a Thread local variable, or worst case as an instance variable.

I'd like to open a conversation with any interested parties to discuss whether there might be a better approach, or if we should consider re-designing the storage of the connection.

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