Go:并发和优先级排序

huangapple go评论86阅读模式
英文:

Go: Concurrency and priority ordering

问题

我有大约1000个作业在外部服务器上运行,每个作业都与我的程序中的一个go例程相关联。我在不同的时间启动了这些作业,它们大致按照它们启动的顺序完成,但这并不保证。

我从每个go例程中轮询其对应的服务器作业:它完成了吗?我的出站请求受到速率限制,所以我需要聪明地进行轮询。

我希望按照先启动作业的go例程的优先级进行轮询。我现在的做法是,我有一个表示速率限制的通道,所有的go例程都等待从这个通道中获取一个值,轮询它们的服务器,然后再放回一个值。

但是,没有保证这些go例程甚至会随机读取(更不用说按优先级顺序了),因为多个go例程在同一个通道上读取的行为是未定义的。

有人能指导我如何思考这个问题吗?它不必具体到细节,但我不确定在Go中应该使用哪些原语和数据结构以按优先级顺序从通道中读取,同时考虑速率限制。

这似乎很困难,因为单个goroutine不知道整个程序的状态——它们的同事例程中哪些是先启动的等等。它们只需要知道在任何给定时间它们是否应该轮询它们的服务器。

谢谢。

英文:

I have n ~=1000 jobs running on outside servers, each tied to a go-routine in my program. I started the jobs at different times, and they finish roughly in the order they were started, but that's not guaranteed.

From each go-routine I poll its corresponding server job: is it done yet? My outbound requests are rate-limited, so I need to poll smartly.

I want to prioritize polling by go-routines whose jobs were started earlier. The way I'm doing it now, I have a channel that represents my rate limit, and all go-routines wait to acquire a value from this channel, poll their server, and then put a value back.

But, there's no guarantee that these go-routines would even read randomly (much less in priority order), because the behavior of multiple go-routines reading on the same channel is undefined.

Could someone guide me as to how to think about this problem? It doesn't have to be specific, but I'm not sure what primitives and data structures I would use in Go to read from channels in priority order, while taking into account rate-limiting.

It seems difficult because individual goroutines do not know the state of the whole program -- which of their colleague routines were started first, etc. They should merely be fed whether or not they should poll their server at any given time.

Thank you.

答案1

得分: 2

你听说过加权公平队列吗?这是一种很成熟的调度方式,通过预测哪个作业理论上应该先完成,然后优先为其提供服务。

英文:

You have heard of Weighted Fair Queues? That is a well developed way to schedule such that a prediction is made about which job should theoretically complete first, and that is the one that is serviced.

huangapple
  • 本文由 发表于 2015年6月2日 06:28:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/30584367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定