<p>Hello,</p>
<p>I'm trying to split requests after every X requests and I'm afraid my current approach is very naive. </p>
<p>Right now I simply increase a counter after every request, but I'm afraid it's not very accurate with a lot of concurrent requests, is it? </p>
<p>What would be the correct approach? </p>
<p>Regards</p>
<hr/>**评论:**<br/><br/>TheMerovius: <pre><p>What is the goal? You said that your approach isn't very accurate with lots of concurrent requests, but say you want to split every N requests, you receive N-1 sequential ones and then 2 requests concurrently - which one should be considered the "Nth"?</p>
<p>FWIW, the word "concurrently" is, in a way, CS-lingo for "incomparable in a partial order derived from time". I.e. by definition, you can't say that one concurrent request arrived "before" the other. And given, that there is only a partial order on events, things like "after N requests" doesn't make inherent sense.</p>
<p>So, the tl;dr is: Depending on what specifically you mean by "splitting", "after X requests" and what the actual underlying goal is (sampling? load balancing?) your approach is probably fine.</p>
<p>You could use, for example, <a href="https://godoc.org/sync/atomic#AddUint64" rel="nofollow">sync/atomic</a> as a counter and see whether <code>new % X == 0</code>. That would provide you with a total order of requests, so "every X requests" would be well-defined and that definition is probably as good as any other you could come up with.</p>
<p>(now, a different question would be, what happens if you have more than one process, as they won't be able to have a single atomic counter. To design that, the actual use case would be required though)</p>
<p>Hope that helps somewhat :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传