As a developer, I want to use the sdk-java in a multi threaded application (like Servers) so that, while my application works on its tasks using the machine's full compute power, I can use sdk-java easily and without any concurrency traps.
Attempted Solutions
I can reach the goal by
not sharing the com.ionoscloud.ApiClient instance amongst different threads (because it apparently is not thread safe),
avoiding all the default constructors like
com.ionoscloud.ApiClient#ApiClient(), because that one always creates a new OKHttpClientwhich is discouraged by the JavaDoc (as it wastes resources),
com.ionoscloud.api.DataCenterApi#DataCenterApi(), because that one always uses the same ApiClient instance (from com.ionoscloud.Configuration#getDefaultApiClient), which however is not thread safe,
creating a fresh ApiClient instance for every thread, and linking it to the same OkHttpClient instance using the com.ionoscloud.ApiClient#ApiClient(okhttp3.OkHttpClient) constructor,
which, however, has the drawback that there usually seems to be some amount of management in com.ionoscloud.ApiClient#initHttpClient(java.util.List<okhttp3.Interceptor>) invoked from the zero-arg constructor, which is not done using that one-argument constructor -- that leaves a bad taste and the question, whether things could break, also in future versions of the library.
Proposal
From my POV, the best approach would be to use thread safe data structures in the ApiClient -- especially looking at the Map and InputStream types, but basically anything that can be changed at runtime (e.g. using a setter) can lead to unexpected effects when used in a multi-threaded situation.
Another possibility -- but only for a new major release -- could also be to use a two-step instantiation approach, where the user first configures the ApiClient using a Builder, and then receives an immutable instance.
Current SDK Version
Use-cases
As a developer, I want to use the
sdk-java
in a multi threaded application (like Servers) so that, while my application works on its tasks using the machine's full compute power, I can usesdk-java
easily and without any concurrency traps.Attempted Solutions
I can reach the goal by
com.ionoscloud.ApiClient
instance amongst different threads (because it apparently is not thread safe),com.ionoscloud.ApiClient#ApiClient()
, because that one always creates a newOKHttpClient
which is discouraged by the JavaDoc (as it wastes resources),com.ionoscloud.api.DataCenterApi#DataCenterApi()
, because that one always uses the sameApiClient
instance (fromcom.ionoscloud.Configuration#getDefaultApiClient
), which however is not thread safe,ApiClient
instance for every thread, and linking it to the sameOkHttpClient
instance using thecom.ionoscloud.ApiClient#ApiClient(okhttp3.OkHttpClient)
constructor,com.ionoscloud.ApiClient#initHttpClient(java.util.List<okhttp3.Interceptor>)
invoked from the zero-arg constructor, which is not done using that one-argument constructor -- that leaves a bad taste and the question, whether things could break, also in future versions of the library.Proposal
From my POV, the best approach would be to use thread safe data structures in the
ApiClient
-- especially looking at theMap
andInputStream
types, but basically anything that can be changed at runtime (e.g. using a setter) can lead to unexpected effects when used in a multi-threaded situation.Another possibility -- but only for a new major release -- could also be to use a two-step instantiation approach, where the user first configures the
ApiClient
using aBuilder
, and then receives an immutable instance.References
None.