All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.sms.common.loadbalancer.WeightRoundRobinLoadBalancer Maven / Gradle / Ivy

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.ArrayList;
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;

/**
 * weight round robin Load Balancer
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:53:18
 */
public class WeightRoundRobinLoadBalancer extends AbstractLoadBalancer {

    private final Map weightMap = new ConcurrentHashMap<>();

    private final Lock lock = new ReentrantLock();

    private int position = 0;

    /** instantiation Load Balancer with CopyOnWriteArrayList */
    public WeightRoundRobinLoadBalancer() {
        super();
    }

    /**
     * instantiation Load Balancer with appoint list
     *
     * @param targetList target object list
     */
    public WeightRoundRobinLoadBalancer(List> targetList) {
        super(targetList);
    }

    @Override
    protected void afterAdd(TargetWrapper wrapper) {
        weightMap.put(wrapper.getTarget(), MIN_WEIGHT);
    }

    @Override
    protected void afterRemove(TargetWrapper wrapper) {
        weightMap.remove(wrapper.getTarget());
    }

    @Override
    public void setWeight(T target, int weight) {
        if (target == null) {
            return;
        }

        weightMap.put(target, Math.max(weight, MIN_WEIGHT));
    }

    @Override
    protected T choose0(List> activeTargetList, C chooseReferenceObject) {
        List> targetList = new ArrayList<>();

        for (TargetWrapper wrapper : activeTargetList) {
            int weight = weightMap.getOrDefault(wrapper.getTarget(), MIN_WEIGHT);
            for (int i = 0; i < weight; i++) {
                targetList.add(wrapper);
            }
        }

        int size = targetList.size();

        lock.lock();
        try {
            if (position >= size) {
                position = 0;
            }
            TargetWrapper wrapper = targetList.get(position);
            position++;

            return wrapper == null ? null : wrapper.getTarget();
        } finally {
            lock.unlock();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy