Skip to content

Commit

Permalink
refactor: refine client opts
Browse files Browse the repository at this point in the history
  • Loading branch information
ifooth committed Dec 15, 2023
1 parent b1585aa commit fc1252f
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 111 deletions.
54 changes: 27 additions & 27 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ type Client interface {
// Client is the bscp client
type client struct {
pairs map[string]string
opts ClientOptions
opts Options
fingerPrint sfs.FingerPrint
watcher *watch.Watcher
upstream upstream.Upstream
}

// New return a bscp client instance
func New(opts ...ClientOption) (Client, error) {
clientOpt := &ClientOptions{}
func New(opts ...Option) (Client, error) {
clientOpt := &Options{}
fp, err := sfs.GetFingerPrint()
if err != nil {
return nil, fmt.Errorf("get instance fingerprint failed, err: %s", err.Error())
}
logger.Info("instance fingerprint", slog.String("fingerprint", fp.Encode()))
clientOpt.Fingerprint = fp.Encode()
clientOpt.UID = clientOpt.Fingerprint
clientOpt.fingerprint = fp.Encode()
clientOpt.uid = clientOpt.fingerprint
for _, opt := range opts {
if e := opt(clientOpt); e != nil {
return nil, e
Expand All @@ -79,8 +79,8 @@ func New(opts ...ClientOption) (Client, error) {
pairs[constant.SideUserKey] = "TODO-USER"
// add finger printer
mh := sfs.SidecarMetaHeader{
BizID: clientOpt.BizID,
Fingerprint: clientOpt.Fingerprint,
BizID: clientOpt.bizID,
Fingerprint: clientOpt.fingerprint,
}
mhBytes, err := json.Marshal(mh)
if err != nil {
Expand All @@ -89,9 +89,9 @@ func New(opts ...ClientOption) (Client, error) {
pairs[constant.SidecarMetaKey] = string(mhBytes)
// prepare upstream
u, err := upstream.New(
upstream.WithFeedAddrs(clientOpt.FeedAddrs),
upstream.WithDialTimeoutMS(clientOpt.DialTimeoutMS),
upstream.WithBizID(clientOpt.BizID))
upstream.WithFeedAddrs(clientOpt.feedAddrs),
upstream.WithDialTimeoutMS(clientOpt.dialTimeoutMS),
upstream.WithBizID(clientOpt.bizID))
if err != nil {
return nil, fmt.Errorf("init upstream client failed, err: %s", err.Error())
}
Expand All @@ -106,7 +106,7 @@ func New(opts ...ClientOption) (Client, error) {
msg := &pbfs.HandshakeMessage{
ApiVersion: sfs.CurrentAPIVersion,
Spec: &pbfs.SidecarSpec{
BizId: clientOpt.BizID,
BizId: clientOpt.bizID,
Version: c.upstream.Version(),
},
}
Expand All @@ -119,16 +119,16 @@ func New(opts ...ClientOption) (Client, error) {
if err != nil {
return nil, fmt.Errorf("decode handshake payload failed, err: %s, rid: %s", err.Error(), vas.Rid)
}
err = downloader.Init(vas, clientOpt.BizID, clientOpt.Token, u, pl.RuntimeOption.RepositoryTLS)
err = downloader.Init(vas, clientOpt.bizID, clientOpt.token, u, pl.RuntimeOption.RepositoryTLS)
if err != nil {
return nil, fmt.Errorf("init downloader failed, err: %s", err.Error())
}
if clientOpt.UseFileCache {
cache.Init(true, clientOpt.FileCacheDir)
if clientOpt.useFileCache {
cache.Init(true, clientOpt.fileCacheDir)
}
watcher, err := watch.New(u, watch.WatchOptions{
BizID: clientOpt.BizID,
Labels: clientOpt.Labels,
BizID: clientOpt.bizID,
Labels: clientOpt.labels,
Fingerprint: fp.Encode(),
})
if err != nil {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (c *client) StopWatch() {

// ResetLabels reset bscp client labels, if key conflict, app value will overwrite client value
func (c *client) ResetLabels(labels map[string]string) {
c.opts.Labels = labels
c.opts.labels = labels
for _, subscriber := range c.watcher.Subscribers() {
subscriber.ResetLabels(labels)
}
Expand All @@ -173,17 +173,17 @@ func (c *client) PullFiles(app string, opts ...types.AppOption) (*types.Release,
vas, _ := c.buildVas()
req := &pbfs.PullAppFileMetaReq{
ApiVersion: sfs.CurrentAPIVersion,
BizId: c.opts.BizID,
BizId: c.opts.bizID,
AppMeta: &pbfs.AppMeta{
App: app,
Labels: c.opts.Labels,
Uid: c.opts.UID,
Labels: c.opts.labels,
Uid: c.opts.uid,
},
Token: c.opts.Token,
Token: c.opts.token,
Key: option.Key,
}
// merge labels, if key conflict, app value will overwrite client value
req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels)
req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels)
// reset uid
if option.UID != "" {
req.AppMeta.Uid = option.UID
Expand Down Expand Up @@ -227,16 +227,16 @@ func (c *client) Get(app string, key string, opts ...types.AppOption) (string, e
vas, _ := c.buildVas()
req := &pbfs.GetKvValueReq{
ApiVersion: sfs.CurrentAPIVersion,
BizId: c.opts.BizID,
BizId: c.opts.bizID,
AppMeta: &pbfs.AppMeta{
App: app,
Labels: c.opts.Labels,
Uid: c.opts.UID,
Labels: c.opts.labels,
Uid: c.opts.uid,
},
Token: c.opts.Token,
Token: c.opts.token,
Key: key,
}
req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels)
req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels)
// reset uid
if option.UID != "" {
req.AppMeta.Uid = option.UID
Expand Down
93 changes: 38 additions & 55 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,94 +12,77 @@

package client

// ClientOptions options for bscp sdk client
type ClientOptions struct {
// Options options for bscp sdk
type Options struct {
// FeedAddr BSCP feed_server address
FeedAddrs []string
feedAddrs []string
// BizID BSCP business id
BizID uint32
bizID uint32
// Labels instance labels
Labels map[string]string
// Version SDK version
Version string
labels map[string]string
// Fingerprint sdk fingerprint
Fingerprint string
fingerprint string
// UID sdk uid
UID string
uid string
// UseFileCache use file cache
UseFileCache bool
useFileCache bool
// FileCacheDir file cache directory
FileCacheDir string
fileCacheDir string
// DialTimeoutMS dial upstream timeout in millisecond
DialTimeoutMS int64
dialTimeoutMS int64
// Token sdk token
Token string
token string
}

// ClientOption setter for bscp sdk options
type ClientOption func(*ClientOptions) error
// Option setter for bscp sdk options
type Option func(*Options) error

// FeedAddrs set feed_server addresses
func FeedAddrs(addrs []string) ClientOption {
// WithFeedAddrs set feed_server addresses
func WithFeedAddrs(addrs []string) Option {
// TODO: validate Address
return func(o *ClientOptions) error {
o.FeedAddrs = addrs
return func(o *Options) error {
o.feedAddrs = addrs
return nil
}
}

// BizID set bscp business id
func BizID(id uint32) ClientOption {
return func(o *ClientOptions) error {
o.BizID = id
return nil
}
}

// Labels set instance labels
func Labels(labels map[string]string) ClientOption {
return func(o *ClientOptions) error {
o.Labels = labels
return nil
}
}

// UID set sdk uid
func UID(uid string) ClientOption {
return func(o *ClientOptions) error {
o.UID = uid
// WithFeedAddr set feed_server addresse
func WithFeedAddr(addr string) Option {
// TODO: validate Address
return func(o *Options) error {
o.feedAddrs = []string{addr}
return nil
}
}

// UseFileCache cache file to local file system
func UseFileCache(useFileCache bool) ClientOption {
return func(o *ClientOptions) error {
o.UseFileCache = useFileCache
// WithBizID set bscp business id
func WithBizID(id uint32) Option {
return func(o *Options) error {
o.bizID = id
return nil
}
}

// FileCacheDir file local cache directory
func FileCacheDir(dir string) ClientOption {
return func(o *ClientOptions) error {
o.FileCacheDir = dir
// WithLabels set instance labels
func WithLabels(labels map[string]string) Option {
return func(o *Options) error {
o.labels = labels
return nil
}
}

// WithDialTimeoutMS set dial timeout in millisecond
func WithDialTimeoutMS(timeout int64) ClientOption {
return func(o *ClientOptions) error {
o.DialTimeoutMS = timeout
// WithUID set sdk uid
func WithUID(uid string) Option {
return func(o *Options) error {
o.uid = uid
return nil
}
}

// Token set sdk token
func Token(token string) ClientOption {
return func(o *ClientOptions) error {
o.Token = token
// WithToken set sdk token
func WithToken(token string) Option {
return func(o *Options) error {
o.token = token
return nil
}
}
10 changes: 5 additions & 5 deletions cmd/bscp/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func Pull(cmd *cobra.Command, args []string) {
conf.Labels = pkgutil.MergeLabels(conf.Labels, labels)
}
bscp, err := client.New(
client.FeedAddrs(conf.FeedAddrs),
client.BizID(conf.Biz),
client.Token(conf.Token),
client.Labels(conf.Labels),
client.UID(conf.UID),
client.WithFeedAddrs(conf.FeedAddrs),
client.WithBizID(conf.Biz),
client.WithToken(conf.Token),
client.WithLabels(conf.Labels),
client.WithUID(conf.UID),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
Expand Down
10 changes: 5 additions & 5 deletions cmd/bscp/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func Watch(cmd *cobra.Command, args []string) {
}

bscp, err := client.New(
client.FeedAddrs(conf.FeedAddrs),
client.BizID(conf.Biz),
client.Token(conf.Token),
client.Labels(confLabels),
client.UID(conf.UID),
client.WithFeedAddrs(conf.FeedAddrs),
client.WithBizID(conf.Biz),
client.WithToken(conf.Token),
client.WithLabels(confLabels),
client.WithUID(conf.UID),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
Expand Down
8 changes: 4 additions & 4 deletions examples/kv-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func execute() {
}

bscp, err := client.New(
client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.BizID(uint32(biz)),
client.Token(os.Getenv("BSCP_TOKEN")),
client.Labels(labels),
client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.WithBizID(uint32(biz)),
client.WithToken(os.Getenv("BSCP_TOKEN")),
client.WithLabels(labels),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
Expand Down
7 changes: 3 additions & 4 deletions examples/pull-file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main
import (
"os"
"strconv"
"strings"

"golang.org/x/exp/slog"

Expand All @@ -38,9 +37,9 @@ func main() {
}

bscp, err := client.New(
client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.BizID(uint32(biz)),
client.Token(os.Getenv("BSCP_TOKEN")),
client.WithFeedAddr(os.Getenv("BSCP_FEED_ADDRS")),
client.WithBizID(uint32(biz)),
client.WithToken(os.Getenv("BSCP_TOKEN")),
)
if err != nil {
slog.Error("init client", logger.ErrAttr(err))
Expand Down
8 changes: 4 additions & 4 deletions examples/pull-kv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func main() {
}

bscp, err := client.New(
client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.BizID(uint32(biz)),
client.Token(os.Getenv("BSCP_TOKEN")),
client.Labels(labels),
client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.WithBizID(uint32(biz)),
client.WithToken(os.Getenv("BSCP_TOKEN")),
client.WithLabels(labels),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
Expand Down
6 changes: 3 additions & 3 deletions examples/watch-file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func main() {
}

bscp, err := client.New(
client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.BizID(uint32(biz)),
client.Token(os.Getenv("BSCP_TOKEN")),
client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.WithBizID(uint32(biz)),
client.WithToken(os.Getenv("BSCP_TOKEN")),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
Expand Down
8 changes: 4 additions & 4 deletions examples/watch-kv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func main() {
}

bscp, err := client.New(
client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.BizID(uint32(biz)),
client.Token(os.Getenv("BSCP_TOKEN")),
client.Labels(labels),
client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")),
client.WithBizID(uint32(biz)),
client.WithToken(os.Getenv("BSCP_TOKEN")),
client.WithLabels(labels),
)
if err != nil {
logger.Error("init bscp client", logger.ErrAttr(err))
Expand Down

0 comments on commit fc1252f

Please sign in to comment.