mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 00:56:50 +00:00
Add namespace to provisioning pub-sub
This commit is contained in:
1 parent
367854ea14
commit
5fde1d8f7b
2 files changed
+60
-5
No files matched your search
+26
-5
@@ -7,6 +7,7 @@ package org.whispersystems.textsecuregcm.push;
|
||||
|
||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import io.dropwizard.lifecycle.Managed;
|
||||
@@ -41,6 +42,8 @@ public class ProvisioningManager extends RedisPubSubAdapter<byte[], byte[]> impl
|
||||
private static final String RECEIVE_PROVISIONING_MESSAGE_COUNTER_NAME =
|
||||
name(ProvisioningManager.class, "receiveProvisioningMessage");
|
||||
|
||||
private static final String CHANNEL_PREFIX = "provisioning::";
|
||||
|
||||
private static final String RETRY_NAME = ResilienceUtil.name(ProvisioningManager.class);
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProvisioningManager.class);
|
||||
@@ -64,12 +67,14 @@ public class ProvisioningManager extends RedisPubSubAdapter<byte[], byte[]> impl
|
||||
|
||||
public void addListener(final String address, final Consumer<PubSubProtos.PubSubMessage> listener) {
|
||||
listenersByProvisioningAddress.put(address, listener);
|
||||
pubSubConnection.usePubSubConnection(connection -> connection.sync().subscribe(address.getBytes(StandardCharsets.UTF_8)));
|
||||
pubSubConnection.usePubSubConnection(connection -> connection.sync().subscribe(
|
||||
getPrefixedChannel(address), getUnprefixedChannel(address)));
|
||||
}
|
||||
|
||||
public void removeListener(final String address) {
|
||||
RedisOperation.unchecked(() ->
|
||||
pubSubConnection.usePubSubConnection(connection -> connection.sync().unsubscribe(address.getBytes(StandardCharsets.UTF_8))));
|
||||
pubSubConnection.usePubSubConnection(connection -> connection.sync().unsubscribe(
|
||||
getPrefixedChannel(address), getUnprefixedChannel(address))));
|
||||
|
||||
listenersByProvisioningAddress.remove(address);
|
||||
}
|
||||
@@ -82,7 +87,7 @@ public class ProvisioningManager extends RedisPubSubAdapter<byte[], byte[]> impl
|
||||
|
||||
final boolean receiverPresent = ResilienceUtil.getGeneralRedisRetry(RETRY_NAME)
|
||||
.executeSupplier(() -> pubSubClient.withBinaryConnection(connection ->
|
||||
connection.sync().publish(address.getBytes(StandardCharsets.UTF_8), pubSubMessage.toByteArray()) > 0));
|
||||
connection.sync().publish(getUnprefixedChannel(address), pubSubMessage.toByteArray()) > 0));
|
||||
|
||||
Metrics.counter(SEND_PROVISIONING_MESSAGE_COUNTER_NAME, "online", String.valueOf(receiverPresent)).increment();
|
||||
|
||||
@@ -92,7 +97,7 @@ public class ProvisioningManager extends RedisPubSubAdapter<byte[], byte[]> impl
|
||||
@Override
|
||||
public void message(final byte[] channel, final byte[] message) {
|
||||
try {
|
||||
final String address = new String(channel, StandardCharsets.UTF_8);
|
||||
final String address = getAddress(channel);
|
||||
final PubSubProtos.PubSubMessage pubSubMessage = PubSubProtos.PubSubMessage.parseFrom(message);
|
||||
|
||||
if (pubSubMessage.getType() == PubSubProtos.PubSubMessage.Type.DELIVER) {
|
||||
@@ -114,6 +119,22 @@ public class ProvisioningManager extends RedisPubSubAdapter<byte[], byte[]> impl
|
||||
|
||||
@Override
|
||||
public void unsubscribed(final byte[] channel, final long count) {
|
||||
listenersByProvisioningAddress.remove(new String(channel, StandardCharsets.UTF_8));
|
||||
listenersByProvisioningAddress.remove(getAddress(channel));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static byte[] getPrefixedChannel(final String address) {
|
||||
return (CHANNEL_PREFIX + address).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static byte[] getUnprefixedChannel(final String address) {
|
||||
return address.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static String getAddress(final byte[] channel) {
|
||||
final String channelName = new String(channel, StandardCharsets.UTF_8);
|
||||
|
||||
return channelName.startsWith(CHANNEL_PREFIX) ? channelName.substring(CHANNEL_PREFIX.length()) : channelName;
|
||||
}
|
||||
}
|
||||
+34
@@ -14,6 +14,8 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.whispersystems.textsecuregcm.redis.RedisServerExtension;
|
||||
import org.whispersystems.textsecuregcm.storage.PubSubProtos;
|
||||
@@ -72,4 +74,36 @@ class ProvisioningManagerTest {
|
||||
// Make sure that we give the message enough time to show up (if it was going to) before declaring victory
|
||||
verify(subscribedConsumer, after(PUBSUB_TIMEOUT_MILLIS).never()).accept(any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void receiveProvisioningMessageChannelMigration(final boolean prefixedChannel) {
|
||||
final String provisioningAddress = UUID.randomUUID().toString();
|
||||
|
||||
final byte[] channel = prefixedChannel
|
||||
? ProvisioningManager.getPrefixedChannel(provisioningAddress)
|
||||
: ProvisioningManager.getUnprefixedChannel(provisioningAddress);
|
||||
|
||||
final byte[] content = TestRandomUtil.nextBytes(16);
|
||||
|
||||
@SuppressWarnings("unchecked") final Consumer<PubSubProtos.PubSubMessage> subscribedConsumer = mock(Consumer.class);
|
||||
|
||||
provisioningManager.addListener(provisioningAddress, subscribedConsumer);
|
||||
|
||||
final PubSubProtos.PubSubMessage pubSubMessage = PubSubProtos.PubSubMessage.newBuilder()
|
||||
.setType(PubSubProtos.PubSubMessage.Type.DELIVER)
|
||||
.setContent(ByteString.copyFrom(content))
|
||||
.build();
|
||||
|
||||
REDIS_EXTENSION.getRedisClient().useBinaryConnection(connection ->
|
||||
connection.sync().publish(channel, pubSubMessage.toByteArray()));
|
||||
|
||||
final ArgumentCaptor<PubSubProtos.PubSubMessage> messageCaptor =
|
||||
ArgumentCaptor.forClass(PubSubProtos.PubSubMessage.class);
|
||||
|
||||
verify(subscribedConsumer, timeout(PUBSUB_TIMEOUT_MILLIS)).accept(messageCaptor.capture());
|
||||
|
||||
assertEquals(PubSubProtos.PubSubMessage.Type.DELIVER, messageCaptor.getValue().getType());
|
||||
assertEquals(ByteString.copyFrom(content), messageCaptor.getValue().getContent());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user