Skip to content

Commit 5942db8

Browse files
committed
Add connection timeout
1 parent 4013d73 commit 5942db8

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bag_address_lookup"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
edition = "2024"
55
default-run = "bag-service"
66
license = "MIT OR Apache-2.0"
@@ -23,5 +23,5 @@ create = ["zip", "quick-xml"]
2323
flate2 = { version = "1.1.8", optional = true }
2424
quick-xml = { version = "0.39.0", optional = true }
2525
serde_json = "1.0.149"
26-
tokio = { version = "1.49.0", features = ["rt-multi-thread", "macros", "net", "io-util", "signal"] }
26+
tokio = { version = "1.49.0", features = ["rt-multi-thread", "macros", "net", "io-util", "signal", "time"] }
2727
zip = { version = "8.5.0", optional = true }

src/create.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use std::{
44
time::Instant,
55
};
66

7-
use crate::{Database, log_with_elapsed, parsing::ParsedData, parsing::municipalities};
7+
use crate::{
8+
Database, log_with_elapsed,
9+
parsing::{ParsedData, municipalities},
10+
};
811

912
static DOWNLOAD_URL: &str =
1013
"https://service.pdok.nl/kadaster/adressen/atom/v1_0/downloads/lvbag-extract-nl.zip";

src/service/mod.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
use serde_json::json;
2-
use std::{error::Error, future::Future, sync::Arc, time::Instant};
2+
use std::{
3+
error::Error,
4+
future::Future,
5+
sync::Arc,
6+
time::{Duration, Instant},
7+
};
38

49
use tokio::{
510
io::{AsyncReadExt, AsyncWriteExt},
611
net::TcpListener,
712
};
813

14+
/// Maximum time allowed for handling a single connection (read + process + write).
15+
const CONNECTION_TIMEOUT: Duration = Duration::from_secs(5);
16+
917
use crate::database::DatabaseHandle;
1018

1119
mod localities_list;
@@ -68,14 +76,31 @@ where
6876
let db = database.clone();
6977
tokio::spawn(async move {
7078
let mut stream = stream;
71-
if let Err(err) = handle_connection(&mut stream, db).await {
72-
let _ = write_response(
73-
&mut stream,
74-
500,
75-
&json_error(&err.to_string()),
76-
None,
77-
)
78-
.await;
79+
match tokio::time::timeout(
80+
CONNECTION_TIMEOUT,
81+
handle_connection(&mut stream, db),
82+
)
83+
.await
84+
{
85+
Ok(Err(err)) => {
86+
let _ = write_response(
87+
&mut stream,
88+
500,
89+
&json_error(&err.to_string()),
90+
None,
91+
)
92+
.await;
93+
}
94+
Err(_elapsed) => {
95+
let _ = write_response(
96+
&mut stream,
97+
408,
98+
&json_error("request timeout"),
99+
None,
100+
)
101+
.await;
102+
}
103+
Ok(Ok(())) => {}
79104
}
80105
});
81106
}
@@ -186,6 +211,7 @@ async fn write_response(
186211
400 => "Bad Request",
187212
404 => "Not Found",
188213
405 => "Method Not Allowed",
214+
408 => "Request Timeout",
189215
_ => "Internal Server Error",
190216
};
191217

src/transform.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::{collections::HashMap, error::Error};
22

3-
use crate::parsing::{MunicipalityRelation, municipalities::Municipality};
4-
use crate::{Address, Locality, NumberRange, PublicSpace, encode_pc};
3+
use crate::{
4+
Address, Locality, NumberRange, PublicSpace, encode_pc,
5+
parsing::{MunicipalityRelation, municipalities::Municipality},
6+
};
57

68
pub struct LocalityMap {
79
pub locality_names: Vec<String>,

0 commit comments

Comments
 (0)