Skip to content

Commit b4da6f4

Browse files
authored
"prime" libpostal library when running in server mode (#16)
This allows us to respond to our first request immediately without loading a 2 GB perceptron model during the request.
1 parent ecc99aa commit b4da6f4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/geocoders/libpostal.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ impl LibPostal {
6464
.collect::<Vec<_>>();
6565
LibPostal { column_names }
6666
}
67+
68+
pub async fn prime() {
69+
// "Prime" libpostal by forcing it to load its language model and associated data
70+
// into memory with a dummy call.
71+
let libpostal = LibPostal::new();
72+
let _ = libpostal
73+
.geocode_addresses(&[Address {
74+
street: "1 Main St".to_owned(),
75+
city: Some("Anytown".to_owned()),
76+
state: Some("VT".to_owned()),
77+
zipcode: None,
78+
}])
79+
.await;
80+
}
6781
}
6882

6983
#[async_trait]

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ async fn main() -> Result<()> {
279279
let result = match opt.cmd {
280280
// Run in server mode.
281281
Some(Command::Server { listen_address }) => {
282+
// If we're running in server mode, then prime libpostal to load its
283+
// model and data into memory. This can take 5-10 seconds,
284+
// and we'd prefer that it happens as part of application startup,
285+
// rather than at the time of the first request.
286+
LibPostal::prime().await;
282287
run_server(&listen_address, geocoder).await
283288
}
284289
// Run in CLI pipeline mode.

0 commit comments

Comments
 (0)