Skip to content

Commit

Permalink
Merge #410
Browse files Browse the repository at this point in the history
410: fix: network panic errors r=jjyr a=jjyr

* Peer Store no such table
* get peer index panic

Co-authored-by: jjy <jjyruby@gmail.com>
  • Loading branch information
bors[bot] and jjyr committed Apr 8, 2019
2 parents 314f45e + f75da93 commit 6025ce8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
13 changes: 5 additions & 8 deletions network/src/peer_store/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ pub type ConnectionPool = Pool<SqliteConnectionManager>;
pub type PooledConnection = r2d2::PooledConnection<SqliteConnectionManager>;

lazy_static! {
static ref MEMORY_OPEN_FLAGS: OpenFlags = OpenFlags::SQLITE_OPEN_READ_WRITE
static ref OPEN_FLAGS: OpenFlags = OpenFlags::SQLITE_OPEN_READ_WRITE
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_SHARED_CACHE
| OpenFlags::SQLITE_OPEN_NO_MUTEX;
static ref FILE_OPEN_FLAGS: OpenFlags = OpenFlags::SQLITE_OPEN_READ_WRITE
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_NO_MUTEX;
}

pub enum StorePath {
Expand All @@ -54,11 +51,11 @@ pub fn open_pool(store_path: StorePath, max_size: u32) -> Result<ConnectionPool,
StorePath::Memory(db) => {
let manager =
SqliteConnectionManager::file(format!("file:{}?mode=memory&cache=shared", db));
manager.with_flags(*MEMORY_OPEN_FLAGS)
manager.with_flags(*OPEN_FLAGS)
}
StorePath::File(file_path) => {
let manager = SqliteConnectionManager::file(file_path);
manager.with_flags(*FILE_OPEN_FLAGS)
manager.with_flags(*OPEN_FLAGS)
}
};
Pool::builder()
Expand All @@ -71,11 +68,11 @@ pub fn open(store_path: StorePath) -> Result<Connection, DBError> {
match store_path {
StorePath::Memory(db) => Connection::open_with_flags(
format!("file:{}?mode=memory&cache=shared", db),
*MEMORY_OPEN_FLAGS,
*OPEN_FLAGS,
)
.map_err(Into::into),
StorePath::File(file_path) => {
Connection::open_with_flags(file_path, *FILE_OPEN_FLAGS).map_err(Into::into)
Connection::open_with_flags(file_path, *OPEN_FLAGS).map_err(Into::into)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion network/src/peer_store/sqlite/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) const LAST_CONNECTED_TIMEOUT_SECS: u64 = 14 * 24 * 3600;
/// Clear banned list if the list reach this size
const BAN_LIST_CLEAR_EXPIRES_SIZE: usize = 1024;
/// SQLITE connection pool size
const DEFAULT_POOL_SIZE: u32 = 16;
const DEFAULT_POOL_SIZE: u32 = 32;
const DEFAULT_ADDRS: u32 = 3;

pub struct SqlitePeerStore {
Expand Down
23 changes: 11 additions & 12 deletions network/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,17 @@ impl ServiceProtocol for CKBHandler {
network.modify_peer(&peer_id, |peer| {
peer.last_message_time = Some(now);
});
let peer_index = network
.get_peer_index(&peer_id)
.expect("get peer index failed");
self.handler.received(
Box::new(DefaultCKBProtocolContext::new(
self.id,
Arc::clone(network),
context.control().clone(),
)),
peer_index,
data,
)
if let Some(peer_index) = network.get_peer_index(&peer_id) {
self.handler.received(
Box::new(DefaultCKBProtocolContext::new(
self.id,
Arc::clone(network),
context.control().clone(),
)),
peer_index,
data,
)
}
} else {
warn!(target: "network", "can not get peer_id, disconnect it");
context.disconnect(session.id);
Expand Down

0 comments on commit 6025ce8

Please sign in to comment.