1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 16: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

@ -10,6 +10,8 @@
#ifdef _WIN32
# include <fileapi.h>
# include "windows-error.hh"
#else
# include <poll.h>
#endif
@ -158,6 +160,29 @@ bool FdSource::good()
}
bool FdSource::hasData()
{
if (BufferedSource::hasData()) return true;
while (true) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
auto n = select(fd + 1, &fds, nullptr, nullptr, &timeout);
if (n < 0) {
if (errno == EINTR) continue;
throw SysError("polling file descriptor");
}
return FD_ISSET(fd, &fds);
}
}
size_t StringSource::read(char * data, size_t len)
{
if (pos == s.size()) throw EndOfFile("end of string reached");

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);
};