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

Share a client instead of creating one per request #58

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions examples/error_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

// Register a location command which will send a location to requests like /location 2.321 12.32
enum LocationErr {
Expand All @@ -32,7 +32,7 @@ fn main() {

if let Some(pos) = msg.text.take() {
let mut elms = pos.split_whitespace().take(2).filter_map(|x| x.parse::<f32>().ok());

if let (Some(a), Some(l)) = (elms.next(), elms.next()) {
return Ok((bot, msg, a, l));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/inline_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

let stream = bot.get_stream()
.filter_map(|(bot, msg)| msg.inline_query.map(|query| (bot, query)))
Expand Down
2 changes: 1 addition & 1 deletion examples/print_everything.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

let stream = bot.get_stream().and_then(|(_, msg)| {
println!("Received: {:#?}", msg);
Expand Down
2 changes: 1 addition & 1 deletion examples/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

// Register a reply command which answers a message
let handle = bot.new_cmd("/reply").and_then(|(bot, msg)| {
Expand Down
2 changes: 1 addition & 1 deletion examples/send_mediagroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

let handle = bot.new_cmd("/send_mediagroup")
.and_then(|(bot, msg)| {
Expand Down
2 changes: 1 addition & 1 deletion examples/send_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

let text = r"
Dearest creature in creation,
Expand Down
2 changes: 1 addition & 1 deletion examples/send_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

let handle = bot.new_cmd("/send_self")
.and_then(|(bot, msg)| {
Expand Down
2 changes: 1 addition & 1 deletion examples/unknown_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
let mut lp = Core::new().unwrap();

// Create the bot
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).update_interval(200);
let bot = RcBot::new(lp.handle(), &env::var("TELEGRAM_BOT_KEY").unwrap()).unwrap().update_interval(200);

// Every possible command is unknown
let handle = bot.unknown_cmd().and_then(|(bot, msg)| bot.message(msg.chat.id, "Unknown command".into()).send());
Expand Down
31 changes: 15 additions & 16 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub struct RcBot {
}

impl RcBot {
pub fn new(handle: Handle, key: &str) -> RcBot {
RcBot {
inner: Rc::new(Bot::new(handle, key)),
}
pub fn new(handle: Handle, key: &str) -> Result<RcBot, Error> {
Ok(RcBot {
inner: Rc::new(Bot::new(handle, key)?),
})
}
}

Expand All @@ -42,13 +42,14 @@ pub struct Bot {
pub timeout: Cell<u64>,
pub handlers: RefCell<HashMap<String, UnboundedSender<(RcBot, objects::Message)>>>,
pub unknown_handler: RefCell<Option<UnboundedSender<(RcBot, objects::Message)>>>,
pub client: Client<HttpsConnector<HttpConnector>, Body>
}

impl Bot {
pub fn new(handle: Handle, key: &str) -> Bot {
pub fn new(handle: Handle, key: &str) -> Result<Bot, Error> {
debug!("Create a new bot with the key {}", key);

Bot {
Ok(Bot {
handle: handle.clone(),
key: key.into(),
name: RefCell::new(None),
Expand All @@ -57,7 +58,12 @@ impl Bot {
timeout: Cell::new(30),
handlers: RefCell::new(HashMap::new()),
unknown_handler: RefCell::new(None),
}
client: Client::builder()
.keep_alive(true)
.keep_alive_timeout(None)
.build(HttpsConnector::new(4)
.context(ErrorKind::HttpsInitializeError)?)
})
}

/// Creates a new request and adds a JSON message to it. The returned Future contains a the
Expand Down Expand Up @@ -87,15 +93,12 @@ impl Bot {
let url: Result<Uri, _> =
format!("https://api.telegram.org/bot{}/{}", self.key, func).parse();

let client = Client::builder()
.build(HttpsConnector::new(2).context(ErrorKind::HttpsInitializeError)?);

let req = Request::post(url.context(ErrorKind::Uri)?)
.header(CONTENT_TYPE, "application/json")
.body(msg.into())
.context(ErrorKind::Hyper)?;

Ok((client, req))
Ok((self.client.clone(), req))
}

/// Creates a new request with some byte content (e.g. a file). The method properties have to be
Expand Down Expand Up @@ -131,10 +134,6 @@ impl Bot {
),
Error,
> {
let client: Client<HttpsConnector<_>, Body> = Client::builder()
.keep_alive(true)
.build(HttpsConnector::new(4).context(ErrorKind::HttpsInitializeError)?);

let url: Result<Uri, _> =
format!("https://api.telegram.org/bot{}/{}", self.key, func).parse();

Expand Down Expand Up @@ -167,7 +166,7 @@ impl Bot {

let req = form.set_body(&mut req_builder).context(ErrorKind::Hyper)?;

Ok((client, req))
Ok((self.client.clone(), req))
}
}

Expand Down