Using Lua to modify entity behavior in a Go Game Server on the fly?

blov · · 748 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a tiny game server cluster in Akka Scala and am interested in porting it to Go to see what all the rage is about. One of the things I was looking forward to doing in my Akka Scala implementation was hot code reload so that I could modify entity behavior on the fly without bringing the cluster down. </p> <p>My research indicates that this is not really possible in Go, but I was thinking that a decent workaround would be to use Gopher Lua with Go and rewrite the entity behavior in Lua. However, I&#39;m worried that doing so will significantly impact performance -- even with LuaJIT.</p> <p>Am I out of my mind or does this seem like a reasonable idea?</p> <hr/>**评论:**<br/><br/>BlackReape_r: <pre><p>You could also try to do that with Otto (JavaScript Interpreter). Used it a while back to implement a little scripting &#34;Engine&#34; so I could define behaviors in seperate files and still use all the Syntax that JavaScript has to do logic stuff. Maybe not what you would want but still worth a Wach.</p></pre>titpetric: <pre><p>I did something similar with otto (or motto?) a while back, +1 as it was quite reliable. I did have some <a href="https://github.com/robertkrimen/otto/issues/209" rel="nofollow">minor issues</a>, and it had <a href="https://github.com/robertkrimen/otto/issues/207" rel="nofollow">problems with vendoring at the time</a> which was resolved a few days ago. Over-all, very happy and impressed with it!</p></pre>matiasbaruch: <pre><p>I did a small LuaJIT + cgo implementation for writing Lua middlewares and use them to modify requests inside a Go HTTP application. It works in a decent way however an alternative approach I&#39;ve used (with other languages, not Lua) is to call custom middleware via gRPC, so the middleware runs its own gRPC server and Go just calls it, it might perform better in some situations, because the cgo overhead is big. I suggest running benchmarks based on your use case, I did this in the past [0].</p> <p>[0] <a href="https://github.com/matiasinsaurralde/go-lua-benchmark" rel="nofollow">https://github.com/matiasinsaurralde/go-lua-benchmark</a></p></pre>Stradigos: <pre><p>Interesting! Was your Lua implementation found in your C code then or directly in Go? Sorry if that was supposed to be obvious, I&#39;m still learning some of this terminology. Thanks for the tip about gRPC as well. I&#39;ll definitely check that out.</p> <p>How does Go handle persistence, or doesn&#39;t it? Is there a way to capture a snapshot of state in Go like Akka captures it? I&#39;m guess no since I think the reason why Akka can capture it in the first place is because of messages. It can even replay them, which is kind of neat.</p></pre>ericzhill: <pre><p>Honestly, having used both Akka and Go, you might look at the gob package. Structures in flight are nearly identical to Akka messages, chan (struct) is like the Akka mailbox, and you can use gob to persist structs to a file during a restart.</p> <p>Akka does have a nice cluster system built in that is missing from the Go core. If you build consensus into your app (etcd), you&#39;ve covered about 80% of Akka&#39;s fundamentals.</p></pre>firik: <pre><p>In theory you could do some hot code reload using Go plugins: <a href="https://beta.golang.org/pkg/plugin/" rel="nofollow">https://beta.golang.org/pkg/plugin/</a></p> <p>The idea is that you use function pointers for the functions you want to be able to reload. Then when you want to reload them you compile a new plugin with them, load that plugin and set the pointers to the new functions from the new plugin.</p></pre>tmornini: <pre><p>Go programs commonly compile and start in a fraction of a second.</p> <p>Rather than doing something kinky, I&#39;d recommend running more than one copy behind a load balancer (like nginx) and restarting them one at a time.</p></pre>ericzhill: <pre><p>Regarding nginx, you can use the restful interface on nginx to add instances to the balancer automatically on startup. If you gracefully shutdown your old Go instance, it can self unregister from the group as well. It&#39;s very slick to see working and requires very little code to accomplish.</p></pre>tmornini: <pre><p>Nice!</p></pre>titpetric: <pre><p>I&#39;d appreciate the source for this. I use nginx in production for something like 5y+ and I never came across this feature. From what I&#39;m searching for now, it seems that it&#39;s only part of their commercial offering? <a href="https://nginx.org/en/docs/http/ngx_http_upstream_conf_module.html" rel="nofollow">ngx_http_upstream_conf_module</a>. I&#39;ve seen bunches of hacks in LUA that add custom load balancer logic however. What are you refering to?</p></pre>ericzhill: <pre><p>Yes, sorry, it is only available in their commercial version. But it cost us less than having to develop similar functionality in house, so it was worth it.</p></pre>

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

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