Skip to content

Commit 621dade

Browse files
committed
perf: enhanced configuration example
Signed-off-by: sadath-12 <[email protected]>
1 parent 04f7b59 commit 621dade

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

examples/configuration/README.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -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, with value = myConfigValue
51+
52+
got config key = mySubscribeKey1, with value = mySubscribeValue1
53+
got config key = mySubscribeKey2, with value = mySubscribeValue1
54+
got config key = mySubscribeKey3, with value = mySubscribeValue1
55+
got config key = mySubscribeKey1, with value = mySubscribeValue2
56+
got config key = mySubscribeKey2, with value = mySubscribeValue2
57+
got config key = mySubscribeKey3, with value = mySubscribeValue2
58+
got config key = mySubscribeKey1, with value = mySubscribeValue3
59+
got config key = mySubscribeKey2, with value = mySubscribeValue3
60+
got config key = mySubscribeKey3, with value = mySubscribeValue3
61+
got config key = mySubscribeKey1, with value = mySubscribeValue4
62+
got config key = mySubscribeKey2, with value = mySubscribeValue4
63+
got config key = mySubscribeKey3, with value = mySubscribeValue4
64+
got config key = mySubscribeKey1, with value = mySubscribeValue5
65+
got config key = mySubscribeKey2, with value = mySubscribeValue5
66+
got config key = mySubscribeKey3, with value = mySubscribeValue5
6067
dapr configuration subscribe finished.
68+
dapr configuration unsubscribed
6169
✅ Exited App successfully
6270
6371
```

examples/configuration/main.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ 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
@@ -32,6 +34,8 @@ func init() {
3234
}
3335

3436
func main() {
37+
var wg sync.WaitGroup
38+
addItems(&wg)
3539
ctx := context.Background()
3640
client, err := dapr.NewClient()
3741
if err != nil {
@@ -42,25 +46,27 @@ func main() {
4246
if err != nil {
4347
panic(err)
4448
}
45-
fmt.Printf("get config = %s\n", (*items).Value)
49+
fmt.Printf("got config key = mykey, with value = %s \n", (*items).Value)
4650

4751
ctx, f := context.WithTimeout(ctx, 60*time.Second)
4852
md := metadata.Pairs("dapr-app-id", "configuration-api")
4953
ctx = metadata.NewOutgoingContext(ctx, md)
5054
defer f()
5155
subscribeID, err := client.SubscribeConfigurationItems(ctx, "example-config", []string{"mySubscribeKey1", "mySubscribeKey2", "mySubscribeKey3"}, func(id string, items map[string]*dapr.ConfigurationItem) {
56+
wg.Done()
5257
for k, v := range items {
53-
fmt.Printf("get updated config key = %s, value = %s \n", k, v.Value)
58+
fmt.Printf("got config key = %s, with value = %s \n", k, v.Value)
5459
}
5560
})
5661
if err != nil {
5762
panic(err)
5863
}
59-
time.Sleep(time.Second*3 + time.Millisecond*500)
64+
wg.Wait()
6065

6166
// dapr configuration unsubscribe called.
6267
if err := client.UnsubscribeConfigurationItems(ctx, "example-config", subscribeID); err != nil {
6368
panic(err)
6469
}
65-
time.Sleep(time.Second * 5)
70+
fmt.Println("dapr configuration unsubscribed")
71+
time.Sleep(time.Second)
6672
}

0 commit comments

Comments
 (0)