About answering an http request asynchronously

agolangf · · 45 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Let&#39;s say I have two microservices, A and B. For every incoming http request A must process a part and B another part before delivering an http response.</p> <p>Let&#39;s say I have a Kafka instance between A and B in which I want to queue the http requests, so B can read from here at its own pace and process. I want B to be able to perform the response.</p> <p>In Go, can I use net/context to pass the information, and keeping the request open until B answers?. If not, how will you do this?.</p> <hr/>**评论:**<br/><br/>justinisrael: <pre><p>Somewhere in this mix you have the application server that is serving the http endpoint. It may be &#34;A&#34;, which would need to delegate part of the request to &#34;B&#34;. Or &#34;B&#34; to &#34;A&#34;. Or even a &#34;C&#34; that serves http and will call &#34;A&#34; and &#34;B&#34; in order to form a response. But in a nutshell, whoever is handling the http request will be able to delegate to any number of microservices in order to ask extra questions, and then collect everything into a response. </p></pre>Dorubah: <pre><p>The thing is that I want &#34;A&#34; to call &#34;B&#34;. Can &#34;A&#34; delegate on &#34;B&#34; the response of the request without having &#34;B&#34; contacting &#34;A&#34; back?.</p> <p>I want the flow of the request to be &#34;Browser -&gt; A -&gt; B -&gt; Browser&#34; instead of &#34;Browser -&gt; A -&gt; B -&gt; A -&gt; Browser&#34;.</p> <p>Thanks for your answer :).</p></pre>TheMerovius: <pre><p>At best, you&#39;ll have to do &#34;Browser -&gt; LB -&gt; A -&gt; B -&gt; LB -&gt; Browser&#34;. The TCP connection of the browser needs to terminate somewhere, you can&#39;t just move it between different services.</p> <p>It would also be useful to know, why you want to do this in the first place; in essence, HTTP is a synchronous protocol; why do you care whether A waits around for the response?</p> <p>What you are trying to do is certainly possible, but it&#39;s incredibly complicated. Your LB needs to pass an identifier of the open request, which B can then pass back at some point; it needs to keep state while doing that and you must make sure that the response is passed back to the same instance of the LB, because that is where the state is held. In the end, you&#39;ll operationally revert to just having a synchronous request, but will have put several layers of abstractions and round-trips on top of it, without any benefit.</p> <p>So, likely, the solution to your problem is, to get rid of the &#34;I have a Kafka instance between A and B&#34;. Kafka is for asynchronous messages, you are doing synchronous things, use RPCs and a LB.</p></pre>Dorubah: <pre><p>Thank you. Actually I&#39;m using RPC calls right now. Was just wondering if there was another way of replaying to the HTTP request from another microservice. But you are right, I need to close the TCP connection to send the data, so no benefit on this approach I said.</p></pre>justinisrael: <pre><p>I&#39;ve not written a setup like this before, but I am assuming it involves giving the file descriptor from A to B and letting B handle writing the response, since I am guessing they are two independent processes connected via the Kafka message bus. But that sounds like really strong coupling between microservices. I don&#39;t even know how you would do it on different hosts. Shouldn&#39;t B just be asked to provide some data? Someone has to finish the http request and I don&#39;t know how you go about getting an async external process to do it over a message bus, unless B has an outgoing connection to the browser as well. </p></pre>Dorubah: <pre><p>Yes, this was my initial thought, send everything into the message so &#34;B&#34; can use it. There is no benefit in that setup, right now B is just asked to provide some data using RPC, but <a href="https://www.oreilly.com/ideas/the-evolution-of-scalable-microservices" rel="nofollow">there is people who consider this to keep a strong coupling between them</a> and consider this Microliths, but I guess they are what they have to be, a pattern that fits well the problem.</p></pre>matart: <pre><p>I don&#39;t believe the browser can receive a request from B out of nowhere. Does the browser have a connection open to B like a websocket? Is this all behind an initial HTTP endpoint? Do you poll B and see if there is data?</p> <p>You could make the request to A. Then poll B for a response.</p></pre>
45 次点击  
加入收藏 微博
0 回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传