Skip to content

Commit 71c7182

Browse files
committed
Support API-Style Options
1 parent 24aa528 commit 71c7182

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

pkg/api/api.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,70 @@ import (
1919
"github.com/opensergo/opensergo-go/pkg/transport/subscribe"
2020
)
2121

22+
type ClientOptions struct {
23+
connectRetryTimes uint
24+
}
25+
26+
func NewDefaultClientOptions() *ClientOptions {
27+
return &ClientOptions{
28+
connectRetryTimes: 3,
29+
}
30+
}
31+
32+
func (opts *ClientOptions) ConnectRetryTimes() uint {
33+
return opts.connectRetryTimes
34+
}
35+
36+
type ClientOption func(*ClientOptions)
37+
38+
func WithConnectRetryTimes(connectRetryTimes uint) ClientOption {
39+
return func(opts *ClientOptions) {
40+
opts.connectRetryTimes = connectRetryTimes
41+
}
42+
}
43+
2244
// SubscribeOptions represents the options of OpenSergo data subscription.
2345
type SubscribeOptions struct {
24-
Subscribers []subscribe.Subscriber
25-
Attachments map[string]interface{}
46+
subscribers []subscribe.Subscriber
47+
attachments map[string]interface{}
48+
}
49+
50+
func (opts *SubscribeOptions) Subscribers() []subscribe.Subscriber {
51+
return opts.subscribers
52+
}
53+
54+
func (opts *SubscribeOptions) Attachments() map[string]interface{} {
55+
return opts.attachments
2656
}
2757

2858
type SubscribeOption func(*SubscribeOptions)
2959

3060
// WithSubscriber provides a subscriber.
3161
func WithSubscriber(subscriber subscribe.Subscriber) SubscribeOption {
3262
return func(opts *SubscribeOptions) {
33-
if opts.Subscribers == nil {
34-
opts.Subscribers = make([]subscribe.Subscriber, 0)
63+
if opts.subscribers == nil {
64+
opts.subscribers = make([]subscribe.Subscriber, 0)
3565
}
36-
opts.Subscribers = append(opts.Subscribers, subscriber)
66+
opts.subscribers = append(opts.subscribers, subscriber)
3767
}
3868
}
3969

4070
// WithAttachment provides an attachment (key-value pair).
4171
func WithAttachment(key string, value interface{}) SubscribeOption {
4272
return func(opts *SubscribeOptions) {
43-
if opts.Attachments == nil {
44-
opts.Attachments = make(map[string]interface{})
73+
if opts.attachments == nil {
74+
opts.attachments = make(map[string]interface{})
4575
}
46-
opts.Attachments[key] = value
76+
opts.attachments[key] = value
4777
}
4878
}
4979

5080
// OpenSergoClient is the universal interface of OpenSergo client.
5181
type OpenSergoClient interface {
5282
// Start the client.
5383
Start() error
84+
// Close the client.
85+
Close() error
5486
// SubscribeConfig subscribes data for given subscribe target.
5587
SubscribeConfig(key model.SubscribeKey, opts ...SubscribeOption) error
5688
// UnsubscribeConfig unsubscribes data for given subscribe target.

pkg/client/opensergo_client.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ import (
3333

3434
// OpenSergoClient is the client to communicate with opensergo-control-plane.
3535
type OpenSergoClient struct {
36-
host string
37-
port uint32
36+
host string
37+
port uint32
38+
clientOptions *api.ClientOptions
39+
3840
transportServiceClient transportPb.OpenSergoUniversalTransportServiceClient
3941

4042
subscribeConfigStreamPtr atomic.Value // type of value is *client.subscribeConfigStream
@@ -48,7 +50,15 @@ type OpenSergoClient struct {
4850
}
4951

5052
// NewOpenSergoClient returns an instance of OpenSergoClient, and init some properties.
51-
func NewOpenSergoClient(host string, port uint32) (*OpenSergoClient, error) {
53+
func NewOpenSergoClient(host string, port uint32, opts ...api.ClientOption) (*OpenSergoClient, error) {
54+
clientOptions := api.NewDefaultClientOptions()
55+
// override default ClientOptions by params
56+
if len(opts) > 0 {
57+
for _, opt := range opts {
58+
opt(clientOptions)
59+
}
60+
}
61+
5262
address := host + ":" + strconv.FormatUint(uint64(port), 10)
5363
clientConn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
5464
if err != nil {
@@ -59,6 +69,7 @@ func NewOpenSergoClient(host string, port uint32) (*OpenSergoClient, error) {
5969
openSergoClient := &OpenSergoClient{
6070
host: host,
6171
port: port,
72+
clientOptions: clientOptions,
6273
transportServiceClient: transportServiceClient,
6374
subscribeDataCache: &subscribe.SubscribeDataCache{},
6475
subscriberRegistry: &subscribe.SubscriberRegistry{},
@@ -156,8 +167,8 @@ func (c *OpenSergoClient) SubscribeConfig(subscribeKey model.SubscribeKey, opts
156167
}
157168

158169
// Register subscribers.
159-
if len(options.Subscribers) > 0 {
160-
for _, subscriber := range options.Subscribers {
170+
if len(options.Subscribers()) > 0 {
171+
for _, subscriber := range options.Subscribers() {
161172
c.subscriberRegistry.RegisterSubscriber(subscribeKey, subscriber)
162173
}
163174
}

samples/main/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
package main
1616

1717
import (
18+
"log"
19+
1820
"github.com/opensergo/opensergo-go/pkg/client"
1921
"github.com/opensergo/opensergo-go/pkg/common/logging"
20-
"log"
2122

2223
"github.com/opensergo/opensergo-go/pkg/api"
2324
"github.com/opensergo/opensergo-go/pkg/configkind"
@@ -45,7 +46,7 @@ func StartAndSubscribeOpenSergoConfig() error {
4546
// logging.NewFileLogger("./opensergo-universal-transport-service.log", logging.InfoLevel, logging.JsonFormat, true)
4647

4748
// Create a OpenSergoClient.
48-
openSergoClient, err := client.NewOpenSergoClient("127.0.0.1", 10246)
49+
openSergoClient, err := client.NewOpenSergoClient("127.0.0.1", 10246, api.WithConnectRetryTimes(3))
4950
if err != nil {
5051
return err
5152
}

0 commit comments

Comments
 (0)