ionos-cloud / sdk-java

IONOS Cloud Official Java SDK
Apache License 2.0
3 stars 4 forks source link

Thread safe API #4

Open fxnn opened 3 years ago

fxnn commented 3 years ago

Current SDK Version

com.ionoscloud:ionos-cloud-sdk:5.0.1

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 use sdk-java easily and without any concurrency traps.

Attempted Solutions

I can reach the goal by

  1. not sharing the com.ionoscloud.ApiClient instance amongst different threads (because it apparently is not thread safe),
  2. avoiding all the default constructors like
    • com.ionoscloud.ApiClient#ApiClient(), because that one always creates a new OKHttpClient which 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,
  3. 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.

References

None.

dpoerschke commented 9 months ago

This issue report is now almost 3 years old - is there any news on this?