Skip to content

Commit

Permalink
Add entropy source to the containers http client
Browse files Browse the repository at this point in the history
This commit fixes websocket connections to containers.

Thanks to @kenton for all the pointers.
  • Loading branch information
gabivlj committed Feb 25, 2025
1 parent b59626b commit 5ef7b57
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/workerd/api/container.c++
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ void Container::signal(jsg::Lock& js, int signo) {
class Container::TcpPortWorkerInterface final: public WorkerInterface {
public:
TcpPortWorkerInterface(capnp::ByteStreamFactory& byteStreamFactory,
kj::EntropySource& entropySource,
const kj::HttpHeaderTable& headerTable,
rpc::Container::Port::Client port)
: byteStreamFactory(byteStreamFactory),
entropySource(entropySource),
headerTable(headerTable),
port(kj::mv(port)) {}

Expand Down Expand Up @@ -120,7 +122,7 @@ class Container::TcpPortWorkerInterface final: public WorkerInterface {
connectImpl(*pipe.ends[1]).then([]() -> kj::Promise<void> { return kj::NEVER_DONE; });

// ... and then stack an HttpClient on it ...
auto client = kj::newHttpClient(headerTable, *pipe.ends[0]);
auto client = kj::newHttpClient(headerTable, *pipe.ends[0], {.entropySource = entropySource});

// ... and then adapt that to an HttpService ...
auto service = kj::newHttpService(*client);
Expand Down Expand Up @@ -167,6 +169,7 @@ class Container::TcpPortWorkerInterface final: public WorkerInterface {

private:
capnp::ByteStreamFactory& byteStreamFactory;
kj::EntropySource& entropySource;
const kj::HttpHeaderTable& headerTable;
rpc::Container::Port::Client port;

Expand Down Expand Up @@ -209,19 +212,22 @@ class Container::TcpPortWorkerInterface final: public WorkerInterface {
class Container::TcpPortOutgoingFactory final: public Fetcher::OutgoingFactory {
public:
TcpPortOutgoingFactory(capnp::ByteStreamFactory& byteStreamFactory,
kj::EntropySource& entropySource,
const kj::HttpHeaderTable& headerTable,
rpc::Container::Port::Client port)
: byteStreamFactory(byteStreamFactory),
entropySource(entropySource),
headerTable(headerTable),
port(kj::mv(port)) {}

kj::Own<WorkerInterface> newSingleUseClient(kj::Maybe<kj::String> cfStr) override {
// At present we have no use for `cfStr`.
return kj::heap<TcpPortWorkerInterface>(byteStreamFactory, headerTable, port);
return kj::heap<TcpPortWorkerInterface>(byteStreamFactory, entropySource, headerTable, port);
}

private:
capnp::ByteStreamFactory& byteStreamFactory;
kj::EntropySource& entropySource;
const kj::HttpHeaderTable& headerTable;
rpc::Container::Port::Client port;
};
Expand All @@ -234,8 +240,9 @@ jsg::Ref<Fetcher> Container::getTcpPort(jsg::Lock& js, int port) {

auto& ioctx = IoContext::current();

kj::Own<Fetcher::OutgoingFactory> factory = kj::heap<TcpPortOutgoingFactory>(
ioctx.getByteStreamFactory(), ioctx.getHeaderTable(), req.send().getPort());
kj::Own<Fetcher::OutgoingFactory> factory =
kj::heap<TcpPortOutgoingFactory>(ioctx.getByteStreamFactory(), ioctx.getEntropySource(),
ioctx.getHeaderTable(), req.send().getPort());

return jsg::alloc<Fetcher>(
ioctx.addObject(kj::mv(factory)), Fetcher::RequiresHostAndProtocol::YES, true);
Expand Down

0 comments on commit 5ef7b57

Please sign in to comment.