Skip to content

Commit 507b174

Browse files
committed
Improve client.NewOpenSergoClient API with functional option style
Signed-off-by: Jiangnan Jia <[email protected]>
1 parent ec731f6 commit 507b174

File tree

4 files changed

+137
-15
lines changed

4 files changed

+137
-15
lines changed

pkg/api/api.go

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

22+
type OpensergoOptions struct {
23+
}
24+
25+
type OpensergoOption func(*OpensergoOptions)
26+
27+
type ClientOptions struct {
28+
connectRetryTimes uint
29+
}
30+
31+
func (opts *ClientOptions) ConnectRetryTimes() uint {
32+
return opts.connectRetryTimes
33+
}
34+
35+
type ClientOption func(*ClientOptions)
36+
37+
func WithConnectRetryTimes(connectRetryTimes uint) ClientOption {
38+
return func(opts *ClientOptions) {
39+
opts.connectRetryTimes = connectRetryTimes
40+
}
41+
}
42+
2243
// SubscribeOptions represents the options of OpenSergo data subscription.
2344
type SubscribeOptions struct {
24-
Subscribers []subscribe.Subscriber
25-
Attachments map[string]interface{}
45+
subscribers []subscribe.Subscriber
46+
attachments map[string]interface{}
47+
}
48+
49+
func (opts *SubscribeOptions) Subscribers() []subscribe.Subscriber {
50+
return opts.subscribers
51+
}
52+
53+
func (opts *SubscribeOptions) Attachments() map[string]interface{} {
54+
return opts.attachments
2655
}
2756

2857
type SubscribeOption func(*SubscribeOptions)
2958

3059
// WithSubscriber provides a subscriber.
3160
func WithSubscriber(subscriber subscribe.Subscriber) SubscribeOption {
3261
return func(opts *SubscribeOptions) {
33-
if opts.Subscribers == nil {
34-
opts.Subscribers = make([]subscribe.Subscriber, 0)
62+
if opts.subscribers == nil {
63+
opts.subscribers = make([]subscribe.Subscriber, 0)
3564
}
36-
opts.Subscribers = append(opts.Subscribers, subscriber)
65+
opts.subscribers = append(opts.subscribers, subscriber)
3766
}
3867
}
3968

4069
// WithAttachment provides an attachment (key-value pair).
4170
func WithAttachment(key string, value interface{}) SubscribeOption {
4271
return func(opts *SubscribeOptions) {
43-
if opts.Attachments == nil {
44-
opts.Attachments = make(map[string]interface{})
72+
if opts.attachments == nil {
73+
opts.attachments = make(map[string]interface{})
4574
}
46-
opts.Attachments[key] = value
75+
opts.attachments[key] = value
4776
}
4877
}
4978

5079
// OpenSergoClient is the universal interface of OpenSergo client.
5180
type OpenSergoClient interface {
5281
// Start the client.
5382
Start() error
83+
// Close the client.
84+
Close() error
5485
// SubscribeConfig subscribes data for given subscribe target.
5586
SubscribeConfig(key model.SubscribeKey, opts ...SubscribeOption) error
5687
// UnsubscribeConfig unsubscribes data for given subscribe target.

pkg/global/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package global
16+
package global

pkg/global/global_options.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package global
16+
17+
import (
18+
"github.com/opensergo/opensergo-go/pkg/api"
19+
"sync"
20+
)
21+
22+
type GlobalOptions struct {
23+
opensergoOptions *api.OpensergoOptions
24+
clientOptions *api.ClientOptions
25+
}
26+
27+
var globalOptions *GlobalOptions
28+
var globalOptionsOnce sync.Once
29+
30+
func GetGlobalOptions() *GlobalOptions {
31+
globalOptionsOnce.Do(func() {
32+
globalOptions = &GlobalOptions{}
33+
// TODO set default OpensergoOptions
34+
//globalOptions.SetOpensergoOpts()
35+
globalOptions.setDefaultOpensergoOptions()
36+
globalOptions.setDefaultClientOptions()
37+
})
38+
return globalOptions
39+
}
40+
41+
func (g *GlobalOptions) OpensergoOptions() *api.OpensergoOptions {
42+
return g.opensergoOptions
43+
}
44+
45+
func (g *GlobalOptions) SetOpensergoOpts(opts ...api.OpensergoOption) *GlobalOptions {
46+
options := &api.OpensergoOptions{}
47+
if len(opts) > 0 {
48+
for _, opt := range opts {
49+
opt(options)
50+
}
51+
}
52+
g.opensergoOptions = options
53+
return g
54+
}
55+
56+
func (g *GlobalOptions) ClientOptions() *api.ClientOptions {
57+
return g.clientOptions
58+
}
59+
60+
func (g *GlobalOptions) SetClientOptions(opts ...api.ClientOption) *GlobalOptions {
61+
options := &api.ClientOptions{}
62+
if len(opts) > 0 {
63+
for _, opt := range opts {
64+
opt(options)
65+
}
66+
}
67+
g.clientOptions = options
68+
return g
69+
}
70+
71+
func (g *GlobalOptions) setDefaultOpensergoOptions() {
72+
73+
}
74+
75+
func (g *GlobalOptions) setDefaultClientOptions() {
76+
g.SetClientOptions(api.WithConnectRetryTimes(3))
77+
}

samples/main/main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
package main
1616

1717
import (
18-
"github.com/opensergo/opensergo-go/pkg/common/logging"
19-
"log"
20-
2118
"github.com/opensergo/opensergo-go/pkg/api"
2219
"github.com/opensergo/opensergo-go/pkg/client"
20+
"github.com/opensergo/opensergo-go/pkg/common/logging"
2321
"github.com/opensergo/opensergo-go/pkg/configkind"
2422
"github.com/opensergo/opensergo-go/pkg/model"
2523
"github.com/opensergo/opensergo-go/samples"
@@ -32,25 +30,25 @@ func main() {
3230
err := StartAndSubscribeOpenSergoConfig()
3331
if err != nil {
3432
// Handle error here.
35-
log.Printf("Failed to StartAndSubscribeOpenSergoConfig: %s\n", err.Error())
33+
logging.Error(err, "Failed to StartAndSubscribeOpenSergoConfig")
3634
}
3735

3836
select {}
3937
}
4038

4139
func StartAndSubscribeOpenSergoConfig() error {
4240
// Set OpenSergo console logger (optional)
43-
logging.NewConsoleLogger(logging.InfoLevel, logging.SeparateFormat, true)
41+
logging.NewConsoleLogger(logging.DebugLevel, logging.JsonFormat, false)
4442
// Set OpenSergo file logger (optional)
4543
// logging.NewFileLogger("./opensergo-universal-transport-service.log", logging.InfoLevel, logging.JsonFormat, true)
4644

4745
// Create a OpenSergoClient.
48-
openSergoClient, err := client.NewOpenSergoClient("127.0.0.1", 10246)
46+
openSergoClient, err := client.NewOpenSergoClient("127.0.0.1", 10246, api.WithConnectRetryTimes(10))
4947
if err != nil {
5048
return err
5149
}
5250

53-
// Start OpenSergoClient
51+
// start OpenSergoClient
5452
err = openSergoClient.Start()
5553
if err != nil {
5654
return err

0 commit comments

Comments
 (0)