将第三方异步API与Cap'n Proto RPC集成的好方法是什么? [英] what is a good way to integrate 3rd party asynchronous APIs with Cap'n Proto RPC?

查看:135
本文介绍了将第三方异步API与Cap'n Proto RPC集成的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理Cap'n Proto RPC请求的Linux服务器.其中一些请求需要将请求中的数据转发到另一台运行的服务器(在本例中为Kafka代理).librdkafka库和Cap'n Proto KJ库都可以使用poll(),因此我认为操作系统将确保它们都可以异步运行,但是我不确定是否需要进一步集成或有益.有人有经验吗?

I have a Linux server which processes Cap'n Proto RPC requests. Some of those requests need to forward data in the request to another server running, in this case, a Kafka broker. The librdkafka and Cap'n Proto KJ libraries can both use poll() so i think the OS will ensure they can both run asynchronously but I'm not sure if further integration is required or beneficial. Does anyone have experience with this?

这个问题比我列出的细节更广泛..将来我可能会从Cap'n Proto RPC调用其他API,因此可以理解任何广泛的指导原则.

The question is a bit broader than the specifics i've listed.. there are other APIs which i may call in future from Cap'n Proto RPC so any broad guidelines would be appreciated.

推荐答案

不幸的是,这并不是那么简单.是的,它们都使用poll(),但是问题是,只有一个库一次会调用poll(),只有该库实际上会接收任何事件-另一个卡住了.这是事件循环库的经典挑战-默认情况下,它们不能一起使用.

Unfortunately, this is not so simple. Yes, they both use poll(), but the problem is, only one library will be calling poll() at a time, and only that library will actually receive any events -- the other is stuck. This is a classic challenge with event loop libraries -- by default they cannot be used together.

一种选择是尝试在单独的线程中使用这些库.但是,通常,事件驱动库的设计假设是您在单个线程中完成所有操作,否则为什么您需要事件循环?

One option is to try to use the libraries in separate threads. But, often event-driven libraries design around the assumption that you're doing everything in a single thread because otherwise why would you need an event loop?

但是正确的事情"是集成事件循环.KJ的事件循环能够与其他事件库集成.例如,我将其与libuv集成为node-capnp.请参阅此文件的第一部分:

But "The Right Thing" is to integrate the event loops. KJ's event loop is capable of being integrated with other event libraries. For example, I integrated it with libuv for node-capnp; see the first part of this file:

https://github.com/kentonv/node-capnp/blob/master/src/node-capnp/capnp.cc

(有时,我计划将与libuv相关的代码分离到Cap'n Proto附带的单独的库中.)

(At some point I plan to separate out the libuv-related code here into a separate library shipped with Cap'n Proto.)

再举一个例子,这是内森·霍特(Nathan Hourt)的拉取请求,以添加与Qt的事件循环的集成-但请注意,该请求不包括I/O集成,我想是因为内森(Nathan)正在使用 AsyncIoStream的实现在可用时将其手动推送到其中:

For another example, here's a pull request by Nathan Hourt to add integration with Qt's event loop -- but note that this one doesn't include I/O integration, I think because Nathan is using an implementation of AsyncIoStream into which he manually pushes data when it's available:

https://github.com/sandstorm-io/capnproto/pull/253

无论如何,您都需要对Kafka的使用进行类似的操作.希望您随后将您的代码贡献回Cap'n Proto!:)

In any case, you'll need to do something similar with whatever Kafka uses. Hopefully you'll then contribute your code back to Cap'n Proto! :)

这篇关于将第三方异步API与Cap'n Proto RPC集成的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆