chore: 初始开源发布
iotlcm-app:小程序端 + 用户端服务。 - app/:uni-app 小程序前端(基于 unibest,MIT) - 微信 AppID → YOUR_WX_APPID,uni-app AppID → __UNI__YOURAPPID - 内网后端地址 → admin.iotlcm.example.com - 应用标题与 unibest 模板 README 重写(保留上游致谢与 MIT 声明) - server/:GoFrame 用户端服务(基于 XiuAdmin,Apache-2.0) - module xiuadmin → git.5iot.com/iotlcm-public/iotlcm-app(360 文件) - 支付回调地址 → app.iotlcm.example.com,商户名与应用名泛化 - 真实手机号 → 合法 11 位编造号(保留号段前 3 位 + 后 4 位) - 依赖号码尾部的断言同步更新 - 补 server/LICENSE(Apache-2.0) - 移除内部资产:CI 触发器 - 补根 README,LF 行尾 + .gitattributes 已知:TestSysDept_RefreshDeptAncestors 在源仓同一提交上同样失败,属既有缺陷。
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var ErrNotFoundTopic = errors.New("not found topic")
|
||||
|
||||
type Payload struct {
|
||||
Ts int64 `json:"ts"` // 毫秒时间戳
|
||||
TraceId string `json:"trace_id"` // 链路ID
|
||||
Topic string `json:"topic"` // 消费主题 队列名
|
||||
Data json.RawMessage `json:"data"` // 数据
|
||||
Expire int64 `json:"expire"` // 过期时长毫秒 0:不过期 业务内自行处理
|
||||
ClientId string `json:"client_id"` // 客户端ID
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
RedisName string `json:"redis_name"` // redis名称
|
||||
Workers int `json:"workers"` // 工作线程数
|
||||
ClientId string `json:"client_id"` // 客户端ID
|
||||
}
|
||||
|
||||
type Process interface {
|
||||
GetTopic() string // 获取消费主题
|
||||
GetType() QueueType // 获取类型
|
||||
Handle(ctx context.Context, p Payload) (err error) // 处理过程的方法
|
||||
}
|
||||
|
||||
type QueueType string
|
||||
|
||||
const (
|
||||
QueueTypeMemory QueueType = "memory_queue"
|
||||
QueueTypeRedis QueueType = "redis_queue"
|
||||
QueueTypeRedisBroadcast QueueType = "redis_broadcast"
|
||||
)
|
||||
|
||||
type sProducer interface {
|
||||
GetType() QueueType // 获取类型
|
||||
GetTopic() string // 获取主题
|
||||
Push(ctx context.Context, topic string, data interface{}, expire int64) (err error) // 推送消息
|
||||
}
|
||||
|
||||
type sConsumer interface {
|
||||
GetType() QueueType
|
||||
GetTopic() string // 获取消费主题
|
||||
Handle(ctx context.Context) (err error) // 处理过程的方法
|
||||
}
|
||||
|
||||
var (
|
||||
rwLock sync.RWMutex
|
||||
producer = make(map[string]sProducer)
|
||||
consumer = make(map[string]sConsumer)
|
||||
)
|
||||
|
||||
func NewProducer(ctx context.Context, t QueueType, topic string, config *Config) error {
|
||||
if config == nil {
|
||||
config = &Config{}
|
||||
}
|
||||
if _, ok := producer[topic]; ok {
|
||||
return errors.New("producer already exists")
|
||||
}
|
||||
var p sProducer
|
||||
switch t {
|
||||
case QueueTypeMemory:
|
||||
p = RegisterMemoryQueue(ctx, topic, nil, config)
|
||||
case QueueTypeRedis:
|
||||
p = RegisterRedisQueue(ctx, topic, nil, config)
|
||||
case QueueTypeRedisBroadcast:
|
||||
p = RegisterRedisBroadcast(ctx, topic, nil, config)
|
||||
}
|
||||
rwLock.Lock()
|
||||
producer[topic] = p
|
||||
rwLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewConsumer(ctx context.Context, p Process, config *Config) error {
|
||||
if p == nil {
|
||||
return errors.New("process is nil")
|
||||
}
|
||||
if config == nil {
|
||||
config = &Config{}
|
||||
}
|
||||
if _, ok := consumer[p.GetTopic()+"__"+config.ClientId]; ok {
|
||||
return errors.New("consumer already exists")
|
||||
}
|
||||
var c sConsumer
|
||||
switch p.GetType() {
|
||||
case QueueTypeMemory:
|
||||
c = RegisterMemoryQueue(ctx, p.GetTopic(), p, config)
|
||||
case QueueTypeRedis:
|
||||
c = RegisterRedisQueue(ctx, p.GetTopic(), p, config)
|
||||
case QueueTypeRedisBroadcast:
|
||||
c = RegisterRedisBroadcast(ctx, p.GetTopic(), p, config)
|
||||
}
|
||||
rwLock.Lock()
|
||||
consumer[p.GetTopic()+"__"+config.ClientId] = c
|
||||
rwLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnregisterProducer(ctx context.Context, topic string) error {
|
||||
rwLock.Lock()
|
||||
defer rwLock.Unlock()
|
||||
p, ok := producer[topic]
|
||||
if !ok {
|
||||
return errors.New("producer not found")
|
||||
}
|
||||
switch p.GetType() {
|
||||
case QueueTypeMemory:
|
||||
UnregisterMemoryQueue(ctx, topic)
|
||||
case QueueTypeRedis:
|
||||
UnregisterRedisQueue(ctx, topic)
|
||||
case QueueTypeRedisBroadcast:
|
||||
UnregisterRedisBroadcast(ctx, topic, "")
|
||||
}
|
||||
delete(producer, topic)
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnregisterConsumer(ctx context.Context, topic string, groupId string) error {
|
||||
rwLock.Lock()
|
||||
defer rwLock.Unlock()
|
||||
c, ok := consumer[topic+"__"+groupId]
|
||||
if !ok {
|
||||
return ErrNotFoundTopic
|
||||
}
|
||||
switch c.GetType() {
|
||||
case QueueTypeMemory:
|
||||
UnregisterMemoryQueue(ctx, topic)
|
||||
case QueueTypeRedis:
|
||||
UnregisterRedisQueue(ctx, topic)
|
||||
case QueueTypeRedisBroadcast:
|
||||
UnregisterRedisBroadcast(ctx, topic, groupId)
|
||||
}
|
||||
delete(consumer, topic+"__"+groupId)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ProducerTopicExists(topic string) bool {
|
||||
rwLock.RLock()
|
||||
_, ok := producer[topic]
|
||||
rwLock.RUnlock()
|
||||
return ok
|
||||
}
|
||||
|
||||
func ConsumerTopicExists(topic string) bool {
|
||||
rwLock.RLock()
|
||||
_, ok := consumer[topic]
|
||||
rwLock.RUnlock()
|
||||
return ok
|
||||
}
|
||||
|
||||
func Push(ctx context.Context, topic string, data interface{}) (err error) {
|
||||
rwLock.RLock()
|
||||
p, ok := producer[topic]
|
||||
rwLock.RUnlock()
|
||||
if !ok {
|
||||
g.Log().Errorf(ctx, "topic not found. topic: %s", topic)
|
||||
return ErrNotFoundTopic
|
||||
}
|
||||
return p.Push(ctx, topic, data, 0)
|
||||
}
|
||||
|
||||
func PushWithExpire(ctx context.Context, topic string, data interface{}, expire int64) (err error) {
|
||||
rwLock.RLock()
|
||||
p, ok := producer[topic]
|
||||
rwLock.RUnlock()
|
||||
if !ok {
|
||||
g.Log().Errorf(ctx, "topic not found. topic: %s", topic)
|
||||
return ErrNotFoundTopic
|
||||
}
|
||||
return p.Push(ctx, topic, data, expire)
|
||||
}
|
||||
Reference in New Issue
Block a user