我正在写一个基于facebook proxygen的http视频流服务器。没有计划的寻找。使用proxygen::ResponseBuilder我能够将webm编码的视频的块作为http响应发送,即块传输编码正在工作。我的问题是,proxygen在发送响应头之前会等待proxygen::ResponseBuilder::sendWithEOM()。我希望它在每次调用proxygen::ResponseBuilder::send()后尽快发送数据。
我试图使用evb->runInLoop()evb->runInEventBaseThread()从eventbasethread执行的lambda运行responsebuilder调用。

using namespace folly;
using namespace proxygen;

std::thread t([&](){
    EventBase* evb = EventBaseManager::get()->getExistingEventBase();
    // send headers ...
    while ( chunks avail. ) {
        //...
        evb->runInLoop([&](){
            ResponseBuilder(downstream_)
                     .body(std::move(chunk))
                     .send();
        });
        //...
    }
    // sendWithEOM ...
});
t.detach();

这段代码是从myonRequest()RequestHandler方法调用的。我试图调用ResponseBuilder::send()而不将其包装成evb->runInLoop(),但是proxygen v0.25.0和folly v0.42.0禁止使用断言从另一个线程调用ResponseBuilder::send()。我从这里删除了这个断言:https://github.com/facebook/folly/blob/v0.42.0/folly/io/async/EventBase.cpp#L491
现在,模拟流正在工作,但如果有并行请求,它将崩溃。我想它不是用来这样的,这就是断言的目的。但是也许有人知道如何在我的用例中正确地使用proxygen基础设施?

最佳答案

也有同样的问题。我让它和这样的东西一起工作。

folly::EventBase* eventBase = folly::EventBaseManager::get()->getExistingEventBase();
thread t([&, eventBase]() {
    while( chunks exist ) {
        auto chunk = getChunk();
        eventBase->runInEventBaseThread([&, chunk=chunk]() mutable {
            ResponseBuilder(downstream_).body(move(chunk)).send();
        });
    }
});
// sendWithEOM ...
t.detach();

10-08 09:41