forked from dapr/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zachary Edgell <[email protected]>
- Loading branch information
Showing
8 changed files
with
167 additions
and
1 deletion.
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,31 @@ | ||
# 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 my-data' | ||
- '== APP - distributed-lock-example == Successfully unlocked my-data' | ||
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,14 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: statestore | ||
spec: | ||
type: state.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: localhost:6379 | ||
- name: redisPassword | ||
value: "" | ||
- name: actorStateStore | ||
value: "true" |
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,43 @@ | ||
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 files = vec![("my-data", b"some-data".to_vec())]; | ||
|
||
client.save_state("statestore", files).await.unwrap(); | ||
|
||
let result = client | ||
.lock(dapr::client::TryLockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "my-data".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
expiry_in_seconds: 60, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(result.success); | ||
|
||
println!("Successfully locked my-data"); | ||
|
||
let result = client | ||
.unlock(dapr::client::UnlockRequest { | ||
store_name: "lockstore".to_string(), | ||
resource_id: "my-data".to_string(), | ||
lock_owner: "some-random-id".to_string(), | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(0, result.status); | ||
|
||
println!("Successfully unlocked my-data"); | ||
|
||
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