master
kevin 4 years ago
parent aa3ef20011
commit 6ac5a80f9a

@ -1,4 +1,4 @@
package redisqueue package internal
import ( import (
"zero/core/queue" "zero/core/queue"

@ -1,4 +1,4 @@
package constant package internal
const ( const (
Delimeter = "/" Delimeter = "/"

@ -1,4 +1,4 @@
package rq package internal
import ( import (
"math/rand" "math/rand"

@ -1,4 +1,4 @@
package redisqueue package internal
type TimedMessage struct { type TimedMessage struct {
Time int64 `json:"time"` Time int64 `json:"time"`

@ -1,4 +1,4 @@
package redisqueue package internal
import ( import (
"strconv" "strconv"

@ -1,4 +1,4 @@
package redisqueue package internal
import ( import (
"fmt" "fmt"

@ -1,4 +1,4 @@
package redisqueue package internal
import ( import (
"fmt" "fmt"

@ -9,7 +9,7 @@ import (
"zero/core/hash" "zero/core/hash"
"zero/core/jsonx" "zero/core/jsonx"
"zero/rq/constant" "zero/rq/internal"
) )
var ErrInvalidServerChange = errors.New("not a server change message") var ErrInvalidServerChange = errors.New("not a server change message")
@ -82,7 +82,7 @@ func (sc ServerChange) GetCode() string {
} }
func IsServerChange(message string) bool { func IsServerChange(message string) bool {
return len(message) > 0 && message[0] == constant.ServerSensitivePrefix return len(message) > 0 && message[0] == internal.ServerSensitivePrefix
} }
func (sc ServerChange) Marshal() (string, error) { func (sc ServerChange) Marshal() (string, error) {
@ -91,7 +91,7 @@ func (sc ServerChange) Marshal() (string, error) {
return "", err return "", err
} }
return string(append([]byte{constant.ServerSensitivePrefix}, body...)), nil return string(append([]byte{internal.ServerSensitivePrefix}, body...)), nil
} }
func UnmarshalServerChange(body string) (ServerChange, error) { func UnmarshalServerChange(body string) (ServerChange, error) {

@ -14,11 +14,10 @@ import (
"zero/core/lang" "zero/core/lang"
"zero/core/logx" "zero/core/logx"
"zero/core/queue" "zero/core/queue"
"zero/core/redisqueue"
"zero/core/stores/redis" "zero/core/stores/redis"
"zero/core/threading" "zero/core/threading"
"zero/rq/constant" "zero/rq/internal"
"zero/rq/update" "zero/rq/internal/update"
) )
const ( const (
@ -155,7 +154,7 @@ func (pusher *Pusher) failover(server string) error {
return return
} }
if option == constant.TimedQueueType { if option == internal.TimedQueueType {
message, err = unwrapTimedMessage(message) message, err = unwrapTimedMessage(message)
if err != nil { if err != nil {
logx.Errorf("invalid timedMessage: %s, error: %s", message, err.Error()) logx.Errorf("invalid timedMessage: %s, error: %s", message, err.Error())
@ -179,11 +178,11 @@ func UnmarshalPusher(server string) (queue.QueuePusher, error) {
return nil, err return nil, err
} }
if option == constant.TimedQueueType { if option == internal.TimedQueueType {
return redisqueue.NewPusher(store, key, redisqueue.WithTime()), nil return internal.NewPusher(store, key, internal.WithTime()), nil
} }
return redisqueue.NewPusher(store, key), nil return internal.NewPusher(store, key), nil
} }
func WithBatchConsistentStrategy(keysFn KeysFn, assembleFn AssembleFn, opts ...discov.BalanceOption) PusherOption { func WithBatchConsistentStrategy(keysFn KeysFn, assembleFn AssembleFn, opts ...discov.BalanceOption) PusherOption {
@ -375,15 +374,15 @@ func getName(key string) string {
func newPusher(server string) (queue.QueuePusher, error) { func newPusher(server string) (queue.QueuePusher, error) {
if rds, key, option, err := newRedisWithKey(server); err != nil { if rds, key, option, err := newRedisWithKey(server); err != nil {
return nil, err return nil, err
} else if option == constant.TimedQueueType { } else if option == internal.TimedQueueType {
return redisqueue.NewPusher(rds, key, redisqueue.WithTime()), nil return internal.NewPusher(rds, key, internal.WithTime()), nil
} else { } else {
return redisqueue.NewPusher(rds, key), nil return internal.NewPusher(rds, key), nil
} }
} }
func newRedisWithKey(server string) (rds *redis.Redis, key, option string, err error) { func newRedisWithKey(server string) (rds *redis.Redis, key, option string, err error) {
fields := strings.Split(server, constant.Delimeter) fields := strings.Split(server, internal.Delimeter)
if len(fields) < etcdRedisFields { if len(fields) < etcdRedisFields {
err = fmt.Errorf("wrong redis queue: %s, should be ip:port/type/password/key/[option]", server) err = fmt.Errorf("wrong redis queue: %s, should be ip:port/type/password/key/[option]", server)
return return
@ -437,7 +436,7 @@ func broadcast(servers []string, message string) error {
} }
func unwrapTimedMessage(message string) (string, error) { func unwrapTimedMessage(message string) (string, error) {
var tm redisqueue.TimedMessage var tm internal.TimedMessage
if err := jsonx.UnmarshalFromString(message, &tm); err != nil { if err := jsonx.UnmarshalFromString(message, &tm); err != nil {
return "", err return "", err
} }

@ -11,13 +11,12 @@ import (
"zero/core/discov" "zero/core/discov"
"zero/core/logx" "zero/core/logx"
"zero/core/queue" "zero/core/queue"
"zero/core/redisqueue"
"zero/core/service" "zero/core/service"
"zero/core/stores/redis" "zero/core/stores/redis"
"zero/core/stringx" "zero/core/stringx"
"zero/core/threading" "zero/core/threading"
"zero/rq/constant" "zero/rq/internal"
"zero/rq/update" "zero/rq/internal/update"
) )
const keyLen = 6 const keyLen = 6
@ -107,8 +106,8 @@ func (q *MessageQueue) Stop() {
func (q *MessageQueue) buildQueue() *queue.Queue { func (q *MessageQueue) buildQueue() *queue.Queue {
inboundStore := redis.NewRedis(q.c.Redis.Host, q.c.Redis.Type, q.c.Redis.Pass) inboundStore := redis.NewRedis(q.c.Redis.Host, q.c.Redis.Type, q.c.Redis.Pass)
producerFactory := redisqueue.NewProducerFactory(inboundStore, q.c.Redis.Key, producerFactory := internal.NewProducerFactory(inboundStore, q.c.Redis.Key,
redisqueue.TimeSensitive(q.c.DropBefore)) internal.TimeSensitive(q.c.DropBefore))
mq := queue.NewQueue(producerFactory, q.consumerFactory) mq := queue.NewQueue(producerFactory, q.consumerFactory)
if len(q.c.Name) > 0 { if len(q.c.Name) > 0 {
@ -140,7 +139,7 @@ func (q *MessageQueue) maybeAppendRenewer(group *service.ServiceGroup, mq *queue
if len(q.c.Etcd.Hosts) > 0 || len(q.c.Etcd.Key) > 0 { if len(q.c.Etcd.Hosts) > 0 || len(q.c.Etcd.Key) > 0 {
etcdValue := MarshalQueue(q.c.Redis) etcdValue := MarshalQueue(q.c.Redis)
if q.c.DropBefore > 0 { if q.c.DropBefore > 0 {
etcdValue = strings.Join([]string{etcdValue, constant.TimedQueueType}, constant.Delimeter) etcdValue = strings.Join([]string{etcdValue, internal.TimedQueueType}, internal.Delimeter)
} }
keepAliver := discov.NewRenewer(q.c.Etcd.Hosts, q.c.Etcd.Key, etcdValue, q.options.renewId) keepAliver := discov.NewRenewer(q.c.Etcd.Hosts, q.c.Etcd.Key, etcdValue, q.options.renewId)
mq.AddListener(pauseResumeHandler{ mq.AddListener(pauseResumeHandler{
@ -156,7 +155,7 @@ func MarshalQueue(rds redis.RedisKeyConf) string {
rds.Type, rds.Type,
rds.Pass, rds.Pass,
rds.Key, rds.Key,
}, constant.Delimeter) }, internal.Delimeter)
} }
func WithHandle(handle ConsumeHandle) queue.ConsumerFactory { func WithHandle(handle ConsumeHandle) queue.ConsumerFactory {
@ -251,7 +250,7 @@ func (c *serverSensitiveConsumer) Consume(msg string) error {
oldHash := change.CreatePrevHash() oldHash := change.CreatePrevHash()
newHash := change.CreateCurrentHash() newHash := change.CreateCurrentHash()
hashChange := NewHashChange(oldHash, newHash) hashChange := internal.NewHashChange(oldHash, newHash)
c.mq.redisQueue.Broadcast(hashChange) c.mq.redisQueue.Broadcast(hashChange)
return nil return nil

Loading…
Cancel
Save