|
|
|
@ -28,15 +28,18 @@ type officeServer struct {
|
|
|
|
|
rpcRegisterName string
|
|
|
|
|
etcdSchema string
|
|
|
|
|
etcdAddr []string
|
|
|
|
|
ch chan tagSendStruct
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewOfficeServer(port int) *officeServer {
|
|
|
|
|
log.NewPrivateLog(constant.LogFileName)
|
|
|
|
|
ch := make(chan tagSendStruct, 100000)
|
|
|
|
|
return &officeServer{
|
|
|
|
|
rpcPort: port,
|
|
|
|
|
rpcRegisterName: config.Config.RpcRegisterName.OpenImOfficeName,
|
|
|
|
|
etcdSchema: config.Config.Etcd.EtcdSchema,
|
|
|
|
|
etcdAddr: config.Config.Etcd.EtcdAddr,
|
|
|
|
|
ch: ch,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -80,6 +83,7 @@ func (s *officeServer) Run() {
|
|
|
|
|
log.NewError("0", "RegisterEtcd failed ", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go s.sendTagMsgRoutine()
|
|
|
|
|
err = srv.Serve(listener)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.NewError("0", "Serve failed ", err.Error())
|
|
|
|
@ -88,6 +92,25 @@ func (s *officeServer) Run() {
|
|
|
|
|
log.NewInfo("0", "message cms rpc success")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tagSendStruct struct {
|
|
|
|
|
operationID string
|
|
|
|
|
user *db.User
|
|
|
|
|
userID string
|
|
|
|
|
content string
|
|
|
|
|
senderPlatformID int32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *officeServer) sendTagMsgRoutine() {
|
|
|
|
|
log.NewInfo("", utils.GetSelfFuncName(), "start")
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case v := <-s.ch:
|
|
|
|
|
msg.TagSendMessage(v.operationID, v.user, v.userID, v.content, v.senderPlatformID)
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *officeServer) GetUserTags(_ context.Context, req *pbOffice.GetUserTagsReq) (resp *pbOffice.GetUserTagsResp, err error) {
|
|
|
|
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req ", req.String())
|
|
|
|
|
resp = &pbOffice.GetUserTagsResp{
|
|
|
|
@ -228,30 +251,46 @@ func (s *officeServer) SendMsg2Tag(_ context.Context, req *pbOffice.SendMsg2TagR
|
|
|
|
|
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(len(userIDList))
|
|
|
|
|
var successUserIDList []string
|
|
|
|
|
for _, userID := range userIDList {
|
|
|
|
|
go func(userID string) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
msg.TagSendMessage(req.OperationID, user, userID, req.Content, req.SenderPlatformID)
|
|
|
|
|
}(userID)
|
|
|
|
|
t := tagSendStruct{
|
|
|
|
|
operationID: req.OperationID,
|
|
|
|
|
user: user,
|
|
|
|
|
userID: userID,
|
|
|
|
|
content: req.Content,
|
|
|
|
|
senderPlatformID: req.SenderPlatformID,
|
|
|
|
|
}
|
|
|
|
|
select {
|
|
|
|
|
case s.ch <- t:
|
|
|
|
|
log.NewDebug(t.operationID, utils.GetSelfFuncName(), "msg: ", t, "send success")
|
|
|
|
|
successUserIDList = append(successUserIDList, userID)
|
|
|
|
|
// if channel is full, return grpc req
|
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
|
log.NewError(t.operationID, utils.GetSelfFuncName(), s.ch, "channel is full")
|
|
|
|
|
resp.CommonResp.ErrCode = constant.ErrSendLimit.ErrCode
|
|
|
|
|
resp.CommonResp.ErrMsg = constant.ErrSendLimit.ErrMsg
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
var tagSendLogs db.TagSendLog
|
|
|
|
|
|
|
|
|
|
wg.Add(len(userIDList))
|
|
|
|
|
for _, userID := range userIDList {
|
|
|
|
|
var tagSendLogs db.TagSendLog
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(len(successUserIDList))
|
|
|
|
|
var lock sync.Mutex
|
|
|
|
|
for _, userID := range successUserIDList {
|
|
|
|
|
go func(userID string) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
userName, err := im_mysql_model.GetUserNameByUserID(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserNameByUserID failed", err.Error())
|
|
|
|
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserNameByUserID failed", err.Error(), userID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
lock.Lock()
|
|
|
|
|
tagSendLogs.UserList = append(tagSendLogs.UserList, db.TagUser{
|
|
|
|
|
UserID: userID,
|
|
|
|
|
UserName: userName,
|
|
|
|
|
})
|
|
|
|
|
lock.Unlock()
|
|
|
|
|
}(userID)
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|