mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-11-02 17:32:11 +08:00
feat: init
This commit is contained in:
parent
2421669ac4
commit
04f56a9c00
@ -125,6 +125,7 @@ func (*ConsumerHandler) Setup(sarama.ConsumerGroupSession) error { return nil }
|
|||||||
func (*ConsumerHandler) Cleanup(sarama.ConsumerGroupSession) error { return nil }
|
func (*ConsumerHandler) Cleanup(sarama.ConsumerGroupSession) error { return nil }
|
||||||
|
|
||||||
func (c *ConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
func (c *ConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||||
|
|
||||||
for msg := range claim.Messages() {
|
for msg := range claim.Messages() {
|
||||||
ctx := c.pushConsumerGroup.GetContextFromMsg(msg)
|
ctx := c.pushConsumerGroup.GetContextFromMsg(msg)
|
||||||
c.handleMs2PsChat(ctx, msg.Value)
|
c.handleMs2PsChat(ctx, msg.Value)
|
||||||
|
|||||||
@ -2,8 +2,11 @@ package rpccache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/openimsdk/protocol/user"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||||
@ -23,18 +26,23 @@ func NewOnlineCache(user rpcclient.UserRpcClient, group *GroupLocalCache, rdb re
|
|||||||
user: user,
|
user: user,
|
||||||
group: group,
|
group: group,
|
||||||
fullUserCache: fullUserCache,
|
fullUserCache: fullUserCache,
|
||||||
|
initDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
switch x.fullUserCache {
|
switch x.fullUserCache {
|
||||||
case true:
|
case true:
|
||||||
x.mapCache = cacheutil.NewCache[string, []int32]()
|
x.mapCache = cacheutil.NewCache[string, []int32]()
|
||||||
if err := x.initUsersOnlineStatus(mcontext.SetOperationID(context.TODO(), strconv.FormatInt(time.Now().UnixNano()+int64(rand.Uint32()), 10))); err != nil {
|
go func() {
|
||||||
return nil, err
|
ctx := mcontext.SetOperationID(context.TODO(), strconv.FormatInt(time.Now().UnixNano()+int64(rand.Uint32()), 10))
|
||||||
}
|
if err := x.initUsersOnlineStatus(ctx); err != nil {
|
||||||
|
log.ZError(ctx, "initUsersOnlineStatus failed", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
case false:
|
case false:
|
||||||
x.lruCache = lru.NewSlotLRU(1024, localcache.LRUStringHash, func() lru.LRU[string, []int32] {
|
x.lruCache = lru.NewSlotLRU(1024, localcache.LRUStringHash, func() lru.LRU[string, []int32] {
|
||||||
return lru.NewLayLRU[string, []int32](2048, cachekey.OnlineExpire/2, time.Second*3, localcache.EmptyTarget{}, func(key string, value []int32) {})
|
return lru.NewLayLRU[string, []int32](2048, cachekey.OnlineExpire/2, time.Second*3, localcache.EmptyTarget{}, func(key string, value []int32) {})
|
||||||
})
|
})
|
||||||
|
close(x.initDone)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -77,42 +85,81 @@ type OnlineCache struct {
|
|||||||
|
|
||||||
lruCache lru.LRU[string, []int32]
|
lruCache lru.LRU[string, []int32]
|
||||||
mapCache *cacheutil.Cache[string, []int32]
|
mapCache *cacheutil.Cache[string, []int32]
|
||||||
|
initDone chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OnlineCache) initUsersOnlineStatus(ctx context.Context) error {
|
func (o *OnlineCache) initUsersOnlineStatus(ctx context.Context) error {
|
||||||
log.ZDebug(ctx, "init users online status begin")
|
log.ZDebug(ctx, "init users online status begin")
|
||||||
|
defer func() {
|
||||||
|
close(o.initDone)
|
||||||
|
}()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
totalSet int
|
totalSet atomic.Int64
|
||||||
|
gr, _ = errgroup.WithContext(ctx)
|
||||||
)
|
)
|
||||||
|
gr.SetLimit(10)
|
||||||
|
|
||||||
time.Sleep(time.Second * 10)
|
time.Sleep(time.Second * 10)
|
||||||
|
|
||||||
defer func(t time.Time) {
|
defer func(t time.Time) {
|
||||||
log.ZInfo(ctx, "init users online status end", "cost", time.Since(t), "totalSet", totalSet)
|
log.ZInfo(ctx, "init users online status end", "cost", time.Since(t), "totalSet", totalSet.Load())
|
||||||
}(time.Now())
|
}(time.Now())
|
||||||
|
|
||||||
for page := int32(1); ; page++ {
|
page := int32(1)
|
||||||
resp, err := o.user.GetAllUserID(ctx, page, constant.ParamMaxLength)
|
resp, err := o.user.GetAllUserID(ctx, page, constant.ParamMaxLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
for page = 2; (page-1)*constant.ParamMaxLength < resp.Total; page++ {
|
||||||
|
page := page
|
||||||
|
|
||||||
usersStatus, err := o.user.GetUsersOnlinePlatform(ctx, resp.UserIDs)
|
gr.Go(func() error {
|
||||||
if err != nil {
|
var (
|
||||||
return err
|
usersStatus []*user.OnlineStatus
|
||||||
}
|
resp *user.GetAllUserIDResp
|
||||||
|
err error
|
||||||
|
maxTries = 5
|
||||||
|
)
|
||||||
|
|
||||||
for _, user := range usersStatus {
|
for i := 0; i < maxTries; i++ {
|
||||||
if user.Status == constant.Online {
|
resp, err = o.user.GetAllUserID(ctx, page, constant.ParamMaxLength)
|
||||||
o.setUserOnline(user.UserID, user.PlatformIDs)
|
if err != nil {
|
||||||
|
log.ZWarn(ctx, "initUsersOnlineStatus: GetAllUserID failed", err, "page", page, "retries", i+1)
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
totalSet++
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(resp.UserIDs) < constant.ParamMaxLength {
|
for i := 0; i < maxTries; i++ {
|
||||||
break
|
usersStatus, err = o.user.GetUsersOnlinePlatform(ctx, resp.UserIDs)
|
||||||
}
|
if err != nil {
|
||||||
|
log.ZError(ctx, "initUsersOnlineStatus: GetAllUserID failed", err, "page", page, "retries", i+1)
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, u := range usersStatus {
|
||||||
|
if u.Status == constant.Online {
|
||||||
|
o.setUserOnline(u.UserID, u.PlatformIDs)
|
||||||
|
}
|
||||||
|
totalSet.Add(1)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
if err = gr.Wait(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user