      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    //! Pending request set.
    //!
    //! Stores pending requests and does timeout computation according to the rule
    //! that only the earliest submitted request within the structure may time out.
    //!
    //! Whenever a request becomes the earliest, its timeout period begins at that moment.

    use std::collections::{BTreeMap, HashMap};
    use std::iter::FromIterator;

    use request::{self, Request};
    use net::{timeout, ReqId};

    use time::{Duration, SteadyTime};

    /// Request set.
    #[derive(Debug)]
    pub struct RequestSet {
    	counter: u64,
    	base: Option<SteadyTime>,
    	ids: HashMap<ReqId, u64>,
    	reqs: BTreeMap<u64, Request>,
    }

    impl Default for RequestSet {
    	fn default() -> Self {
    		RequestSet {
    			counter: 0,
    			base: None,
    			ids: HashMap::new(),
    			reqs: BTreeMap::new(),
    		}
    	}
    }

    impl RequestSet {
    	/// Push a request onto the stack.
    	pub fn insert(&mut self, req_id: ReqId, req: Request, now: SteadyTime) {
    		let counter = self.counter;
    		self.ids.insert(req_id, counter);
    		self.reqs.insert(counter, req);

    		if self.reqs.keys().next().map_or(true, |x| *x == counter) {
    			self.base = Some(now);
    		}

    		self.counter += 1;
    	}

    	/// Remove a request from the stack.
    	pub fn remove(&mut self, req_id: &ReqId, now: SteadyTime) -> Option<Request> {
    		let id = match self.ids.remove(&req_id) {
    			Some(id) => id,
    			None => return None,
    		};

    		let req = self.reqs.remove(&id).expect("entry in `ids` implies entry in `reqs`; qed");

    		match self.reqs.keys().next() {
    			Some(k) if *k > id => self.base = Some(now),
    			None => self.base = None,
    			_ => {}
    		}

    		Some(req)
    	}

    	/// Check for timeout against the given time. Returns true if
    	/// has timed out, false otherwise.
    	pub fn check_timeout(&self, now: SteadyTime) -> bool {
    		let base = match self.base.as_ref().cloned() {
    			Some(base) => base,
    			None => return false,
    		};

    		let kind = self.reqs.values()
    			.next()
    			.map(|r| r.kind())
    			.expect("base time implies `reqs` non-empty; qed");

    		let kind_timeout = match kind {
    			request::Kind::Headers => timeout::HEADERS,
    			request::Kind::Bodies => timeout::BODIES,
    			request::Kind::Receipts => timeout::RECEIPTS,
    			request::Kind::StateProofs => timeout::PROOFS,
    			request::Kind::Codes => timeout::CONTRACT_CODES,
    			request::Kind::HeaderProofs => timeout::HEADER_PROOFS,
    		};

    		base + Duration::milliseconds(kind_timeout) <= now
    	}

    	/// Collect all pending request ids.
    	pub fn collect_ids<F>(&self) -> F where F: FromIterator<ReqId> {
    		self.ids.keys().cloned().collect()
    	}
    }

    #[cfg(test)]
    mod tests {
    	use net::{timeout, ReqId};
    	use request::{Request, Receipts};
    	use time::{SteadyTime, Duration};
    	use super::RequestSet;

    	#[test]
    	fn multi_timeout() {
    		let test_begin = SteadyTime::now();
    		let mut req_set = RequestSet::default();

    		let the_req = Request::Receipts(Receipts { block_hashes: Vec::new() });
    		req_set.insert(ReqId(0), the_req.clone(), test_begin);
    		req_set.insert(ReqId(1), the_req, test_begin + Duration::seconds(1));

    		assert_eq!(req_set.base, Some(test_begin));

    		let test_end = test_begin + Duration::milliseconds(timeout::RECEIPTS);
    		assert!(req_set.check_timeout(test_end));

    		req_set.remove(&ReqId(0), test_begin + Duration::seconds(1)).unwrap();
    		assert!(!req_set.check_timeout(test_end));
    		assert!(req_set.check_timeout(test_end + Duration::seconds(1)));
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)