增加基于redis的事件销毁逻辑
This commit is contained in:
parent
0eeb2ef42e
commit
87b2742e36
@ -8,8 +8,10 @@
|
|||||||
package define
|
package define
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultRedisPartitionNum = 1
|
DefaultRedisPartitionNum = 1
|
||||||
DefaultRedisTopic = "EVENT_TOPIC_C6DBE0AAE846C5C0DE35802107326B1E"
|
DefaultRedisTopic = "EVENT_TOPIC_C6DBE0AAE846C5C0DE35802107326B1E"
|
||||||
|
DefaultRedisMessageBufferSize = 1024
|
||||||
|
DefaultRedisCloseMaxWaitTime = 5000 // 默认最大等待 : 5s
|
||||||
)
|
)
|
||||||
|
|
||||||
// RedisEventPubSubConfig redis事件配置
|
// RedisEventPubSubConfig redis事件配置
|
||||||
@ -18,6 +20,8 @@ const (
|
|||||||
//
|
//
|
||||||
// Date : 16:25 2024/6/25
|
// Date : 16:25 2024/6/25
|
||||||
type RedisEventPubSubConfig struct {
|
type RedisEventPubSubConfig struct {
|
||||||
Topic string `json:"topic"` // topic key, 不指定随机生成
|
Topic string `json:"topic"` // topic key, 不指定随机生成
|
||||||
PartitionNum int `json:"partition_num"` // 多少个分区, 默认值 : 1
|
PartitionNum int `json:"partition_num"` // 多少个分区, 默认值 : 1
|
||||||
|
MessageBufferSize int `json:"message_buffer_size"` // 消息缓冲区大小
|
||||||
|
CloseMaxWaitTime int `json:"close_max_wait_time"` // 关闭消息实例, 最大等待时长, 单位 : ms
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,11 @@ package event
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"git.zhangdeman.cn/zhangdeman/event/abstract"
|
"git.zhangdeman.cn/zhangdeman/event/abstract"
|
||||||
"git.zhangdeman.cn/zhangdeman/event/define"
|
"git.zhangdeman.cn/zhangdeman/event/define"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -40,6 +42,8 @@ func InitRedisPubSubEvent(redisClient *redis.Client, pubSubConfig *define.RedisE
|
|||||||
type RedisEventPubSub struct {
|
type RedisEventPubSub struct {
|
||||||
redisClient *redis.Client // redis客户端
|
redisClient *redis.Client // redis客户端
|
||||||
pubSubConfig *define.RedisEventPubSubConfig // 事件配置
|
pubSubConfig *define.RedisEventPubSubConfig // 事件配置
|
||||||
|
messageChan chan *define.EventData // 消息队列
|
||||||
|
stopConsumer chan bool // 停止消费者
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisEventPubSub) SendEvent(ctx context.Context, eventData *define.EventData) (*define.SendResult, error) {
|
func (r *RedisEventPubSub) SendEvent(ctx context.Context, eventData *define.EventData) (*define.SendResult, error) {
|
||||||
@ -77,14 +81,34 @@ func (r *RedisEventPubSub) ConsumeSuccessCallback(eventData *define.EventData) {
|
|||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Destroy 销毁事件实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 15:42 2024/6/26
|
||||||
func (r *RedisEventPubSub) Destroy() {
|
func (r *RedisEventPubSub) Destroy() {
|
||||||
//TODO implement me
|
r.stopConsumer <- true // 停止消费者
|
||||||
panic("implement me")
|
messageChan := make(chan bool, 1)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
if len(r.messageChan) == 0 {
|
||||||
|
messageChan <- true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-time.After(time.Millisecond * time.Duration(r.pubSubConfig.CloseMaxWaitTime)):
|
||||||
|
// 定时器到期
|
||||||
|
return
|
||||||
|
case <-messageChan:
|
||||||
|
// 没有待消费数据
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisEventPubSub) DriverType() string {
|
func (r *RedisEventPubSub) DriverType() string {
|
||||||
//TODO implement me
|
return consts.EventDriverRedis
|
||||||
panic("implement me")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRedisClient 设置redis客户端
|
// SetRedisClient 设置redis客户端
|
||||||
@ -95,8 +119,9 @@ func (r *RedisEventPubSub) DriverType() string {
|
|||||||
func (r *RedisEventPubSub) SetRedisClient(redisClient *redis.Client, pubSubConfig *define.RedisEventPubSubConfig) {
|
func (r *RedisEventPubSub) SetRedisClient(redisClient *redis.Client, pubSubConfig *define.RedisEventPubSubConfig) {
|
||||||
if nil == pubSubConfig {
|
if nil == pubSubConfig {
|
||||||
pubSubConfig = &define.RedisEventPubSubConfig{
|
pubSubConfig = &define.RedisEventPubSubConfig{
|
||||||
Topic: "",
|
Topic: "",
|
||||||
PartitionNum: 0,
|
PartitionNum: 0,
|
||||||
|
MessageBufferSize: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(pubSubConfig.Topic) == 0 {
|
if len(pubSubConfig.Topic) == 0 {
|
||||||
@ -105,6 +130,14 @@ func (r *RedisEventPubSub) SetRedisClient(redisClient *redis.Client, pubSubConfi
|
|||||||
if pubSubConfig.PartitionNum <= 0 {
|
if pubSubConfig.PartitionNum <= 0 {
|
||||||
pubSubConfig.PartitionNum = define.DefaultRedisPartitionNum
|
pubSubConfig.PartitionNum = define.DefaultRedisPartitionNum
|
||||||
}
|
}
|
||||||
|
if pubSubConfig.MessageBufferSize <= 0 {
|
||||||
|
r.pubSubConfig.MessageBufferSize = define.DefaultRedisMessageBufferSize
|
||||||
|
}
|
||||||
|
if pubSubConfig.CloseMaxWaitTime <= 0 {
|
||||||
|
r.pubSubConfig.CloseMaxWaitTime = define.DefaultRedisCloseMaxWaitTime
|
||||||
|
}
|
||||||
r.redisClient = redisClient
|
r.redisClient = redisClient
|
||||||
r.pubSubConfig = pubSubConfig
|
r.pubSubConfig = pubSubConfig
|
||||||
|
r.stopConsumer = make(chan bool, 1)
|
||||||
|
r.messageChan = make(chan *define.EventData, r.pubSubConfig.MessageBufferSize)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user