[Question] - Do we always need to use go channels to take advantage of multiple cores?

agolangf · · 437 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Forgive the silly question, but, I am coming from a node background because quite frankly I am really sick of spinning up multiple instances and handling shared data with Redis. I just want to run one instance of my game per server and I heard GoLang can do that. I also heard Go blows node out of water doing it. And I&#39;ve seen the benchmarks and it&#39;s really intriguing.</p> <p>For example, a simple websocket server utilizing: <a href="https://godoc.org/golang.org/x/net/websocket" rel="nofollow">https://godoc.org/golang.org/x/net/websocket</a></p> <p>Let&#39;s say, a player is in a game and 5 other players are in his same game. I will need to send movement signals to all other 5 players when player 1 moves. Would that have to be done through a go channel, or not because it&#39;s not really a cpu power hungry thing to do. But, when let&#39;s say there are hundrends of players online each sending data around... It probably would be a good idea, right? I&#39;m kind of new and still learning channels and reading the Go documentation / tutorials, but that&#39;s the gist of that. Thanks for any advice ~</p> <hr/>**评论:**<br/><br/>hayzeus: <pre><p>Channels are just used for communications. Goroutines are used for concurrency. By default (with go 1.5 and up), these will be multiplexed over as many os threads as you have cores. This number can be adjusted up or down depending on the setting of GOMAXPROCS. </p></pre>Fwippy: <pre><p>You don&#39;t need to use channels to communicate between goroutines, but it&#39;s a good idea up until the point that your server is too slow <em>and</em> CPU profiling shows that channel communication is a significant bottleneck.</p> <p>Channels might not be the best solution to the &#34;everyone broadcasts to everyone&#34; setup you&#39;re envisioning, but the overhead isn&#39;t as much as you&#39;d think.</p></pre>BillOReillyYUPokeMe: <pre><blockquote> <p>Channels might not be the best solution to the &#34;everyone broadcasts to everyone&#34;</p> </blockquote> <p>When a player logs in, I usually send a message to all their friends on their friend list (that are online). I&#39;m just not sure if I need to pass this through a goroutine. I got channels and goroutines mixed up in my title I think. </p></pre>xsolarwindx: <pre><p>Think of goroutines as threads. Channels are just queues. That&#39;s all they are underneath and that&#39;s exactly how they behave: thread safe first in first out queues. </p></pre>dinkumator: <pre><p>presumably you&#39;ll have 6 goroutines for each of the players&#39; connections, so you&#39;d probably want some channels or other way of distributing the movement signal to each of the other goroutines.</p></pre>TheDukeOfAnkh: <pre><p>From what I gathered about your use case, using go routines isn&#39;t the solution, since scalability will be a problem. What would happen when you have 100, 1000, 10000 players logged in? I think you should consider some of the pub-sub solutions that are out there.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

437 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传