-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ChannelAuthorizer supports only synchronous flow #340
Comments
Hi, thanks for describing a possible solution. We will take a look and see if it is a feature we can prioritise. |
I met this issue too recently. I am trying to implement batch authentication in java.
For examplein public synchronized void queueOnAuthenticationThread(final Runnable r) {
if (authenticationQueue == null) {
authenticationQueue = Executors.newCachedThreadPool(new DaemonThreadFactory("authenticationQueue"));
}
authenticationQueue.execute(r);
} in private void sendOrQueueSubscribeMessage(final InternalChannel channel) {
factory.queueOnAuthenticationThread(() -> {
if (connection.getState() == ConnectionState.CONNECTED) {
try {
final String message = channel.toSubscribeMessage();
factory.queueOnEventThread(() -> {
connection.sendMessage(message);
channel.updateState(ChannelState.SUBSCRIBE_SENT);
});
} catch (final AuthorizationFailureException e) {
handleAuthenticationFailure(channel, e);
}
}
});
} But this solution seems break the order of Hope we could have a conclusion and then I will prepare a PR for this issue. |
Maybe per channel object lock could avoid |
What is the issue?
Currently
ChannelAuthorizer
expects a synchronousauthorize
implementation that returns a string.It would be nice to rewrite it in a more asynchronous way, so that
ChannelAuthorizer
interface wouldn't force the implementation to block the thread ifauthorize
requires async job.It limits Kotlin usage e.g. in your React Native SDK, that needs async job by design (of React Native bridge):
Native Java/Kotlin module sends an event to Javascript and later Javascript calls Native module with auth object.
Related issue in your React Native SDK: pusher/pusher-websocket-react-native#37
Is it a crash report? Submit stack traces or anything that you think would help
Nope, but please check the linked issues for more context and possible freezes/errors.
Any improvements you suggest
The React Native SDK (Android/Kotlin code) uses mutex to block the thread and wait until JavaScript returns proper auth object through RN bridge.
It's not really a common flow in RN world (to block the thread in RN module), because it can lead to various issues/freezes/crashes/leaks.
Instead, it is preferred to implement logic in an asynchronous way (listeners/storing callbacks) since RN bridge is also asynchronous.
There was a similar but more serious issue in iOS part of your React Native SDK:
pusher/pusher-websocket-react-native#34
But it can be fixed by simply storing callbacks instead of blocking the thread:
pusher/pusher-websocket-react-native#36
However, it is not possible to implement it like this in Android (Kotlin) part, because of the
ChannelAuthorizer
interface.So my suggestion would be to rewrite
ChannelAuthorizer
in a way that supports async/delayed response.For example (there might be a better way, but this should do the job):
I know it would be a breaking change, but it should be possible to add backwards compatible util (that simply calls
authResultCallback
instead ofreturn
) or support both ways.CC @pusher/mobile
The text was updated successfully, but these errors were encountered: