Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: network panic errors #410

Merged
merged 2 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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