Skip to content

Commit

Permalink
perf: enhanced configuration example
Browse files Browse the repository at this point in the history
Signed-off-by: sadath-12 <[email protected]>
  • Loading branch information
sadath-12 committed Dec 14, 2023
1 parent 04f7b59 commit 621dade
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
28 changes: 18 additions & 10 deletions examples/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,25 @@ dapr run --app-id configuration-api\
The subscription event order may out of order.

```
get config = myConfigValue
get updated config key = mySubscribeKey1, value = mySubscribeValue1
get updated config key = mySubscribeKey2, value = mySubscribeValue1
get updated config key = mySubscribeKey3, value = mySubscribeValue1
get updated config key = mySubscribeKey1, value = mySubscribeValue2
get updated config key = mySubscribeKey2, value = mySubscribeValue2
get updated config key = mySubscribeKey3, value = mySubscribeValue2
get updated config key = mySubscribeKey1, value = mySubscribeValue3
get updated config key = mySubscribeKey2, value = mySubscribeValue3
get updated config key = mySubscribeKey3, value = mySubscribeValue3
got config key = mykey, with value = myConfigValue
got config key = mySubscribeKey1, with value = mySubscribeValue1
got config key = mySubscribeKey2, with value = mySubscribeValue1
got config key = mySubscribeKey3, with value = mySubscribeValue1
got config key = mySubscribeKey1, with value = mySubscribeValue2
got config key = mySubscribeKey2, with value = mySubscribeValue2
got config key = mySubscribeKey3, with value = mySubscribeValue2
got config key = mySubscribeKey1, with value = mySubscribeValue3
got config key = mySubscribeKey2, with value = mySubscribeValue3
got config key = mySubscribeKey3, with value = mySubscribeValue3
got config key = mySubscribeKey1, with value = mySubscribeValue4
got config key = mySubscribeKey2, with value = mySubscribeValue4
got config key = mySubscribeKey3, with value = mySubscribeValue4
got config key = mySubscribeKey1, with value = mySubscribeValue5
got config key = mySubscribeKey2, with value = mySubscribeValue5
got config key = mySubscribeKey3, with value = mySubscribeValue5
dapr configuration subscribe finished.
dapr configuration unsubscribed
✅ Exited App successfully
```
16 changes: 11 additions & 5 deletions examples/configuration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"context"
"fmt"
"strconv"
"sync"
"time"

dapr "github.com/dapr/go-sdk/client"
"github.com/go-redis/redis/v8"
"google.golang.org/grpc/metadata"
)

func init() {
func addItems(wg *sync.WaitGroup) {
opts := &redis.Options{
Addr: "127.0.0.1:6379",
}
client := redis.NewClient(opts)
// set config value
client.Set(context.Background(), "mykey", "myConfigValue", -1)
ticker := time.NewTicker(time.Second)
wg.Add(3*5)
go func() {
for i := 0; i < 5; i++ {
<-ticker.C
Expand All @@ -32,6 +34,8 @@ func init() {
}

func main() {
var wg sync.WaitGroup
addItems(&wg)
ctx := context.Background()
client, err := dapr.NewClient()
if err != nil {
Expand All @@ -42,25 +46,27 @@ func main() {
if err != nil {
panic(err)
}
fmt.Printf("get config = %s\n", (*items).Value)
fmt.Printf("got config key = mykey, with value = %s \n", (*items).Value)

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

// dapr configuration unsubscribe called.
if err := client.UnsubscribeConfigurationItems(ctx, "example-config", subscribeID); err != nil {
panic(err)
}
time.Sleep(time.Second * 5)
fmt.Println("dapr configuration unsubscribed")
time.Sleep(time.Second)
}

0 comments on commit 621dade

Please sign in to comment.