Skip to content
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

perf: enhanced configuration example #483

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
sadath-12 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading