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
// Copyright 2015-2016 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::HashMap;

use futures::Future;

use client::ClientHandle;
use client::rc_future::{rc_future, RcFuture};
use error::*;
use op::{Message, Query};

/// A ClienHandle for memoized (cached) responses to queries.
///
/// This wraps a ClientHandle, changing the implementation `send()` to store the response against
///  the Message.Query that was sent. This should reduce network traffic especially during things
///  like DNSSec validation. *Warning* this will currently cache for the life of the Client.
#[derive(Clone)]
#[must_use = "queries can only be sent through a ClientHandle"]
pub struct MemoizeClientHandle<H: ClientHandle> {
    client: H,
    active_queries: Rc<RefCell<HashMap<Query,
                                       RcFuture<Box<Future<Item = Message,
                                                           Error = ClientError>>>>>>,
}

impl<H> MemoizeClientHandle<H>
    where H: ClientHandle
{
    /// Returns a new handle wrapping the specified client
    pub fn new(client: H) -> MemoizeClientHandle<H> {
        MemoizeClientHandle {
            client: client,
            active_queries: Rc::new(RefCell::new(HashMap::new())),
        }
    }
}

impl<H> ClientHandle for MemoizeClientHandle<H>
    where H: ClientHandle
{
    fn send(&mut self, message: Message) -> Box<Future<Item = Message, Error = ClientError>> {
        let query = message.queries()
            .first()
            .expect("no query!")
            .clone();

        if let Some(rc_future) = self.active_queries.borrow().get(&query) {
            // FIXME check TTLs?
            return Box::new(rc_future.clone());
        }

        // check if there are active queries
        {
            let map = self.active_queries.borrow();
            let request = map.get(&query);
            if request.is_some() {
                return Box::new(request.unwrap().clone());
            }
        }

        let request = rc_future(self.client.send(message));
        let mut map = self.active_queries.borrow_mut();
        map.insert(query, request.clone());

        return Box::new(request);
    }
}

#[cfg(test)]
mod test {
    use std::cell::Cell;
    use client::*;
    use error::*;
    use op::*;
    use rr::*;
    use futures::*;

    #[derive(Clone)]
    struct TestClient {
        i: Cell<u16>,
    }

    impl ClientHandle for TestClient {
        fn send(&mut self, _: Message) -> Box<Future<Item = Message, Error = ClientError>> {
            let mut message = Message::new();
            let i = self.i.get();

            message.set_id(i);
            self.i.set(i + 1);

            Box::new(finished(message))
        }
    }

    #[test]
    fn test_memoized() {
        let mut client = MemoizeClientHandle::new(TestClient { i: Cell::new(0) });

        let mut test1 = Message::new();
        test1.add_query(Query::new().set_query_type(RecordType::A).clone());

        let mut test2 = Message::new();
        test2.add_query(Query::new().set_query_type(RecordType::AAAA).clone());

        let result = client.send(test1.clone())
            .wait()
            .ok()
            .unwrap();
        assert_eq!(result.id(), 0);

        let result = client.send(test2.clone())
            .wait()
            .ok()
            .unwrap();
        assert_eq!(result.id(), 1);

        // should get the same result for each...
        let result = client.send(test1)
            .wait()
            .ok()
            .unwrap();
        assert_eq!(result.id(), 0);

        let result = client.send(test2)
            .wait()
            .ok()
            .unwrap();
        assert_eq!(result.id(), 1);
    }

}