Skip to content

Commit d5ffe77

Browse files
authored
perf: enhanced configuration example (dapr#483)
* perf: enhanced configuration example Signed-off-by: sadath-12 <[email protected]> * remove "with" Signed-off-by: sadath-12 <[email protected]> * update markdown Signed-off-by: sadath-12 <[email protected]> * minor fix Signed-off-by: sadath-12 <[email protected]> --------- Signed-off-by: sadath-12 <[email protected]>
1 parent 04f7b59 commit d5ffe77

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

examples/configuration/README.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ name: Run Configuration Client
1313
output_match_mode: substring
1414
match_order: none
1515
expected_stdout_lines:
16-
- '== APP == get config = myConfigValue'
17-
- '== APP == get updated config key = mySubscribeKey1, value = mySubscribeValue1'
18-
- '== APP == get updated config key = mySubscribeKey2, value = mySubscribeValue1'
19-
- '== APP == get updated config key = mySubscribeKey3, value = mySubscribeValue1'
20-
- '== APP == get updated config key = mySubscribeKey1, value = mySubscribeValue2'
21-
- '== APP == get updated config key = mySubscribeKey2, value = mySubscribeValue2'
22-
- '== APP == get updated config key = mySubscribeKey3, value = mySubscribeValue2'
23-
- '== APP == get updated config key = mySubscribeKey1, value = mySubscribeValue3'
24-
- '== APP == get updated config key = mySubscribeKey2, value = mySubscribeValue3'
25-
- '== APP == get updated config key = mySubscribeKey3, value = mySubscribeValue3'
16+
- '== APP == got config key = mykey, value = myConfigValue'
17+
- '== APP == got config key = mySubscribeKey1, value = mySubscribeValue1'
18+
- '== APP == got config key = mySubscribeKey2, value = mySubscribeValue1'
19+
- '== APP == got config key = mySubscribeKey3, value = mySubscribeValue1'
20+
- '== APP == got config key = mySubscribeKey1, value = mySubscribeValue2'
21+
- '== APP == got config key = mySubscribeKey2, value = mySubscribeValue2'
22+
- '== APP == got config key = mySubscribeKey3, value = mySubscribeValue2'
23+
- '== APP == got config key = mySubscribeKey1, value = mySubscribeValue3'
24+
- '== APP == got config key = mySubscribeKey2, value = mySubscribeValue3'
25+
- '== APP == got config key = mySubscribeKey3, value = mySubscribeValue3'
2626
- '== APP == dapr configuration subscribe finished.'
2727
background: false
2828
sleep: 40
@@ -47,17 +47,25 @@ dapr run --app-id configuration-api\
4747
The subscription event order may out of order.
4848

4949
```
50-
get config = myConfigValue
51-
get updated config key = mySubscribeKey1, value = mySubscribeValue1
52-
get updated config key = mySubscribeKey2, value = mySubscribeValue1
53-
get updated config key = mySubscribeKey3, value = mySubscribeValue1
54-
get updated config key = mySubscribeKey1, value = mySubscribeValue2
55-
get updated config key = mySubscribeKey2, value = mySubscribeValue2
56-
get updated config key = mySubscribeKey3, value = mySubscribeValue2
57-
get updated config key = mySubscribeKey1, value = mySubscribeValue3
58-
get updated config key = mySubscribeKey2, value = mySubscribeValue3
59-
get updated config key = mySubscribeKey3, value = mySubscribeValue3
50+
got config key = mykey, value = myConfigValue
51+
52+
got config key = mySubscribeKey1, value = mySubscribeValue1
53+
got config key = mySubscribeKey2, value = mySubscribeValue1
54+
got config key = mySubscribeKey3, value = mySubscribeValue1
55+
got config key = mySubscribeKey1, value = mySubscribeValue2
56+
got config key = mySubscribeKey2, value = mySubscribeValue2
57+
got config key = mySubscribeKey3, value = mySubscribeValue2
58+
got config key = mySubscribeKey1, value = mySubscribeValue3
59+
got config key = mySubscribeKey2, value = mySubscribeValue3
60+
got config key = mySubscribeKey3, value = mySubscribeValue3
61+
got config key = mySubscribeKey1, value = mySubscribeValue4
62+
got config key = mySubscribeKey2, value = mySubscribeValue4
63+
got config key = mySubscribeKey3, value = mySubscribeValue4
64+
got config key = mySubscribeKey1, value = mySubscribeValue5
65+
got config key = mySubscribeKey2, value = mySubscribeValue5
66+
got config key = mySubscribeKey3, value = mySubscribeValue5
6067
dapr configuration subscribe finished.
68+
dapr configuration unsubscribed
6169
✅ Exited App successfully
6270
6371
```

examples/configuration/main.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ import (
44
"context"
55
"fmt"
66
"strconv"
7+
"sync"
78
"time"
89

910
dapr "github.com/dapr/go-sdk/client"
1011
"github.com/go-redis/redis/v8"
1112
"google.golang.org/grpc/metadata"
1213
)
1314

14-
func init() {
15+
func addItems(wg *sync.WaitGroup) {
1516
opts := &redis.Options{
1617
Addr: "127.0.0.1:6379",
1718
}
1819
client := redis.NewClient(opts)
1920
// set config value
2021
client.Set(context.Background(), "mykey", "myConfigValue", -1)
2122
ticker := time.NewTicker(time.Second)
23+
wg.Add(3 * 5)
2224
go func() {
2325
for i := 0; i < 5; i++ {
2426
<-ticker.C
25-
// update config value
2627
client.Set(context.Background(), "mySubscribeKey1", "mySubscribeValue"+strconv.Itoa(i+1), -1)
2728
client.Set(context.Background(), "mySubscribeKey2", "mySubscribeValue"+strconv.Itoa(i+1), -1)
2829
client.Set(context.Background(), "mySubscribeKey3", "mySubscribeValue"+strconv.Itoa(i+1), -1)
@@ -32,6 +33,8 @@ func init() {
3233
}
3334

3435
func main() {
36+
var wg sync.WaitGroup
37+
addItems(&wg)
3538
ctx := context.Background()
3639
client, err := dapr.NewClient()
3740
if err != nil {
@@ -42,25 +45,27 @@ func main() {
4245
if err != nil {
4346
panic(err)
4447
}
45-
fmt.Printf("get config = %s\n", (*items).Value)
48+
fmt.Printf("got config key = mykey, value = %s \n", (*items).Value)
4649

4750
ctx, f := context.WithTimeout(ctx, 60*time.Second)
4851
md := metadata.Pairs("dapr-app-id", "configuration-api")
4952
ctx = metadata.NewOutgoingContext(ctx, md)
5053
defer f()
5154
subscribeID, err := client.SubscribeConfigurationItems(ctx, "example-config", []string{"mySubscribeKey1", "mySubscribeKey2", "mySubscribeKey3"}, func(id string, items map[string]*dapr.ConfigurationItem) {
55+
wg.Done()
5256
for k, v := range items {
53-
fmt.Printf("get updated config key = %s, value = %s \n", k, v.Value)
57+
fmt.Printf("got config key = %s, value = %s \n", k, v.Value)
5458
}
5559
})
5660
if err != nil {
5761
panic(err)
5862
}
59-
time.Sleep(time.Second*3 + time.Millisecond*500)
63+
wg.Wait()
6064

6165
// dapr configuration unsubscribe called.
6266
if err := client.UnsubscribeConfigurationItems(ctx, "example-config", subscribeID); err != nil {
6367
panic(err)
6468
}
65-
time.Sleep(time.Second * 5)
69+
fmt.Println("dapr configuration unsubscribed")
70+
time.Sleep(time.Second)
6671
}

0 commit comments

Comments
 (0)