mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-04 03:13:15 +08:00 
			
		
		
		
	* fix: GroupApplicationAcceptedNotification * fix: GroupApplicationAcceptedNotification * fix: NotificationUserInfoUpdate * cicd: robot automated Change * fix: component * fix: getConversationInfo * feat: cron task * feat: cron task * feat: cron task * feat: cron task * feat: cron task * fix: minio config url recognition error * new mongo * new mongo * new mongo * new mongo * new mongo * new mongo * new mongo * new mongo * friend incr sync * friend incr sync * friend incr sync * friend incr sync * friend incr sync * mage * optimization version log * optimization version log * sync * sync * sync * group sync * sync option * sync option * refactor: replace `friend` package with `realtion`. * refactor: update lastest commit to relation. * sync option * sync option * sync option * sync * sync * go.mod * seq * update: go mod * refactor: change incremental to full * feat: get full friend user ids * feat: api and config * seq * group version * merge * seq * seq * seq * fix: sort by id avoid unstable sort friends. * group * group * group * fix: sort by id avoid unstable sort friends. * fix: sort by id avoid unstable sort friends. * fix: sort by id avoid unstable sort friends. * user version * seq * seq * seq user * user online * implement minio expire delete. * user online * config * fix * fix * implement minio expire delete logic. * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * feat: implement scheduled delete outdated object in minio. * update gomake version * update gomake version * implement FindExpires pagination. * remove unnesseary incr. * fix uncorrect args call. * online push * online push * online push * resolving conflicts * resolving conflicts * test * api prommetrics * api prommetrics * api prommetrics * api prommetrics * api prommetrics * rpc prommetrics * rpc prommetrics * online status * online status * online status * online status * sub * conversation version incremental * merge seq * merge online * merge online * merge online * merge seq * GetOwnerConversation * fix: change incremental syncer router name. * rockscache batch get * rockscache seq batch get * fix: GetMsgDocModelByIndex bug * update go.mod * update go.mod * merge * feat: prometheus * feat: prometheus --------- Co-authored-by: withchao <withchao@users.noreply.github.com> Co-authored-by: Monet Lee <monet_lee@163.com> Co-authored-by: OpenIM-Gordon <46924906+FGadvancer@users.noreply.github.com> Co-authored-by: icey-yu <1186114839@qq.com>
		
			
				
	
	
		
			113 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package msggateway
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"crypto/md5"
 | 
						|
	"encoding/binary"
 | 
						|
	"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
 | 
						|
	pbuser "github.com/openimsdk/protocol/user"
 | 
						|
	"github.com/openimsdk/tools/log"
 | 
						|
	"github.com/openimsdk/tools/mcontext"
 | 
						|
	"github.com/openimsdk/tools/utils/datautil"
 | 
						|
	"math/rand"
 | 
						|
	"strconv"
 | 
						|
	"time"
 | 
						|
)
 | 
						|
 | 
						|
func (ws *WsServer) ChangeOnlineStatus(concurrent int) {
 | 
						|
	if concurrent < 1 {
 | 
						|
		concurrent = 1
 | 
						|
	}
 | 
						|
	const renewalTime = cachekey.OnlineExpire / 3
 | 
						|
	//const renewalTime = time.Second * 10
 | 
						|
	renewalTicker := time.NewTicker(renewalTime)
 | 
						|
 | 
						|
	requestChs := make([]chan *pbuser.SetUserOnlineStatusReq, concurrent)
 | 
						|
	changeStatus := make([][]UserState, concurrent)
 | 
						|
 | 
						|
	for i := 0; i < concurrent; i++ {
 | 
						|
		requestChs[i] = make(chan *pbuser.SetUserOnlineStatusReq, 64)
 | 
						|
		changeStatus[i] = make([]UserState, 0, 100)
 | 
						|
	}
 | 
						|
 | 
						|
	mergeTicker := time.NewTicker(time.Second)
 | 
						|
 | 
						|
	local2pb := func(u UserState) *pbuser.UserOnlineStatus {
 | 
						|
		return &pbuser.UserOnlineStatus{
 | 
						|
			UserID:  u.UserID,
 | 
						|
			Online:  u.Online,
 | 
						|
			Offline: u.Offline,
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	rNum := rand.Uint64()
 | 
						|
	pushUserState := func(us ...UserState) {
 | 
						|
		for _, u := range us {
 | 
						|
			sum := md5.Sum([]byte(u.UserID))
 | 
						|
			i := (binary.BigEndian.Uint64(sum[:]) + rNum) % uint64(concurrent)
 | 
						|
			changeStatus[i] = append(changeStatus[i], u)
 | 
						|
			status := changeStatus[i]
 | 
						|
			if len(status) == cap(status) {
 | 
						|
				req := &pbuser.SetUserOnlineStatusReq{
 | 
						|
					Status: datautil.Slice(status, local2pb),
 | 
						|
				}
 | 
						|
				changeStatus[i] = status[:0]
 | 
						|
				select {
 | 
						|
				case requestChs[i] <- req:
 | 
						|
				default:
 | 
						|
					log.ZError(context.Background(), "user online processing is too slow", nil)
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	pushAllUserState := func() {
 | 
						|
		for i, status := range changeStatus {
 | 
						|
			if len(status) == 0 {
 | 
						|
				continue
 | 
						|
			}
 | 
						|
			req := &pbuser.SetUserOnlineStatusReq{
 | 
						|
				Status: datautil.Slice(status, local2pb),
 | 
						|
			}
 | 
						|
			changeStatus[i] = status[:0]
 | 
						|
			select {
 | 
						|
			case requestChs[i] <- req:
 | 
						|
			default:
 | 
						|
				log.ZError(context.Background(), "user online processing is too slow", nil)
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	opIdCtx := mcontext.SetOperationID(context.Background(), "r"+strconv.FormatUint(rNum, 10))
 | 
						|
	doRequest := func(req *pbuser.SetUserOnlineStatusReq) {
 | 
						|
		ctx, cancel := context.WithTimeout(opIdCtx, time.Second*5)
 | 
						|
		defer cancel()
 | 
						|
		if _, err := ws.userClient.Client.SetUserOnlineStatus(ctx, req); err != nil {
 | 
						|
			log.ZError(ctx, "update user online status", err)
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	for i := 0; i < concurrent; i++ {
 | 
						|
		go func(ch <-chan *pbuser.SetUserOnlineStatusReq) {
 | 
						|
			for req := range ch {
 | 
						|
				doRequest(req)
 | 
						|
			}
 | 
						|
		}(requestChs[i])
 | 
						|
	}
 | 
						|
 | 
						|
	for {
 | 
						|
		select {
 | 
						|
		case <-mergeTicker.C:
 | 
						|
			pushAllUserState()
 | 
						|
		case now := <-renewalTicker.C:
 | 
						|
			deadline := now.Add(-cachekey.OnlineExpire / 3)
 | 
						|
			users := ws.clients.GetAllUserStatus(deadline, now)
 | 
						|
			log.ZDebug(context.Background(), "renewal ticker", "deadline", deadline, "nowtime", now, "num", len(users))
 | 
						|
			pushUserState(users...)
 | 
						|
		case state := <-ws.clients.UserState():
 | 
						|
			log.ZDebug(context.Background(), "OnlineCache user online change", "userID", state.UserID, "online", state.Online, "offline", state.Offline)
 | 
						|
			pushUserState(state)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |