Extracted from this disussion thread...

Just to summarize:

  • Contexts and apis are thread-safe (or should! Otherwise it is an issue)
  • Apis are not bound as singletons to the Guice context, however
  • The main accessors available to get an Api return the same instance

In more detail: Contexts and apis are thread-safe. Regarding whether APIs are singleton or not, the api objects (all apis have just one impl which is the proxy that generates the http requests) are generated here. That provider class is declared to be a singleton, but that applies only to the provider itself; not to the result produced by its get() method. The provider and the api class are bound to the Guice context here and here, and as you can see those bindings are not using .in(Scopes.SINGLETON), so every time the provider is called (an injection is requested or an instance is directly requested to the Guice injector) a new api proxy will be created.

This said, there are two main points where apis are requested to Guice: when calling ContextBuilder.buildApi() and when calling the context.getApi() method on an existing context.

In the first case users are not building an entire context, just the api, so that's all they get. There will only be that instance unless they manually request one to the Guice injector or build a new one.

In the second case, the api is a member variable of the context, and it is injected in the context constructor, so the same instance of the api will be returned on every call to its getApi() method.

 

  • No labels