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 caa4646
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ class RelayListListener(
messageHandler.trySendRequest(Request.SetWireguardConstraints(value))
}

fun updateSelectedOwnershipFilter(value: Constraint<Ownership>) {
messageHandler.trySendRequest(Request.SetOwnership(value))
}

fun updateSelectedProvidersFilter(value: Constraint<Providers>) {
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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class RelayListFilterUseCase(
private val relayListListener: RelayListListener,
private val settingsRepository: SettingsRepository
) {
fun updateOwnershipFilter(ownership: Constraint<Ownership>) {
relayListListener.updateSelectedOwnershipFilter(ownership)
}

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

fun selectedOwnership(): Flow<Constraint<Ownership>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ sealed class Request : Message.RequestMessage() {
data class SetWireGuardQuantumResistant(val quantumResistant: QuantumResistantState) :
Request()

@Parcelize data class SetOwnership(val ownership: Constraint<Ownership>) : Request()

@Parcelize data class SetProviders(val providers: Constraint<Providers>) : Request()

@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 @@ -67,25 +67,20 @@ class RelayListListener(
}

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

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

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

Expand Down

0 comments on commit caa4646

Please sign in to comment.