-
Notifications
You must be signed in to change notification settings - Fork 70
Added distributed lock API #159
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
Open
zedgell
wants to merge
15
commits into
dapr:main
Choose a base branch
from
zedgell:feature/distributed_lock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a1843fd
Added distributed lock
zedgell 3b06b4c
Updated examples
zedgell 64be79e
Updated examples
zedgell ab9f22b
Updated examples
zedgell 886748e
fix: update example and clarity and validation
mikeee d863019
chore: fmt
mikeee 8a2fde6
fix: update example lock resource
mikeee 5815b09
fix: refactor example wording
mikeee 8165931
Merge branch 'main' into feature/distributed_lock
mikeee b4ee51a
Update client.rs
zedgell 22a1cf2
Update client.rs
zedgell 9a066b8
Update client.rs
zedgell 71c3b4e
Merge branch 'main' into feature/distributed_lock
mikeee 3b1d0d7
Merge branch 'main' into feature/distributed_lock
mikeee 61b20b2
Merge branch 'main' into feature/distributed_lock
mikeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Distributed Lock | ||
|
||
This is a simple example that demonstrates Dapr's Distributed Lock capabilities. | ||
|
||
> **Note:** Make sure to use latest version of proto bindings. | ||
## Running | ||
|
||
To run this example: | ||
|
||
1. Run the multi-app run template: | ||
|
||
<!-- STEP | ||
name: Run multi-app | ||
output_match_mode: substring | ||
match_order: none | ||
expected_stdout_lines: | ||
- '== APP - distributed-lock-example == Successfully locked some-data' | ||
- '== APP - distributed-lock-example == Unsuccessfully locked some-data' | ||
- '== APP - distributed-lock-example == Successfully unlocked some-data' | ||
mikeee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
background: true | ||
sleep: 30 | ||
timeout_seconds: 90 | ||
--> | ||
|
||
```bash | ||
dapr run -f . | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
2. Stop with `ctrl + c` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: lockstore | ||
spec: | ||
type: lock.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: localhost:6379 | ||
- name: redisPassword | ||
value: "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: 1 | ||
common: | ||
daprdLogDestination: console | ||
apps: | ||
- appID: distributed-lock-example | ||
appDirPath: ./ | ||
daprGRPCPort: 35002 | ||
logLevel: debug | ||
command: [ "cargo", "run", "--example", "distributed-lock" ] | ||
resourcesPath: ./components |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use tokio::time::sleep; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
sleep(std::time::Duration::new(2, 0)).await; | ||
let port: u16 = std::env::var("DAPR_GRPC_PORT")?.parse()?; | ||
let addr = format!("https://127.0.0.1:{}", port); | ||
|
||
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?; | ||
|
||
let result = client | ||
.lock(dapr::client::TryLockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "some-data".to_string(), | ||
mikeee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
lock_owner: "some-random-id".to_string(), | ||
expiry_in_seconds: 60, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(result.success); | ||
|
||
println!("Successfully locked some-data"); | ||
mikeee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
let result = client | ||
.lock(dapr::client::TryLockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "some-data".to_string(), | ||
mikeee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
lock_owner: "some-random-id".to_string(), | ||
expiry_in_seconds: 60, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(!result.success); | ||
|
||
println!("Unsuccessfully locked some-data"); | ||
mikeee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
let result = client | ||
.unlock(dapr::client::UnlockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "some-data".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(0, result.status); | ||
|
||
println!("Successfully unlocked some-data"); | ||
|
||
Ok(()) | ||
mikeee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -455,6 +455,24 @@ impl<T: DaprInterface> Client<T> { | |
.collect(); | ||
self.0.decrypt(requested_items).await | ||
} | ||
|
||
/// Distributed lock request call | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `request` - Request to be made, TryLockRequest | ||
pub async fn lock(&mut self, request: TryLockRequest) -> Result<TryLockResponse, Error> { | ||
self.0.lock(request).await | ||
} | ||
|
||
/// Distributed lock request call | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `request` - Request to be made, TryLockRequest | ||
pub async fn unlock(&mut self, request: UnlockRequest) -> Result<UnlockResponse, Error> { | ||
self.0.unlock(request).await | ||
} | ||
Comment on lines
+493
to
+509
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be beneficial to have examples here in the doc comments of building and using the api call e.g.
|
||
} | ||
|
||
#[async_trait] | ||
|
@@ -501,6 +519,10 @@ pub trait DaprInterface: Sized { | |
-> Result<Vec<StreamPayload>, Status>; | ||
|
||
async fn decrypt(&mut self, payload: Vec<DecryptRequest>) -> Result<Vec<u8>, Status>; | ||
|
||
async fn lock(&mut self, request: TryLockRequest) -> Result<TryLockResponse, Error>; | ||
zedgell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
async fn unlock(&mut self, request: UnlockRequest) -> Result<UnlockResponse, Error>; | ||
} | ||
|
||
#[async_trait] | ||
|
@@ -661,6 +683,24 @@ impl DaprInterface for dapr_v1::dapr_client::DaprClient<TonicChannel> { | |
} | ||
Ok(data) | ||
} | ||
|
||
/// Distributed lock request call | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `request` - Request to be made, TryLockRequest | ||
async fn lock(&mut self, request: TryLockRequest) -> Result<TryLockResponse, Error> { | ||
Ok(self.try_lock_alpha1(request).await?.into_inner()) | ||
} | ||
|
||
/// Distributed unlock request call | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `request` - Request to be made, UnlockRequest | ||
async fn unlock(&mut self, request: UnlockRequest) -> Result<UnlockResponse, Error> { | ||
Ok(self.unlock_alpha1(request).await?.into_inner()) | ||
} | ||
zedgell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
/// A request from invoking a service | ||
|
@@ -752,6 +792,18 @@ pub type EncryptRequestOptions = crate::dapr::dapr::proto::runtime::v1::EncryptR | |
/// Decryption request options | ||
pub type DecryptRequestOptions = crate::dapr::dapr::proto::runtime::v1::DecryptRequestOptions; | ||
|
||
/// Lock response | ||
pub type TryLockResponse = crate::dapr::dapr::proto::runtime::v1::TryLockResponse; | ||
|
||
/// Lock request | ||
pub type TryLockRequest = crate::dapr::dapr::proto::runtime::v1::TryLockRequest; | ||
|
||
/// Unlock request | ||
pub type UnlockRequest = crate::dapr::dapr::proto::runtime::v1::UnlockRequest; | ||
|
||
/// Unlock response | ||
pub type UnlockResponse = crate::dapr::dapr::proto::runtime::v1::UnlockResponse; | ||
|
||
zedgell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
type StreamPayload = crate::dapr::dapr::proto::common::v1::StreamPayload; | ||
impl<K> From<(K, Vec<u8>)> for common_v1::StateItem | ||
where | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.