You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The fields of the Client in xrpc/xrpc.go are currently accessed directly for reads and writes. When using the client across multiple threads, this could lead to race conditions (e.g. when simultaneously updating the AuthInfo while also trying to make an API request). Thus it would be useful to have the option to access these fields (and generally, use the XRPC client) in a thread-safe manner.
The alternative of isolating the full interactions with the client in the caller code, e.g. wrapping every API call in a Lock()/Unlock(), would be very inefficient.
Implementation
Since many tools and applications use the XRPC client, but most of them do not require this thread-safe access, it would probably be a good idea to let them interact with the client the same way as before, while providing an opt-in alternative for multithreaded applications.
One option would be to leave the fields as they are, public, but add a single RWMutex and additional getters and setters for the all client fields, which lock the mutex when accessing the field. Furthermore, all the client field accesses in func (c *Client) Do(...) error {} would be replaced with the getters & setters.
Then any code using the Client could easily use the same client across different threads while using the thread-safe getters and setters to access the fields, making API calls, updating auth tokens, etc.
And applications that don't require this can just use the client as before.
This is the implementation I currently use in my fork. I am happy to provide a PR if you think this is a good solution.
There could of course be other ways of doing this, the above seemed like a simple and flexible way to do it, but lmk if you have better ideas.
Example use case
An client application/bot could have various threads running that interact with the API/PDSes, e.g. event handlers/listeners for things like mentions, likes, etc., while also asynchronously update the auth tokens.
The text was updated successfully, but these errors were encountered:
Description
The fields of the
Client
inxrpc/xrpc.go
are currently accessed directly for reads and writes. When using the client across multiple threads, this could lead to race conditions (e.g. when simultaneously updating theAuthInfo
while also trying to make an API request). Thus it would be useful to have the option to access these fields (and generally, use the XRPC client) in a thread-safe manner.The alternative of isolating the full interactions with the client in the caller code, e.g. wrapping every API call in a
Lock()
/Unlock()
, would be very inefficient.Implementation
Since many tools and applications use the XRPC client, but most of them do not require this thread-safe access, it would probably be a good idea to let them interact with the client the same way as before, while providing an opt-in alternative for multithreaded applications.
One option would be to leave the fields as they are, public, but add a single
RWMutex
and additional getters and setters for the all client fields, which lock the mutex when accessing the field. Furthermore, all the client field accesses infunc (c *Client) Do(...) error {}
would be replaced with the getters & setters.Then any code using the Client could easily use the same client across different threads while using the thread-safe getters and setters to access the fields, making API calls, updating auth tokens, etc.
And applications that don't require this can just use the client as before.
This is the implementation I currently use in my fork. I am happy to provide a PR if you think this is a good solution.
There could of course be other ways of doing this, the above seemed like a simple and flexible way to do it, but lmk if you have better ideas.
Example use case
An client application/bot could have various threads running that interact with the API/PDSes, e.g. event handlers/listeners for things like mentions, likes, etc., while also asynchronously update the auth tokens.
The text was updated successfully, but these errors were encountered: