mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-07-06 20:03:23 +08:00
* fix: performance issues with Kafka caused by encapsulating the MQ interface * fix: admin token in standalone mode * fix: full id version * fix: resolve deadlock in cache eviction and improve GetBatch implementation * refactor: replace LongConn with ClientConn interface and simplify message handling * refactor: replace LongConn with ClientConn interface and simplify message handling * fix: seq use $setOnInsert for min_seq in conversation update * feat: add error code for handled friend requests and improve error handling in friend operations * refactor(msg): update regex pattern for conversationID to include a trailing colon * refactor: streamline cache initialization and enhance queue engine configuration * refactor: streamline cache initialization and enhance queue engine configuration * feat: add RegisterIP to API config and implement Redis server registration * feat(redis): implement standalone gateway registration with Redis * chore: update openimsdk/tools dependency to v0.0.50-alpha.121 * fix: change timer to ticker for improved periodic execution * refactor: replace Kafka topic configuration with mqbuild constants for improved readability * feat(redis): implement Redis-based locking mechanism for cron tasks
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package push
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"strconv"
|
|
|
|
"github.com/openimsdk/tools/mq"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/openimsdk/open-im-server/v3/internal/push/offlinepush"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/redis"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/controller"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/dbbuild"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/mqbuild"
|
|
pbpush "github.com/openimsdk/protocol/push"
|
|
"github.com/openimsdk/tools/discovery"
|
|
"github.com/openimsdk/tools/log"
|
|
"github.com/openimsdk/tools/mcontext"
|
|
)
|
|
|
|
type pushServer struct {
|
|
pbpush.UnimplementedPushMsgServiceServer
|
|
database controller.PushDatabase
|
|
disCov discovery.Conn
|
|
offlinePusher offlinepush.OfflinePusher
|
|
}
|
|
|
|
type Config struct {
|
|
RpcConfig config.Push
|
|
RedisConfig config.Redis
|
|
MongoConfig config.Mongo
|
|
KafkaConfig config.Kafka
|
|
NotificationConfig config.Notification
|
|
Share config.Share
|
|
WebhooksConfig config.Webhooks
|
|
LocalCacheConfig config.LocalCache
|
|
Discovery config.Discovery
|
|
FcmConfigPath config.Path
|
|
}
|
|
|
|
func (p pushServer) DelUserPushToken(ctx context.Context,
|
|
req *pbpush.DelUserPushTokenReq) (resp *pbpush.DelUserPushTokenResp, err error) {
|
|
if err = p.database.DelFcmToken(ctx, req.UserID, int(req.PlatformID)); err != nil {
|
|
return nil, err
|
|
}
|
|
return &pbpush.DelUserPushTokenResp{}, nil
|
|
}
|
|
|
|
func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryRegistry, server grpc.ServiceRegistrar) error {
|
|
dbb := dbbuild.NewBuilder(&config.MongoConfig, &config.RedisConfig)
|
|
rdb, err := dbb.Redis(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cacheModel := redis.NewThirdCache(rdb)
|
|
offlinePusher, err := offlinepush.NewOfflinePusher(&config.RpcConfig, cacheModel, string(config.FcmConfigPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
builder, err := mqbuild.NewBuilder(config.Share.Queue, &config.KafkaConfig, rdb)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
offlinePushProducer, err := builder.GetTopicProducer(ctx, mqbuild.TopicToOfflinePush)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
database := controller.NewPushDatabase(cacheModel, offlinePushProducer)
|
|
|
|
pushConsumer, err := builder.GetTopicConsumer(ctx, mqbuild.TopicToPush)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
offlinePushConsumer, err := builder.GetTopicConsumer(ctx, mqbuild.TopicToOfflinePush)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pushHandler, err := NewConsumerHandler(ctx, config, database, offlinePusher, rdb, client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
offlineHandler := NewOfflinePushConsumerHandler(offlinePusher)
|
|
|
|
pbpush.RegisterPushMsgServiceServer(server, &pushServer{
|
|
database: database,
|
|
disCov: client,
|
|
offlinePusher: offlinePusher,
|
|
})
|
|
|
|
go func() {
|
|
pushHandler.WaitCache()
|
|
fn := func(msg mq.Message) error {
|
|
pushHandler.HandleMs2PsChat(authverify.WithTempAdmin(msg.Context()), msg.Value())
|
|
return nil
|
|
}
|
|
consumerCtx := mcontext.SetOperationID(context.Background(), "push_"+strconv.Itoa(int(rand.Uint32())))
|
|
log.ZInfo(consumerCtx, "begin consume messages")
|
|
for {
|
|
if err := pushConsumer.Subscribe(consumerCtx, fn); err != nil {
|
|
log.ZError(consumerCtx, "subscribe err", err)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
fn := func(msg mq.Message) error {
|
|
offlineHandler.HandleMsg2OfflinePush(msg.Context(), msg.Value())
|
|
return nil
|
|
}
|
|
consumerCtx := mcontext.SetOperationID(context.Background(), "push_"+strconv.Itoa(int(rand.Uint32())))
|
|
log.ZInfo(consumerCtx, "begin consume messages")
|
|
for {
|
|
if err := offlinePushConsumer.Subscribe(consumerCtx, fn); err != nil {
|
|
log.ZError(consumerCtx, "subscribe err", err)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|