Skip to content

Commit 0080deb

Browse files
committed
feat: rewrite using actix
2 parents ad37ab7 + 6235efa commit 0080deb

20 files changed

+1653
-680
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ before_script:
2121
- sudo apt-get install -y postgresql-9.6-postgis-2.4
2222
- psql -U postgres -c 'create database test'
2323
- psql -U postgres -d test -c 'create extension postgis'
24-
- psql -U postgres -d test -f tests/fixtures/points.sql
24+
# - psql -U postgres -d test -f tests/fixtures/points.sql
2525

2626
script:
2727
- true

Cargo.lock

100644100755
Lines changed: 1239 additions & 287 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

100644100755
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@ name = "martin"
33
version = "0.1.0"
44
authors = ["Stepan Kuzmin <to.stepan.kuzmin@gmail.com>"]
55

6-
[[bin]]
7-
name = "martin"
8-
path = "src/main.rs"
9-
106
[dependencies]
11-
env_logger = "0.5.1"
12-
iron = "0.6.0"
13-
iron-test = "0.6.0"
14-
lazy_static = "0.2.11"
15-
log = "0.4.1"
16-
logger = "0.4.0"
17-
lru = "0.1.7"
18-
mapbox_expressions_to_sql = { git = "https://github.com/stepankuzmin/rust-mapbox-expressions-to-sql" }
19-
persistent = "0.4.0"
7+
actix = "0.5"
8+
actix-web = "0.4"
9+
env_logger = "0.5"
10+
futures = "0.1"
11+
log = "0.4"
12+
mapbox_expressions_to_sql = "0.1.0"
2013
postgres = { version = "0.15", features = ["with-time", "with-uuid", "with-serde_json"] }
21-
r2d2 = "0.7.4"
22-
r2d2_postgres = "0.13.0"
23-
regex = "0.2.2"
24-
rererouter = "0.1.1"
14+
r2d2 = "0.8"
15+
r2d2_postgres = "0.14"
2516
serde = "1.0"
2617
serde_derive = "1.0"
2718
serde_json = "1.0"
28-
tilejson = "0.1.0"
29-
urlencoded = "0.6.0"
19+
tilejson = "0.1"

Dockerfile

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ekidd/rust-musl-builder:1.23.0 as builder
1+
FROM ekidd/rust-musl-builder:1.25.0 as builder
22

33
ADD . .
44
RUN sudo chmod -R 0777 *

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

100644100755
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ PostGIS [Mapbox Vector Tiles](https://github.com/mapbox/vector-tile-spec) server
1717

1818
DATABASE_URL=postgres://postgres:password@localhost:5432/test martin
1919

20+
## Environment variables
21+
22+
DATABASE_URL
23+
DATABASE_POOL_SIZE
24+
WORKER_PROCESSES
25+
KEEP_ALIVE
26+
2027
## Using with Docker
2128

2229
docker run -d —rm —name martin \

src/cache.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/coordinator_actor.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use actix::prelude::*;
2+
3+
use super::messages;
4+
use super::worker_actor::WorkerActor;
5+
6+
pub struct CoordinatorActor {
7+
workers: Vec<Addr<Syn, WorkerActor>>,
8+
}
9+
10+
impl Default for CoordinatorActor {
11+
fn default() -> CoordinatorActor {
12+
CoordinatorActor { workers: vec![] }
13+
}
14+
}
15+
16+
impl Actor for CoordinatorActor {
17+
type Context = Context<Self>;
18+
19+
fn started(&mut self, _ctx: &mut Self::Context) {
20+
info!("Starting CoordinatorActor");
21+
}
22+
}
23+
24+
impl Handler<messages::Connect> for CoordinatorActor {
25+
type Result = Addr<Syn, WorkerActor>;
26+
27+
fn handle(&mut self, msg: messages::Connect, _: &mut Context<Self>) -> Self::Result {
28+
info!("WorkerActor connected");
29+
self.workers.push(msg.addr.clone());
30+
msg.addr
31+
}
32+
}
33+
34+
impl Handler<messages::RefreshSources> for CoordinatorActor {
35+
type Result = ();
36+
37+
fn handle(&mut self, msg: messages::RefreshSources, _: &mut Context<Self>) -> Self::Result {
38+
for worker in &self.workers {
39+
worker.do_send(messages::RefreshSources {
40+
sources: msg.sources.clone(),
41+
});
42+
}
43+
}
44+
}

src/cors.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/db.rs

100644100755
Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1+
use actix::prelude::*;
2+
use r2d2_postgres::{PostgresConnectionManager, TlsMode};
3+
use r2d2::{Pool, PooledConnection};
14
use std::error::Error;
5+
use std::io;
26

3-
use iron::typemap::Key;
4-
use iron::prelude::{Plugin, Request};
5-
use persistent::Read;
6-
use r2d2::{Config, Pool, PooledConnection};
7-
use r2d2_postgres::{PostgresConnectionManager, TlsMode};
7+
use super::messages;
8+
use super::source::{get_sources, Sources, Tile};
89

910
pub type PostgresPool = Pool<PostgresConnectionManager>;
1011
pub type PostgresConnection = PooledConnection<PostgresConnectionManager>;
1112

12-
pub struct DB;
13-
impl Key for DB {
14-
type Value = PostgresPool;
15-
}
16-
1713
pub fn setup_connection_pool(cn_str: &str, pool_size: u32) -> Result<PostgresPool, Box<Error>> {
18-
let config = Config::builder().pool_size(pool_size).build();
19-
let manager = try!(PostgresConnectionManager::new(cn_str, TlsMode::None));
20-
let pool = try!(Pool::new(config, manager));
14+
let manager = PostgresConnectionManager::new(cn_str, TlsMode::None)?;
15+
let pool = Pool::builder().max_size(pool_size).build(manager)?;
2116
Ok(pool)
2217
}
2318

24-
pub fn get_connection(req: &mut Request) -> Result<PostgresConnection, Box<Error>> {
25-
let pool = try!(req.get::<Read<DB>>());
26-
let conn = try!(pool.get());
27-
Ok(conn)
19+
#[derive(Debug)]
20+
pub struct DbExecutor(pub PostgresPool);
21+
22+
impl Actor for DbExecutor {
23+
type Context = SyncContext<Self>;
24+
}
25+
26+
impl Handler<messages::GetSources> for DbExecutor {
27+
type Result = Result<Sources, io::Error>;
28+
29+
fn handle(&mut self, _msg: messages::GetSources, _: &mut Self::Context) -> Self::Result {
30+
let conn = self.0.get().unwrap();
31+
let sources = get_sources(conn)?;
32+
Ok(sources)
33+
}
34+
}
35+
36+
impl Handler<messages::GetTile> for DbExecutor {
37+
type Result = Result<Tile, io::Error>;
38+
39+
fn handle(&mut self, msg: messages::GetTile, _: &mut Self::Context) -> Self::Result {
40+
let conn = self.0.get().unwrap();
41+
42+
let tile = msg.source
43+
.get_tile(conn, msg.z, msg.x, msg.y, msg.condition)?;
44+
45+
Ok(tile)
46+
}
2847
}

0 commit comments

Comments
 (0)