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 all commits
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
48 changes: 28 additions & 20 deletions examples/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ name: Run Configuration Client
output_match_mode: substring
match_order: none
expected_stdout_lines:
- '== APP == get config = myConfigValue'
- '== APP == get updated config key = mySubscribeKey1, value = mySubscribeValue1'
- '== APP == get updated config key = mySubscribeKey2, value = mySubscribeValue1'
- '== APP == get updated config key = mySubscribeKey3, value = mySubscribeValue1'
- '== APP == get updated config key = mySubscribeKey1, value = mySubscribeValue2'
- '== APP == get updated config key = mySubscribeKey2, value = mySubscribeValue2'
- '== APP == get updated config key = mySubscribeKey3, value = mySubscribeValue2'
- '== APP == get updated config key = mySubscribeKey1, value = mySubscribeValue3'
- '== APP == get updated config key = mySubscribeKey2, value = mySubscribeValue3'
- '== APP == get updated config key = mySubscribeKey3, value = mySubscribeValue3'
- '== APP == got config key = mykey, value = myConfigValue'
- '== APP == got config key = mySubscribeKey1, value = mySubscribeValue1'
- '== APP == got config key = mySubscribeKey2, value = mySubscribeValue1'
- '== APP == got config key = mySubscribeKey3, value = mySubscribeValue1'
- '== APP == got config key = mySubscribeKey1, value = mySubscribeValue2'
- '== APP == got config key = mySubscribeKey2, value = mySubscribeValue2'
- '== APP == got config key = mySubscribeKey3, value = mySubscribeValue2'
- '== APP == got config key = mySubscribeKey1, value = mySubscribeValue3'
- '== APP == got config key = mySubscribeKey2, value = mySubscribeValue3'
- '== APP == got config key = mySubscribeKey3, value = mySubscribeValue3'
- '== APP == dapr configuration subscribe finished.'
background: false
sleep: 40
Expand All @@ -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, value = myConfigValue

got config key = mySubscribeKey1, value = mySubscribeValue1
got config key = mySubscribeKey2, value = mySubscribeValue1
got config key = mySubscribeKey3, value = mySubscribeValue1
got config key = mySubscribeKey1, value = mySubscribeValue2
got config key = mySubscribeKey2, value = mySubscribeValue2
got config key = mySubscribeKey3, value = mySubscribeValue2
got config key = mySubscribeKey1, value = mySubscribeValue3
got config key = mySubscribeKey2, value = mySubscribeValue3
got config key = mySubscribeKey3, value = mySubscribeValue3
got config key = mySubscribeKey1, value = mySubscribeValue4
got config key = mySubscribeKey2, value = mySubscribeValue4
got config key = mySubscribeKey3, value = mySubscribeValue4
got config key = mySubscribeKey1, value = mySubscribeValue5
got config key = mySubscribeKey2, value = mySubscribeValue5
got config key = mySubscribeKey3, value = mySubscribeValue5
dapr configuration subscribe finished.
dapr configuration unsubscribed
✅ Exited App successfully

```
17 changes: 11 additions & 6 deletions examples/configuration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ 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
// update config value
client.Set(context.Background(), "mySubscribeKey1", "mySubscribeValue"+strconv.Itoa(i+1), -1)
client.Set(context.Background(), "mySubscribeKey2", "mySubscribeValue"+strconv.Itoa(i+1), -1)
client.Set(context.Background(), "mySubscribeKey3", "mySubscribeValue"+strconv.Itoa(i+1), -1)
Expand All @@ -32,6 +33,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 +45,27 @@ func main() {
if err != nil {
panic(err)
}
fmt.Printf("get config = %s\n", (*items).Value)
fmt.Printf("got config key = mykey, 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, 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