From f19499d80a972f587a5b82c835589b42d7652062 Mon Sep 17 00:00:00 2001 From: Harry Li Date: Wed, 27 Mar 2024 11:10:38 +0800 Subject: [PATCH 1/3] pref: use a simple pool reuse client. --- pkg/client/opensergo_client.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/client/opensergo_client.go b/pkg/client/opensergo_client.go index f2807a7..d06f7de 100644 --- a/pkg/client/opensergo_client.go +++ b/pkg/client/opensergo_client.go @@ -17,6 +17,7 @@ package client import ( "context" "strconv" + "sync" "sync/atomic" "time" @@ -70,6 +71,32 @@ func NewOpenSergoClient(host string, port uint32) (*OpenSergoClient, error) { return openSergoClient, nil } +var ( + clientMu sync.Mutex + clientPool = make(map[string]*OpenSergoClient) +) + +// GetOpenSergoClientByPool returns an instance of OpenSergoClient from a pool, based on host:port, +// if it doesn't exist, it will be created and reused for the next call. +func GetOpenSergoClientByPool(host string, port uint32) (*OpenSergoClient, error) { + address := host + ":" + strconv.FormatUint(uint64(port), 10) + + clientMu.Lock() + defer clientMu.Unlock() + + if client, ok := clientPool[address]; ok { + return client, nil + } + + client, err := NewOpenSergoClient(host, port) + if err != nil { + return nil, err + } + clientPool[address] = client + + return client, nil +} + func (c *OpenSergoClient) SubscribeDataCache() *subscribe.SubscribeDataCache { return c.subscribeDataCache } From 85e296ac7b35c5be20763caf5e950aab580a7c7c Mon Sep 17 00:00:00 2001 From: Harry Li Date: Wed, 27 Mar 2024 11:14:02 +0800 Subject: [PATCH 2/3] feat: add an example about reuse OpenSergoClient. --- samples/main/main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/samples/main/main.go b/samples/main/main.go index 937e79e..a608b00 100644 --- a/samples/main/main.go +++ b/samples/main/main.go @@ -34,6 +34,12 @@ func main() { logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfig: %s\n") } + err = StartAndSubscribeOpenSergoConfigByPool() + if err != nil { + // Handle error here. + logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfigByPool: %s\n") + } + select {} } @@ -86,3 +92,53 @@ func StartAndSubscribeOpenSergoConfig() error { return err } + +func StartAndSubscribeOpenSergoConfigByPool() error { + // Set OpenSergo console logger (optional) + consoleLogger := logging.NewConsoleLogger(logging.InfoLevel, logging.JsonFormat, true) + logging.AddLogger(consoleLogger) + // Set OpenSergo file logger (optional) + // fileLogger := logging.NewFileLogger("./opensergo-universal-transport-service.log", logging.InfoLevel, logging.JsonFormat, true) + //logging.AddLogger(fileLogger) + + // Get a OpenSergoClient by pool. + openSergoClient, err := client.GetOpenSergoClientByPool("127.0.0.1", 10246) + if err != nil { + return err + } + + // Start OpenSergoClient + err = openSergoClient.Start() + if err != nil { + return err + } + + // Create a SubscribeKey for FaultToleranceRule. + faultToleranceSubscribeKey := model.NewSubscribeKey("default", "foo-app", configkind.ConfigKindRefFaultToleranceRule{}) + // Create a Subscriber. + sampleFaultToleranceRuleSubscriber := &samples.SampleFaultToleranceRuleSubscriber{} + // Subscribe data with the key and subscriber. + err = openSergoClient.SubscribeConfig(*faultToleranceSubscribeKey, api.WithSubscriber(sampleFaultToleranceRuleSubscriber)) + if err != nil { + return err + } + + // Create a SubscribeKey for RateLimitStrategy. + rateLimitSubscribeKey := model.NewSubscribeKey("default", "foo-app", configkind.ConfigKindRefRateLimitStrategy{}) + // Create another Subscriber. + sampleRateLimitStrategySubscriber := &samples.SampleRateLimitStrategySubscriber{} + // Subscribe data with the key and subscriber. + err = openSergoClient.SubscribeConfig(*rateLimitSubscribeKey, api.WithSubscriber(sampleRateLimitStrategySubscriber)) + + if err != nil { + return err + } + // Create a SubscribeKey for TrafficRouter + trafficRouterSubscribeKey := model.NewSubscribeKey("default", "service-provider", configkind.ConfigKindTrafficRouterStrategy{}) + // Create another Subscriber + sampleTrafficRouterSubscriber := &samples.SampleTrafficRouterSubscriber{} + // Subscribe data with the key and subscriber + err = openSergoClient.SubscribeConfig(*trafficRouterSubscribeKey, api.WithSubscriber(sampleTrafficRouterSubscriber)) + + return err +} From d488de19f6c430aaf75cc669c7ec350d1350b805 Mon Sep 17 00:00:00 2001 From: Harry Li Date: Wed, 27 Mar 2024 13:19:14 +0800 Subject: [PATCH 3/3] style: update reuse OpenSergoClient function name --- pkg/client/opensergo_client.go | 4 ++-- samples/main/main.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/client/opensergo_client.go b/pkg/client/opensergo_client.go index d06f7de..f3615de 100644 --- a/pkg/client/opensergo_client.go +++ b/pkg/client/opensergo_client.go @@ -76,9 +76,9 @@ var ( clientPool = make(map[string]*OpenSergoClient) ) -// GetOpenSergoClientByPool returns an instance of OpenSergoClient from a pool, based on host:port, +// GetOpenSergoClientFromPool returns an instance of OpenSergoClient from a pool, based on host:port, // if it doesn't exist, it will be created and reused for the next call. -func GetOpenSergoClientByPool(host string, port uint32) (*OpenSergoClient, error) { +func GetOpenSergoClientFromPool(host string, port uint32) (*OpenSergoClient, error) { address := host + ":" + strconv.FormatUint(uint64(port), 10) clientMu.Lock() diff --git a/samples/main/main.go b/samples/main/main.go index a608b00..6f2efc7 100644 --- a/samples/main/main.go +++ b/samples/main/main.go @@ -34,10 +34,10 @@ func main() { logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfig: %s\n") } - err = StartAndSubscribeOpenSergoConfigByPool() + err = StartAndSubscribeOpenSergoConfigFromPool() if err != nil { // Handle error here. - logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfigByPool: %s\n") + logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfigFromPool: %s\n") } select {} @@ -93,7 +93,7 @@ func StartAndSubscribeOpenSergoConfig() error { return err } -func StartAndSubscribeOpenSergoConfigByPool() error { +func StartAndSubscribeOpenSergoConfigFromPool() error { // Set OpenSergo console logger (optional) consoleLogger := logging.NewConsoleLogger(logging.InfoLevel, logging.JsonFormat, true) logging.AddLogger(consoleLogger) @@ -102,7 +102,7 @@ func StartAndSubscribeOpenSergoConfigByPool() error { //logging.AddLogger(fileLogger) // Get a OpenSergoClient by pool. - openSergoClient, err := client.GetOpenSergoClientByPool("127.0.0.1", 10246) + openSergoClient, err := client.GetOpenSergoClientFromPool("127.0.0.1", 10246) if err != nil { return err }