В настоящее время я создаю пул и разделяю его между несколькими потоками, что-то вроде:
fn main() {
let mut opts = mysql::OptsBuilder::from_opts(dsn);
opts.stmt_cache_size(0);
opts.read_timeout(Some(Duration::new(3, 0)));
opts.write_timeout(Some(Duration::new(3, 0)));
let pool = mysql::Pool::new_manual(1, 5, opts).expect("Could not connect to MySQL");
loop {
let wait_time = Duration::from_secs(30);
let start = Instant::now();
let mut funcs: Vec<fn(mysql::Pool)> = Vec::new();
funcs.push(func1);
funcs.push(func2);
let mut threads = Vec::new();
for f in funcs {
let pool = pool.clone();
threads.push(thread::spawn(move || {
f(pool);
}));
}
for t in threads {
let _ = t.join();
}
let runtime = start.elapsed();
if let Some(remaining) = wait_time.checked_sub(runtime) {
eprintln!("sleeping for: {}", remaining.as_secs());
thread::sleep(remaining);
}
// (how to improve this) ?
eprintln!("pool ping: {:?}", pool);
}
}
fn func1(pool: mysql::Pool){
let mut stmt = match pool.prepare("SELECT user, time, state, info FROM information_schema.processlist WHERE command != 'Sleep' AND time >= ? ORDER BY time DESC, id LIMIT 1;") {
Ok(stmt) => stmt,
Err(e) => {
eprintln!("{}", e);
return;
}
};
for row in stmt.execute((30,)).unwrap() {
let (user, time, state, info) =
mysql::from_row::<(String, i64, String, String)>(row.unwrap());
println!("{} {} {} {}", user, time, state, info);
}
}
Пока все хорошо, но, когда сервер MySQL выходит из строя, ясм .:
DriverError { Could not connect to address `mydb.host:3306': Connection refused (os error 61) }
Когда сервер MySQL снова включен / доступен, пул устанавливает соединение, и запросы работают как положено.Поэтому я хотел бы знать, повторяется ли pool
неограниченное количество раз или существует ли предел повторных попыток, кроме того, как можно определить / изменить интервал повторения?
Также интересно, как обрабатывать ошибки пула, чтобыпредупреждение может быть отправлено, например, когда сервер MySQL не работает.