Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notify web-socket event listeners independently #8042
- Loading branch information
1 parent
23617ce
commit d93cb6e
Showing
28 changed files
with
519 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
modules/admin/admin-event/src/main/java/com/enonic/xp/admin/event/impl/EventEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.enonic.xp.admin.event.impl; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.websocket.CloseReason; | ||
import javax.websocket.Endpoint; | ||
import javax.websocket.EndpointConfig; | ||
import javax.websocket.Session; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class EventEndpoint | ||
extends Endpoint | ||
{ | ||
private static final Logger LOG = LoggerFactory.getLogger( EventEndpoint.class ); | ||
|
||
private static final int INFLIGHT_MAX = 100_000; | ||
|
||
private final WebsocketManager webSocketManager; | ||
|
||
private final AtomicInteger inflightCounter = new AtomicInteger( 0 ); | ||
|
||
private volatile Session session; | ||
|
||
public EventEndpoint( final WebsocketManager webSocketManager ) | ||
{ | ||
this.webSocketManager = webSocketManager; | ||
} | ||
|
||
@Override | ||
public void onOpen( final Session session, final EndpointConfig config ) | ||
{ | ||
this.session = session; | ||
this.webSocketManager.registerSocket( this ); | ||
LOG.debug( "Opened websocket {}", session.getId() ); | ||
} | ||
|
||
@Override | ||
public void onClose( final Session session, final CloseReason closeReason ) | ||
{ | ||
unregister(); | ||
LOG.debug( "Closed websocket {}", session.getId() ); | ||
} | ||
|
||
@Override | ||
public void onError( final Session session, final Throwable error ) | ||
{ | ||
unregister(); | ||
LOG.warn( "Errored websocket {}", session.getId(), error ); | ||
} | ||
|
||
public void sendMessage( final String message ) | ||
{ | ||
final Session session = this.session; | ||
if ( isSessionOpen( session ) ) | ||
{ | ||
final int inflight = inflightCounter.getAndIncrement(); | ||
|
||
if ( inflight < INFLIGHT_MAX ) | ||
{ | ||
session.getAsyncRemote().sendText( message, result -> inflightCounter.decrementAndGet() ); | ||
} | ||
else if ( inflight == INFLIGHT_MAX ) | ||
{ | ||
unregister(); | ||
LOG.warn( "Websocket client is too slow. Closing websocket {}", session.getId() ); | ||
try | ||
{ | ||
session.close( new CloseReason( CloseReason.CloseCodes.TRY_AGAIN_LATER, "Websocket client is too slow" ) ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
LOG.error( "Failed to close slow websocket", e ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public boolean isOpen() | ||
{ | ||
return isSessionOpen( this.session ); | ||
} | ||
|
||
private void unregister() | ||
{ | ||
this.webSocketManager.unregisterSocket( this ); | ||
this.session = null; | ||
} | ||
|
||
private static boolean isSessionOpen( final Session session ) | ||
{ | ||
return session != null && session.isOpen(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../admin/admin-event/src/main/java/com/enonic/xp/admin/event/impl/EventEndpointFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.enonic.xp.admin.event.impl; | ||
|
||
import java.util.List; | ||
|
||
import javax.websocket.Endpoint; | ||
|
||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import com.enonic.xp.web.websocket.EndpointFactory; | ||
|
||
@Component | ||
public class EventEndpointFactory | ||
implements EndpointFactory | ||
{ | ||
private static final List<String> SUB_PROTOCOLS = List.of( "text" ); | ||
|
||
private final WebsocketManager webSocketManager; | ||
|
||
@Activate | ||
public EventEndpointFactory( @Reference final WebsocketManager webSocketManager ) | ||
{ | ||
this.webSocketManager = webSocketManager; | ||
} | ||
|
||
@Override | ||
public Endpoint newEndpoint() | ||
{ | ||
return new EventEndpoint( webSocketManager ); | ||
} | ||
|
||
@Override | ||
public List<String> getSubProtocols() | ||
{ | ||
return SUB_PROTOCOLS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
...les/admin/admin-event/src/main/java/com/enonic/xp/admin/event/impl/EventListenerImpl.java
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
modules/admin/admin-event/src/main/java/com/enonic/xp/admin/event/impl/EventWebSocket.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
modules/admin/admin-event/src/main/java/com/enonic/xp/admin/event/impl/WebSocketManager.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...dmin/admin-event/src/main/java/com/enonic/xp/admin/event/impl/WebsocketEventExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.enonic.xp.admin.event.impl; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
public interface WebsocketEventExecutor | ||
extends Executor | ||
{ | ||
} |
Oops, something went wrong.