-
Notifications
You must be signed in to change notification settings - Fork 66
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
Added distributed lock API #159
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.
+167
−1
Open
Changes from all 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 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 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 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,33 @@ | ||
# 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: sequential | ||
expected_stdout_lines: | ||
- '== APP - distributed-lock-example == Successfully acquired lock on: resource' | ||
- '== APP - distributed-lock-example == Unsuccessfully acquired lock on: resource' | ||
- '== APP - distributed-lock-example == Successfully released lock on: resource' | ||
- '== APP - distributed-lock-example == Successfully acquired lock on: resource' | ||
background: true | ||
sleep: 30 | ||
timeout_seconds: 90 | ||
--> | ||
|
||
```bash | ||
dapr run -f . | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
2. Stop with `ctrl + c` |
This file contains 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 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 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,67 @@ | ||
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: "resource".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
expiry_in_seconds: 60, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(result.success); | ||
|
||
println!("Successfully acquired lock on: resource"); | ||
|
||
let result = client | ||
.lock(dapr::client::TryLockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "resource".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
expiry_in_seconds: 60, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(!result.success); | ||
|
||
println!("Unsuccessfully acquired lock on: resource"); | ||
|
||
let result = client | ||
.unlock(dapr::client::UnlockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "resource".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(0, result.status); | ||
|
||
println!("Successfully released lock on: resource"); | ||
|
||
let result = client | ||
.lock(dapr::client::TryLockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "resource".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
expiry_in_seconds: 60, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(result.success); | ||
|
||
println!("Successfully acquired lock on: resource"); | ||
|
||
Ok(()) | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.