Skip to content

Instantly share code, notes, and snippets.

@nnao45
Last active May 15, 2019 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnao45/d3f3e9dfbb8beaf60af66662cef71f6a to your computer and use it in GitHub Desktop.
Save nnao45/d3f3e9dfbb8beaf60af66662cef71f6a to your computer and use it in GitHub Desktop.
fn main() {
let base_timeout = 30;
let mut counter = create_counter(base_timeout);
// twitter-stream-rsライブラリはsteram apiから4xxコードとかを受け取ると関数が返るのでloopで再生成してあげる必要がある。
loop {
// この.watch()が実質先ほどのサンプルコードのrt::run(bot)に当たる。
// twiqueryでは、連続失敗した時だけretryのインターバルを2倍にしていく。
match twitter_client::TwitterClient::new(&config).watch() {
twitter_client::RESET_FLAG => counter = create_counter(base_timeout),
twitter_client::UNRESET_FLAG => (),
};
// .watch()が返ってくる => steram apiから値取得に失敗してるのでここでカウンタを上げつつsleepする
std::thread::sleep(Duration::from_secs(counter()));
}
}
fn create_counter(mut base_timeout :u64) -> Box<FnMut() -> u64> {
let clj = move || {
// カウンタは 60s => 120s => 180s =>...と増えていく。
base_timeout *= 2;
// なんとなくカウンタが3600より多くなったらプロセスごと落とす。
if base_timeout == 60 * 60 {
std::process::exit(1)
}
error!("stream api error return, sleep {}", &base_timeout);
base_timeout
};
Box::new(clj)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment