1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 17:31:47 +02:00

Merge pull request #11343 from DeterminateSystems/no-framedsink-threads

withFramedSink(): Don't use a thread to monitor the other side
This commit is contained in:
Eelco Dolstra 2024-08-22 14:23:19 +02:00 committed by GitHub
commit 915db74dbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 69 additions and 52 deletions

View file

@ -104,6 +104,9 @@ struct BufferedSource : Source
size_t read(char * data, size_t len) override;
/**
* Return true if the buffer is not empty.
*/
bool hasData();
protected:
@ -162,6 +165,13 @@ struct FdSource : BufferedSource
FdSource & operator=(FdSource && s) = default;
bool good() override;
/**
* Return true if the buffer is not empty after a non-blocking
* read.
*/
bool hasData();
protected:
size_t readUnbuffered(char * data, size_t len) override;
private:
@ -522,15 +532,16 @@ struct FramedSource : Source
/**
* Write as chunks in the format expected by FramedSource.
*
* The exception_ptr reference can be used to terminate the stream when you
* detect that an error has occurred on the remote end.
* The `checkError` function can be used to terminate the stream when you
* detect that an error has occurred. It does so by throwing an exception.
*/
struct FramedSink : nix::BufferedSink
{
BufferedSink & to;
std::exception_ptr & ex;
std::function<void()> checkError;
FramedSink(BufferedSink & to, std::exception_ptr & ex) : to(to), ex(ex)
FramedSink(BufferedSink & to, std::function<void()> && checkError)
: to(to), checkError(checkError)
{ }
~FramedSink()
@ -545,13 +556,9 @@ struct FramedSink : nix::BufferedSink
void writeUnbuffered(std::string_view data) override
{
/* Don't send more data if the remote has
encountered an error. */
if (ex) {
auto ex2 = ex;
ex = nullptr;
std::rethrow_exception(ex2);
}
/* Don't send more data if an error has occured. */
checkError();
to << data.size();
to(data);
};