浏览代码

Use AtomicU64 instead of `Mutex`

develop
Silvestr Predko 3 年前
父节点
当前提交
afc6470efb
共有 1 个文件被更改,包括 10 次插入9 次删除
  1. 10
    9
      near-client/src/client.rs

+ 10
- 9
near-client/src/client.rs 查看文件

@@ -23,11 +23,12 @@ use serde::de::DeserializeOwned;
23 23
 use serde_json::{json, Value};
24 24
 use url::Url;
25 25
 
26
-use std::sync::{Arc, Mutex};
27
-
28
-type AtomicNonce = Arc<Mutex<Nonce>>;
29
-
26
+use std::sync::{
27
+    atomic::{AtomicU64, Ordering},
28
+    Arc,
29
+};
30 30
 
31
+type AtomicNonce = Arc<AtomicU64>;
31 32
 /// Used for signing transactions
32 33
 pub struct Signer {
33 34
     keypair: Keypair,
@@ -40,7 +41,7 @@ impl Signer {
40 41
         Ok(Self {
41 42
             keypair: Keypair::from_expanded_secret(secret_key).map_err(Error::CreateSigner)?,
42 43
             account_id,
43
-            nonce: AtomicNonce::new(Mutex::new(nonce)),
44
+            nonce: AtomicNonce::new(AtomicU64::new(nonce)),
44 45
         })
45 46
     }
46 47
 
@@ -48,7 +49,7 @@ impl Signer {
48 49
         Self {
49 50
             keypair: Keypair::new(secret_key),
50 51
             account_id,
51
-            nonce: AtomicNonce::new(Mutex::new(nonce)),
52
+            nonce: AtomicNonce::new(AtomicU64::new(nonce)),
52 53
         }
53 54
     }
54 55
 
@@ -74,15 +75,15 @@ impl Signer {
74 75
     }
75 76
 
76 77
     pub fn nonce(&self) -> Nonce {
77
-        *self.nonce.lock().unwrap()
78
+        self.nonce.load(Ordering::Acquire)
78 79
     }
79 80
 
80 81
     pub fn update_nonce(&self, nonce: Nonce) {
81
-        *self.nonce.lock().unwrap() = nonce;
82
+        self.nonce.store(nonce, Ordering::Release);
82 83
     }
83 84
 
84 85
     pub fn increment_nonce(&self, value: u64) {
85
-        *self.nonce.lock().unwrap() += value;
86
+        self.nonce.fetch_add(value, Ordering::AcqRel);
86 87
     }
87 88
 }
88 89
 

正在加载...
取消
保存