Skip to content

Commit

Permalink
Fix race condition when updating provider and ownership at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Nov 16, 2023
1 parent 13ea601 commit 8a01911
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,12 @@ class RelayListListener(
messageHandler.trySendRequest(Request.SetProviders(value))
}

fun updateSelectedOwnershipAndProviderFilter(
ownership: Constraint<Ownership>,
providers: Constraint<Providers>
) {
messageHandler.trySendRequest(Request.SetOwnershipAndProviders(ownership, providers))
}

private fun defaultRelayList() = RelayList(ArrayList(), WireguardEndpointData(ArrayList()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class RelayListFilterUseCase(
relayListListener.updateSelectedProvidersFilter(providers)
}

fun updateOwnershipAndProviderFilter(
ownership: Constraint<Ownership>,
providers: Constraint<Providers>
) {
relayListListener.updateSelectedOwnershipAndProviderFilter(ownership, providers)
}

fun selectedOwnership(): Flow<Constraint<Ownership>> =
settingsRepository.settingsUpdates.map { settings ->
settings?.relaySettings?.relayConstraints()?.ownership ?: Constraint.Any()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ sealed class Request : Message.RequestMessage() {

@Parcelize data object FetchRelayList : Request()

@Parcelize
data class SetOwnershipAndProviders(
val ownership: Constraint<Ownership>,
val providers: Constraint<Providers>
) : Request()

companion object {
private const val MESSAGE_KEY = "request"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ class RelayListListener(
relayList = daemon.await().getRelayLocations()
}
}

scope.launch {
endpoint.dispatcher.parsedMessages
.filterIsInstance<Request.SetOwnershipAndProviders>()
.collect { request ->
val update =
getCurrentRelayConstraints()
.copy(ownership = request.ownership, providers = request.providers)
daemon.await().setRelaySettings(RelaySettings.Normal(update))
}
}
}

fun onDestroy() {
Expand Down

0 comments on commit 8a01911

Please sign in to comment.