Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to the RSocket implementation #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import discord4j.common.store.legacy.LegacyStoreLayout;
import discord4j.connect.Constants;
import discord4j.connect.common.ConnectGatewayOptions;
import discord4j.connect.common.PayloadDestinationMapper;
import discord4j.connect.common.UpstreamGatewayClient;
import discord4j.connect.rsocket.gateway.RSocketJacksonSinkMapper;
import discord4j.connect.rsocket.gateway.RSocketJacksonSourceMapper;
Expand Down Expand Up @@ -86,6 +87,7 @@ public static void main(String[] args) {
.indices(0, 2) // only connect this leader to shard IDs 0 and 2
.count(4) // but still split our bot guilds into 4 shards
.build();
PayloadDestinationMapper<String> payloadDestinationMapper = PayloadDestinationMapper.fixed("inbound");

// define the GlobalRouterServer as GRL for all nodes in this architecture
// define the GlobalRouterServer as Router for all request buckets in this architecture
Expand Down Expand Up @@ -118,7 +120,7 @@ public static void main(String[] args) {
.setDispatchEventMapper(DispatchEventMapper.discardEvents())
.setExtraOptions(o -> new ConnectGatewayOptions(o,
new RSocketPayloadSink(payloadServerAddress,
new RSocketJacksonSinkMapper(jackson.getObjectMapper(), "inbound")),
new RSocketJacksonSinkMapper(jackson.getObjectMapper(), payloadDestinationMapper)),
new RSocketPayloadSource(payloadServerAddress, "outbound",
new RSocketJacksonSourceMapper(jackson.getObjectMapper()))))
.login(UpstreamGatewayClient::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import discord4j.connect.Constants;
import discord4j.connect.common.ConnectGatewayOptions;
import discord4j.connect.common.DownstreamGatewayClient;
import discord4j.connect.common.PayloadDestinationMapper;
import discord4j.connect.rsocket.gateway.RSocketJacksonSinkMapper;
import discord4j.connect.rsocket.gateway.RSocketJacksonSourceMapper;
import discord4j.connect.rsocket.gateway.RSocketPayloadSink;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static void main(String[] args) {
.build()))))
.setExtraOptions(o -> new ConnectGatewayOptions(o,
new RSocketPayloadSink(payloadServerAddress,
new RSocketJacksonSinkMapper(jackson.getObjectMapper(), "outbound")),
new RSocketJacksonSinkMapper(jackson.getObjectMapper(), PayloadDestinationMapper.fixed("outbound"))),
new RSocketPayloadSource(payloadServerAddress, "inbound",
new RSocketJacksonSourceMapper(jackson.getObjectMapper()))))
.login(DownstreamGatewayClient::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import discord4j.connect.common.ConnectPayload;
import discord4j.connect.common.PayloadDestinationMapper;
import discord4j.connect.common.SinkMapper;
import io.rsocket.Payload;
import io.rsocket.util.DefaultPayload;
Expand All @@ -33,16 +34,18 @@
public class RSocketJacksonSinkMapper implements SinkMapper<Payload> {

private final ObjectMapper mapper;
private final String topic;
private final PayloadDestinationMapper<String> destinationMapper;

public RSocketJacksonSinkMapper(ObjectMapper mapper, String topic) {
public RSocketJacksonSinkMapper(ObjectMapper mapper, PayloadDestinationMapper<String> destinationMapper) {
this.mapper = mapper;
this.topic = topic;
this.destinationMapper = destinationMapper;
}

@Override
public Publisher<Payload> apply(ConnectPayload payload) {
return Mono.fromCallable(() -> DefaultPayload.create(mapper.writeValueAsBytes(payload),
("produce:" + topic).getBytes(StandardCharsets.UTF_8)));
return Mono.from(destinationMapper.getDestination(payload))
.flatMap((destination) ->
Mono.fromCallable(() -> DefaultPayload.create(mapper.writeValueAsBytes(payload),
("produce:" + destination).getBytes(StandardCharsets.UTF_8))));
}
}