Skip to content

Commit

Permalink
fix leak, fix IllegalMonitorStateException, move jwt to Authorization…
Browse files Browse the repository at this point in the history
… header
  • Loading branch information
rpurdel committed May 17, 2024
1 parent 53c1682 commit d8c0411
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/main/java/org/jitsi/jigasi/transcription/WhisperWebsocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public class WhisperWebsocket

private final static String jwtAudience;

private WebSocketClient ws;

static
{
jwtAudience = JigasiBundleActivator.getConfigurationService()
Expand Down Expand Up @@ -135,11 +137,11 @@ public class WhisperWebsocket
logger.info("Websocket transcription streaming endpoint: " + websocketUrlConfig);
}

private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException
private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException
{
if (privateKey.isEmpty() || privateKeyName.isEmpty())
{
return null;
throw new IOException("Failed generating JWT for Whisper. Missing private key or key name.");
}
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
Expand All @@ -164,15 +166,7 @@ private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException
*/
private void generateWebsocketUrl()
{
try
{
websocketUrl = websocketUrlConfig + "/" + connectionId + "?auth_token=" + getJWT();
}
catch (Exception e)
{
Statistics.incrementTotalTranscriberConnectionErrors();
logger.error("Failed generating JWT for Whisper. " + e);
}
websocketUrl = websocketUrlConfig + "/" + connectionId;
if (logger.isDebugEnabled())
{
logger.debug("Whisper URL: " + websocketUrl);
Expand All @@ -197,9 +191,11 @@ void connect()
{
generateWebsocketUrl();
logger.info("Connecting to " + websocketUrl);
WebSocketClient ws = new WebSocketClient();
ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest();
upgradeRequest.setHeader("Authorization", "Bearer " + getJWT());
ws = new WebSocketClient();
ws.start();
CompletableFuture<Session> connectFuture = ws.connect(this, new URI(websocketUrl));
CompletableFuture<Session> connectFuture = ws.connect(this, new URI(websocketUrl), upgradeRequest);
wsSession = connectFuture.get();
wsSession.setIdleTimeout(Duration.ofSeconds(300));
isConnected = true;
Expand All @@ -216,11 +212,14 @@ void connect()
logger.error(e.toString());
}
attempt++;
try
synchronized (this)
{
wait(waitTime);
try
{
wait(waitTime);
}
catch (InterruptedException ignored) {}
}
catch (InterruptedException ignored) {}
}

if (!isConnected)
Expand All @@ -236,6 +235,18 @@ public void onClose(int statusCode, String reason)
wsSession = null;
participants = null;
participantListeners = null;
try
{
if (ws != null)
{
ws.stop();
ws = null;
}
}
catch (Exception e)
{
logger.error("Error while stopping WebSocketClient", e);
}
}


Expand Down

0 comments on commit d8c0411

Please sign in to comment.