使用循环算法调度负载?

发布于 2024-08-29 21:43:29 字数 102 浏览 5 评论 0原文

我需要编写一个循环算法来安排负载到 n 个端点?

因此,如果我有服务器 A、B 和 C,

我想确保对收到的每个请求进行循环处理。我如何在 C# 中执行此操作?

I need to write a round robin algorithm to schedule load to n endpoints?

So if I have servers A, B and C

I wanted to make sure to round-robin through them for each request I get. How do I do this in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟柳画桥 2024-09-05 21:44:12

如果通过列表或数组访问端点,则只需以循环方式增加索引:

public class RoundRobinIndex
{
    volatile int index = 0;
    int count;

    public int Next
    {
        get
        {
            if (index == count)
            {
                index = 0;
            } 
            return index++;
        }
    }

    public RoundRobinIndex(int countArg)
    {
        count = countArg;
    }
}

If your endpoints are accessed through a List or Array, you only need to increment an index in a circular fashion:

public class RoundRobinIndex
{
    volatile int index = 0;
    int count;

    public int Next
    {
        get
        {
            if (index == count)
            {
                index = 0;
            } 
            return index++;
        }
    }

    public RoundRobinIndex(int countArg)
    {
        count = countArg;
    }
}
看海 2024-09-05 21:44:00

与 ebpower 的想法相同,但关注下一项是什么,而不是下一项的索引是什么。

public class RoundRobinList<T>
{
    private readonly IList<T> _list;
    private readonly int _size;
    private int _position;

    public RoundRobinList(IList<T> list)
    {
        if (!list.Any())
            throw new NullReferenceException("list");

        _list = new List<T>(list);
        _size = _list.Count;            
    }

    public T Next()
    {
        if (_size == 1)
            return _list[0];

        Interlocked.Increment(ref _position);
        var mod = _position % _size;
        return _list[mod];
    }
}

Same idea as ebpower, but focussing on what is the next item rather than what is the index of the next item.

public class RoundRobinList<T>
{
    private readonly IList<T> _list;
    private readonly int _size;
    private int _position;

    public RoundRobinList(IList<T> list)
    {
        if (!list.Any())
            throw new NullReferenceException("list");

        _list = new List<T>(list);
        _size = _list.Count;            
    }

    public T Next()
    {
        if (_size == 1)
            return _list[0];

        Interlocked.Increment(ref _position);
        var mod = _position % _size;
        return _list[mod];
    }
}
旧情勿念 2024-09-05 21:43:51

仅供记录,循环的定义:

http://en.wikipedia.org/wiki/Round -robin_scheduling

只需使用队列。从顶部取下一个,使用它然后放回去。这确保了最近使用的将始终是最后一个被拾取的。

Queue<Server> q = new Queue<Server>();

//get the next one up
Server s = q.DeQueue();


//Use s;


//put s back for later use.
q.Enqueue(s);

队列类的链接:

http://msdn.microsoft.com/en-us /library/7977ey2c.aspx

Just for the record, definition of round robin:

http://en.wikipedia.org/wiki/Round-robin_scheduling

Just use a queue. Take one off of the top, use it and put it back. This ensures that the most recent one used will always be the last one to be picked up.

Queue<Server> q = new Queue<Server>();

//get the next one up
Server s = q.DeQueue();


//Use s;


//put s back for later use.
q.Enqueue(s);

Link to the queue class:

http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文