Renew turnserver credentials when ttl runs out

The previous implementation caches the turnserver response indefinitely.
This breaks VoIP calls as soon as the ttl of the received turnserver
credentials runs out. So, take care to renew the turnserver credentials
by allowing the cache to expire.

Fixes: d8cf44fdc9 ("Simple cache of turn server response")
Signed-off-by: Thomas Zeitlhofer <tz+github@ze-it.at>
This commit is contained in:
Thomas Zeitlhofer 2020-08-20 14:11:44 +02:00
parent 29e4a64475
commit a7ee7d5bad

View file

@ -58,18 +58,32 @@ internal class DefaultCallSignalingService @Inject constructor(
private val activeCalls = mutableListOf<MxCall>() private val activeCalls = mutableListOf<MxCall>()
private var cachedTurnServerResponse: TurnServerResponse? = null private var cachedTurnServerResponse = object {
private val MIN_TTL = 60
private val now = { System.currentTimeMillis() / 1000 }
private var expiresAt: Long = 0
var data: TurnServerResponse? = null
get() = if (expiresAt > now()) field else null
set(value) {
expiresAt = now() + (value?.ttl ?: 0) - MIN_TTL
field = value
}
}
override fun getTurnServer(callback: MatrixCallback<TurnServerResponse>): Cancelable { override fun getTurnServer(callback: MatrixCallback<TurnServerResponse>): Cancelable {
if (cachedTurnServerResponse != null) { if (cachedTurnServerResponse.data != null) {
cachedTurnServerResponse?.let { callback.onSuccess(it) } cachedTurnServerResponse.data?.let { callback.onSuccess(it) }
return NoOpCancellable return NoOpCancellable
} }
return turnServerTask return turnServerTask
.configureWith(GetTurnServerTask.Params) { .configureWith(GetTurnServerTask.Params) {
this.callback = object : MatrixCallback<TurnServerResponse> { this.callback = object : MatrixCallback<TurnServerResponse> {
override fun onSuccess(data: TurnServerResponse) { override fun onSuccess(data: TurnServerResponse) {
cachedTurnServerResponse = data cachedTurnServerResponse.data = data
callback.onSuccess(data) callback.onSuccess(data)
} }