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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![doc(issue_tracker_base_url = "https://github.com/lazureykis/mongo-lock/issues")]

//! Distributed mutually exclusive locks in MongoDB.
//!
//! This crate contains only sync implementation.
//! If you need a async version, use [`mongo-lock-async`](https://crates.io/crates/mongo-lock-async) crate.
//!
//! This implementation relies on system time. Ensure that NTP clients on your servers are configured properly.
//!
//! Usage:
//! ```rust
//! fn main() {
//!     let mongo = mongodb::sync::Client::with_uri_str("mongodb://localhost").unwrap();
//!
//!     // We need to ensure that mongodb collection has a proper index.
//!     mongo_lock::prepare_database(&mongo).unwrap();
//!
//!     if let Ok(Some(lock)) =
//!         mongo_lock::Lock::try_acquire(
//!             &mongo,
//!             "my-key",
//!             std::time::Duration::from_secs(30)
//!         )
//!     {
//!         println!("Lock acquired.");
//!
//!         // The lock will be released automatically after leaving the scope.
//!     }
//! }
//! ```

mod util;

const COLLECTION_NAME: &str = "locks";
const DEFAULT_DB_NAME: &str = "mongo-lock";

use mongodb::bson::{doc, Document};
use mongodb::error::{Error, ErrorKind, WriteError, WriteFailure};
use mongodb::options::{IndexOptions, UpdateOptions};
use mongodb::sync::{Client, Collection};
use mongodb::IndexModel;
use std::time::Duration;

#[inline]
fn collection(mongo: &Client) -> Collection<Document> {
    mongo
        .default_database()
        .unwrap_or_else(|| mongo.database(DEFAULT_DB_NAME))
        .collection(COLLECTION_NAME)
}

/// Prepares MongoDB collection to store locks.
///
/// Creates TTL index to remove old records after they expire.
///
/// The [Lock] itself does not relies on this index,
/// because MongoDB can remove documents with some significant delay.
pub fn prepare_database(mongo: &Client) -> Result<(), Error> {
    let options = IndexOptions::builder()
        .expire_after(Some(Duration::from_secs(0)))
        .build();

    let model = IndexModel::builder()
        .keys(doc! {"expiresAt": 1})
        .options(options)
        .build();

    collection(mongo).create_index(model, None)?;

    Ok(())
}

/// Distributed mutex lock.
pub struct Lock {
    mongo: Client,
    id: String,
    acquired: bool,
}

impl Lock {
    /// Tries to acquire the lock with the given key.
    pub fn try_acquire(
        mongo: &Client,
        key: &str,
        ttl: Duration,
    ) -> Result<Option<Lock>, mongodb::error::Error> {
        let (now, expires_at) = util::now_and_expires_at(ttl);

        // Update expired locks if mongodb didn't clean it yet.
        let query = doc! {
            "_id": key,
            "expiresAt": {"$lte": now},
        };

        let update = doc! {
            "$set": {
                "expiresAt": expires_at,
            },
            "$setOnInsert": {
                "_id": key,
            },
        };

        let options = UpdateOptions::builder().upsert(true).build();

        match collection(mongo).update_one(query, update, options) {
            Ok(result) => {
                if result.upserted_id.is_some() || result.modified_count == 1 {
                    Ok(Some(Lock {
                        mongo: mongo.clone(),
                        id: key.to_string(),
                        acquired: true,
                    }))
                } else {
                    Ok(None)
                }
            }
            Err(err) => {
                if let ErrorKind::Write(WriteFailure::WriteError(WriteError {
                    code: 11000, ..
                })) = *err.kind
                {
                    Ok(None)
                } else {
                    Err(err)
                }
            }
        }
    }

    fn release(&mut self) -> Result<bool, mongodb::error::Error> {
        if self.acquired {
            let result = collection(&self.mongo).delete_one(doc! {"_id": &self.id}, None)?;

            self.acquired = false;

            Ok(result.deleted_count == 1)
        } else {
            Ok(false)
        }
    }
}

impl Drop for Lock {
    fn drop(&mut self) {
        self.release().ok();
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    fn gen_random_key() -> String {
        use rand::{distributions::Alphanumeric, thread_rng, Rng};

        thread_rng()
            .sample_iter(&Alphanumeric)
            .take(30)
            .map(char::from)
            .collect()
    }

    #[test]
    fn simple_locks() {
        let mongo = Client::with_uri_str("mongodb://localhost").unwrap();

        prepare_database(&mongo).unwrap();

        let key1 = gen_random_key();
        let key2 = gen_random_key();

        let lock1 = Lock::try_acquire(&mongo, &key1, Duration::from_secs(5)).unwrap();
        assert!(lock1.is_some());

        let lock1_dup = Lock::try_acquire(&mongo, &key1, Duration::from_secs(5)).unwrap();
        assert!(lock1_dup.is_none());

        let released1 = lock1.unwrap().release().unwrap();
        assert!(released1);

        let lock1 = Lock::try_acquire(&mongo, &key1, Duration::from_secs(5)).unwrap();
        assert!(lock1.is_some());

        let lock2 = Lock::try_acquire(&mongo, &key2, Duration::from_secs(5)).unwrap();
        assert!(lock2.is_some());

        lock1.unwrap().release().unwrap();
        lock2.unwrap().release().unwrap();
    }

    #[test]
    fn with_ttl() {
        let mongo = Client::with_uri_str("mongodb://localhost").unwrap();

        prepare_database(&mongo).unwrap();

        let key = gen_random_key();

        assert!(Lock::try_acquire(&mongo, &key, Duration::from_secs(1))
            .unwrap()
            .is_some());

        std::thread::sleep(Duration::from_secs(1));

        assert!(Lock::try_acquire(&mongo, &key, Duration::from_secs(1))
            .unwrap()
            .is_some());
    }

    #[test]
    fn dropped_locks() {
        let mongo = Client::with_uri_str("mongodb://localhost").unwrap();

        prepare_database(&mongo).unwrap();

        let key = gen_random_key();

        {
            assert!(Lock::try_acquire(&mongo, &key, Duration::from_secs(1))
                .unwrap()
                .is_some());
        }

        {
            assert!(Lock::try_acquire(&mongo, &key, Duration::from_secs(1))
                .unwrap()
                .is_some());
        }

        let lock1 = Lock::try_acquire(&mongo, &key, Duration::from_secs(1)).unwrap();
        let lock2 = Lock::try_acquire(&mongo, &key, Duration::from_secs(1)).unwrap();

        assert!(lock1.is_some());
        assert!(lock2.is_none());

        drop(lock1);

        let lock3 = Lock::try_acquire(&mongo, &key, Duration::from_secs(1)).unwrap();
        assert!(lock3.is_some());
    }
}