Possible / Recommended to write event driven app in Go?

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

Possible / Recommended to write event driven app in Go?

问题

我正在设计一个应用程序来管理RabbitMQ的工作进程,根据一定的规则。例如:

  • 保持最少数量的工作进程
  • 如果队列中的任务数量超过M个,则最多生成N个工作进程
  • 终止超过X分钟的工作进程

我最初考虑使用Go语言来编写,因为它是编译型语言,我可以将应用程序编译为目标操作系统并将其作为守护进程运行。然而,我的概念设计涉及一个循环,每隔Y秒收集数据并通过决策引擎传递数据。然后,引擎会触发事件,其他goroutine会监听这些事件,以生成或终止工作进程。

我找到了Emission库,它可以满足这个需求,但我在某个地方看到了一条评论,说它可能不是线程安全的。老实说,我对Go语言和线程编程的了解还不足以正确评估这个库是否能够满足我的需求,或者在Go语言中是否可能实现这个功能。

我可以很快地用Node.js来编写这个应用程序,甚至可以使用nexe将其编译。然而,我想学习一门新的语言,我喜欢Go语言的目标编译特性,以及它可以在goroutine之外进行多线程处理。

这种做法是否可行?或者我是在试图让Go语言做一些它本不适合做的事情?是否有更好的方法来实现相同的目标,或者是否应该选择另一种语言?

英文:

I am designing an app to manage RabbitMQ workers given certain rules. For example:

  • Maintain a minimum number of workers
  • Spawn up to N max number of workers if queue grows beyond M tasks
  • Kill workers older than X minutes

I originally thought of writing it in Go because it is compiled & I could simply compile the app to the target OS & daemonize it. However, my concept design involves having a loop that gathers data every Y seconds & passes it through a decision engine. The engine would then raise events that would be listened to by other goroutines to either spawn or kill workers.

I've found the Emission library which would acommodate this, but I read a comment somewhere that it might not be thread safe. Honestly, my knowledge of Go & threaded programming are not sufficient to properly evaluate if this library would accomplish what I need or if this is even possible in Go.

I could write this very quickly in NodeJS and even get it compiled using nexe. However, I wanted to learn a new language, I liked the targeted compilation in Go, and that it can be multi-threaded beyond the goroutines themselves.

Is this possible or am I trying to shoehorn something into Go that it wasn't designed to do? Would it be better to accomplish the same goals differently or to just use a different language all together?

答案1

得分: 1

我以前没有见过Emission库,它可以很好地将不同的消息发送给你的工作人员。这也可以通过使用通道来实现,这是一种更灵活的实现方式,但如果你不了解该语言,可能会更加繁琐。

我肯定会阅读关于通道的内容,但要非常小心,因为通过通道进行广播并不简单。可以参考这个例子(https://rogpeppe.wordpress.com/2009/12/01/concurrent-idioms-1-broadcasting-values-in-go-with-linked-channels/)。

总的来说,我会看一下Tunny(https://github.com/Jeffail/tunny),它可以管理工作人员,它已经为你完成了大部分的实现。

英文:

I had not seen Emission library before which could work very neatly to send different messages to your workers. This could also be achieved by using channels which would be a more flexible implementation but also more cumbersome if you do not know the language.

I would certainly read on channels but be very careful as broadcasting through channels is not straightforward. Take a look at this example (https://rogpeppe.wordpress.com/2009/12/01/concurrent-idioms-1-broadcasting-values-in-go-with-linked-channels/)

Overall I would take a look at Tunny (https://github.com/Jeffail/tunny) to manage workers it already has most of the implementation done for you.

答案2

得分: 0

Go语言非常适合处理I/O密集型应用程序,特别是涉及消息传递的应用程序。然而,你的使用情况似乎更关注管理RabbitMQ而不是消息传递。对于这个目的,Go语言在这方面并没有太多突出的特点。

你可能需要退后一步,考虑整个系统。有多少部分需要使用RabbitMQ?如果这是一个开放性问题,你可能会发现轻量级的goroutines和channels是一个有用的工具。也许,你可以直接在Go程序中实现使用MQ的系统的部分功能。但是,如果你尝试这样做,请记住,基本的CSP模型是同步/阻塞的消息传递,与MQ模型完全不同。缓冲、排队、异步操作等都是在这种原始行为的基础上构建的。

如果你真的只是想管理RabbitMQ,也许可以看看rabbitmq-http项目,它可以帮助你做出决定。

英文:

Go is a very good language for i/o intensive apps, particularly if they involve messaging. However, your use case appears to concern managing RabbitMQ rather than messaging. For this purpose, Go has rather less that makes it stand out in its favour.

You might want to stand back and consider the system as a whole. How much of it needs to use RabbitMQ? If that is an open question, you might find that the lightweight goroutines and channels are a useful tool. Just maybe, you can implement parts of your system that use MQ directly in a Go program. But if you try this, remember that the fundamental CSP model is of synchronous / blocking message passing, quite different from the MQ model. Buffering, queueing, asynchronous operations etc are layered on top of this primitive behaviour.

If your intention really is just managing RabbitMQ, maybe take a look at the rabbitmq-http project to help you make up your mind.

huangapple
  • 本文由 发表于 2015年7月5日 23:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/31232209.html
匿名

发表评论

匿名网友

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

确定