This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| import logging | |
| import os | |
| from dataclasses import dataclass | |
| from typing import Iterable | |
| from aiofile import AIOFile | |
| logging.basicConfig(level=logging.INFO) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| import signal | |
| from random import randint | |
| class Timeout(Exception): | |
| pass | |
| def handle_timeout(signum, frame): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use tokio::sync::mpsc; | |
| #[tokio::main] | |
| pub async fn main() { | |
| let (tx, mut rx) = mpsc::channel(32); | |
| let tx2 = tx.clone(); | |
| tokio::spawn(async move { | |
| tx.clone().send("sending from first handle").await; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::net::{TcpStream, IpAddr, SocketAddr}; | |
| use std::time::Duration; | |
| use threadpool::ThreadPool; | |
| use std::sync::mpsc::{channel, Sender}; | |
| use ipnet::Ipv4Net; | |
| use std::thread::sleep; | |
| static NETWORK: &str = "172.111.0.0/10"; | |
| static PORT: u16 = 80; | |
| static POOLSIZE: usize = 7000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use regex::Regex; // 1.1.8 | |
| fn split_keep<'a>(r: &Regex, text: &'a str) -> Vec<&'a str> { | |
| let mut result = Vec::new(); | |
| let mut last = 0; | |
| for (index, matched) in text.match_indices(r) { | |
| if last != index { | |
| result.push(&text[last..index]); | |
| } | |
| result.push(matched); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use tokio::process; | |
| use std::process::Stdio; | |
| use std::time::Instant; | |
| async fn sleep() -> Result<String, Box<dyn std::error::Error>> { | |
| let ps = process::Command::new("sleep") | |
| .stdout(Stdio::piped()) | |
| .arg("3") | |
| .output().await?; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use regex::{Regex, Captures}; | |
| use strfmt::strfmt; | |
| use std::collections::HashMap; | |
| fn main() { | |
| #[derive(Debug)] | |
| struct LogSchema{ | |
| datetime: String, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use regex::Regex; | |
| fn main() { | |
| let datetime = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]"; | |
| let host = r"([^\s]+)"; | |
| let service = r"([^\s]+)"; | |
| let message = r"(.*)"; | |
| let re = Regex::new(&format!("(?P<datetime>({d})) (?P<hostname>({h}))] (?P<service>({s})) (?P<message>({m}))", | |
| d = datetime, h = host, s = service, m = message)).unwrap(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::thread; | |
| use std::sync::mpsc; | |
| fn main() { | |
| let (tx, rx) = mpsc::channel(); | |
| let h1 = thread::spawn(move || { | |
| let val = String::from("hi"); | |
| tx.send(val).unwrap(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use ureq; | |
| use std::collections::HashMap; | |
| use std::time::{Instant, Duration}; | |
| pub struct Cacher { | |
| call: Box<dyn Fn(&str) -> String>, | |
| data: HashMap<String, (String, Instant)>, | |
| cache_time: Duration, | |
| } |