Merge remote-tracking branch 'origin/errcode' into errcode

# Conflicts:
#	cmd/msggateway/main.go
#	go.sum
#	internal/msggateway/init.go
This commit is contained in:
Gordon
2023-03-08 18:42:25 +08:00
87 changed files with 2538 additions and 2232 deletions
+17
View File
@@ -0,0 +1,17 @@
package cmd
import "github.com/spf13/cobra"
type ApiCmd struct {
*RootCmd
}
func NewApiCmd() *ApiCmd {
return &ApiCmd{NewRootCmd()}
}
func (a *ApiCmd) AddApi(f func(port int) error) {
a.Command.RunE = func(cmd *cobra.Command, args []string) error {
return f(a.getPortFlag(cmd))
}
}
+22
View File
@@ -0,0 +1,22 @@
package cmd
import "github.com/spf13/cobra"
type CronTaskCmd struct {
*RootCmd
}
func NewCronTaskCmd() *CronTaskCmd {
return &CronTaskCmd{NewRootCmd()}
}
func (c *CronTaskCmd) addRunE(f func() error) {
c.Command.RunE = func(cmd *cobra.Command, args []string) error {
return f()
}
}
func (c *CronTaskCmd) Exec(f func() error) error {
c.addRunE(f)
return c.Execute()
}
+36
View File
@@ -0,0 +1,36 @@
package cmd
import (
//"OpenIM/internal/msggateway"
"OpenIM/pkg/common/constant"
"github.com/spf13/cobra"
)
type MsgGatewayCmd struct {
*RootCmd
}
func NewMsgGatewayCmd() MsgGatewayCmd {
return MsgGatewayCmd{NewRootCmd()}
}
func (m *MsgGatewayCmd) AddWsPortFlag() {
m.Command.Flags().IntP(constant.FlagWsPort, "w", 0, "ws server listen port")
}
func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int {
port, _ := cmd.Flags().GetInt(constant.FlagWsPort)
return port
}
func (m *MsgGatewayCmd) addRun() {
m.Command.Run = func(cmd *cobra.Command, args []string) {
//msggateway.Init(m.getPortFlag(cmd), m.getWsPortFlag(cmd))
//msggateway.Run(m.getPrometheusPortFlag(cmd))
}
}
func (m *MsgGatewayCmd) Exec() error {
m.addRun()
return m.Execute()
}
+25
View File
@@ -0,0 +1,25 @@
package cmd
import (
"OpenIM/internal/msgtransfer"
"github.com/spf13/cobra"
)
type MsgTransferCmd struct {
*RootCmd
}
func NewMsgTransferCmd() MsgTransferCmd {
return MsgTransferCmd{NewRootCmd()}
}
func (m *MsgTransferCmd) addRunE() {
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
return msgtransfer.StartTransfer(m.getPrometheusPortFlag(cmd))
}
}
func (m *MsgTransferCmd) Exec() error {
m.addRunE()
return m.Execute()
}
+157
View File
@@ -0,0 +1,157 @@
package cmd
import (
"OpenIM/internal/tools"
"context"
"github.com/spf13/cobra"
)
type MsgUtilsCmd struct {
*RootCmd
userID string
superGroupID string
clearAll bool
fixAll bool
}
func NewMsgUtilsCmd() MsgUtilsCmd {
return MsgUtilsCmd{RootCmd: NewRootCmd()}
}
func (m *MsgUtilsCmd) AddUserIDFlag() {
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
}
func (m *MsgUtilsCmd) GetUserIDFlag() string {
return m.userID
}
func (m *MsgUtilsCmd) AddFixAllFlag() {
m.Command.PersistentFlags().BoolP("fixAll", "c", false, "openIM fix all seqs")
}
func (m *MsgUtilsCmd) GetFixAllFlag() bool {
return m.fixAll
}
func (m *MsgUtilsCmd) AddSuperGroupIDFlag() {
m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID")
}
func (m *MsgUtilsCmd) GetSuperGroupIDFlag() string {
return m.superGroupID
}
func (m *MsgUtilsCmd) AddClearAllFlag() bool {
return m.clearAll
}
func (m *MsgUtilsCmd) GetClearAllFlag() bool {
return m.clearAll
}
type SeqCmd struct {
Command *cobra.Command
}
func (SeqCmd) RunCommand(cmdLines *cobra.Command, args []string) error {
msgTool, err := tools.InitMsgTool()
if err != nil {
return err
}
userID, _ := cmdLines.Flags().GetString("userID")
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
fixAll, _ := cmdLines.Flags().GetBool("fixAll")
ctx := context.Background()
switch {
case cmdLines.Parent() == GetCmd:
switch {
case userID != "":
msgTool.ShowUserSeqs(ctx, userID)
case superGroupID != "":
msgTool.ShowSuperGroupSeqs(ctx, superGroupID)
}
case cmdLines.Parent() == FixCmd:
switch {
case userID != "":
_, _, err = msgTool.GetAndFixUserSeqs(ctx, userID)
case superGroupID != "":
err = msgTool.FixGroupSeq(ctx, userID)
case fixAll:
err = msgTool.FixAllSeq(ctx)
}
}
return err
}
func NewSeqCmd() SeqCmd {
seqCmd := SeqCmd{&cobra.Command{
Use: "seq",
Short: "seq operation",
}}
seqCmd.Command.Flags().BoolP("fixAll", "c", false, "openIM fix all seqs")
seqCmd.Command.RunE = seqCmd.RunCommand
return seqCmd
}
type MsgCmd struct {
Command *cobra.Command
}
func NewMsgCmd() MsgCmd {
msgCmd := MsgCmd{&cobra.Command{
Use: "msg",
Short: "msg operation",
}}
msgCmd.Command.RunE = msgCmd.RunCommand
msgCmd.Command.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs")
return msgCmd
}
func (*MsgCmd) RunCommand(cmdLines *cobra.Command, args []string) error {
msgTool, err := tools.InitMsgTool()
if err != nil {
return err
}
userID, _ := cmdLines.Flags().GetString("userID")
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
clearAll, _ := cmdLines.Flags().GetBool("clearAll")
ctx := context.Background()
switch {
case cmdLines.Parent() == GetCmd:
switch {
case userID != "":
msgTool.ShowUserSeqs(ctx, userID)
case superGroupID != "":
msgTool.ShowSuperGroupSeqs(ctx, superGroupID)
}
case cmdLines.Parent() == ClearCmd:
switch {
case userID != "":
msgTool.ClearUsersMsg(ctx, []string{userID})
case superGroupID != "":
msgTool.ClearSuperGroupMsg(ctx, []string{superGroupID})
case clearAll:
msgTool.AllUserClearMsgAndFixSeq()
}
}
return nil
}
var GetCmd = &cobra.Command{
Use: "get",
Short: "get operation",
}
var FixCmd = &cobra.Command{
Use: "fix",
Short: "fix seq operation",
}
var ClearCmd = &cobra.Command{
Use: "clear",
Short: "clear operation",
}
+22
View File
@@ -0,0 +1,22 @@
package cmd
import (
"OpenIM/internal/push"
"OpenIM/internal/startrpc"
"OpenIM/pkg/common/config"
"github.com/spf13/cobra"
)
type PushCmd struct {
*RpcCmd
}
func NewPushCmd() *PushCmd {
return &PushCmd{NewRpcCmd(config.Config.RpcRegisterName.OpenImPushName)}
}
func (r *RpcCmd) AddPush() {
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
return startrpc.Start(r.getPortFlag(cmd), r.rpcRegisterName, r.getPrometheusPortFlag(cmd), push.Start)
}
}
+62 -2
View File
@@ -1,13 +1,73 @@
package cmd
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"github.com/spf13/cobra"
)
func NewRootCmd() *cobra.Command {
return &cobra.Command{
type RootCmd struct {
Command cobra.Command
}
func NewRootCmd() (rootCmd *RootCmd) {
rootCmd = &RootCmd{}
c := cobra.Command{
Use: "start",
Short: "Start the server",
Long: `Start the server`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return rootCmd.getConfFromCmdAndInit(cmd)
},
}
rootCmd.Command = c
rootCmd.init()
return rootCmd
}
func (r *RootCmd) AddRunE(f func(cmd RootCmd) error) {
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
return f(*r)
}
}
func (r *RootCmd) AddRpc(f func(port, prometheusPort int) error) {
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
return f(r.getPortFlag(cmd), r.getPrometheusPortFlag(cmd))
}
}
func (r *RootCmd) init() {
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
}
func (r *RootCmd) AddPortFlag() {
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
}
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
port, _ := cmd.Flags().GetInt(constant.FlagPort)
return port
}
func (r *RootCmd) AddPrometheusPortFlag() {
r.Command.Flags().String(constant.FlagPrometheusPort, "", "server prometheus listen port")
}
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
return port
}
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
return config.InitConfig(configFolderPath)
}
func (r *RootCmd) Execute() error {
return r.Command.Execute()
}
func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
r.Command.AddCommand(cmds...)
}
+28
View File
@@ -0,0 +1,28 @@
package cmd
import (
"OpenIM/internal/startrpc"
"OpenIM/pkg/discoveryregistry"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)
type RpcCmd struct {
*RootCmd
rpcRegisterName string
}
func NewRpcCmd(rpcRegisterName string) *RpcCmd {
return &RpcCmd{NewRootCmd(), rpcRegisterName}
}
func (r *RpcCmd) AddRpc(rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) {
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
return startrpc.Start(r.getPortFlag(cmd), r.rpcRegisterName, r.getPrometheusPortFlag(cmd), rpcFn)
}
}
func (r *RpcCmd) Exec(rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
r.AddRpc(rpcFn)
return r.Execute()
}
+7
View File
@@ -8,9 +8,13 @@ import (
"path/filepath"
"runtime"
_ "embed"
"gopkg.in/yaml.v3"
)
//go:embed version
var Version string
var (
_, b, _, _ = runtime.Caller(0)
// Root folder of this project
@@ -502,6 +506,9 @@ func (c *config) initConfig(config interface{}, configName, configFolderPath str
configFolderPath = DefaultFolderPath
}
configPath := filepath.Join(configFolderPath, configName)
defer func() {
fmt.Println("use config", configPath)
}()
_, err := os.Stat(configPath)
if err != nil {
if !os.IsNotExist(err) {
+1
View File
@@ -0,0 +1 @@
v3.0.0
+5 -7
View File
@@ -310,17 +310,15 @@ func GroupIsBanPrivateChat(status int32) bool {
return true
}
const BigVersion = "v2"
const LogFileName = "OpenIM.log"
const CurrentVersion = "v2.3.4-rc0"
const LocalHost = "0.0.0.0"
// flag parse
const (
FlagPort = "port"
PrometheusPort = "prometheus_port"
FlagConf = "config_folder_path"
FlagPort = "port"
FlagWsPort = "ws_port"
FlagPrometheusPort = "prometheus_port"
FlagConf = "config_folder_path"
)
-89
View File
@@ -1,89 +0,0 @@
package constant
import (
"github.com/pkg/errors"
"strings"
)
type ErrInfo struct {
ErrCode int32
ErrMsg string
DetailErrMsg string
}
func NewErrInfo(code int32, msg, detail string) *ErrInfo {
return &ErrInfo{
ErrCode: code,
ErrMsg: msg,
DetailErrMsg: detail,
}
}
func (e *ErrInfo) Error() string {
return "errMsg: " + e.ErrMsg + " detail errMsg: " + e.DetailErrMsg
}
func (e *ErrInfo) Code() int32 {
return e.ErrCode
}
func (e *ErrInfo) Msg() string {
return e.ErrMsg
}
func (e *ErrInfo) Detail() string {
return e.DetailErrMsg
}
func (e *ErrInfo) Wrap(msg ...string) error {
return errors.Wrap(e, strings.Join(msg, "--"))
}
func NewErrNetwork(err error) error {
return toDetail(err, ErrNetwork)
}
func NewErrData(err error) error {
return toDetail(err, ErrData)
}
func toDetail(err error, info *ErrInfo) *ErrInfo {
errInfo := *info
errInfo.DetailErrMsg = err.Error()
return &errInfo
}
func ToAPIErrWithErr(err error) *ErrInfo {
return &ErrInfo{}
//unwrap := utils.Unwrap(err)
//if unwrap == gorm.ErrRecordNotFound {
// return &ErrInfo{
// ErrCode: ErrRecordNotFound.Code(),
// ErrMsg: ErrRecordNotFound.Msg(),
// DetailErrMsg: fmt.Sprintf("%+v", err),
// }
//}
//if errInfo, ok := unwrap.(*ErrInfo); ok {
// return &ErrInfo{
// ErrCode: errInfo.Code(),
// ErrMsg: errInfo.Msg(),
// DetailErrMsg: fmt.Sprintf("%+v", err),
// }
//}
//
//errComm := errors.New("")
//var marshalErr *json.MarshalerError
//errInfo := &ErrInfo{}
//switch {
//case errors.As(err, &errComm):
// if errors.Is(err, gorm.ErrRecordNotFound) {
// return toDetail(err, ErrRecordNotFound)
// }
// return toDetail(err, ErrData)
//case errors.As(err, &marshalErr):
// return toDetail(err, ErrData)
//case errors.As(err, &errInfo):
// return toDetail(err, errInfo)
//}
//return toDetail(err, ErrDefaultOther)
}
+1
View File
@@ -5,6 +5,7 @@ import (
"context"
)
// for mongoDB
type ExtendMsgDatabase interface {
CreateExtendMsgSet(ctx context.Context, set *unRelationTb.ExtendMsgSetModel) error
GetAllExtendMsgSet(ctx context.Context, ID string, opts *unRelationTb.GetAllExtendMsgSetOpts) (sets []*unRelationTb.ExtendMsgSetModel, err error)
+9 -4
View File
@@ -1,10 +1,12 @@
package controller
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/cache"
unRelationTb "OpenIM/pkg/common/db/table/unrelation"
"OpenIM/pkg/common/db/unrelation"
"OpenIM/pkg/common/kafka"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/prome"
"OpenIM/pkg/common/tracelog"
@@ -68,7 +70,7 @@ type MsgDatabase interface {
DeleteReactionExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*sdkws.KeyValue) error
SetSendMsgStatus(ctx context.Context, id string, status int32) error
GetSendMsgStatus(ctx context.Context, id string) (int32, error)
MsgToMQ(ctx context.Context, key string, mq *pbMsg.MsgDataToMQ) error
MsgToMQ(ctx context.Context, key string, msg2mq *pbMsg.MsgDataToMQ) error
GetUserMaxSeq(ctx context.Context, userID string) (int64, error)
GetUserMinSeq(ctx context.Context, userID string) (int64, error)
GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
@@ -79,6 +81,7 @@ func NewMsgDatabase(msgDocModel unRelationTb.MsgDocModelInterface, cacheModel ca
return &msgDatabase{
msgDocDatabase: msgDocModel,
cache: cacheModel,
producer: kafka.NewKafkaProducer(config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.Ws2mschat.Topic),
}
}
@@ -93,6 +96,8 @@ type msgDatabase struct {
msgDocDatabase unRelationTb.MsgDocModelInterface
extendMsgDatabase unRelationTb.ExtendMsgSetModelInterface
cache cache.Model
producer *kafka.Producer
// model
msg unRelationTb.MsgDocModel
extendMsgSetModel unRelationTb.ExtendMsgSetModel
}
@@ -165,9 +170,9 @@ func (db *msgDatabase) GetSendMsgStatus(ctx context.Context, id string) (int32,
return db.cache.GetSendMsgStatus(ctx, id)
}
func (db *msgDatabase) MsgToMQ(ctx context.Context, key string, mq *pbMsg.MsgDataToMQ) error {
//TODO implement me
panic("implement me")
func (db *msgDatabase) MsgToMQ(ctx context.Context, key string, msg2mq *pbMsg.MsgDataToMQ) error {
_, _, err := db.producer.SendMessage(ctx, key, msg2mq)
return err
}
func (db *msgDatabase) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
+2 -2
View File
@@ -10,7 +10,7 @@ import (
const (
singleGocMsgNum = 5000
CChat = "msg"
Msg = "msg"
OldestList = 0
NewestList = -1
)
@@ -38,7 +38,7 @@ type MsgDocModelInterface interface {
}
func (MsgDocModel) TableName() string {
return CChat
return Msg
}
func (MsgDocModel) GetSingleGocMsgNum() int64 {
+1 -1
View File
@@ -61,7 +61,7 @@ func (m *Mongo) GetDatabase() *mongo.Database {
}
func (m *Mongo) CreateMsgIndex() error {
return m.createMongoIndex(unrelation.CChat, false, "uid")
return m.createMongoIndex(unrelation.Msg, false, "uid")
}
func (m *Mongo) CreateSuperGroupIndex() error {
+2 -3
View File
@@ -9,7 +9,6 @@ package http
import (
"OpenIM/pkg/callbackstruct"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/errs"
"bytes"
"encoding/json"
@@ -79,13 +78,13 @@ func callBackPostReturn(url, command string, input interface{}, output callbacks
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return errs.ErrCallbackContinue
}
return constant.NewErrNetwork(err)
return errs.ErrNetwork.Wrap(err.Error())
}
if err = json.Unmarshal(b, output); err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return errs.ErrCallbackContinue
}
return constant.NewErrData(err)
return errs.ErrData.Wrap(err.Error())
}
return output.Parse()
}
+1 -1
View File
@@ -46,7 +46,7 @@ func NewKafkaProducer(addr []string, topic string) *Producer {
return &p
}
func (p *Producer) SendMessage(ctx context.Context, m proto.Message, key string) (int32, int64, error) {
func (p *Producer) SendMessage(ctx context.Context, key string, m proto.Message) (int32, int64, error) {
operationID := tracelog.GetOperationID(ctx)
log.Info(operationID, "SendMessage", "key ", key, m.String(), p.producer)
kMsg := &sarama.ProducerMessage{}