Skip to content

Commit

Permalink
fix: Fixes leak and IllegalMonitorStateException, move jwt to Authori…
Browse files Browse the repository at this point in the history
…zation… (#538)

* fix leak, fix IllegalMonitorStateException, move jwt to Authorization header

* simpler connection
  • Loading branch information
rpurdel committed May 18, 2024
1 parent 53c1682 commit 7a3211b
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 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,10 +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));
wsSession = connectFuture.get();
wsSession = ws.connect(this, new URI(websocketUrl), upgradeRequest).get();
wsSession.setIdleTimeout(Duration.ofSeconds(300));
isConnected = true;
logger.info("Successfully connected to " + websocketUrl);
Expand All @@ -216,11 +211,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 +234,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 7a3211b

Please sign in to comment.