cleanup
This commit is contained in:
parent
f0b7bf7318
commit
02e47b758f
@ -1,59 +0,0 @@
|
||||
use chrono::prelude::*;
|
||||
use std::{
|
||||
thread::sleep};
|
||||
|
||||
fn get_timestamp() -> i64 {
|
||||
let local: DateTime<Local> = Local::now();
|
||||
|
||||
local.timestamp_millis() as i64
|
||||
}
|
||||
|
||||
struct Timer {
|
||||
init: bool,
|
||||
timestamp: i64,
|
||||
limit: i64,
|
||||
messages: Vec<String>,
|
||||
}
|
||||
|
||||
impl Timer {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
init: true,
|
||||
timestamp: get_timestamp(),
|
||||
limit: 10 * 1000,
|
||||
messages: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.messages.clear();
|
||||
self.timestamp = get_timestamp();
|
||||
}
|
||||
|
||||
fn send(&mut self, msg: String) {
|
||||
let now = get_timestamp();
|
||||
self.messages.push(msg);
|
||||
|
||||
if self.init {
|
||||
self.reset();
|
||||
self.init = false;
|
||||
}
|
||||
|
||||
if now >= self.timestamp + self.limit {
|
||||
println!("Send messages: {:?}", self.messages);
|
||||
|
||||
self.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut timer = Timer::new();
|
||||
|
||||
for i in 0..40 {
|
||||
println!("{:?}", i);
|
||||
timer.send(format!("{:?}", i));
|
||||
|
||||
sleep(std::time::Duration::from_secs(1));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user