Skip to content

Commit 38b0b7b

Browse files
Avoid expensive queries at startup
Previously, the initial startup would synchronize() the configuration, and query the experiment assigned to each agent. Querying the assigned experiment could take upwards of ~80 seconds per agent, leading to a long time before it was able to respond to queries.
1 parent c413840 commit 38b0b7b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/server/agents.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,19 @@ impl Agents {
8686
fn synchronize(&self, tokens: &Tokens) -> Fallible<()> {
8787
self.db.transaction(|trans| {
8888
let mut real = tokens.agents.values().collect::<HashSet<&String>>();
89-
for agent in &self.all()? {
90-
if !real.remove(&agent.name) {
91-
trans.execute("DELETE FROM agents WHERE name = ?1;", &[&agent.name])?;
89+
let current: Vec<String> = self
90+
.db
91+
.query("select name from agents;", [], |r| r.get(0))?;
92+
93+
// If the token is no longer configured, then drop this agent from
94+
// our list.
95+
for current_name in current {
96+
if !real.remove(&current_name) {
97+
trans.execute("DELETE FROM agents WHERE name = ?1;", &[&current_name])?;
9298
}
9399
}
94100

101+
// And any *new* agents need to be inserted.
95102
for missing in &real {
96103
trans.execute(
97104
"INSERT INTO agents (name) VALUES (?1);",

0 commit comments

Comments
 (0)