1 package de.juplo.kafka.chat.backend.implementation.haproxy;
3 import de.juplo.kafka.chat.backend.domain.ShardingPublisherStrategy;
4 import lombok.RequiredArgsConstructor;
5 import lombok.extern.slf4j.Slf4j;
6 import reactor.core.publisher.Mono;
8 import java.io.IOException;
9 import java.net.SocketAddress;
10 import java.nio.ByteBuffer;
11 import java.nio.channels.SocketChannel;
14 @RequiredArgsConstructor
16 public class HaproxyShardingPublisherStrategy implements ShardingPublisherStrategy
18 private final SocketAddress haproxyAddress;
19 private final String map;
20 private final String instanceId;
24 public Mono<String> publishOwnership(int shard)
28 SocketChannel socketChannel = SocketChannel.open(haproxyAddress);
29 String command = "set map " + map + " " + Integer.toString(shard) + " " + instanceId + "\n";
30 byte[] commandBytes = command.getBytes();
31 ByteBuffer buffer = ByteBuffer.wrap(commandBytes);
32 socketChannel.write(buffer);
33 socketChannel.close();
34 return Mono.just(instanceId);