Skip to content

Commit 0ce7197

Browse files
71rdmweinelt
authored andcommitted
Stream files from store instead of buffering them
When an artifact is requested from hydra the output is first copied from the nix store into memory and then sent as a response, delaying the download and taking up significant amounts of memory. As reported in #1357 Instead of calling a command and blocking while reading in the entire output, this adds read_into_socket(). the function takes a command, starting a subprocess with that command, returning a file descriptor attached to stdout. This file descriptor is then by responsebuilder of Catalyst to steam the output directly (cherry picked from commit 459aa0a)
1 parent 4f09fb5 commit 0ce7197

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/lib/Hydra/Controller/Build.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ sub serveFile {
238238
# XSS hole.
239239
$c->response->header('Content-Security-Policy' => 'sandbox allow-scripts');
240240

241-
$c->stash->{'plain'} = { data => grab(cmd => ["nix", "--experimental-features", "nix-command",
241+
$c->stash->{'plain'} = { data => readIntoSocket(cmd => ["nix", "--experimental-features", "nix-command",
242242
"store", "cat", "--store", getStoreUri(), "$path"]) };
243243

244244
# Detect MIME type.

src/lib/Hydra/Helper/Nix.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ our @EXPORT = qw(
3636
jobsetOverview
3737
jobsetOverview_
3838
pathIsInsidePrefix
39+
readIntoSocket
3940
readNixFile
4041
registerRoot
4142
restartBuilds
@@ -416,6 +417,17 @@ sub pathIsInsidePrefix {
416417
return $cur;
417418
}
418419

420+
sub readIntoSocket{
421+
my (%args) = @_;
422+
my $sock;
423+
424+
eval {
425+
my $x= join(" ", @{$args{cmd}});
426+
open($sock, "-|", $x) or die q(failed to open socket from command:\n $x);
427+
};
428+
429+
return $sock;
430+
}
419431

420432

421433

0 commit comments

Comments
 (0)