Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Jan 15, 2024
1 parent 396c7a6 commit 539c145
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 61 deletions.
52 changes: 5 additions & 47 deletions redis/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ package redis

import (
"context"
"crypto/tls"
"fmt"
"net"
"time"

"github.com/bytedance/sonic"
"github.com/cloudwego/hertz/pkg/app/server/registry"
"github.com/redis/go-redis/v9"
)

const (
Expand All @@ -34,50 +31,11 @@ const (
)

const (
defaultExpireTime = 60
defaultTickerTime = time.Second * 30
defaultKeepAliveTime = time.Second * 60
defaultWeight = 10
defaultExpireTime = 60
defaultRefreshInterval = 30
defaultWeight = 10
)

type Option func(opts *redis.Options)

func WithPassword(password string) Option {
return func(opts *redis.Options) {
opts.Password = password
}
}

func WithDB(db int) Option {
return func(opts *redis.Options) {
opts.DB = db
}
}

func WithTLSConfig(t *tls.Config) Option {
return func(opts *redis.Options) {
opts.TLSConfig = t
}
}

func WithDialer(dialer func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(opts *redis.Options) {
opts.Dialer = dialer
}
}

func WithReadTimeout(t time.Duration) Option {
return func(opts *redis.Options) {
opts.ReadTimeout = t
}
}

func WithWriteTimeout(t time.Duration) Option {
return func(opts *redis.Options) {
opts.WriteTimeout = t
}
}

type registryHash struct {
key string
field string
Expand Down Expand Up @@ -130,12 +88,12 @@ func convertInfo(info *registry.Info) *registryInfo {
}

func keepAlive(ctx context.Context, hash *registryHash, r *redisRegistry) {
ticker := time.NewTicker(defaultTickerTime)
ticker := time.NewTicker(time.Duration(r.options.refreshInterval) * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
r.client.Expire(ctx, hash.key, defaultKeepAliveTime)
r.client.Expire(ctx, hash.key, time.Duration(r.options.expireTime)*time.Second)
case <-ctx.Done():
break
}
Expand Down
86 changes: 86 additions & 0 deletions redis/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2023 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package redis

import (
"context"
"crypto/tls"
"net"
"time"

"github.com/redis/go-redis/v9"
)

type Option func(opts *Options)

type Options struct {
*redis.Options
expireTime int
refreshInterval int
}

// WithExpireTime redis key expiration time in seconds
// NOTE: expiration time must be greater than refresh interval
// Default: 60s
func WithExpireTime(time int) Option {
return func(opts *Options) {
opts.expireTime = time
}
}

// WithRefreshInterval redis key refresh interval in seconds
// NOTE: refresh interval must be less than expiration time
// Default: 30s
func WithRefreshInterval(interval int) Option {
return func(opts *Options) {
opts.refreshInterval = interval
}
}

func WithPassword(password string) Option {
return func(opts *Options) {
opts.Password = password
}
}

func WithDB(db int) Option {
return func(opts *Options) {
opts.DB = db
}
}

func WithTLSConfig(t *tls.Config) Option {
return func(opts *Options) {
opts.TLSConfig = t
}
}

func WithDialer(dialer func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(opts *Options) {
opts.Dialer = dialer
}
}

func WithReadTimeout(t time.Duration) Option {
return func(opts *Options) {
opts.ReadTimeout = t
}
}

func WithWriteTimeout(t time.Duration) Option {
return func(opts *Options) {
opts.WriteTimeout = t
}
}
28 changes: 17 additions & 11 deletions redis/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
var _ registry.Registry = (*redisRegistry)(nil)

type redisRegistry struct {
client *redis.Client
rctx *registryContext
mu sync.Mutex
mu sync.Mutex
options *Options
client *redis.Client
rctx *registryContext
}

type registryContext struct {
Expand All @@ -39,17 +40,22 @@ type registryContext struct {

// NewRedisRegistry creates a redis registry
func NewRedisRegistry(addr string, opts ...Option) registry.Registry {
redisOpts := &redis.Options{
Addr: addr,
Password: "",
DB: 0,
options := &Options{
Options: &redis.Options{
Addr: addr,
Password: "",
DB: 0,
},
expireTime: defaultExpireTime,
refreshInterval: defaultRefreshInterval,
}
for _, opt := range opts {
opt(redisOpts)
opt(options)
}
rdb := redis.NewClient(redisOpts)
rdb := redis.NewClient(options.Options)
return &redisRegistry{
client: rdb,
options: options,
client: rdb,
}
}

Expand Down Expand Up @@ -77,7 +83,7 @@ func (r *redisRegistry) Register(info *registry.Info) error {
args := []interface{}{
hash.field,
hash.value,
defaultExpireTime,
r.options.expireTime,
}

err = registerScript.Run(rctx.ctx, rdb, keys, args).Err()
Expand Down
10 changes: 7 additions & 3 deletions redis/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ type redisResolver struct {

// NewRedisResolver creates a redis resolver
func NewRedisResolver(addr string, opts ...Option) discovery.Resolver {
redisOpts := &redis.Options{Addr: addr}
options := &Options{
Options: &redis.Options{
Addr: addr,
},
}
for _, opt := range opts {
opt(redisOpts)
opt(options)
}
rdb := redis.NewClient(redisOpts)
rdb := redis.NewClient(options.Options)
return &redisResolver{
client: rdb,
}
Expand Down

0 comments on commit 539c145

Please sign in to comment.