
io.datakernel.rpc.client.sender.RpcStrategyRoundRobin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-rpc Show documentation
Show all versions of datakernel-rpc Show documentation
High-performance and fault-tolerant remote procedure call module for building distributed applications.
Provides a high-performance asynchronous binary RPC streaming protocol.
The newest version!
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datakernel.rpc.client.sender;
import io.datakernel.async.callback.Callback;
import io.datakernel.rpc.client.RpcClientConnectionPool;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Set;
import static io.datakernel.common.Preconditions.checkArgument;
public final class RpcStrategyRoundRobin implements RpcStrategy {
private final RpcStrategyList list;
private final int minActiveSubStrategies;
private RpcStrategyRoundRobin(RpcStrategyList list, int minActiveSubStrategies) {
this.list = list;
this.minActiveSubStrategies = minActiveSubStrategies;
}
public static RpcStrategyRoundRobin create(RpcStrategyList list) {
return new RpcStrategyRoundRobin(list, 0);
}
public RpcStrategyRoundRobin withMinActiveSubStrategies(int minActiveSubStrategies) {
return new RpcStrategyRoundRobin(list, minActiveSubStrategies);
}
@Override
public Set getAddresses() {
return list.getAddresses();
}
@Nullable
@Override
public RpcSender createSender(RpcClientConnectionPool pool) {
List subSenders = list.listOfSenders(pool);
if (subSenders.size() < minActiveSubStrategies)
return null;
if (subSenders.size() == 0)
return null;
if (subSenders.size() == 1)
return subSenders.get(0);
return new Sender(subSenders);
}
static final class Sender implements RpcSender {
private int nextSender;
private final RpcSender[] subSenders;
Sender(@NotNull List senders) {
checkArgument(senders.size() > 0, "List of senders must contain at least one sender");
this.subSenders = senders.toArray(new RpcSender[0]);
this.nextSender = 0;
}
@Override
public void sendRequest(I request, int timeout, @NotNull Callback cb) {
RpcSender sender = subSenders[nextSender];
nextSender = (nextSender + 1) % subSenders.length;
sender.sendRequest(request, timeout, cb);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy