diff mbox series

[bug#45018,3/6] daemon: Factorize substituter agent spawning.

Message ID 20201203101930.11210-3-ludo@gnu.org
State Accepted
Headers show
Series Process and connection reuse for substitutions | expand

Checks

Context Check Description
cbaines/submitting builds success
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

Ludovic Courtès Dec. 3, 2020, 10:19 a.m. UTC
* nix/libstore/local-store.hh (class LocalStore)[substituter]: New
method.
[runningSubstituter]: Turn into a shared_ptr.
* nix/libstore/local-store.cc (LocalStore::querySubstitutablePaths):
Call 'substituter' instead of using inline code.
(LocalStore::querySubstitutablePathInfos): Likewise.
(LocalStore::substituter): New method.
---
 nix/libstore/local-store.cc | 29 +++++++++++++----------------
 nix/libstore/local-store.hh |  5 ++++-
 2 files changed, 17 insertions(+), 17 deletions(-)

Comments

Mathieu Othacehe Dec. 4, 2020, 8:19 a.m. UTC | #1
Hey Ludo,

> +	const std::map<string, string> env = { { "_NIX_OPTIONS", settings.pack() } };
> +	runningSubstituter = std::make_shared<Agent>(settings.guixProgram, args, env);

The only owner of this Agent is LocalStore, so why not using
make_unique? This function could return a const reference to the Agent
instead.

Thanks,

Mathieu
diff mbox series

Patch

diff --git a/nix/libstore/local-store.cc b/nix/libstore/local-store.cc
index c7df8da085..c304e2ddd1 100644
--- a/nix/libstore/local-store.cc
+++ b/nix/libstore/local-store.cc
@@ -850,14 +850,7 @@  PathSet LocalStore::querySubstitutablePaths(const PathSet & paths)
 
     if (!settings.useSubstitutes || paths.empty()) return res;
 
-    if (!runningSubstituter) {
-	const Strings args = { "substitute", "--query" };
-	const std::map<string, string> env = { { "_NIX_OPTIONS", settings.pack() } };
-	std::unique_ptr<Agent>fresh(new Agent(settings.guixProgram, args, env));
-	runningSubstituter.swap(fresh);
-    }
-
-    Agent & run = *runningSubstituter;
+    Agent & run = *substituter();
 
     string s = "have ";
     foreach (PathSet::const_iterator, j, paths)
@@ -877,18 +870,22 @@  PathSet LocalStore::querySubstitutablePaths(const PathSet & paths)
 }
 
 
+std::shared_ptr<Agent> LocalStore::substituter()
+{
+    if (!runningSubstituter) {
+	const Strings args = { "substitute", "--query" };
+	const std::map<string, string> env = { { "_NIX_OPTIONS", settings.pack() } };
+	runningSubstituter = std::make_shared<Agent>(settings.guixProgram, args, env);
+    }
+
+    return runningSubstituter;
+}
+
 void LocalStore::querySubstitutablePathInfos(PathSet & paths, SubstitutablePathInfos & infos)
 {
     if (!settings.useSubstitutes) return;
 
-    if (!runningSubstituter) {
-	const Strings args = { "substitute", "--query" };
-	const std::map<string, string> env = { { "_NIX_OPTIONS", settings.pack() } };
-	std::unique_ptr<Agent>fresh(new Agent(settings.guixProgram, args, env));
-	runningSubstituter.swap(fresh);
-    }
-
-    Agent & run = *runningSubstituter;
+    Agent & run = *substituter();
 
     string s = "info ";
     foreach (PathSet::const_iterator, i, paths)
diff --git a/nix/libstore/local-store.hh b/nix/libstore/local-store.hh
index 57d15bac7e..9ba37219da 100644
--- a/nix/libstore/local-store.hh
+++ b/nix/libstore/local-store.hh
@@ -42,7 +42,10 @@  class LocalStore : public StoreAPI
 {
 private:
     /* The currently running substituter or empty.  */
-    std::unique_ptr<Agent> runningSubstituter;
+    std::shared_ptr<Agent> runningSubstituter;
+
+    /* Ensure the substituter is running and return it.  */
+    std::shared_ptr<Agent> substituter();
 
     Path linksDir;