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:
commit
915db74dbf
7 changed files with 69 additions and 52 deletions
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue