mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-01 00:42:13 +08:00 
			
		
		
		
	* fix: fix update groupName invalid. (#2673) * refactor: change platform to platformID (#2670) * feat: don`t return nil data (#2675) Co-authored-by: Monet Lee <monet_lee@163.com> * refactor: update fields type in userStatus and check registered. (#2676) * fix: usertoken auth. (#2677) * refactor: update fields type in userStatus and check registered. * fix: usertoken auth. * update contents. * update content. * update * fix * update pb file. * feat: add friend agree after callback (#2680) * fix: sn not sort (#2682) * 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 * update gomake version * update gomake version * fix: seq conversion bug * fix: redis pipe exec * fix: ImportFriends * fix: A large number of logs keysAndValues length is not even * feat: mark read aggregate write * feat: online status supports redis cluster * feat: online status supports redis cluster * feat: online status supports redis cluster * merge * merge * read seq is written to mongo * read seq is written to mongo * fix: invitation to join group notification * fix: friend op_user_id * feat: optimizing asynchronous context * feat: optimizing memamq size * feat: add GetSeqMessage * feat: GroupApplicationAgreeMemberEnterNotification * feat: GroupApplicationAgreeMemberEnterNotification * feat: go.mod * feat: go.mod * feat: join group notification and get seq * feat: join group notification and get seq * feat: avoid pulling messages from sessions with a large number of max seq values of 0 * feat: API supports gzip * go.mod * fix: nil pointer error on close * fix: listen error * fix: listen error * update go.mod * feat: add log * fix: token parse token value * fix: GetMsgBySeqs boundary issues * fix: sn_ not sort * fix: sn_ not sort * fix: sn_ not sort --------- Co-authored-by: withchao <withchao@users.noreply.github.com> * refactor: add GetAdminToken interface. (#2684) * refactor: add GetAdminToken interface. * update config. * fix: admin token (#2686) * fix: update workflows logic. (#2688) * refactor: add GetAdminToken interface. * update config. * update workflows logic. * fix: admin token (#2687) * update the front image (#2692) * update the front image * update version * feat: improve publish docker image workflows (#2697) * refactor: add GetAdminToken interface. * update config. * update workflows logic. * feat: improve publish docker image workflows * update condition logic. * fix: update load file logic. (#2700) * refactor: add GetAdminToken interface. * update config. * update workflows logic. * feat: improve publish docker image workflows * update condition logic. * fix: update load file logic. * feat: Msg filter (#2703) * feat: msg filter * feat: msg filter * feat: msg filter * feat: provide the interface required by js sdk (#2712) * 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 * update gomake version * update gomake version * fix: seq conversion bug * fix: redis pipe exec * fix: ImportFriends * fix: A large number of logs keysAndValues length is not even * feat: mark read aggregate write * feat: online status supports redis cluster * feat: online status supports redis cluster * feat: online status supports redis cluster * merge * merge * read seq is written to mongo * read seq is written to mongo * fix: invitation to join group notification * fix: friend op_user_id * feat: optimizing asynchronous context * feat: optimizing memamq size * feat: add GetSeqMessage * feat: GroupApplicationAgreeMemberEnterNotification * feat: GroupApplicationAgreeMemberEnterNotification * feat: go.mod * feat: go.mod * feat: join group notification and get seq * feat: join group notification and get seq * feat: avoid pulling messages from sessions with a large number of max seq values of 0 * feat: API supports gzip * go.mod * fix: nil pointer error on close * fix: listen error * fix: listen error * update go.mod * feat: add log * fix: token parse token value * fix: GetMsgBySeqs boundary issues * fix: sn_ not sort * fix: sn_ not sort * fix: sn_ not sort * fix: jssdk add * fix: jssdk support * fix: jssdk support * fix: jssdk support --------- Co-authored-by: withchao <withchao@users.noreply.github.com> * Line webhook (#2716) * feat: online and offline webhook * feat: online and offline webhook * feat: remove zk * fix: the message I sent is not set to read seq in mongodb (#2718) * 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 * update gomake version * update gomake version * fix: seq conversion bug * fix: redis pipe exec * fix: ImportFriends * fix: A large number of logs keysAndValues length is not even * feat: mark read aggregate write * feat: online status supports redis cluster * feat: online status supports redis cluster * feat: online status supports redis cluster * merge * merge * read seq is written to mongo * read seq is written to mongo * fix: invitation to join group notification * fix: friend op_user_id * feat: optimizing asynchronous context * feat: optimizing memamq size * feat: add GetSeqMessage * feat: GroupApplicationAgreeMemberEnterNotification * feat: GroupApplicationAgreeMemberEnterNotification * feat: go.mod * feat: go.mod * feat: join group notification and get seq * feat: join group notification and get seq * feat: avoid pulling messages from sessions with a large number of max seq values of 0 * feat: API supports gzip * go.mod * fix: nil pointer error on close * fix: listen error * fix: listen error * update go.mod * feat: add log * fix: token parse token value * fix: GetMsgBySeqs boundary issues * fix: sn_ not sort * fix: sn_ not sort * fix: sn_ not sort * fix: jssdk add * fix: jssdk support * fix: jssdk support * fix: jssdk support * fix: the message I sent is not set to read seq in mongodb --------- Co-authored-by: withchao <withchao@users.noreply.github.com> * fix: cannot modify group member avatars (#2719) * 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 * update gomake version * update gomake version * fix: seq conversion bug * fix: redis pipe exec * fix: ImportFriends * fix: A large number of logs keysAndValues length is not even * feat: mark read aggregate write * feat: online status supports redis cluster * feat: online status supports redis cluster * feat: online status supports redis cluster * merge * merge * read seq is written to mongo * read seq is written to mongo * fix: invitation to join group notification * fix: friend op_user_id * feat: optimizing asynchronous context * feat: optimizing memamq size * feat: add GetSeqMessage * feat: GroupApplicationAgreeMemberEnterNotification * feat: GroupApplicationAgreeMemberEnterNotification * feat: go.mod * feat: go.mod * feat: join group notification and get seq * feat: join group notification and get seq * feat: avoid pulling messages from sessions with a large number of max seq values of 0 * feat: API supports gzip * go.mod * fix: nil pointer error on close * fix: listen error * fix: listen error * update go.mod * feat: add log * fix: token parse token value * fix: GetMsgBySeqs boundary issues * fix: sn_ not sort * fix: sn_ not sort * fix: sn_ not sort * fix: jssdk add * fix: jssdk support * fix: jssdk support * fix: jssdk support * fix: the message I sent is not set to read seq in mongodb * fix: cannot modify group member avatars --------- Co-authored-by: withchao <withchao@users.noreply.github.com> --------- Co-authored-by: Monet Lee <monet_lee@163.com> Co-authored-by: icey-yu <119291641+icey-yu@users.noreply.github.com> Co-authored-by: chao <48119764+withchao@users.noreply.github.com> Co-authored-by: withchao <withchao@users.noreply.github.com> Co-authored-by: skiffer-git <72860476+skiffer-git@users.noreply.github.com>
		
			
				
	
	
		
			131 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package msggateway
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"crypto/md5"
 | |
| 	"encoding/binary"
 | |
| 	"fmt"
 | |
| 	"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"
 | |
| 	"os"
 | |
| 	"strconv"
 | |
| 	"sync/atomic"
 | |
| 	"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)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	var count atomic.Int64
 | |
| 	operationIDPrefix := fmt.Sprintf("p_%d_", os.Getpid())
 | |
| 	doRequest := func(req *pbuser.SetUserOnlineStatusReq) {
 | |
| 		opIdCtx := mcontext.SetOperationID(context.Background(), operationIDPrefix+strconv.FormatInt(count.Add(1), 10))
 | |
| 		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 _, ss := range req.Status {
 | |
| 			for _, online := range ss.Online {
 | |
| 				client, _, _ := ws.clients.Get(ss.UserID, int(online))
 | |
| 				back := false
 | |
| 				if len(client) > 0 {
 | |
| 					back = client[0].IsBackground
 | |
| 				}
 | |
| 				ws.webhookAfterUserOnline(ctx, &ws.msgGatewayConfig.WebhooksConfig.AfterUserOnline, ss.UserID, int(online), back, ss.ConnID)
 | |
| 			}
 | |
| 			for _, offline := range ss.Offline {
 | |
| 				ws.webhookAfterUserOffline(ctx, &ws.msgGatewayConfig.WebhooksConfig.AfterUserOffline, ss.UserID, int(offline), ss.ConnID)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	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), "users", 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)
 | |
| 		}
 | |
| 	}
 | |
| }
 |