com.taotao.boot.sms.common.loadbalancer.SmoothWeightRoundRobinLoadBalancer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-boot-starter-sms-common Show documentation
Show all versions of taotao-boot-starter-sms-common Show documentation
taotao-boot-starter-sms-common
The newest version!
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* 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
*
* https://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 com.taotao.boot.sms.common.loadbalancer;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* smooth weight round robin Load Balancer
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:53:07
*/
public class SmoothWeightRoundRobinLoadBalancer extends AbstractLoadBalancer {
private final Map originWeightMap = new ConcurrentHashMap<>();
private final Map currentWeightMap = new ConcurrentHashMap<>();
private final Lock lock = new ReentrantLock();
/** instantiation Load Balancer with CopyOnWriteArrayList */
public SmoothWeightRoundRobinLoadBalancer() {
super();
}
/**
* instantiation Load Balancer with appoint list
*
* @param targetList target object list
*/
public SmoothWeightRoundRobinLoadBalancer(List> targetList) {
super(targetList);
}
@Override
protected void afterAdd(TargetWrapper wrapper) {
originWeightMap.put(wrapper.getTarget(), MIN_WEIGHT);
currentWeightMap.put(wrapper.getTarget(), MIN_WEIGHT);
}
@Override
protected void afterRemove(TargetWrapper wrapper) {
originWeightMap.remove(wrapper.getTarget());
currentWeightMap.remove(wrapper.getTarget());
}
@Override
public void setWeight(T target, int weight) {
if (target == null) {
return;
}
originWeightMap.put(target, Math.max(weight, MIN_WEIGHT));
}
@Override
protected T choose0(List> activeTargetList, C chooseReferenceObject) {
lock.lock();
try {
int originWeightSum = 0;
int currentMaxWeight = Integer.MIN_VALUE;
TargetWrapper currentMaxWeightWrapper = null;
for (TargetWrapper wrapper : activeTargetList) {
int originWeight = originWeightMap.getOrDefault(wrapper.getTarget(), MIN_WEIGHT);
int currentWeight = currentWeightMap.getOrDefault(wrapper.getTarget(), originWeight);
originWeightSum += originWeight;
if (currentWeight > currentMaxWeight) {
currentMaxWeight = currentWeight;
currentMaxWeightWrapper = wrapper;
}
}
if (currentMaxWeightWrapper == null) {
return null;
}
currentWeightMap.put(currentMaxWeightWrapper.getTarget(), currentMaxWeight - originWeightSum);
for (TargetWrapper wrapper : activeTargetList) {
int originWeight = originWeightMap.getOrDefault(wrapper.getTarget(), MIN_WEIGHT);
int currentWeight = currentWeightMap.getOrDefault(wrapper.getTarget(), MIN_WEIGHT);
currentWeightMap.put(wrapper.getTarget(), currentWeight + originWeight);
}
return currentMaxWeightWrapper.getTarget();
} finally {
lock.unlock();
}
}
}