From abf8e37e7883c11609922e3409a038fac4164a4d Mon Sep 17 00:00:00 2001 From: xuexihuang <1339326187@qq.com> Date: Mon, 16 Oct 2023 18:02:12 +0800 Subject: [PATCH 01/21] feat: Add the help chart script to support k8s deployment (#1145) * Code adaptation k8s: service discovery and registration adaptation, configuration adaptation * Initial submission of the help charts script for openim API * change the help charts script * change the help charts script * change helm chart codes * change dockerfiles script * change chart script:add configmap mounts * change chart script:change repository * change chart script:msggateway add one service * change config.yaml * roll back some config values * change chart script:change Ingress rule with a rewrite annotation * add mysql charts scrible * change chart script:add mysql.config.yaml * add nfs provisioner charts * change chart script:add nfs.config.yaml * add ingress-nginx charts * change chart script:add ingress-nginx.config.yaml * add redis &mongodb charts * add kafka&minio charts * change chart script:change redis.values.yaml * change chart script:add redis.config.yaml * change chart script:change redis.config.yaml * change chart script:change mongodb.value.yaml * change chart script:change mongodb.value.yaml * change chart script:add mongodb.config.yaml * change chart script:change minio.values.yaml * change chart script:add minio.config.yaml * change chart script:change kafka.values.yaml * change chart script:add kafka.config.yaml * change chart script:change services.config.yaml * bug fix:Delete websocket's Port restrictions * bug fix:change port value * change chart script:Submit a stable version script * fix bug:Implement option interface * fix bug:change K8sDR.Register * change config.yaml * change chats script:minio service add ingress * change chats script:minio service add ingress * change chats script:kafka.replicaCount=3& change minio.api ingress * delete change chats script * change config.yaml * change openim.yaml --------- Co-authored-by: lin.huang --- cmd/openim-api/main.go | 19 ++-- cmd/openim-push/main.go | 2 +- cmd/openim-rpc/openim-rpc-auth/main.go | 2 +- .../openim-rpc-conversation/main.go | 2 +- cmd/openim-rpc/openim-rpc-friend/main.go | 2 +- cmd/openim-rpc/openim-rpc-group/main.go | 2 +- cmd/openim-rpc/openim-rpc-msg/main.go | 2 +- cmd/openim-rpc/openim-rpc-third/main.go | 2 +- cmd/openim-rpc/openim-rpc-user/main.go | 2 +- config/config.yaml | 4 + deployments/templates/openim.yaml | 3 + internal/msggateway/n_ws_server.go | 3 - internal/msgtransfer/init.go | 15 ++-- internal/tools/msg.go | 14 +-- manifest/build-docker.sh | 37 ++++++++ manifest/dockerfiles/openim-api/Dockerfile | 27 ++++++ .../dockerfiles/openim-crontask/Dockerfile | 27 ++++++ .../dockerfiles/openim-msggateway/Dockerfile | 27 ++++++ .../dockerfiles/openim-msgtransfer/Dockerfile | 27 ++++++ manifest/dockerfiles/openim-push/Dockerfile | 27 ++++++ .../dockerfiles/openim-rpc-auth/Dockerfile | 27 ++++++ .../openim-rpc-conversation/Dockerfile | 27 ++++++ .../dockerfiles/openim-rpc-friend/Dockerfile | 27 ++++++ .../dockerfiles/openim-rpc-group/Dockerfile | 27 ++++++ .../dockerfiles/openim-rpc-msg/Dockerfile | 27 ++++++ .../dockerfiles/openim-rpc-third/Dockerfile | 27 ++++++ .../dockerfiles/openim-rpc-user/Dockerfile | 27 ++++++ pkg/common/cmd/api.go | 19 +++- pkg/common/cmd/constant.go | 12 +++ pkg/common/cmd/cron_task.go | 4 +- pkg/common/cmd/msg_gateway.go | 22 ++++- pkg/common/cmd/msg_transfer.go | 6 +- pkg/common/cmd/root.go | 23 ++++- pkg/common/cmd/rpc.go | 45 +++++++++- pkg/common/config/config.go | 12 ++- .../k8s_discovery_register.go | 88 +++++++++++++++++++ pkg/common/startrpc/start.go | 21 +++-- scripts/install/environment.sh | 2 + 38 files changed, 628 insertions(+), 61 deletions(-) create mode 100755 manifest/build-docker.sh create mode 100644 manifest/dockerfiles/openim-api/Dockerfile create mode 100644 manifest/dockerfiles/openim-crontask/Dockerfile create mode 100644 manifest/dockerfiles/openim-msggateway/Dockerfile create mode 100644 manifest/dockerfiles/openim-msgtransfer/Dockerfile create mode 100644 manifest/dockerfiles/openim-push/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-auth/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-conversation/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-friend/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-group/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-msg/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-third/Dockerfile create mode 100644 manifest/dockerfiles/openim-rpc-user/Dockerfile create mode 100644 pkg/common/cmd/constant.go create mode 100644 pkg/common/discovery_register/k8s_discovery_register.go diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index 17a7c4724..3034d6645 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -17,15 +17,13 @@ package main import ( "context" "fmt" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "net" - "strconv" - "time" - _ "net/http/pprof" + "strconv" "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/discoveryregistry" - openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" "github.com/openimsdk/open-im-server/v3/internal/api" @@ -44,6 +42,7 @@ func main() { } func run(port int) error { + fmt.Println("*****openimapi port:", port) if port == 0 { return fmt.Errorf("port is empty") } @@ -53,11 +52,13 @@ func run(port int) error { } fmt.Println("api start init discov client") var client discoveryregistry.SvcDiscoveryRegistry - client, err = openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - openkeeper.WithFreq(time.Hour), openkeeper.WithUserNameAndPassword( - config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password, - ), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger())) + client, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + /* + client, err = openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + openkeeper.WithFreq(time.Hour), openkeeper.WithUserNameAndPassword( + config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password, + ), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/ if err != nil { return err } diff --git a/cmd/openim-push/main.go b/cmd/openim-push/main.go index 6ab2fedab..c19cfda60 100644 --- a/cmd/openim-push/main.go +++ b/cmd/openim-push/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - pushCmd := cmd.NewRpcCmd("push") + pushCmd := cmd.NewRpcCmd(cmd.RpcPushServer) pushCmd.AddPortFlag() pushCmd.AddPrometheusPortFlag() if err := pushCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-auth/main.go b/cmd/openim-rpc/openim-rpc-auth/main.go index d27a3f11b..645d8cab8 100644 --- a/cmd/openim-rpc/openim-rpc-auth/main.go +++ b/cmd/openim-rpc/openim-rpc-auth/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - authCmd := cmd.NewRpcCmd("auth") + authCmd := cmd.NewRpcCmd(cmd.RpcAuthServer) authCmd.AddPortFlag() authCmd.AddPrometheusPortFlag() if err := authCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-conversation/main.go b/cmd/openim-rpc/openim-rpc-conversation/main.go index 80a92de92..13d7db605 100644 --- a/cmd/openim-rpc/openim-rpc-conversation/main.go +++ b/cmd/openim-rpc/openim-rpc-conversation/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - rpcCmd := cmd.NewRpcCmd("conversation") + rpcCmd := cmd.NewRpcCmd(cmd.RpcConversationServer) rpcCmd.AddPortFlag() rpcCmd.AddPrometheusPortFlag() if err := rpcCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-friend/main.go b/cmd/openim-rpc/openim-rpc-friend/main.go index c0c91e4dc..ec18306a2 100644 --- a/cmd/openim-rpc/openim-rpc-friend/main.go +++ b/cmd/openim-rpc/openim-rpc-friend/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - rpcCmd := cmd.NewRpcCmd("friend") + rpcCmd := cmd.NewRpcCmd(cmd.RpcFriendServer) rpcCmd.AddPortFlag() rpcCmd.AddPrometheusPortFlag() if err := rpcCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-group/main.go b/cmd/openim-rpc/openim-rpc-group/main.go index d2d4f727a..887329926 100644 --- a/cmd/openim-rpc/openim-rpc-group/main.go +++ b/cmd/openim-rpc/openim-rpc-group/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - rpcCmd := cmd.NewRpcCmd("group") + rpcCmd := cmd.NewRpcCmd(cmd.RpcGroupServer) rpcCmd.AddPortFlag() rpcCmd.AddPrometheusPortFlag() if err := rpcCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-msg/main.go b/cmd/openim-rpc/openim-rpc-msg/main.go index 0ac258d0c..dcc3abef5 100644 --- a/cmd/openim-rpc/openim-rpc-msg/main.go +++ b/cmd/openim-rpc/openim-rpc-msg/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - rpcCmd := cmd.NewRpcCmd("msg") + rpcCmd := cmd.NewRpcCmd(cmd.RpcMsgServer) rpcCmd.AddPortFlag() rpcCmd.AddPrometheusPortFlag() if err := rpcCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-third/main.go b/cmd/openim-rpc/openim-rpc-third/main.go index 913962b82..cf0bf4b70 100644 --- a/cmd/openim-rpc/openim-rpc-third/main.go +++ b/cmd/openim-rpc/openim-rpc-third/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - rpcCmd := cmd.NewRpcCmd("third") + rpcCmd := cmd.NewRpcCmd(cmd.RpcThirdServer) rpcCmd.AddPortFlag() rpcCmd.AddPrometheusPortFlag() if err := rpcCmd.Exec(); err != nil { diff --git a/cmd/openim-rpc/openim-rpc-user/main.go b/cmd/openim-rpc/openim-rpc-user/main.go index f2ebc94db..cbf2a8fc3 100644 --- a/cmd/openim-rpc/openim-rpc-user/main.go +++ b/cmd/openim-rpc/openim-rpc-user/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - rpcCmd := cmd.NewRpcCmd("user") + rpcCmd := cmd.NewRpcCmd(cmd.RpcUserServer) rpcCmd.AddPortFlag() rpcCmd.AddPrometheusPortFlag() if err := rpcCmd.Exec(); err != nil { diff --git a/config/config.yaml b/config/config.yaml index d2162d4b2..0f980b4e4 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -26,6 +26,9 @@ # Zookeeper address # Zookeeper username # Zookeeper password +envs: + discovery: zookeeper + zookeeper: schema: openim address: [ 172.28.0.1:12181 ] @@ -213,6 +216,7 @@ log: # Websocket connection handshake timeout longConnSvr: openImWsPort: [ 10001 ] + openImMessageGatewayPort: [ 10140 ] websocketMaxConnNum: 100000 websocketMaxMsgLen: 4096 websocketTimeout: 10 diff --git a/deployments/templates/openim.yaml b/deployments/templates/openim.yaml index b029826a9..ff0121062 100644 --- a/deployments/templates/openim.yaml +++ b/deployments/templates/openim.yaml @@ -19,6 +19,9 @@ # --| target: config/config.yaml # ----------------------------------------------------------------- +envs: + discovery: ${ENVS_DISCOVERY} + ###################### Zookeeper ###################### # Zookeeper configuration # It's not recommended to modify the schema diff --git a/internal/msggateway/n_ws_server.go b/internal/msggateway/n_ws_server.go index f437f9d8c..07a83fb5c 100644 --- a/internal/msggateway/n_ws_server.go +++ b/internal/msggateway/n_ws_server.go @@ -143,9 +143,6 @@ func NewWsServer(opts ...Option) (*WsServer, error) { for _, o := range opts { o(&config) } - if config.port < 1024 { - return nil, errors.New("port not allow to listen") - } v := validator.New() return &WsServer{ port: config.port, diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index db48ead70..4487826ee 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -16,14 +16,11 @@ package msgtransfer import ( "fmt" - "sync" - "time" - + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "sync" - openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" - "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" "github.com/openimsdk/open-im-server/v3/pkg/common/config" @@ -62,9 +59,11 @@ func StartTransfer(prometheusPort int) error { if err := mongo.CreateMsgIndex(); err != nil { return err } - client, err := openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - openkeeper.WithFreq(time.Hour), openkeeper.WithRoundRobin(), openkeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger())) + client, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + /* + client, err := openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + openkeeper.WithFreq(time.Hour), openkeeper.WithRoundRobin(), openkeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/ if err != nil { return err } diff --git a/internal/tools/msg.go b/internal/tools/msg.go index 93f5c3a8a..5397689b2 100644 --- a/internal/tools/msg.go +++ b/internal/tools/msg.go @@ -17,14 +17,12 @@ package tools import ( "context" "fmt" - "math" - "time" - + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "github.com/redis/go-redis/v9" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "math" - "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" @@ -74,9 +72,11 @@ func InitMsgTool() (*MsgTool, error) { if err != nil { return nil, err } - discov, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - zookeeper.WithFreq(time.Hour), zookeeper.WithRoundRobin(), zookeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger())) + discov, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + /* + discov, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + zookeeper.WithFreq(time.Hour), zookeeper.WithRoundRobin(), zookeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/ if err != nil { return nil, err } diff --git a/manifest/build-docker.sh b/manifest/build-docker.sh new file mode 100755 index 000000000..648e6370d --- /dev/null +++ b/manifest/build-docker.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +IMAGEHUB="registry.cn-shenzhen.aliyuncs.com/huanglin_hub" +PROJECT=$1 +ALLPRO="all" +servers=(openim-api openim-crontask openim-msggateway openim-msgtransfer openim-push openim-rpc-auth openim-rpc-conversation openim-rpc-friend openim-rpc-group openim-rpc-msg openim-rpc-third openim-rpc-user) + + +if [ "$1" != "" ] +then + if [[ "${servers[@]}" =~ "${1}" ]] + then + echo "building ${PROJECT}" + DOCKER_PUSHIMG=${IMAGEHUB}/${PROJECT}:dev + docker rmi ${DOCKER_PUSHIMG} + docker build -f manifest/dockerfiles/${PROJECT}/Dockerfile -t ${DOCKER_PUSHIMG} . + docker push ${DOCKER_PUSHIMG} + elif [[ ! "${servers[@]}" =~ "${1}" ]] + then + if [ ${PROJECT} == ${ALLPRO} ] + then + echo "building allproject" + for element in ${servers[@]} + do + SUB_IMG=${element} + SUB_PUSHIMG=${IMAGEHUB}/${element}:dev + docker rmi ${SUB_PUSHIMG} + docker build -f manifest/dockerfiles/${SUB_IMG}/Dockerfile -t ${SUB_PUSHIMG} . + docker push ${SUB_PUSHIMG} + done + else + echo "输入的项目名称不正确" + fi + fi +else + echo "请传入一个参数" +fi \ No newline at end of file diff --git a/manifest/dockerfiles/openim-api/Dockerfile b/manifest/dockerfiles/openim-api/Dockerfile new file mode 100644 index 000000000..c5198d2ad --- /dev/null +++ b/manifest/dockerfiles/openim-api/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-api/openim-api cmd/openim-api/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-api/openim-api /app/bin/ +ENTRYPOINT ["/app/bin/openim-api"] diff --git a/manifest/dockerfiles/openim-crontask/Dockerfile b/manifest/dockerfiles/openim-crontask/Dockerfile new file mode 100644 index 000000000..bba54b2e2 --- /dev/null +++ b/manifest/dockerfiles/openim-crontask/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-crontask/openim-crontask cmd/openim-crontask/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-crontask/openim-crontask /app/bin/ +ENTRYPOINT ["/app/bin/openim-crontask"] diff --git a/manifest/dockerfiles/openim-msggateway/Dockerfile b/manifest/dockerfiles/openim-msggateway/Dockerfile new file mode 100644 index 000000000..582178013 --- /dev/null +++ b/manifest/dockerfiles/openim-msggateway/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-msggateway/openim-msggateway cmd/openim-msggateway/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-msggateway/openim-msggateway /app/bin/ +ENTRYPOINT ["/app/bin/openim-msggateway"] diff --git a/manifest/dockerfiles/openim-msgtransfer/Dockerfile b/manifest/dockerfiles/openim-msgtransfer/Dockerfile new file mode 100644 index 000000000..1e08eb38a --- /dev/null +++ b/manifest/dockerfiles/openim-msgtransfer/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-msgtransfer/openim-msgtransfer cmd/openim-msgtransfer/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-msgtransfer/openim-msgtransfer /app/bin/ +ENTRYPOINT ["/app/bin/openim-msgtransfer"] diff --git a/manifest/dockerfiles/openim-push/Dockerfile b/manifest/dockerfiles/openim-push/Dockerfile new file mode 100644 index 000000000..c1ae3ed84 --- /dev/null +++ b/manifest/dockerfiles/openim-push/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-push/openim-push cmd/openim-push/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-push/openim-push /app/bin/ +ENTRYPOINT ["/app/bin/openim-push"] diff --git a/manifest/dockerfiles/openim-rpc-auth/Dockerfile b/manifest/dockerfiles/openim-rpc-auth/Dockerfile new file mode 100644 index 000000000..5124da7a1 --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-auth/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-auth/openim-rpc-auth cmd/openim-rpc/openim-rpc-auth/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-auth/openim-rpc-auth /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-auth"] diff --git a/manifest/dockerfiles/openim-rpc-conversation/Dockerfile b/manifest/dockerfiles/openim-rpc-conversation/Dockerfile new file mode 100644 index 000000000..539d441b9 --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-conversation/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-conversation/openim-rpc-conversation cmd/openim-rpc/openim-rpc-conversation/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-conversation/openim-rpc-conversation /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-conversation"] diff --git a/manifest/dockerfiles/openim-rpc-friend/Dockerfile b/manifest/dockerfiles/openim-rpc-friend/Dockerfile new file mode 100644 index 000000000..9927c1d30 --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-friend/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-friend/openim-rpc-friend cmd/openim-rpc/openim-rpc-friend/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-friend/openim-rpc-friend /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-friend"] diff --git a/manifest/dockerfiles/openim-rpc-group/Dockerfile b/manifest/dockerfiles/openim-rpc-group/Dockerfile new file mode 100644 index 000000000..754151156 --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-group/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-group/openim-rpc-group cmd/openim-rpc/openim-rpc-group/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-group/openim-rpc-group /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-group"] diff --git a/manifest/dockerfiles/openim-rpc-msg/Dockerfile b/manifest/dockerfiles/openim-rpc-msg/Dockerfile new file mode 100644 index 000000000..0f942ba8e --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-msg/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-msg/openim-rpc-msg cmd/openim-rpc/openim-rpc-msg/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-msg/openim-rpc-msg /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-msg"] diff --git a/manifest/dockerfiles/openim-rpc-third/Dockerfile b/manifest/dockerfiles/openim-rpc-third/Dockerfile new file mode 100644 index 000000000..a1391c0c3 --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-third/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-third/openim-rpc-third cmd/openim-rpc/openim-rpc-third/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-third/openim-rpc-third /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-third"] diff --git a/manifest/dockerfiles/openim-rpc-user/Dockerfile b/manifest/dockerfiles/openim-rpc-user/Dockerfile new file mode 100644 index 000000000..b45b4aa2f --- /dev/null +++ b/manifest/dockerfiles/openim-rpc-user/Dockerfile @@ -0,0 +1,27 @@ + +# build container +FROM golang:1.20-alpine3.18 AS builder +ENV GOPROXY https://goproxy.cn,direct +ENV GOSUMDB=sum.golang.google.cn +ENV GO111MODULE=on + +WORKDIR /app +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add git pkgconfig build-base +ADD go.mod . +ADD go.sum . +RUN go mod download +ADD . . +RUN go build -o cmd/openim-rpc/openim-rpc-user/openim-rpc-user cmd/openim-rpc/openim-rpc-user/main.go + +# archive container +FROM alpine:3.18 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +RUN apk --no-cache add ca-certificates libdrm +RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone \ + && apk del tzdata + +WORKDIR /app/bin +COPY --from=builder /app/cmd/openim-rpc/openim-rpc-user/openim-rpc-user /app/bin/ +ENTRYPOINT ["/app/bin/openim-rpc-user"] diff --git a/pkg/common/cmd/api.go b/pkg/common/cmd/api.go index 4cb5e34fc..19b11d4b2 100644 --- a/pkg/common/cmd/api.go +++ b/pkg/common/cmd/api.go @@ -14,14 +14,21 @@ package cmd -import "github.com/spf13/cobra" +import ( + "fmt" + "github.com/OpenIMSDK/protocol/constant" + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/spf13/cobra" +) type ApiCmd struct { *RootCmd } func NewApiCmd() *ApiCmd { - return &ApiCmd{NewRootCmd("api")} + ret := &ApiCmd{NewRootCmd("api")} + ret.SetRootCmdPt(ret) + return ret } func (a *ApiCmd) AddApi(f func(port int) error) { @@ -29,3 +36,11 @@ func (a *ApiCmd) AddApi(f func(port int) error) { return f(a.getPortFlag(cmd)) } } +func (a *ApiCmd) GetPortFromConfig(portType string) int { + fmt.Println("GetPortFromConfig:", portType) + if portType == constant.FlagPort { + return config2.Config.Api.OpenImApiPort[0] + } else { + return 0 + } +} diff --git a/pkg/common/cmd/constant.go b/pkg/common/cmd/constant.go new file mode 100644 index 000000000..835593bbe --- /dev/null +++ b/pkg/common/cmd/constant.go @@ -0,0 +1,12 @@ +package cmd + +const ( + RpcPushServer = "push" + RpcAuthServer = "auth" + RpcConversationServer = "conversation" + RpcFriendServer = "friend" + RpcGroupServer = "group" + RpcMsgServer = "msg" + RpcThirdServer = "third" + RpcUserServer = "user" +) diff --git a/pkg/common/cmd/cron_task.go b/pkg/common/cmd/cron_task.go index 25dc9aae4..1b0e796ac 100644 --- a/pkg/common/cmd/cron_task.go +++ b/pkg/common/cmd/cron_task.go @@ -21,7 +21,9 @@ type CronTaskCmd struct { } func NewCronTaskCmd() *CronTaskCmd { - return &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())} + ret := &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())} + ret.SetRootCmdPt(ret) + return ret } func (c *CronTaskCmd) addRunE(f func() error) { diff --git a/pkg/common/cmd/msg_gateway.go b/pkg/common/cmd/msg_gateway.go index 34a9f3b4e..c96bbd7af 100644 --- a/pkg/common/cmd/msg_gateway.go +++ b/pkg/common/cmd/msg_gateway.go @@ -16,6 +16,8 @@ package cmd import ( "github.com/openimsdk/open-im-server/v3/internal/msggateway" + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + //"github.com/openimsdk/open-im-server/internal/msggateway". "github.com/spf13/cobra" @@ -26,8 +28,10 @@ type MsgGatewayCmd struct { *RootCmd } -func NewMsgGatewayCmd() MsgGatewayCmd { - return MsgGatewayCmd{NewRootCmd("msgGateway")} +func NewMsgGatewayCmd() *MsgGatewayCmd { + ret := &MsgGatewayCmd{NewRootCmd("msgGateway")} + ret.SetRootCmdPt(ret) + return ret } func (m *MsgGatewayCmd) AddWsPortFlag() { @@ -36,6 +40,9 @@ func (m *MsgGatewayCmd) AddWsPortFlag() { func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int { port, _ := cmd.Flags().GetInt(constant.FlagWsPort) + if port == 0 { + port = m.PortFromConfig(constant.FlagWsPort) + } return port } @@ -49,3 +56,14 @@ func (m *MsgGatewayCmd) Exec() error { m.addRunE() return m.Execute() } +func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int { + if portType == constant.FlagWsPort { + return config2.Config.LongConnSvr.OpenImWsPort[0] + } else if portType == constant.FlagPort { + return config2.Config.LongConnSvr.OpenImMessageGatewayPort[0] + } else if portType == constant.FlagPrometheusPort { + return 0 + } else { + return 0 + } +} diff --git a/pkg/common/cmd/msg_transfer.go b/pkg/common/cmd/msg_transfer.go index ae67ee9f7..20349ebbb 100644 --- a/pkg/common/cmd/msg_transfer.go +++ b/pkg/common/cmd/msg_transfer.go @@ -24,8 +24,10 @@ type MsgTransferCmd struct { *RootCmd } -func NewMsgTransferCmd() MsgTransferCmd { - return MsgTransferCmd{NewRootCmd("msgTransfer")} +func NewMsgTransferCmd() *MsgTransferCmd { + ret := &MsgTransferCmd{NewRootCmd("msgTransfer")} + ret.SetRootCmdPt(ret) + return ret } func (m *MsgTransferCmd) addRunE() { diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 508ef8377..6973da9d6 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -26,11 +26,15 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) +type RootCmdPt interface { + GetPortFromConfig(portType string) int +} type RootCmd struct { Command cobra.Command Name string port int prometheusPort int + cmdItf RootCmdPt } type CmdOpts struct { @@ -76,7 +80,9 @@ func NewRootCmd(name string, opts ...func(*CmdOpts)) (rootCmd *RootCmd) { rootCmd.addConfFlag() return rootCmd } - +func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) { + r.cmdItf = cmdItf +} func (r *RootCmd) addConfFlag() { r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") } @@ -87,6 +93,9 @@ func (r *RootCmd) AddPortFlag() { func (r *RootCmd) getPortFlag(cmd *cobra.Command) int { port, _ := cmd.Flags().GetInt(constant.FlagPort) + if port == 0 { + port = r.PortFromConfig(constant.FlagPort) + } return port } @@ -100,6 +109,9 @@ func (r *RootCmd) AddPrometheusPortFlag() { func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int { port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort) + if port == 0 { + port = r.PortFromConfig(constant.FlagPrometheusPort) + } return port } @@ -120,3 +132,12 @@ func (r *RootCmd) Execute() error { func (r *RootCmd) AddCommand(cmds ...*cobra.Command) { r.Command.AddCommand(cmds...) } + +func (r *RootCmd) GetPortFromConfig(portType string) int { + fmt.Println("RootCmd.GetPortFromConfig:", portType) + return 0 +} +func (r *RootCmd) PortFromConfig(portType string) int { + fmt.Println("PortFromConfig:", portType) + return r.cmdItf.GetPortFromConfig(portType) +} diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go index a5fc1164c..224edc0a0 100644 --- a/pkg/common/cmd/rpc.go +++ b/pkg/common/cmd/rpc.go @@ -16,7 +16,8 @@ package cmd import ( "errors" - + "github.com/OpenIMSDK/protocol/constant" + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/spf13/cobra" "google.golang.org/grpc" @@ -30,8 +31,9 @@ type RpcCmd struct { } func NewRpcCmd(name string) *RpcCmd { - authCmd := &RpcCmd{NewRootCmd(name)} - return authCmd + ret := &RpcCmd{NewRootCmd(name)} + ret.SetRootCmdPt(ret) + return ret } func (a *RpcCmd) Exec() error { @@ -51,3 +53,40 @@ func (a *RpcCmd) StartSvr( } return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn) } +func (a *RpcCmd) GetPortFromConfig(portType string) int { + switch a.Name { + case RpcPushServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImPushPort[0] + } + case RpcAuthServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImAuthPort[0] + } + case RpcConversationServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImConversationPort[0] + } + case RpcFriendServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImFriendPort[0] + } + case RpcGroupServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImGroupPort[0] + } + case RpcMsgServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImMessagePort[0] + } + case RpcThirdServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImThirdPort[0] + } + case RpcUserServer: + if portType == constant.FlagPort { + return config2.Config.RpcPort.OpenImUserPort[0] + } + } + return 0 +} diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 966033247..95f4a864e 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -46,6 +46,9 @@ type POfflinePush struct { } type configStruct struct { + Envs struct { + Discovery string `yaml:"discovery"` + } Zookeeper struct { Schema string `yaml:"schema"` ZkAddr []string `yaml:"address"` @@ -185,10 +188,11 @@ type configStruct struct { } `yaml:"log"` LongConnSvr struct { - OpenImWsPort []int `yaml:"openImWsPort"` - WebsocketMaxConnNum int `yaml:"websocketMaxConnNum"` - WebsocketMaxMsgLen int `yaml:"websocketMaxMsgLen"` - WebsocketTimeout int `yaml:"websocketTimeout"` + OpenImMessageGatewayPort []int `yaml:"openImMessageGatewayPort"` + OpenImWsPort []int `yaml:"openImWsPort"` + WebsocketMaxConnNum int `yaml:"websocketMaxConnNum"` + WebsocketMaxMsgLen int `yaml:"websocketMaxMsgLen"` + WebsocketTimeout int `yaml:"websocketTimeout"` } `yaml:"longConnSvr"` Push struct { diff --git a/pkg/common/discovery_register/k8s_discovery_register.go b/pkg/common/discovery_register/k8s_discovery_register.go new file mode 100644 index 000000000..70f9f39f3 --- /dev/null +++ b/pkg/common/discovery_register/k8s_discovery_register.go @@ -0,0 +1,88 @@ +package discovery_register + +import ( + "context" + "errors" + "fmt" + "github.com/OpenIMSDK/tools/discoveryregistry" + openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" + "github.com/OpenIMSDK/tools/log" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "google.golang.org/grpc" + "time" +) + +func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) { + var client discoveryregistry.SvcDiscoveryRegistry + var err error + switch envType { + case "zookeeper": + client, err = openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + openkeeper.WithFreq(time.Hour), openkeeper.WithUserNameAndPassword( + config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password, + ), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger())) + case "k8s": + client, err = NewK8sDiscoveryRegister() + default: + client = nil + err = errors.New("envType not correct") + } + return client, err +} + +type K8sDR struct { + options []grpc.DialOption + rpcRegisterAddr string +} + +func NewK8sDiscoveryRegister() (discoveryregistry.SvcDiscoveryRegistry, error) { + return &K8sDR{}, nil +} + +func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error { + cli.rpcRegisterAddr = serviceName + return nil +} +func (cli *K8sDR) UnRegister() error { + + return nil +} +func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error { + + return nil +} +func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error { + + return nil +} + +func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) { + + return nil, nil +} +func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) { + + conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) + return []*grpc.ClientConn{conn}, err +} +func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + + return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) +} +func (cli *K8sDR) GetSelfConnTarget() string { + + return cli.rpcRegisterAddr +} +func (cli *K8sDR) AddOption(opts ...grpc.DialOption) { + cli.options = append(cli.options, opts...) +} +func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) { + conn.Close() +} + +// do not use this method for call rpc +func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn { + fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!") + return nil +} diff --git a/pkg/common/startrpc/start.go b/pkg/common/startrpc/start.go index 3ef6e569d..19da8a586 100644 --- a/pkg/common/startrpc/start.go +++ b/pkg/common/startrpc/start.go @@ -16,19 +16,16 @@ package startrpc import ( "fmt" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "net" "strconv" - "time" - - "github.com/openimsdk/open-im-server/v3/pkg/common/config" grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "github.com/OpenIMSDK/tools/discoveryregistry" - "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" - "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" "github.com/OpenIMSDK/tools/network" "github.com/OpenIMSDK/tools/prome" @@ -60,15 +57,17 @@ func Start( return err } defer listener.Close() - zkClient, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - zookeeper.WithFreq(time.Hour), zookeeper.WithUserNameAndPassword( - config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password, - ), zookeeper.WithRoundRobin(), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger())) + zkClient, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + /* + zkClient, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + zookeeper.WithFreq(time.Hour), zookeeper.WithUserNameAndPassword( + config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password, + ), zookeeper.WithRoundRobin(), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/ if err != nil { return utils.Wrap1(err) } - defer zkClient.CloseZK() + //defer zkClient.CloseZK() zkClient.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) registerIP, err := network.GetRpcRegisterIP(config.Config.Rpc.RegisterIP) if err != nil { diff --git a/scripts/install/environment.sh b/scripts/install/environment.sh index ce8eb56ec..c5407f5e3 100755 --- a/scripts/install/environment.sh +++ b/scripts/install/environment.sh @@ -150,6 +150,8 @@ def "OPENIM_CHAT_NAME" "chat" # openim-chat chat用户名 # TODO 注意: 一般的配置都可以使用 def 函数来定义,如果是包含特殊字符,比如说: # TODO readonly MSG_DESTRUCT_TIME=${MSG_DESTRUCT_TIME:-'0 2 * * *'} # TODO 使用 readonly 来定义合适,负责无法正常解析, 并且 yaml 模板需要加 "" 来包裹 +###################### Env 配置信息 ###################### +def "ENVS_DISCOVERY" "zookeeper" ###################### Zookeeper 配置信息 ###################### def "ZOOKEEPER_SCHEMA" "openim" # Zookeeper的模式 From a648beecb30f1c5e4590e5634dfbb4974e3db8fa Mon Sep 17 00:00:00 2001 From: "fengyun.rui" Date: Wed, 18 Oct 2023 19:47:57 +0800 Subject: [PATCH 02/21] optimize: reduce lock latency for conversation cache (#1231) Signed-off-by: rfyiamcool --- pkg/common/db/localcache/conversation.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/common/db/localcache/conversation.go b/pkg/common/db/localcache/conversation.go index 3ede6fd4c..c40bcdbce 100644 --- a/pkg/common/db/localcache/conversation.go +++ b/pkg/common/db/localcache/conversation.go @@ -60,9 +60,11 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID if err != nil { return nil, err } + g.lock.Lock() - defer g.lock.Unlock() hash, ok := g.conversationIDs[userID] + g.lock.Unlock() + if !ok || hash.hash != resp.Hash { conversationIDsResp, err := g.client.Client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{ UserID: userID, @@ -70,11 +72,16 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID if err != nil { return nil, err } + + g.lock.Lock() + defer g.lock.Unlock() g.conversationIDs[userID] = Hash{ hash: resp.Hash, ids: conversationIDsResp.ConversationIDs, } + return conversationIDsResp.ConversationIDs, nil } + return hash.ids, nil } From 0363763171a2cb156af966bdf1d17e2d27896f1b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:18:14 +0800 Subject: [PATCH 03/21] feat(deps): bump the gomod-deps group with 7 updates (#1248) Bumps the gomod-deps group with 7 updates: | Package | From | To | | --- | --- | --- | | [golang.org/x/image](https://github.com/golang/image) | `0.12.0` | `0.13.0` | | [google.golang.org/api](https://github.com/googleapis/google-api-go-client) | `0.143.0` | `0.147.0` | | [google.golang.org/grpc](https://github.com/grpc/grpc-go) | `1.58.2` | `1.59.0` | | [gorm.io/driver/mysql](https://github.com/go-gorm/mysql) | `1.5.1` | `1.5.2` | | [github.com/IBM/sarama](https://github.com/IBM/sarama) | `1.41.2` | `1.41.3` | | [github.com/OpenIMSDK/protocol](https://github.com/OpenIMSDK/protocol) | `0.0.26` | `0.0.27` | | [github.com/tencentyun/cos-go-sdk-v5](https://github.com/tencentyun/cos-go-sdk-v5) | `0.7.44` | `0.7.45` | Updates `golang.org/x/image` from 0.12.0 to 0.13.0 - [Commits](https://github.com/golang/image/compare/v0.12.0...v0.13.0) Updates `google.golang.org/api` from 0.143.0 to 0.147.0 - [Release notes](https://github.com/googleapis/google-api-go-client/releases) - [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md) - [Commits](https://github.com/googleapis/google-api-go-client/compare/v0.143.0...v0.147.0) Updates `google.golang.org/grpc` from 1.58.2 to 1.59.0 - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.58.2...v1.59.0) Updates `gorm.io/driver/mysql` from 1.5.1 to 1.5.2 - [Commits](https://github.com/go-gorm/mysql/compare/v1.5.1...v1.5.2) Updates `github.com/IBM/sarama` from 1.41.2 to 1.41.3 - [Release notes](https://github.com/IBM/sarama/releases) - [Changelog](https://github.com/IBM/sarama/blob/main/CHANGELOG.md) - [Commits](https://github.com/IBM/sarama/compare/v1.41.2...v1.41.3) Updates `github.com/OpenIMSDK/protocol` from 0.0.26 to 0.0.27 - [Release notes](https://github.com/OpenIMSDK/protocol/releases) - [Commits](https://github.com/OpenIMSDK/protocol/compare/v0.0.26...v0.0.27) Updates `github.com/tencentyun/cos-go-sdk-v5` from 0.7.44 to 0.7.45 - [Release notes](https://github.com/tencentyun/cos-go-sdk-v5/releases) - [Changelog](https://github.com/tencentyun/cos-go-sdk-v5/blob/master/CHANGELOG.md) - [Commits](https://github.com/tencentyun/cos-go-sdk-v5/compare/v0.7.44...v0.7.45) --- updated-dependencies: - dependency-name: golang.org/x/image dependency-type: direct:production update-type: version-update:semver-minor dependency-group: gomod-deps - dependency-name: google.golang.org/api dependency-type: direct:production update-type: version-update:semver-minor dependency-group: gomod-deps - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor dependency-group: gomod-deps - dependency-name: gorm.io/driver/mysql dependency-type: direct:production update-type: version-update:semver-patch dependency-group: gomod-deps - dependency-name: github.com/IBM/sarama dependency-type: direct:production update-type: version-update:semver-patch dependency-group: gomod-deps - dependency-name: github.com/OpenIMSDK/protocol dependency-type: direct:production update-type: version-update:semver-patch dependency-group: gomod-deps - dependency-name: github.com/tencentyun/cos-go-sdk-v5 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: gomod-deps ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 31 +++++++++++++-------------- go.sum | 67 ++++++++++++++++++++++++++++------------------------------ 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index 7413edd8c..8510f780d 100644 --- a/go.mod +++ b/go.mod @@ -24,34 +24,34 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/stretchr/testify v1.8.4 go.mongodb.org/mongo-driver v1.12.1 - golang.org/x/image v0.12.0 - google.golang.org/api v0.143.0 - google.golang.org/grpc v1.58.2 + golang.org/x/image v0.13.0 + google.golang.org/api v0.147.0 + google.golang.org/grpc v1.59.0 google.golang.org/protobuf v1.31.0 gopkg.in/yaml.v3 v3.0.1 - gorm.io/driver/mysql v1.5.1 + gorm.io/driver/mysql v1.5.2 gorm.io/gorm v1.25.5 ) require github.com/google/uuid v1.3.1 require ( - github.com/IBM/sarama v1.41.2 - github.com/OpenIMSDK/protocol v0.0.26 + github.com/IBM/sarama v1.41.3 + github.com/OpenIMSDK/protocol v0.0.27 github.com/OpenIMSDK/tools v0.0.14 github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible github.com/go-redis/redis v6.15.9+incompatible github.com/go-sql-driver/mysql v1.7.1 github.com/redis/go-redis/v9 v9.2.1 - github.com/tencentyun/cos-go-sdk-v5 v0.7.44 + github.com/tencentyun/cos-go-sdk-v5 v0.7.45 ) require ( - cloud.google.com/go v0.110.7 // indirect + cloud.google.com/go v0.110.8 // indirect cloud.google.com/go/compute v1.23.0 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect - cloud.google.com/go/firestore v1.12.0 // indirect - cloud.google.com/go/iam v1.1.1 // indirect + cloud.google.com/go/firestore v1.13.0 // indirect + cloud.google.com/go/iam v1.1.2 // indirect cloud.google.com/go/longrunning v0.5.1 // indirect cloud.google.com/go/storage v1.30.1 // indirect github.com/beorn7/perks v1.0.1 // indirect @@ -71,7 +71,6 @@ require ( github.com/go-zookeeper/zk v1.0.3 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/snappy v0.0.4 // indirect - github.com/google/go-cmp v0.5.9 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/s2a-go v0.1.7 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect @@ -120,16 +119,16 @@ require ( go.uber.org/multierr v1.6.0 // indirect golang.org/x/arch v0.3.0 // indirect golang.org/x/net v0.17.0 // indirect - golang.org/x/oauth2 v0.12.0 // indirect - golang.org/x/sync v0.3.0 // indirect + golang.org/x/oauth2 v0.13.0 // indirect + golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect golang.org/x/time v0.3.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect + google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect ) require ( diff --git a/go.sum b/go.sum index a72bd884a..0e19ebfbf 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,14 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.110.7 h1:rJyC7nWRg2jWGZ4wSJ5nY65GTdYJkg0cd/uXb+ACI6o= -cloud.google.com/go v0.110.7/go.mod h1:+EYjdK8e5RME/VY/qLCAtuyALQ9q67dvuum8i+H5xsI= +cloud.google.com/go v0.110.8 h1:tyNdfIxjzaWctIiLYOTalaLKZ17SI44SKFW26QbOhME= +cloud.google.com/go v0.110.8/go.mod h1:Iz8AkXJf1qmxC3Oxoep8R1T36w8B92yU29PcBhHO5fk= cloud.google.com/go/compute v1.23.0 h1:tP41Zoavr8ptEqaW6j+LQOnyBBhO7OkOMAGrgLopTwY= cloud.google.com/go/compute v1.23.0/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM= cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= -cloud.google.com/go/firestore v1.12.0 h1:aeEA/N7DW7+l2u5jtkO8I0qv0D95YwjggD8kUHrTHO4= -cloud.google.com/go/firestore v1.12.0/go.mod h1:b38dKhgzlmNNGTNZZwe7ZRFEuRab1Hay3/DBsIGKKy4= -cloud.google.com/go/iam v1.1.1 h1:lW7fzj15aVIXYHREOqjRBV9PsH0Z6u8Y46a1YGvQP4Y= -cloud.google.com/go/iam v1.1.1/go.mod h1:A5avdyVL2tCppe4unb0951eI9jreack+RJ0/d+KUZOU= +cloud.google.com/go/firestore v1.13.0 h1:/3S4RssUV4GO/kvgJZB+tayjhOfyAHs+KcpJgRVu/Qk= +cloud.google.com/go/firestore v1.13.0/go.mod h1:QojqqOh8IntInDUSTAh0c8ZsPYAr68Ma8c5DWOy8xb8= +cloud.google.com/go/iam v1.1.2 h1:gacbrBdWcoVmGLozRuStX45YKvJtzIjJdAolzUs1sm4= +cloud.google.com/go/iam v1.1.2/go.mod h1:A5avdyVL2tCppe4unb0951eI9jreack+RJ0/d+KUZOU= cloud.google.com/go/longrunning v0.5.1 h1:Fr7TXftcqTudoyRJa113hyaqlGdiBQkp0Gq7tErFDWI= cloud.google.com/go/longrunning v0.5.1/go.mod h1:spvimkwdz6SPWKEt/XBij79E9fiTkHSQl/fRUUQJYJc= cloud.google.com/go/storage v1.30.1 h1:uOdMxAs8HExqBlnLtnQyP0YkvbiDpdGShGKtx6U/oNM= @@ -16,10 +16,10 @@ cloud.google.com/go/storage v1.30.1/go.mod h1:NfxhC0UJE1aXSx7CIIbCf7y9HKT7Biccwk firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4= firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/IBM/sarama v1.41.2 h1:ZDBZfGPHAD4uuAtSv4U22fRZBgst0eEwGFzLj0fb85c= -github.com/IBM/sarama v1.41.2/go.mod h1:xdpu7sd6OE1uxNdjYTSKUfY8FaKkJES9/+EyjSgiGQk= -github.com/OpenIMSDK/protocol v0.0.26 h1:jzq7EemhkO8W5kvOOg2f0b7OAMzMPrIBUG0GVbSNhDA= -github.com/OpenIMSDK/protocol v0.0.26/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= +github.com/IBM/sarama v1.41.3 h1:MWBEJ12vHC8coMjdEXFq/6ftO6DUZnQlFYcxtOJFa7c= +github.com/IBM/sarama v1.41.3/go.mod h1:Xxho9HkHd4K/MDUo/T/sOqwtX/17D33++E9Wib6hUdQ= +github.com/OpenIMSDK/protocol v0.0.27 h1:ee1Y6idLahgJrTHkgWPboKIvCT9zvf7jitavVpffpPw= +github.com/OpenIMSDK/protocol v0.0.27/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= github.com/OpenIMSDK/tools v0.0.14 h1:WLof/+WxyPyRST+QkoTKubYCiV73uCLiL8pgnpH/yKQ= github.com/OpenIMSDK/tools v0.0.14/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= @@ -52,6 +52,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dtm-labs/rockscache v0.1.1 h1:6S1vgaHvGqrLd8Ka4hRTKeKPV7v+tT0MSkTIX81LRyA= @@ -131,7 +132,6 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= @@ -295,8 +295,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563/go.mod h1:uom4Nvi9W+Qkom0exYiJ9VWJjXwyxtPYTkKkaLMlfE0= -github.com/tencentyun/cos-go-sdk-v5 v0.7.44 h1:Vvz28KVdmSUrwTH2MWgAMlhzUAh+lQBSSAW1J7qJDW8= -github.com/tencentyun/cos-go-sdk-v5 v0.7.44/go.mod h1:LUFnaqRmGk6pEHOaRmdn2dCZR2j0cSsM5xowWFPTPao= +github.com/tencentyun/cos-go-sdk-v5 v0.7.45 h1:5/ZGOv846tP6+2X7w//8QjLgH2KcUK+HciFbfjWquFU= +github.com/tencentyun/cos-go-sdk-v5 v0.7.45/go.mod h1:DH9US8nB+AJXqwu/AMOrCFN1COv3dpytXuJWHgdg7kE= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= @@ -335,15 +335,14 @@ golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58 golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ= -golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk= +golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg= +golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -366,8 +365,8 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4= -golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba4= +golang.org/x/oauth2 v0.13.0 h1:jDDenyj+WgFtmV3zYVoi8aE2BwtXFLWOA67ZfNWftiY= +golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -375,9 +374,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -424,15 +422,14 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= -google.golang.org/api v0.143.0 h1:o8cekTkqhywkbZT6p1UHJPZ9+9uuCAJs/KYomxZB8fA= -google.golang.org/api v0.143.0/go.mod h1:FoX9DO9hT7DLNn97OuoZAGSDuNAXdJRuGK98rSUgurk= +google.golang.org/api v0.147.0 h1:Can3FaQo9LlVqxJCodNmeZW/ib3/qKAY3rFeXiHo5gc= +google.golang.org/api v0.147.0/go.mod h1:pQ/9j83DcmPd/5C9e2nFOdjjNkDZ1G+zkbK2uvdkJMs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= @@ -440,19 +437,19 @@ google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCID google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb h1:XFBgcDwm7irdHTbz4Zk2h7Mh+eis4nfJEFQFYzJzuIA= -google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4= -google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb h1:lK0oleSc7IQsUxO3U5TjL9DWlsxpEBemh+zpB7IqhWI= -google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 h1:N3bU/SQDCDyD6R528GJ/PwW9KjYcJA3dgyH+MovAkIM= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13/go.mod h1:KSqppvjFjtoCI+KGd4PELB0qLNxdJHRGqRI09mB6pQA= +google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 h1:SeZZZx0cP0fqUyA+oRzP9k7cSwJlvDFiROO72uwD6i0= +google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97/go.mod h1:t1VqOqqvce95G3hIDCT5FeO3YUc6Q4Oe24L/+rNMxRk= +google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU= +google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c h1:jHkCUWkseRf+W+edG5hMzr/Uh1xkDREY4caybAq4dpY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c/go.mod h1:4cYg8o5yUbm77w8ZX00LhMVNl/YVBFJRYWDc0uYWMs0= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= -google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= +google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -481,9 +478,9 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= -gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= -gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs= +gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8= +gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 2d86a1a0e5b72265cb9b430260639679f75ecd5a Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:32:00 +0800 Subject: [PATCH 04/21] fix: remove repeat platform on online status. (#1221) * fix: to start im or chat, ZooKeeper must be started first. * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: go mod update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: get all userID Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: msggateway add online status call Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * chore: network mode change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * feat: add api of get server time Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: remove go work sum Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: pull message add isRead field Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: check msg-transfer script Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: start don't kill old process Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: check component Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: pull message set isRead only message come from single. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add ex field to update group info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: message log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: send message test log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * test: remove info log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: api of send message add sendTime field. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add callback for update user's info. * cicd: robot automated Change * fix: change callback command name. * cicd: robot automated Change * fix: single chat unread status change. * fix: single chat unread status change. * fix: single chat unread status change. * fix: user status change. * cicd: robot automated Change * fix: user status change. * fix: user status change. * fix: user status change. * cicd: robot automated Change * fix: ws close when user logout. * fix: remove repeat platform on online status. --------- Signed-off-by: Gordon <1432970085@qq.com> Signed-off-by: withchao <993506633@qq.com> Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: withchao <993506633@qq.com> Co-authored-by: Xinwei Xiong <3293172751NSS@gmail.com> Co-authored-by: FGadvancer --- pkg/common/db/cache/user.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index 5fb0fdde6..b821b4a52 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -287,10 +287,12 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu if err != nil { return errs.Wrap(err) } + onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID)) + } else { + onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID) } onlineStatus.Status = constant.Online onlineStatus.UserID = userID - onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID) newjsonData, err := json.Marshal(&onlineStatus) if err != nil { return errs.Wrap(err) @@ -304,3 +306,20 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu return nil } + +type Comparable interface { + ~int | ~string | ~float64 | ~int32 +} + +func RemoveRepeatedElementsInList[T Comparable](slc []T) []T { + var result []T + tempMap := map[T]struct{}{} + for _, e := range slc { + if _, found := tempMap[e]; !found { + tempMap[e] = struct{}{} + result = append(result, e) + } + } + + return result +} From 2840bc27fe8c65245fb19c15c95b8b6dc4ae6eb7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:36:13 +0800 Subject: [PATCH 05/21] chore(deps): bump the github-actions group with 1 update (#1169) Bumps the github-actions group with 1 update: [stefanzweifel/git-auto-commit-action](https://github.com/stefanzweifel/git-auto-commit-action). - [Release notes](https://github.com/stefanzweifel/git-auto-commit-action/releases) - [Changelog](https://github.com/stefanzweifel/git-auto-commit-action/blob/master/CHANGELOG.md) - [Commits](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4...v5) --- updated-dependencies: - dependency-name: stefanzweifel/git-auto-commit-action dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/create_branch_on_tag.yml | 4 ++-- .github/workflows/openimci.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/create_branch_on_tag.yml b/.github/workflows/create_branch_on_tag.yml index 26d5ed888..1ea5583bb 100644 --- a/.github/workflows/create_branch_on_tag.yml +++ b/.github/workflows/create_branch_on_tag.yml @@ -48,7 +48,7 @@ jobs: fi - name: Push Changes - uses: stefanzweifel/git-auto-commit-action@v4 + uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: "Auto Commit CHANGELOG" branch: release-v${VERSION_PARTS[0]}.${VERSION_PARTS[1]} @@ -68,7 +68,7 @@ jobs: fi - name: Push Changes - uses: stefanzweifel/git-auto-commit-action@v4 + uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: "Auto Commit CHANGELOG" branch: main diff --git a/.github/workflows/openimci.yml b/.github/workflows/openimci.yml index ec07c45ff..0f0aa2049 100644 --- a/.github/workflows/openimci.yml +++ b/.github/workflows/openimci.yml @@ -86,7 +86,7 @@ jobs: run: sudo make clean - name: Push Changes to Main - uses: stefanzweifel/git-auto-commit-action@v4 + uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: "cicd: robot automated Change" branch: main From 1c1f2a96bac276a171f33b2dc6f42defc4afcb0a Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:39:01 +0800 Subject: [PATCH 07/21] feat: Add unit test for RemoveRepeatedElementsInLi (#1223) Co-authored-by: sweep-ai[bot] <128439645+sweep-ai[bot]@users.noreply.github.com> --- tests/user_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/user_test.go diff --git a/tests/user_test.go b/tests/user_test.go new file mode 100644 index 000000000..5278f27f1 --- /dev/null +++ b/tests/user_test.go @@ -0,0 +1,51 @@ +package cache_test + +import ( + "reflect" + "testing" + + "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" +) + +func TestRemoveRepeatedElementsInList(t *testing.T) { + testCases := []struct { + name string + input []string + expected []string + }{ + { + name: "No duplicates", + input: []string{"a", "b", "c"}, + expected: []string{"a", "b", "c"}, + }, + { + name: "All duplicates", + input: []string{"a", "a", "a"}, + expected: []string{"a"}, + }, + { + name: "Some duplicates", + input: []string{"a", "b", "a", "c", "b"}, + expected: []string{"a", "b", "c"}, + }, + { + name: "Empty list", + input: []string{}, + expected: []string{}, + }, + { + name: "Single element", + input: []string{"a"}, + expected: []string{"a"}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := cache.RemoveRepeatedElementsInList(tc.input) + if !reflect.DeepEqual(result, tc.expected) { + t.Errorf("expected %v, got %v", tc.expected, result) + } + }) + } +} From 8a54e465a2d32e60e30bf3d5d572034b0a755dd9 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:41:49 +0800 Subject: [PATCH 08/21] feat: Add unit test for DeleteGroupMemberHash meth (#1200) Co-authored-by: sweep-ai[bot] <128439645+sweep-ai[bot]@users.noreply.github.com> --- tests/group_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/group_test.go diff --git a/tests/group_test.go b/tests/group_test.go new file mode 100644 index 000000000..54878805e --- /dev/null +++ b/tests/group_test.go @@ -0,0 +1,22 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" +) + +func TestDeleteGroupMemberHash(t *testing.T) { + mockGroupDB := new(controller.MockGroupDatabase) + + testGroupMemberHash := "testGroupMemberHash" + + err := mockGroupDB.DeleteGroupMemberHash(testGroupMemberHash) + assert.Nil(t, err) + + nonExistentGroupMemberHash := "nonExistentGroupMemberHash" + + err = mockGroupDB.DeleteGroupMemberHash(nonExistentGroupMemberHash) + assert.NotNil(t, err) +} From b3d2f3b182270670842abe7485bccb5cb248ecc4 Mon Sep 17 00:00:00 2001 From: CNCSMonster <99234657+CNCSMonster@users.noreply.github.com> Date: Thu, 19 Oct 2023 11:36:51 +0800 Subject: [PATCH 09/21] feat:order logs by time desc (#1250) --- pkg/common/db/relation/log_model.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/common/db/relation/log_model.go b/pkg/common/db/relation/log_model.go index bf39512e8..53365ca5b 100644 --- a/pkg/common/db/relation/log_model.go +++ b/pkg/common/db/relation/log_model.go @@ -24,6 +24,7 @@ func (l *LogGorm) Search(ctx context.Context, keyword string, start time.Time, e if end.UnixMilli() != 0 { db = l.db.WithContext(ctx).Where("create_time <= ?", end) } + db = db.Order("create_time desc") return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber) } From 4b231716c3ee37a83b5ac0d64bee2657bd5b226e Mon Sep 17 00:00:00 2001 From: dong Date: Thu, 19 Oct 2023 19:18:32 +0800 Subject: [PATCH 10/21] fix: mogodb url connent (#1232) * fix: mogodb url connent * fix: mogodb url connent * fix: mogodb url connent * mogodb url connent * mogodb url connent --- tools/component/component.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/component/component.go b/tools/component/component.go index c995da5f1..4e44bb7ba 100644 --- a/tools/component/component.go +++ b/tools/component/component.go @@ -35,7 +35,6 @@ import ( "github.com/go-zookeeper/zk" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" - "go.mongodb.org/mongo-driver/mongo/readpref" "gorm.io/driver/mysql" "gorm.io/gorm" @@ -158,26 +157,38 @@ func checkMysql() error { func checkMongo() error { var client *mongo.Client + uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" defer func() { if client != nil { client.Disconnect(context.TODO()) } }() - mongodbHosts := "" - for i, v := range config.Config.Mongo.Address { - if i == len(config.Config.Mongo.Address)-1 { - mongodbHosts += v + if config.Config.Mongo.Uri != "" { + uri = config.Config.Mongo.Uri + } else { + mongodbHosts := "" + for i, v := range config.Config.Mongo.Address { + if i == len(config.Config.Mongo.Address)-1 { + mongodbHosts += v + } else { + mongodbHosts += v + "," + } + } + if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" { + uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", + config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts, + config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize) } else { - mongodbHosts += v + "," + uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", + mongodbHosts, config.Config.Mongo.Database, + config.Config.Mongo.MaxPoolSize) } } - client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI( - fmt.Sprintf("mongodb://%v:%v@%v/?authSource=admin", - config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts))) + client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) if err != nil { return errs.Wrap(err) } else { - err = client.Ping(context.TODO(), &readpref.ReadPref{}) + err = client.Ping(context.TODO(), nil) if err != nil { return errs.Wrap(err) } From c69d4da8b81414d333b850880af897f2bfa20055 Mon Sep 17 00:00:00 2001 From: withchao <48119764+withchao@users.noreply.github.com> Date: Thu, 19 Oct 2023 21:49:00 -0500 Subject: [PATCH 11/21] fate: api, rpc QuitGroup req add userID (#1244) * feat: v2 to v3 data conversion * feat: v2 to v3 data conversion * fix: CallbackBeforeCreateGroup * fix: NotificationUserInfoUpdate * fix: NotificationUserInfoUpdate * chore: update pkg github.com/OpenIMSDK/protocol v0.0.26 * chore: code format * update pkg * feat: QuitGroup support administrator operations * feat: QuitGroup support administrator operations * fix: checkMongo uri * fix: k8s minio prefix * fix: k8s minio prefix * fix: k8s minio prefix * fix: k8s minio prefix test * fix: k8s minio prefix test --- internal/rpc/group/group.go | 20 +++++++++---- pkg/common/db/s3/minio/minio.go | 52 +++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 8d529584a..de08d2eac 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -852,32 +852,40 @@ func (s *groupServer) JoinGroup(ctx context.Context, req *pbgroup.JoinGroupReq) func (s *groupServer) QuitGroup(ctx context.Context, req *pbgroup.QuitGroupReq) (*pbgroup.QuitGroupResp, error) { resp := &pbgroup.QuitGroupResp{} + if req.UserID == "" { + req.UserID = mcontext.GetOpUserID(ctx) + } else { + if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil { + return nil, err + } + } group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID) if err != nil { return nil, err } if group.GroupType == constant.SuperGroup { - if err := s.GroupDatabase.DeleteSuperGroupMember(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)}); err != nil { + if err := s.GroupDatabase.DeleteSuperGroupMember(ctx, req.GroupID, []string{req.UserID}); err != nil { return nil, err } - s.Notification.SuperGroupNotification(ctx, mcontext.GetOpUserID(ctx), mcontext.GetOpUserID(ctx)) + _ = s.Notification.SuperGroupNotification(ctx, req.UserID, req.UserID) } else { - info, err := s.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx)) + info, err := s.TakeGroupMember(ctx, req.GroupID, req.UserID) if err != nil { return nil, err } if info.RoleLevel == constant.GroupOwner { return nil, errs.ErrNoPermission.Wrap("group owner can't quit") } - err = s.GroupDatabase.DeleteGroupMember(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)}) + err = s.GroupDatabase.DeleteGroupMember(ctx, req.GroupID, []string{req.UserID}) if err != nil { return nil, err } - s.Notification.MemberQuitNotification(ctx, s.groupMemberDB2PB(info, 0)) + _ = s.Notification.MemberQuitNotification(ctx, s.groupMemberDB2PB(info, 0)) } - if err := s.deleteMemberAndSetConversationSeq(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)}); err != nil { + if err := s.deleteMemberAndSetConversationSeq(ctx, req.GroupID, []string{req.UserID}); err != nil { return nil, err } + return resp, nil } diff --git a/pkg/common/db/s3/minio/minio.go b/pkg/common/db/s3/minio/minio.go index 037c933bb..7984df5a0 100644 --- a/pkg/common/db/s3/minio/minio.go +++ b/pkg/common/db/s3/minio/minio.go @@ -63,13 +63,12 @@ const ( ) func NewMinio() (s3.Interface, error) { - conf := config.Config.Object.Minio - u, err := url.Parse(conf.Endpoint) + u, err := url.Parse(config.Config.Object.Minio.Endpoint) if err != nil { return nil, err } opts := &minio.Options{ - Creds: credentials.NewStaticV4(conf.AccessKeyID, conf.SecretAccessKey, conf.SessionToken), + Creds: credentials.NewStaticV4(config.Config.Object.Minio.AccessKeyID, config.Config.Object.Minio.SecretAccessKey, config.Config.Object.Minio.SessionToken), Secure: u.Scheme == "https", } client, err := minio.New(u.Host, opts) @@ -77,29 +76,35 @@ func NewMinio() (s3.Interface, error) { return nil, err } m := &Minio{ - bucket: conf.Bucket, + bucket: config.Config.Object.Minio.Bucket, core: &minio.Core{Client: client}, lock: &sync.Mutex{}, init: false, } - if conf.SignEndpoint == "" || conf.SignEndpoint == conf.Endpoint { + if config.Config.Object.Minio.SignEndpoint == "" || config.Config.Object.Minio.SignEndpoint == config.Config.Object.Minio.Endpoint { m.opts = opts m.sign = m.core.Client - m.bucketURL = conf.Endpoint + "/" + conf.Bucket + "/" + m.prefix = u.Path + u.Path = "" + config.Config.Object.Minio.Endpoint = u.String() + m.signEndpoint = config.Config.Object.Minio.Endpoint } else { - su, err := url.Parse(conf.SignEndpoint) + su, err := url.Parse(config.Config.Object.Minio.SignEndpoint) if err != nil { return nil, err } m.opts = &minio.Options{ - Creds: credentials.NewStaticV4(conf.AccessKeyID, conf.SecretAccessKey, conf.SessionToken), + Creds: credentials.NewStaticV4(config.Config.Object.Minio.AccessKeyID, config.Config.Object.Minio.SecretAccessKey, config.Config.Object.Minio.SessionToken), Secure: su.Scheme == "https", } m.sign, err = minio.New(su.Host, m.opts) if err != nil { return nil, err } - m.bucketURL = conf.SignEndpoint + "/" + conf.Bucket + "/" + m.prefix = su.Path + su.Path = "" + config.Config.Object.Minio.SignEndpoint = su.String() + m.signEndpoint = config.Config.Object.Minio.SignEndpoint } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -110,14 +115,15 @@ func NewMinio() (s3.Interface, error) { } type Minio struct { - bucket string - bucketURL string - location string - opts *minio.Options - core *minio.Core - sign *minio.Client - lock sync.Locker - init bool + bucket string + signEndpoint string + location string + opts *minio.Options + core *minio.Core + sign *minio.Client + lock sync.Locker + init bool + prefix string } func (m *Minio) initMinio(ctx context.Context) error { @@ -255,7 +261,7 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi return nil, err } result := s3.AuthSignResult{ - URL: m.bucketURL + name, + URL: m.signEndpoint + "/" + m.bucket + "/" + name, Query: url.Values{"uploadId": {uploadID}}, Parts: make([]s3.SignPart, len(partNumbers)), } @@ -269,11 +275,13 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi request = signer.SignV4Trailer(*request, creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken, m.location, nil) result.Parts[i] = s3.SignPart{ PartNumber: partNumber, - URL: request.URL.String(), Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}}, Header: request.Header, } } + if m.prefix != "" { + result.URL = m.signEndpoint + m.prefix + "/" + m.bucket + "/" + name + } return &result, nil } @@ -285,6 +293,9 @@ func (m *Minio) PresignedPutObject(ctx context.Context, name string, expire time if err != nil { return "", err } + if m.prefix != "" { + rawURL.Path = path.Join(m.prefix, rawURL.Path) + } return rawURL.String(), nil } @@ -396,6 +407,9 @@ func (m *Minio) presignedGetObject(ctx context.Context, name string, expire time if err != nil { return "", err } + if m.prefix != "" { + rawURL.Path = path.Join(m.prefix, rawURL.Path) + } return rawURL.String(), nil } From 363deff6f8fb078dca0b5493fbac8952bd3f57c8 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Sat, 21 Oct 2023 15:23:33 +0800 Subject: [PATCH 12/21] feat(release-v3.4): Enhanced Build, Deployment, and Configuration Management for Optimized Performance and Automation (#1255) * docs: add openim docs Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * docs: add openim images test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: fix openim ci and deployment Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: fix openim ci and deployment Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add openim flag api configpath env set Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * fix: fix openim push logger Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * fix: fix openim config path Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * fix: fix openim config path Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * fix: fix openim config path Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> --------- Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> --- .dockerignore | 4 +- .github/workflows/bot-cherry-pick.yml | 3 +- .github/workflows/build-docker-image.yml | 9 + .github/workflows/build-openim-web-image.yml | 43 +--- .github/workflows/openimci.yml | 2 +- README-zh_CN.md | 5 + README.md | 4 +- build/images/Dockerfile | 10 + build/images/openim-api/Dockerfile | 13 +- build/images/openim-api/Dockerfile.release | 8 - build/images/openim-cmdutils/Dockerfile | 10 +- build/images/openim-crontask/Dockerfile | 13 +- build/images/openim-msggateway/Dockerfile | 17 +- build/images/openim-msgtransfer/Dockerfile | 11 +- build/images/openim-push/Dockerfile | 17 +- build/images/openim-rpc-auth/Dockerfile | 17 +- .../images/openim-rpc-conversation/Dockerfile | 17 +- build/images/openim-rpc-friend/Dockerfile | 17 +- build/images/openim-rpc-group/Dockerfile | 17 +- build/images/openim-rpc-msg/Dockerfile | 17 +- build/images/openim-rpc-third/Dockerfile | 16 +- build/images/openim-rpc-user/Dockerfile | 17 +- cmd/openim-api/main.go | 45 ++-- deployments/README.md | 18 +- deployments/openim-chat/.helmignore | 23 -- deployments/openim-chat/Chart.yaml | 38 ---- deployments/openim-chat/LICENSE | 201 ------------------ deployments/openim-chat/templates/NOTES.txt | 22 -- .../openim-chat/templates/_helpers.tpl | 62 ------ .../openim-chat/templates/deployment.yaml | 75 ------- deployments/openim-chat/templates/hpa.yaml | 42 ---- .../openim-chat/templates/ingress.yaml | 75 ------- .../openim-chat/templates/service.yaml | 29 --- .../openim-chat/templates/serviceaccount.yaml | 26 --- deployments/openim-chat/values.yaml | 96 --------- docs/contrib/environment.md | 82 ++++--- go.mod | 2 +- go.sum | 30 +++ go.work | 2 +- install.sh | 8 +- internal/push/offlinepush/fcm/push.go | 13 +- pkg/common/cmd/api.go | 3 + pkg/common/cmd/root.go | 78 +++++-- pkg/common/config/parse.go | 59 +++-- pkg/common/config/version | 2 +- scripts/check-all.sh | 4 +- scripts/demo.sh | 2 + scripts/docker-check-service.sh | 6 +- scripts/install-im-server.sh | 2 + scripts/install_im_compose.sh | 12 +- scripts/lib/util.sh | 37 ++++ scripts/make-rules/common.mk | 1 + scripts/make-rules/golang.mk | 13 +- scripts/make-rules/image.mk | 28 +-- test/typecheck/go.mod | 2 +- tests/group_test.go | 22 -- tests/user_test.go | 51 ----- tools/changelog/go.mod | 2 +- tools/component/go.mod | 2 +- tools/data-conversion/README.md | 103 +++++++++ .../chat/{ => cmd/conversion-chat}/chat.go | 0 tools/data-conversion/go.mod | 2 +- tools/data-conversion/info.md | 68 ------ .../conversion-msg/conversion-msg.go} | 0 .../conversion-mysql/conversion-mysql.go} | 0 tools/imctl/go.mod | 2 +- tools/infra/go.mod | 2 +- tools/ncpu/go.mod | 2 +- tools/openim-web/go.mod | 2 +- tools/versionchecker/go.mod | 2 +- tools/yamlfmt/go.mod | 2 +- 71 files changed, 505 insertions(+), 1182 deletions(-) create mode 100644 build/images/Dockerfile delete mode 100644 build/images/openim-api/Dockerfile.release delete mode 100644 deployments/openim-chat/.helmignore delete mode 100644 deployments/openim-chat/Chart.yaml delete mode 100644 deployments/openim-chat/LICENSE delete mode 100644 deployments/openim-chat/templates/NOTES.txt delete mode 100644 deployments/openim-chat/templates/_helpers.tpl delete mode 100644 deployments/openim-chat/templates/deployment.yaml delete mode 100644 deployments/openim-chat/templates/hpa.yaml delete mode 100644 deployments/openim-chat/templates/ingress.yaml delete mode 100644 deployments/openim-chat/templates/service.yaml delete mode 100644 deployments/openim-chat/templates/serviceaccount.yaml delete mode 100644 deployments/openim-chat/values.yaml delete mode 100644 tests/group_test.go delete mode 100644 tests/user_test.go create mode 100644 tools/data-conversion/README.md rename tools/data-conversion/chat/{ => cmd/conversion-chat}/chat.go (100%) delete mode 100644 tools/data-conversion/info.md rename tools/data-conversion/openim/{msg.go => cmd/conversion-msg/conversion-msg.go} (100%) rename tools/data-conversion/openim/{mysql.go => cmd/conversion-mysql/conversion-mysql.go} (100%) diff --git a/.dockerignore b/.dockerignore index c35303ed6..705512401 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,4 @@ - +.github/ diff --git a/.github/workflows/bot-cherry-pick.yml b/.github/workflows/bot-cherry-pick.yml index ab8993540..e488e97de 100644 --- a/.github/workflows/bot-cherry-pick.yml +++ b/.github/workflows/bot-cherry-pick.yml @@ -19,7 +19,8 @@ on: jobs: cherry-pick: name: Cherry Pick - if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/cherry-pick') && github.event.comment.user.login=='kubbot' + # && github.event.comment.user.login=='kubbot' + if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/cherry-pick') runs-on: ubuntu-latest steps: - name: Checkout the latest code diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml index 735c160df..79b60fbed 100644 --- a/.github/workflows/build-docker-image.yml +++ b/.github/workflows/build-docker-image.yml @@ -120,6 +120,15 @@ jobs: uses: docker/metadata-action@v5.0.0 with: images: ghcr.io/openimsdk/openim-server + # generate Docker tags based on the following events/attributes + tags: | + type=schedule + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha - name: Log in to GitHub Container Registry uses: docker/login-action@v3 diff --git a/.github/workflows/build-openim-web-image.yml b/.github/workflows/build-openim-web-image.yml index 800500af6..e040d5270 100644 --- a/.github/workflows/build-openim-web-image.yml +++ b/.github/workflows/build-openim-web-image.yml @@ -114,15 +114,12 @@ jobs: uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 +# ghcr.io/openimsdk/openim-web:latest + - name: Extract metadata (tags, labels) for Docker + id: meta2 + uses: docker/metadata-action@v5.0.0 with: - install: true - - name: Cache Docker layers - uses: actions/cache@v3 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-buildx- + images: ghcr.io/openimsdk/openim-web - name: Log in to GitHub Container Registry uses: docker/login-action@v3 @@ -131,38 +128,12 @@ jobs: username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata (tags, labels) for Docker openim-web - id: meta1 - uses: docker/metadata-action@v5.0.0 - with: - images: ghcr.io/openimsdk/openim-web - - - name: Build and push Docker image for openim-web + - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . file: ./build/images/openim-tools/openim-web/Dockerfile platforms: linux/amd64,linux/arm64 push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.meta1.outputs.tags }} - labels: ${{ steps.meta1.outputs.labels }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache - - - name: Extract metadata (tags, labels) for Docker openim-web - id: meta2 - uses: docker/metadata-action@v5.0.0 - with: - images: ghcr.io/openimsdk/component - - - name: Build and push Docker image for component - uses: docker/build-push-action@v5 - with: - context: . - file: ./build/images/openim-tools/component/Dockerfile - platforms: linux/amd64,linux/arm64 - push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta2.outputs.tags }} - labels: ${{ steps.meta2.outputs.labels }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache + labels: ${{ steps.meta2.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/openimci.yml b/.github/workflows/openimci.yml index 0f0aa2049..347f6d762 100644 --- a/.github/workflows/openimci.yml +++ b/.github/workflows/openimci.yml @@ -48,7 +48,7 @@ jobs: strategy: matrix: - go_version: ["1.18","1.19","1.20","1.21"] + go_version: ["1.19","1.20","1.21"] os: [ubuntu-latest] steps: diff --git a/README-zh_CN.md b/README-zh_CN.md index 9234666f4..8addfe20a 100644 --- a/README-zh_CN.md +++ b/README-zh_CN.md @@ -98,6 +98,11 @@ OpenIM 我们的目标是建立一个顶级的开源社区。我们有一套标 + [接口标准](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/interface.md) ++ [OpenIM配置和环境变量设置](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md) + +> **Note** +> 针对中国的用户,阅读我们的 [Docker 镜像标准](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/images.md) 以便使用国内 aliyun 的镜像地址。OpenIM 也有针对中国的 gitee 同步仓库,你可以在 [gitee.com](https://gitee.com/openimsdk) 上找到它。 + ## :link: 链接 + **[完整文档](https://doc.rentsoft.cn/)** diff --git a/README.md b/README.md index d2482f59c..f58625fb0 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ It is recommended to use Docker Compose for deployment, which can easily and qui
Compile from Source -Ur need `Go 1.18` or higher version, and `make`. +Ur need `Go 1.20` or higher version, and `make`. ```bash @@ -212,6 +212,8 @@ Before you start, please make sure your changes are in demand. The best for that - [Interface Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/api.md) - [Log Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/logging.md) - [Error Code Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/error_code.md) +- [OpenIM configuration and environment variable Settings](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md) + ## :busts_in_silhouette: Community diff --git a/build/images/Dockerfile b/build/images/Dockerfile new file mode 100644 index 000000000..518de78b1 --- /dev/null +++ b/build/images/Dockerfile @@ -0,0 +1,10 @@ +FROM BASE_IMAGE + +WORKDIR ${SERVER_WORKDIR} + +# Set HTTP proxy +ARG BINARY_NAME + +COPY BINARY_NAME ./bin/BINARY_NAME + +ENTRYPOINT ["./bin/BINARY_NAME"] \ No newline at end of file diff --git a/build/images/openim-api/Dockerfile b/build/images/openim-api/Dockerfile index 44d4c6ba4..8c3d710f1 100644 --- a/build/images/openim-api/Dockerfile +++ b/build/images/openim-api/Dockerfile @@ -31,20 +31,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=openim-api +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-api /usr/bin/openim-api + # FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10002 -EXPOSE 10002 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-api /usr/bin/openim-api +COPY --from=builder /usr/bin/openim-api ./bin/openim-api -ENTRYPOINT ["bash", "-c", "openim-api -c $OPENIM_SERVER_CONFIG_NAME --port $PORT"] +ENTRYPOINT ["./bin/openim-api"] diff --git a/build/images/openim-api/Dockerfile.release b/build/images/openim-api/Dockerfile.release deleted file mode 100644 index 8d06221a7..000000000 --- a/build/images/openim-api/Dockerfile.release +++ /dev/null @@ -1,8 +0,0 @@ -FROM ghcr.io/openim-sigs/openim-bash-image:latest - -COPY openim-api /usr/bin/ - -# nosemgrep: dockerfile.security.missing-user.missing-user -ENTRYPOINT ["/usr/bin/openim-api"] -# nosemgrep: dockerfile.security.missing-user.missing-user -CMD ["--help"] \ No newline at end of file diff --git a/build/images/openim-cmdutils/Dockerfile b/build/images/openim-cmdutils/Dockerfile index 56923fe8a..413016f24 100644 --- a/build/images/openim-cmdutils/Dockerfile +++ b/build/images/openim-cmdutils/Dockerfile @@ -31,19 +31,15 @@ RUN go mod download COPY . . -RUN go mod download -RUN make clean RUN make build BINS=openim-cmdutils +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-cmdutils /usr/bin/openim-cmdutils FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder $OPENIM_SERVER_BINDIR/platforms /openim/openim-server/_output/bin/platforms -COPY --from=builder ${SERVER_WORKDIR}/config /openim/openim-server/config - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-cmdutils /usr/bin/openim-cmdutils +COPY --from=builder /usr/bin/openim-cmdutils ./bin/openim-cmdutils -ENTRYPOINT ["openim-cmdutils"] +ENTRYPOINT ["./bin/openim-cmdutils"] CMD ["--help"] diff --git a/build/images/openim-crontask/Dockerfile b/build/images/openim-crontask/Dockerfile index 04334e4ff..35ba75784 100644 --- a/build/images/openim-crontask/Dockerfile +++ b/build/images/openim-crontask/Dockerfile @@ -31,18 +31,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=openim-crontask +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-crontask /usr/bin/openim-crontask + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV OPENIM_SERVER_CONFIG_NAME=/openim/openim-server/config - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-crontask /usr/bin/openim-crontask +COPY --from=builder /usr/bin/openim-crontask ./bin/openim-crontask -CMD ["bash", "-c", "openim-crontask -c $OPENIM_SERVER_CONFIG_NAME"] +ENTRYPOINT ["./bin/openim-crontask"] diff --git a/build/images/openim-msggateway/Dockerfile b/build/images/openim-msggateway/Dockerfile index 35c4c7a1d..fe9db8f32 100644 --- a/build/images/openim-msggateway/Dockerfile +++ b/build/images/openim-msggateway/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=openim-msggateway +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-msggateway /usr/bin/openim-msggateway + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10140 \ - WS_PORT 10001 - -EXPOSE 10140 -EXPOSE 10001 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-msggateway /usr/bin/openim-msggateway +COPY --from=builder /usr/bin/openim-msggateway ./bin/openim-msggateway -CMD ["bash", "-c", "openim-msggateway -c $OPENIM_SERVER_CONFIG_NAME --port $PORT --ws_port $WS_PORT"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-msggateway"] diff --git a/build/images/openim-msgtransfer/Dockerfile b/build/images/openim-msgtransfer/Dockerfile index 57d11be52..c04425f94 100644 --- a/build/images/openim-msgtransfer/Dockerfile +++ b/build/images/openim-msgtransfer/Dockerfile @@ -31,16 +31,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=openim-msgtransfer +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-msgtransfer /usr/bin/openim-msgtransfer + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-msgtransfer /usr/bin/openim-msgtransfer +COPY --from=builder /usr/bin/openim-msgtransfer ./bin/openim-msgtransfer -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then openim-msgtransfer -c $OPENIM_SERVER_CONFIG_NAME --prometheus_port $PROMETHEUS_PORT; else openim-msgtransfer -c $OPENIM_SERVER_CONFIG_NAME; fi"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-msgtransfer"] diff --git a/build/images/openim-push/Dockerfile b/build/images/openim-push/Dockerfile index 287c43450..e51b7a44c 100644 --- a/build/images/openim-push/Dockerfile +++ b/build/images/openim-push/Dockerfile @@ -15,7 +15,6 @@ # OpenIM base image: https://github.com/openim-sigs/openim-base-image # Set go mod installation source and proxy -# docker run -e "PORT=10003" -e "PROMETHEUSORT=4321" --network host -it 67ef891ad1ff FROM golang:1.20 AS builder @@ -32,21 +31,15 @@ RUN go mod download COPY . . -RUN make clean RUN make build BINS=openim-push +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-push /usr/bin/openim-push + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10170 \ - PROMETHEUS_PORT 20170 - -EXPOSE 10170 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-push /usr/bin/openim-push +COPY --from=builder /usr/bin/openim-push ./bin/openim-push -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then openim-push -c $OPENIM_SERVER_CONFIG_NAME --port $PORT --prometheus_port $PROMETHEUS_PORT; else openim-push -c $OPENIM_SERVER_CONFIG_NAME --port $PORT; fi"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-push"] diff --git a/build/images/openim-rpc-auth/Dockerfile b/build/images/openim-rpc-auth/Dockerfile index a581bfc04..8054935bd 100644 --- a/build/images/openim-rpc-auth/Dockerfile +++ b/build/images/openim-rpc-auth/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-auth +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-auth /usr/bin/openim-rpc-auth + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10160 \ - PROMETHEUS_PORT 20160 - -EXPOSE 10160 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-auth /usr/bin/openim-rpc-auth +COPY --from=builder /usr/bin/openim-rpc-auth ./bin/openim-rpc-auth -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-auth --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-auth --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-rpc-auth"] diff --git a/build/images/openim-rpc-conversation/Dockerfile b/build/images/openim-rpc-conversation/Dockerfile index 887a77f36..0cfb681a2 100644 --- a/build/images/openim-rpc-conversation/Dockerfile +++ b/build/images/openim-rpc-conversation/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-conversation +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-conversation /usr/bin/openim-rpc-conversation + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10230 \ - PROMETHEUS_PORT 20230 - -EXPOSE 10230 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-conversation /usr/bin/openim-rpc-conversation +COPY --from=builder /usr/bin/openim-rpc-conversation ./bin/openim-rpc-conversation -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-conversation --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-conversation --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-rpc-conversation"] diff --git a/build/images/openim-rpc-friend/Dockerfile b/build/images/openim-rpc-friend/Dockerfile index 9954daf99..36870bc7e 100644 --- a/build/images/openim-rpc-friend/Dockerfile +++ b/build/images/openim-rpc-friend/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-friend +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-friend /usr/bin/openim-rpc-friend + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10120 \ - PROMETHEUS_PORT 20120 - -EXPOSE 10120 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-friend /usr/bin/openim-rpc-friend +COPY --from=builder /usr/bin/openim-rpc-friend ./bin/openim-rpc-friend -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-friend --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-friend --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] +ENTRYPOINT ["./bin/openim-rpc-friend"] diff --git a/build/images/openim-rpc-group/Dockerfile b/build/images/openim-rpc-group/Dockerfile index 627560075..85ba3b74d 100644 --- a/build/images/openim-rpc-group/Dockerfile +++ b/build/images/openim-rpc-group/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-group +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-group /usr/bin/openim-rpc-group + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10150 \ - PROMETHEUS_PORT 20150 - -EXPOSE 10150 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-group /usr/bin/openim-rpc-group +COPY --from=builder /usr/bin/openim-rpc-group ./bin/openim-rpc-group -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-group --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-group --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-rpc-group"] diff --git a/build/images/openim-rpc-msg/Dockerfile b/build/images/openim-rpc-msg/Dockerfile index e7426bcf0..7e1aaf50b 100644 --- a/build/images/openim-rpc-msg/Dockerfile +++ b/build/images/openim-rpc-msg/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-msg +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-msg /usr/bin/openim-rpc-msg + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10130 \ - PROMETHEUS_PORT 20130 - -EXPOSE 10130 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-msg /usr/bin/openim-rpc-msg +COPY --from=builder /usr/bin/openim-rpc-msg ./bin/openim-rpc-msg -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-msg --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-msg --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] \ No newline at end of file +ENTRYPOINT ["./bin/openim-rpc-msg"] diff --git a/build/images/openim-rpc-third/Dockerfile b/build/images/openim-rpc-third/Dockerfile index 700fd2918..ab3e6c949 100644 --- a/build/images/openim-rpc-third/Dockerfile +++ b/build/images/openim-rpc-third/Dockerfile @@ -31,23 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-third +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-third /usr/bin/openim-rpc-third +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10190 \ - PROMETHEUS_PORT 21301 - -EXPOSE 10190 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-third /usr/bin/openim-rpc-third +COPY --from=builder /usr/bin/openim-rpc-third ./bin/openim-rpc-third -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-third --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-third --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] +ENTRYPOINT ["./bin/openim-rpc-third"] diff --git a/build/images/openim-rpc-user/Dockerfile b/build/images/openim-rpc-user/Dockerfile index 00804f2bf..cd0121b7d 100644 --- a/build/images/openim-rpc-user/Dockerfile +++ b/build/images/openim-rpc-user/Dockerfile @@ -31,22 +31,15 @@ RUN go mod download COPY . . -RUN make clean - RUN make build BINS=openim-rpc-user +RUN cp /openim/openim-server/_output/bin/platforms/$(go env GOOS)/$(go env GOARCH)/openim-rpc-user /usr/bin/openim-rpc-user + +# FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest WORKDIR /openim/openim-server -COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms/ -COPY --from=builder /openim/openim-server/config /openim/openim-server/config - -ENV PORT 10110 \ - PROMETHEUS_PORT 20110 - -EXPOSE 10110 - -RUN mv ${OPENIM_SERVER_BINDIR}/platforms/$(get_os)/$(get_arch)/openim-rpc-user /usr/bin/openim-rpc-user +COPY --from=builder /usr/bin/openim-rpc-user ./bin/openim-rpc-user -ENTRYPOINT ["bash", "-c", "if [[ -n $PROMETHEUS_PORT ]]; then "openim-rpc-user --port $PORT -c $OPENIM_SERVER_CONFIG_NAME" --prometheus_port $PROMETHEUS_PORT; else "openim-rpc-user --port $PORT -c $OPENIM_SERVER_CONFIG_NAME"; fi"] +ENTRYPOINT ["./bin/openim-rpc-user"] diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index 3034d6645..f6db87353 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -17,72 +17,83 @@ package main import ( "context" "fmt" - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "net" _ "net/http/pprof" "strconv" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" - "github.com/openimsdk/open-im-server/v3/internal/api" "github.com/openimsdk/open-im-server/v3/pkg/common/cmd" "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" ) + func main() { apiCmd := cmd.NewApiCmd() apiCmd.AddPortFlag() apiCmd.AddApi(run) if err := apiCmd.Execute(); err != nil { + log.ZError(context.Background(), "API command execution failed", err) panic(err.Error()) } } func run(port int) error { - fmt.Println("*****openimapi port:", port) + log.ZInfo(context.Background(), "Openim api port:", "port", port) + if port == 0 { - return fmt.Errorf("port is empty") + err := "port is empty" + log.ZError(context.Background(), err, nil) + + return fmt.Errorf(err) } rdb, err := cache.NewRedis() if err != nil { + log.ZError(context.Background(), "Failed to initialize Redis", err) + return err } - fmt.Println("api start init discov client") + log.ZInfo(context.Background(), "api start init discov client") + var client discoveryregistry.SvcDiscoveryRegistry + + // Determine whether zk is passed according to whether it is a clustered deployment client, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) - /* - client, err = openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - openkeeper.WithFreq(time.Hour), openkeeper.WithUserNameAndPassword( - config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password, - ), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/ if err != nil { + log.ZError(context.Background(), "Failed to initialize discovery register", err) + return err } if err = client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil { + log.ZError(context.Background(), "Failed to create RPC root nodes", err) + return err } - fmt.Println("api register public config to discov") + log.ZInfo(context.Background(), "api register public config to discov") if err = client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, config.Config.EncodeConfig()); err != nil { + log.ZError(context.Background(), "Failed to register public config to discov", err) + return err } - fmt.Println("api register public config to discov success") + log.ZInfo(context.Background(), "api register public config to discov success") router := api.NewGinRouter(client, rdb) - fmt.Println("api init router success") + log.ZInfo(context.Background(), "api init router success") var address string if config.Config.Api.ListenIP != "" { address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port)) } else { address = net.JoinHostPort("0.0.0.0", strconv.Itoa(port)) } - fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version) - log.ZInfo(context.Background(), "start server success", "address", address, "version", config.Version) + log.ZInfo(context.Background(), "start api server", "address", address, "OpenIM version", config.Version) + err = router.Run(address) if err != nil { - log.ZError(context.Background(), "api run failed ", err, "address", address) + log.ZError(context.Background(), "api run failed", err, "address", address) return err } diff --git a/deployments/README.md b/deployments/README.md index 7065d45fa..e070d8e1f 100644 --- a/deployments/README.md +++ b/deployments/README.md @@ -80,6 +80,12 @@ $ sudo sealos run labring/kubernetes:v1.25.0 labring/helm:v3.8.2 labring/calico: -p "$CLUSTER_PASSWORD" ``` +> **Node** +> 卸载的方式:使用 `kubeadm` 卸载并不会清除 `etcd` 和 `cni` 相关,所以需要手动清除,或者使用 `sealos` 卸载。 +> ```bash +> sealos reset +> ``` + ### 安装 helm helm通过打包的方式,支持发布的版本管理和控制,很大程度上简化了Kubernetes应用的部署和管理。 @@ -97,11 +103,17 @@ $ curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bas $ helm repo add brigade https://openimsdk.github.io/openim-charts ``` +### OpenIM 的镜像策略 -### 容器化安装 +自动化提供的 aliyun, ghcr, docker hub: https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/images.md -具体安装步骤如下: +**本地化测试构建方法:** + +```bash +$ make image +``` +### 容器化安装 -### Helm安装 +具体安装步骤如下: diff --git a/deployments/openim-chat/.helmignore b/deployments/openim-chat/.helmignore deleted file mode 100644 index 0e8a0eb36..000000000 --- a/deployments/openim-chat/.helmignore +++ /dev/null @@ -1,23 +0,0 @@ -# Patterns to ignore when building packages. -# This supports shell glob matching, relative path matching, and -# negation (prefixed with !). Only one pattern per line. -.DS_Store -# Common VCS dirs -.git/ -.gitignore -.bzr/ -.bzrignore -.hg/ -.hgignore -.svn/ -# Common backup files -*.swp -*.bak -*.tmp -*.orig -*~ -# Various IDEs -.project -.idea/ -*.tmproj -.vscode/ diff --git a/deployments/openim-chat/Chart.yaml b/deployments/openim-chat/Chart.yaml deleted file mode 100644 index 4466fc928..000000000 --- a/deployments/openim-chat/Chart.yaml +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -apiVersion: v2 -name: openim-chat -description: A Helm chart for Kubernetes - -# A chart can be either an 'application' or a 'library' chart. -# -# Application charts are a collection of templates that can be packaged into versioned archives -# to be deployed. -# -# Library charts provide useful utilities or functions for the chart developer. They're included as -# a dependency of application charts to inject those utilities and functions into the rendering -# pipeline. Library charts do not define any templates and therefore cannot be deployed. -type: application - -# This is the chart version. This version number should be incremented each time you make changes -# to the chart and its templates, including the app version. -# Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.1.0 - -# This is the version number of the application being deployed. This version number should be -# incremented each time you make changes to the application. Versions are not expected to -# follow Semantic Versioning. They should reflect the version the application is using. -# It is recommended to use it with quotes. -appVersion: "1.16.0" diff --git a/deployments/openim-chat/LICENSE b/deployments/openim-chat/LICENSE deleted file mode 100644 index 261eeb9e9..000000000 --- a/deployments/openim-chat/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/deployments/openim-chat/templates/NOTES.txt b/deployments/openim-chat/templates/NOTES.txt deleted file mode 100644 index 17cfb43fe..000000000 --- a/deployments/openim-chat/templates/NOTES.txt +++ /dev/null @@ -1,22 +0,0 @@ -1. Get the application URL by running these commands: -{{- if .Values.ingress.enabled }} -{{- range $host := .Values.ingress.hosts }} - {{- range .paths }} - http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} - {{- end }} -{{- end }} -{{- else if contains "NodePort" .Values.service.type }} - export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "openim-chat.fullname" . }}) - export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") - echo http://$NODE_IP:$NODE_PORT -{{- else if contains "LoadBalancer" .Values.service.type }} - NOTE: It may take a few minutes for the LoadBalancer IP to be available. - You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "openim-chat.fullname" . }}' - export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "openim-chat.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") - echo http://$SERVICE_IP:{{ .Values.service.port }} -{{- else if contains "ClusterIP" .Values.service.type }} - export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "openim-chat.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") - export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") - echo "Visit http://127.0.0.1:8080 to use your application" - kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT -{{- end }} diff --git a/deployments/openim-chat/templates/_helpers.tpl b/deployments/openim-chat/templates/_helpers.tpl deleted file mode 100644 index 3177accfb..000000000 --- a/deployments/openim-chat/templates/_helpers.tpl +++ /dev/null @@ -1,62 +0,0 @@ -{{/* -Expand the name of the chart. -*/}} -{{- define "openim-chat.name" -}} -{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} -{{- end }} - -{{/* -Create a default fully qualified app name. -We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). -If release name contains chart name it will be used as a full name. -*/}} -{{- define "openim-chat.fullname" -}} -{{- if .Values.fullnameOverride }} -{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} -{{- else }} -{{- $name := default .Chart.Name .Values.nameOverride }} -{{- if contains $name .Release.Name }} -{{- .Release.Name | trunc 63 | trimSuffix "-" }} -{{- else }} -{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} -{{- end }} -{{- end }} -{{- end }} - -{{/* -Create chart name and version as used by the chart label. -*/}} -{{- define "openim-chat.chart" -}} -{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} -{{- end }} - -{{/* -Common labels -*/}} -{{- define "openim-chat.labels" -}} -helm.sh/chart: {{ include "openim-chat.chart" . }} -{{ include "openim-chat.selectorLabels" . }} -{{- if .Chart.AppVersion }} -app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} -{{- end }} -app.kubernetes.io/managed-by: {{ .Release.Service }} -{{- end }} - -{{/* -Selector labels -*/}} -{{- define "openim-chat.selectorLabels" -}} -app.kubernetes.io/name: {{ include "openim-chat.name" . }} -app.kubernetes.io/instance: {{ .Release.Name }} -{{- end }} - -{{/* -Create the name of the service account to use -*/}} -{{- define "openim-chat.serviceAccountName" -}} -{{- if .Values.serviceAccount.create }} -{{- default (include "openim-chat.fullname" .) .Values.serviceAccount.name }} -{{- else }} -{{- default "default" .Values.serviceAccount.name }} -{{- end }} -{{- end }} diff --git a/deployments/openim-chat/templates/deployment.yaml b/deployments/openim-chat/templates/deployment.yaml deleted file mode 100644 index 47d88c590..000000000 --- a/deployments/openim-chat/templates/deployment.yaml +++ /dev/null @@ -1,75 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -apiVersion: apps/v1 -kind: Deployment -metadata: - name: {{ include "openim-chat.fullname" . }} - labels: - {{- include "openim-chat.labels" . | nindent 4 }} -spec: - {{- if not .Values.autoscaling.enabled }} - replicas: {{ .Values.replicaCount }} - {{- end }} - selector: - matchLabels: - {{- include "openim-chat.selectorLabels" . | nindent 6 }} - template: - metadata: - {{- with .Values.podAnnotations }} - annotations: - {{- toYaml . | nindent 8 }} - {{- end }} - labels: - {{- include "openim-chat.selectorLabels" . | nindent 8 }} - spec: - {{- with .Values.imagePullSecrets }} - imagePullSecrets: - {{- toYaml . | nindent 8 }} - {{- end }} - serviceAccountName: {{ include "openim-chat.serviceAccountName" . }} - securityContext: - {{- toYaml .Values.podSecurityContext | nindent 8 }} - containers: - - name: {{ .Chart.Name }} - securityContext: - {{- toYaml .Values.securityContext | nindent 12 }} - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" - imagePullPolicy: {{ .Values.image.pullPolicy }} - ports: - - name: http - containerPort: 80 - protocol: TCP - livenessProbe: - httpGet: - path: / - port: http - readinessProbe: - httpGet: - path: / - port: http - resources: - {{- toYaml .Values.resources | nindent 12 }} - {{- with .Values.nodeSelector }} - nodeSelector: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.affinity }} - affinity: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.tolerations }} - tolerations: - {{- toYaml . | nindent 8 }} - {{- end }} diff --git a/deployments/openim-chat/templates/hpa.yaml b/deployments/openim-chat/templates/hpa.yaml deleted file mode 100644 index 5f28b13fe..000000000 --- a/deployments/openim-chat/templates/hpa.yaml +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -{{- if .Values.autoscaling.enabled }} -apiVersion: autoscaling/v2beta1 -kind: HorizontalPodAutoscaler -metadata: - name: {{ include "openim-chat.fullname" . }} - labels: - {{- include "openim-chat.labels" . | nindent 4 }} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{ include "openim-chat.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} - metrics: - {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: cpu - targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} - {{- end }} - {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} - - type: Resource - resource: - name: memory - targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} - {{- end }} -{{- end }} diff --git a/deployments/openim-chat/templates/ingress.yaml b/deployments/openim-chat/templates/ingress.yaml deleted file mode 100644 index 3faa231d0..000000000 --- a/deployments/openim-chat/templates/ingress.yaml +++ /dev/null @@ -1,75 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -{{- if .Values.ingress.enabled -}} -{{- $fullName := include "openim-chat.fullname" . -}} -{{- $svcPort := .Values.service.port -}} -{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} - {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} - {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} - {{- end }} -{{- end }} -{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1 -{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1beta1 -{{- else -}} -apiVersion: extensions/v1beta1 -{{- end }} -kind: Ingress -metadata: - name: {{ $fullName }} - labels: - {{- include "openim-chat.labels" . | nindent 4 }} - {{- with .Values.ingress.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -spec: - {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} - ingressClassName: {{ .Values.ingress.className }} - {{- end }} - {{- if .Values.ingress.tls }} - tls: - {{- range .Values.ingress.tls }} - - hosts: - {{- range .hosts }} - - {{ . | quote }} - {{- end }} - secretName: {{ .secretName }} - {{- end }} - {{- end }} - rules: - {{- range .Values.ingress.hosts }} - - host: {{ .host | quote }} - http: - paths: - {{- range .paths }} - - path: {{ .path }} - {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} - pathType: {{ .pathType }} - {{- end }} - backend: - {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} - service: - name: {{ $fullName }} - port: - number: {{ $svcPort }} - {{- else }} - serviceName: {{ $fullName }} - servicePort: {{ $svcPort }} - {{- end }} - {{- end }} - {{- end }} -{{- end }} diff --git a/deployments/openim-chat/templates/service.yaml b/deployments/openim-chat/templates/service.yaml deleted file mode 100644 index c504be550..000000000 --- a/deployments/openim-chat/templates/service.yaml +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -apiVersion: v1 -kind: Service -metadata: - name: {{ include "openim-chat.fullname" . }} - labels: - {{- include "openim-chat.labels" . | nindent 4 }} -spec: - type: {{ .Values.service.type }} - ports: - - port: {{ .Values.service.port }} - targetPort: http - protocol: TCP - name: http - selector: - {{- include "openim-chat.selectorLabels" . | nindent 4 }} diff --git a/deployments/openim-chat/templates/serviceaccount.yaml b/deployments/openim-chat/templates/serviceaccount.yaml deleted file mode 100644 index 8fd4f4e21..000000000 --- a/deployments/openim-chat/templates/serviceaccount.yaml +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -{{- if .Values.serviceAccount.create -}} -apiVersion: v1 -kind: ServiceAccount -metadata: - name: {{ include "openim-chat.serviceAccountName" . }} - labels: - {{- include "openim-chat.labels" . | nindent 4 }} - {{- with .Values.serviceAccount.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -{{- end }} diff --git a/deployments/openim-chat/values.yaml b/deployments/openim-chat/values.yaml deleted file mode 100644 index b1c083fee..000000000 --- a/deployments/openim-chat/values.yaml +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Default values for openim-chat. -# This is a YAML-formatted file. -# Declare variables to be passed into your templates. - -replicaCount: 1 - -image: - repository: nginx - pullPolicy: IfNotPresent - # Overrides the image tag whose default is the chart appVersion. - tag: "" - -imagePullSecrets: [] -nameOverride: "" -fullnameOverride: "" - -serviceAccount: - # Specifies whether a service account should be created - create: true - # Annotations to add to the service account - annotations: {} - # The name of the service account to use. - # If not set and create is true, a name is generated using the fullname template - name: "" - -podAnnotations: {} - -podSecurityContext: {} - # fsGroup: 2000 - -securityContext: {} - # capabilities: - # drop: - # - ALL - # readOnlyRootFilesystem: true - # runAsNonRoot: true - # runAsUser: 1000 - -service: - type: ClusterIP - port: 80 - -ingress: - enabled: false - className: "" - annotations: {} - # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-acme: "true" - hosts: - - host: chart-example.local - paths: - - path: / - pathType: ImplementationSpecific - tls: [] - # - secretName: chart-example-tls - # hosts: - # - chart-example.local - -resources: {} - # We usually recommend not to specify default resources and to leave this as a conscious - # choice for the user. This also increases chances charts run on environments with little - # resources, such as Minikube. If you do want to specify resources, uncomment the following - # lines, adjust them as necessary, and remove the curly braces after 'resources:'. - # limits: - # cpu: 100m - # memory: 128Mi - # requests: - # cpu: 100m - # memory: 128Mi - -autoscaling: - enabled: false - minReplicas: 1 - maxReplicas: 100 - targetCPUUtilizationPercentage: 80 - # targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {} diff --git a/docs/contrib/environment.md b/docs/contrib/environment.md index 34959047a..6efddafd3 100644 --- a/docs/contrib/environment.md +++ b/docs/contrib/environment.md @@ -1,20 +1,19 @@ -# OpenIM enviroment +# OpenIM ENVIRONMENT CONFIGURATION - * 1. [OpenIM Deployment Guide](#OpenIMDeploymentGuide) * 1.1. [Deployment Strategies](#DeploymentStrategies) * 1.2. [Source Code Deployment](#SourceCodeDeployment) * 1.3. [Docker Compose Deployment](#DockerComposeDeployment) * 1.4. [Environment Variable Configuration](#EnvironmentVariableConfiguration) - * 1.4.1. [[1. Recommended using environment variables:](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#1-recommended-using-environment-variables)](#1.Recommendedusingenvironmentvariables:https:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.md1-recommended-using-environment-variables) - * 1.4.2. [[Additional Configuration](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#additional-configuration)](#AdditionalConfigurationhttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mdadditional-configuration) - * 1.4.3. [[Security Considerations](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#security-considerations)](#SecurityConsiderationshttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mdsecurity-considerations) - * 1.4.4. [[Data Management](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#data-management)](#DataManagementhttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mddata-management) - * 1.4.5. [[Monitoring and Logging](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#monitoring-and-logging)](#MonitoringandLogginghttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mdmonitoring-and-logging) - * 1.4.6. [[Troubleshooting](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#troubleshooting)](#Troubleshootinghttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mdtroubleshooting) - * 1.4.7. [[Conclusion](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#conclusion)](#Conclusionhttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mdconclusion) - * 1.4.8. [[Additional Resources](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#additional-resources)](#AdditionalResourceshttps:github.comopenimsdkopen-im-serverblobmaindocscontribenvironment.mdadditional-resources) + * 1.4.1. [Recommended using environment variables](#Recommendedusingenvironmentvariables) + * 1.4.2. [Additional Configuration](#AdditionalConfiguration) + * 1.4.3. [Security Considerations](#SecurityConsiderations) + * 1.4.4. [Data Management](#DataManagement) + * 1.4.5. [Monitoring and Logging](#MonitoringandLogging) + * 1.4.6. [Troubleshooting](#Troubleshooting) + * 1.4.7. [Conclusion](#Conclusion) + * 1.4.8. [Additional Resources](#AdditionalResources) * 2. [Further Configuration](#FurtherConfiguration) * 2.1. [Image Registry Configuration](#ImageRegistryConfiguration) * 2.2. [OpenIM Docker Network Configuration](#OpenIMDockerNetworkConfiguration) @@ -39,11 +38,38 @@ * 2.20.1. [General Configuration](#GeneralConfiguration) * 2.20.2. [Service-Specific Prometheus Ports](#Service-SpecificPrometheusPorts) - - +## 0. OpenIM Config File + +Ensuring that OpenIM operates smoothly requires clear direction on the configuration file's location. Here's a detailed step-by-step guide on how to provide this essential path to OpenIM: + +1. **Using the Command-line Argument**: + + + **For Configuration Path**: When initializing OpenIM, you can specify the path to the configuration file directly using the `-c` or `--config_folder_path` option. + + ```bash + ❯ _output/bin/platforms/linux/amd64/openim-api --config_folder_path="/your/config/folder/path" + ``` + + + **For Port Specification**: Similarly, if you wish to designate a particular port, utilize the `-p` option followed by the desired port number. + + ```bash + ❯ _output/bin/platforms/linux/amd64/openim-api -p 1234 + ``` + + Note: If the port is not specified here, OpenIM will fetch it from the configuration file. Setting the port via environment variables isn't supported. We recommend consolidating settings in the configuration file for a more consistent and streamlined setup. + +2. **Leveraging the Environment Variable**: + + You have the flexibility to determine OpenIM's configuration path by setting an `OPENIMCONFIG` environment variable. This method provides a seamless way to instruct OpenIM without command-line parameters every time. + + ```bash + export OPENIMCONFIG="/path/to/your/config" + ``` + +3. **Relying on the Default Path**: + + In scenarios where neither command-line arguments nor environment variables are provided, OpenIM will intuitively revert to the `config/` directory to locate its configuration. + ## 1. OpenIM Deployment Guide @@ -96,7 +122,7 @@ Setting a variable, e.g., `export CHAT_BRANCH="release-v1.3"`, will prioritize ` For convenience, configuration through modifying environment variables is recommended: -#### 1.4.1. [1. Recommended using environment variables:](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#1-recommended-using-environment-variables) +#### 1.4.1. Recommended using environment variables + PASSWORD @@ -137,9 +163,9 @@ For convenience, configuration through modifying environment variables is recomm export DATA_DIR="/data/openim" ``` -#### 1.4.2. [Additional Configuration](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#additional-configuration) +#### 1.4.2. Additional Configuration -##### [MinIO Access and Secret Key](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#minio-access-and-secret-key) +##### MinIO Access and Secret Key To secure your MinIO server, you should set up an access key and secret key. These credentials are used to authenticate requests to your MinIO server. @@ -148,7 +174,7 @@ export MINIO_ACCESS_KEY="YourAccessKey" export MINIO_SECRET_KEY="YourSecretKey" ``` -##### [MinIO Browser](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#minio-browser) +##### MinIO Browser MinIO comes with an embedded web-based object browser. You can control the availability of the MinIO browser by setting the `MINIO_BROWSER` environment variable. @@ -156,9 +182,9 @@ MinIO comes with an embedded web-based object browser. You can control the avail export MINIO_BROWSER="on" ``` -#### 1.4.3. [Security Considerations](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#security-considerations) +#### 1.4.3. Security Considerations -##### [TLS/SSL Configuration](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#tls-ssl-configuration) +##### TLS/SSL Configuration For secure communication, it's recommended to enable TLS/SSL for your MinIO server. You can do this by providing the path to the SSL certificate and key files. @@ -166,9 +192,9 @@ For secure communication, it's recommended to enable TLS/SSL for your MinIO serv export MINIO_CERTS_DIR="/path/to/certs/directory" ``` -#### 1.4.4. [Data Management](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#data-management) +#### 1.4.4. Data Management -##### [Data Retention Policy](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#data-retention-policy) +##### Data Retention Policy You may want to set up a data retention policy to automatically delete objects after a specified period. @@ -176,7 +202,7 @@ You may want to set up a data retention policy to automatically delete objects a export MINIO_RETENTION_DAYS="30" ``` -#### 1.4.5. [Monitoring and Logging](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#monitoring-and-logging) +#### 1.4.5. Monitoring and Logging ##### [Audit Logging](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#audit-logging) @@ -186,9 +212,9 @@ Enable audit logging to keep track of access and changes to your data. export MINIO_AUDIT="on" ``` -#### 1.4.6. [Troubleshooting](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#troubleshooting) +#### 1.4.6. Troubleshooting -##### [Debug Mode](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#debug-mode) +##### Debug Mode In case of issues, you may enable debug mode to get more detailed logs to assist in troubleshooting. @@ -196,11 +222,11 @@ In case of issues, you may enable debug mode to get more detailed logs to assist export MINIO_DEBUG="on" ``` -#### 1.4.7. [Conclusion](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#conclusion) +#### 1.4.7. Conclusion With the environment variables configured as per your requirements, your MinIO server should be ready to securely store and manage your object data. Ensure to verify the setup and monitor the logs for any unusual activities or errors. Regularly update the MinIO server and review your configuration to adapt to any changes or improvements in the MinIO system. -#### 1.4.8. [Additional Resources](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md#additional-resources) +#### 1.4.8. Additional Resources + [MinIO Client Quickstart Guide](https://docs.min.io/docs/minio-client-quickstart-guide) + [MinIO Admin Complete Guide](https://docs.min.io/docs/minio-admin-complete-guide) diff --git a/go.mod b/go.mod index 8510f780d..c26bcad59 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/openimsdk/open-im-server/v3 -go 1.18 +go 1.19 require ( firebase.google.com/go v3.13.0+incompatible diff --git a/go.sum b/go.sum index 0e19ebfbf..4bdc0b51b 100644 --- a/go.sum +++ b/go.sum @@ -44,14 +44,21 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y= +github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs= github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I= github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cosiner/argv v0.1.0 h1:BVDiEL32lwHukgJKP87btEPenzrrHUjajs/8yzaqcXg= +github.com/cosiner/argv v0.1.0/go.mod h1:EusR6TucWKX+zFgtdUsKT2Cvg45K5rtpCcWz4hK06d8= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/derekparker/trie v0.0.0-20221213183930-4c74548207f4 h1:atN94qKNhLpy+9BwbE5nxvFj4rScJi6W3x/NfFmMDg4= +github.com/derekparker/trie v0.0.0-20221213183930-4c74548207f4/go.mod h1:C7Es+DLenIpPc9J6IYw4jrK0h7S9bKj4DNl8+KxGEXU= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= @@ -79,6 +86,10 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-delve/delve v1.21.1 h1:oDpED8gvXPLS1VKSYzaMH/ihZtyk04H9jqQ9xpyFXl0= +github.com/go-delve/delve v1.21.1/go.mod h1:FgTAiRUe43RS5EexL06RPyMtP8AMZVL/t9Qqgy3qUe4= +github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d h1:pxjSLshkZJGLVm0wv20f/H0oTWiq/egkoJQ2ja6LEvo= +github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d/go.mod h1:biJCRbqp51wS+I92HMqn5H8/A0PAhxn2vyOT+JqhiGI= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= @@ -128,10 +139,13 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-dap v0.9.1 h1:d8dETjgHMR9/xs+Xza+NrZmB7jxIS5OtM2uRsyJVA/c= +github.com/google/go-dap v0.9.1/go.mod h1:HAeyoSd2WIfTfg+0GRXcFrb+RnojAtGNh+k+XTIxJDE= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= @@ -161,6 +175,8 @@ github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9 github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -207,8 +223,13 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= github.com/lithammer/shortuuid v3.0.0+incompatible h1:NcD0xWW/MZYXEHa6ITy6kaXN5nwm/V115vj2YXfhS0w= github.com/lithammer/shortuuid v3.0.0+incompatible/go.mod h1:FR74pbAuElzOUuenUHTK2Tciko1/vKuIKS9dSkDrA4w= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= @@ -267,11 +288,14 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5X github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg= github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= @@ -316,6 +340,8 @@ go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecq go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.starlark.net v0.0.0-20220816155156-cfacd8902214 h1:MqijAN3S61c7KWasOk+zIqIjHQPN6WUra/X3+YAkQxQ= +go.starlark.net v0.0.0-20220816155156-cfacd8902214/go.mod h1:VZcBMdr3cT3PnBoWunTabuSEXwVAH+ZJ5zxfs3AdASk= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= @@ -335,6 +361,8 @@ golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58 golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= +golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg= golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -390,10 +418,12 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= diff --git a/go.work b/go.work index 6800fa496..1c819212c 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.18 +go 1.19 use ( . diff --git a/install.sh b/install.sh index d43767362..9809646fa 100755 --- a/install.sh +++ b/install.sh @@ -40,7 +40,7 @@ REPO="Open-IM-Server" # Version of Go you want to use, make sure it is compatible with your OpenIM-Server requirements. # Default is 1.18, if you want to use a different version, replace accordingly. -GO_VERSION="1.18" +GO_VERSION="1.20" # Default HTTP_PORT is 80. If you want to use a different port, uncomment and replace the value. # HTTP_PORT=80 @@ -168,7 +168,7 @@ function install_go() { command -v go >/dev/null 2>&1 # Determines if GO_VERSION is defined if [ -z "$GO_VERSION" ]; then - GO_VERSION="1.18" + GO_VERSION="1.20" fi if [[ $? -ne 0 ]]; then @@ -309,7 +309,7 @@ function cmd_help() { color_echo ${BLUE_PREFIX} "-t, --tag ${CYAN_PREFIX}specify the tag (default option, set to latest if not specified)${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "-r, --release ${CYAN_PREFIX}specify the release branch (cannot be used with the tag option)${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "-gt, --github-token ${CYAN_PREFIX}set the GITHUB_TOKEN (default: not set)${COLOR_SUFFIX}" - color_echo ${BLUE_PREFIX} "-g, --go-version ${CYAN_PREFIX}set the Go language version (default: GO_VERSION=\"1.18\")${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-g, --go-version ${CYAN_PREFIX}set the Go language version (default: GO_VERSION=\"1.20\")${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "--install-dir ${CYAN_PREFIX}set the OpenIM installation directory (default: /tmp)${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "--cpu ${CYAN_PREFIX}set the number of concurrent processes${COLOR_SUFFIX}" echo @@ -329,7 +329,7 @@ function parseinput() { # CHINA=false # TAG=latest # RELEASE="" - # GO_VERSION=1.18 + # GO_VERSION=1.20 # INSTALL_DIR=/tmp # GITHUB_TOKEN="" # CPU=$(nproc) diff --git a/internal/push/offlinepush/fcm/push.go b/internal/push/offlinepush/fcm/push.go index 27e7dab18..8145d4c17 100644 --- a/internal/push/offlinepush/fcm/push.go +++ b/internal/push/offlinepush/fcm/push.go @@ -18,8 +18,6 @@ import ( "context" "path/filepath" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" - firebase "firebase.google.com/go" "firebase.google.com/go/messaging" "github.com/redis/go-redis/v9" @@ -42,20 +40,17 @@ type Fcm struct { } func NewClient(cache cache.MsgModel) *Fcm { - opt := option.WithCredentialsFile(filepath.Join(config2.Root, "config", config.Config.Push.Fcm.ServiceAccount)) + projectRoot := config.GetProjectRoot() + credentialsFilePath := filepath.Join(projectRoot, "config", config.Config.Push.Fcm.ServiceAccount) + opt := option.WithCredentialsFile(credentialsFilePath) fcmApp, err := firebase.NewApp(context.Background(), nil, opt) if err != nil { return nil } - // auth - // fcmClient, err := fcmApp.Auth(context.Background()) - // if err != nil { - // return - // } + ctx := context.Background() fcmMsgClient, err := fcmApp.Messaging(ctx) if err != nil { - panic(err.Error()) return nil } return &Fcm{fcmMsgCli: fcmMsgClient, cache: cache} diff --git a/pkg/common/cmd/api.go b/pkg/common/cmd/api.go index 19b11d4b2..7ce872fac 100644 --- a/pkg/common/cmd/api.go +++ b/pkg/common/cmd/api.go @@ -28,6 +28,7 @@ type ApiCmd struct { func NewApiCmd() *ApiCmd { ret := &ApiCmd{NewRootCmd("api")} ret.SetRootCmdPt(ret) + return ret } @@ -36,11 +37,13 @@ func (a *ApiCmd) AddApi(f func(port int) error) { return f(a.getPortFlag(cmd)) } } + func (a *ApiCmd) GetPortFromConfig(portType string) int { fmt.Println("GetPortFromConfig:", portType) if portType == constant.FlagPort { return config2.Config.Api.OpenImApiPort[0] } else { + return 0 } } diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 6973da9d6..f220ab7c1 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -53,38 +53,76 @@ func WithLogName(logName string) func(*CmdOpts) { } } -func NewRootCmd(name string, opts ...func(*CmdOpts)) (rootCmd *RootCmd) { - rootCmd = &RootCmd{Name: name} - c := cobra.Command{ - Use: "start openIM application", +func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd { + rootCmd := &RootCmd{Name: name} + cmd := cobra.Command{ + Use: "Start openIM application", Short: fmt.Sprintf(`Start %s `, name), Long: fmt.Sprintf(`Start %s `, name), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil { - panic(err) - } - cmdOpts := &CmdOpts{} - for _, opt := range opts { - opt(cmdOpts) - } - if cmdOpts.loggerPrefixName == "" { - cmdOpts.loggerPrefixName = "OpenIM.log.all" - } - if err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime); err != nil { - panic(err) - } - return nil + return rootCmd.persistentPreRun(cmd, opts...) }, } - rootCmd.Command = c + rootCmd.Command = cmd rootCmd.addConfFlag() return rootCmd } + +func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error { + if err := rc.initializeConfiguration(cmd); err != nil { + return fmt.Errorf("failed to get configuration from command: %w", err) + } + + cmdOpts := rc.applyOptions(opts...) + + if err := rc.initializeLogger(cmdOpts); err != nil { + return fmt.Errorf("failed to initialize from config: %w", err) + } + + return nil +} + +func (rc *RootCmd) initializeConfiguration(cmd *cobra.Command) error { + return rc.getConfFromCmdAndInit(cmd) +} + +func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts { + cmdOpts := defaultCmdOpts() + for _, opt := range opts { + opt(cmdOpts) + } + + return cmdOpts +} + +func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error { + logConfig := config.Config.Log + + return log.InitFromConfig( + + cmdOpts.loggerPrefixName, + rc.Name, + logConfig.RemainLogLevel, + logConfig.IsStdout, + logConfig.IsJson, + logConfig.StorageLocation, + logConfig.RemainRotationCount, + logConfig.RotationTime, + ) +} + +func defaultCmdOpts() *CmdOpts { + return &CmdOpts{ + loggerPrefixName: "OpenIM.log.all", + } +} + func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) { r.cmdItf = cmdItf } + func (r *RootCmd) addConfFlag() { - r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") + r.Command.Flags().StringP(constant.FlagConf, "c", "", "path to config file folder") } func (r *RootCmd) AddPortFlag() { diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 84cddf0da..e37514ecd 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -19,32 +19,31 @@ import ( "fmt" "os" "path/filepath" - "runtime" + "github.com/OpenIMSDK/protocol/constant" "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" - "gopkg.in/yaml.v3" - - "github.com/OpenIMSDK/protocol/constant" ) //go:embed version var Version string -var ( - _, b, _, _ = runtime.Caller(0) - // Root folder of this project. - Root = filepath.Join(filepath.Dir(b), "../../..") -) - const ( FileName = "config.yaml" NotificationFileName = "notification.yaml" DefaultFolderPath = "../config/" ) +// getProjectRoot returns the absolute path of the project root directory +func GetProjectRoot() string { + b, _ := filepath.Abs(os.Args[0]) + + return filepath.Join(filepath.Dir(b), "../../..") +} + func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { opts := msgprocessor.NewOptions() + if cfg.UnreadCount { opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true)) } @@ -61,40 +60,38 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { } func initConfig(config interface{}, configName, configFolderPath string) error { - if configFolderPath == "" { - configFolderPath = DefaultFolderPath - } - configPath := filepath.Join(configFolderPath, configName) - defer func() { - fmt.Println("use config", configPath) - }() - _, err := os.Stat(configPath) + configFolderPath = filepath.Join(configFolderPath, configName) + _, err := os.Stat(configFolderPath) if err != nil { if !os.IsNotExist(err) { - return err + return fmt.Errorf("stat config path error: %w", err) } - configPath = filepath.Join(Root, "config", configName) - } else { - Root = filepath.Dir(configPath) + configFolderPath = filepath.Join(GetProjectRoot(), "config", configName) } - data, err := os.ReadFile(configPath) + data, err := os.ReadFile(configFolderPath) if err != nil { - return err + return fmt.Errorf("read file error: %w", err) } if err = yaml.Unmarshal(data, config); err != nil { - return err + return fmt.Errorf("unmarshal yaml error: %w", err) } + fmt.Println("use config", configFolderPath) return nil } func InitConfig(configFolderPath string) error { - err := initConfig(&Config, FileName, configFolderPath) - if err != nil { - return err + if configFolderPath == "" { + envConfigPath := os.Getenv("OPENIMCONFIG") + if envConfigPath != "" { + configFolderPath = envConfigPath + } else { + configFolderPath = DefaultFolderPath + } } - err = initConfig(&Config.Notification, NotificationFileName, configFolderPath) - if err != nil { + + if err := initConfig(&Config, FileName, configFolderPath); err != nil { return err } - return nil + + return initConfig(&Config.Notification, NotificationFileName, configFolderPath) } diff --git a/pkg/common/config/version b/pkg/common/config/version index aa6c89679..e682ea429 100644 --- a/pkg/common/config/version +++ b/pkg/common/config/version @@ -1 +1 @@ -v3.2.0 \ No newline at end of file +v3.3.0 \ No newline at end of file diff --git a/scripts/check-all.sh b/scripts/check-all.sh index f1d72c41c..0d849dd50 100755 --- a/scripts/check-all.sh +++ b/scripts/check-all.sh @@ -64,9 +64,9 @@ set +e # Later, after discarding Docker, the Docker keyword is unreliable, and Kubepods is used if grep -qE 'docker|kubepods' /proc/1/cgroup || [ -f /.dockerenv ]; then - openim::color::echo ${COLOR_BLUE} "Environment in the interior of the container" + openim::color::echo ${COLOR_CYAN} "Environment in the interior of the container" else - openim::color::echo ${COLOR_BLUE} "The environment is outside the container" + openim::color::echo ${COLOR_CYAN} "The environment is outside the container" openim::util::check_ports ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]} || return 0 fi diff --git a/scripts/demo.sh b/scripts/demo.sh index 329e04f0f..5f8a2023a 100755 --- a/scripts/demo.sh +++ b/scripts/demo.sh @@ -37,6 +37,8 @@ readonly ipv6regex='(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}: clear . $(dirname ${BASH_SOURCE})/lib/util.sh +openim::util::ensure-bash-version + trap 'openim::util::onCtrlC' INT function openim::util::onCtrlC() { diff --git a/scripts/docker-check-service.sh b/scripts/docker-check-service.sh index c1fb94748..adf383436 100755 --- a/scripts/docker-check-service.sh +++ b/scripts/docker-check-service.sh @@ -19,11 +19,7 @@ source "${OPENIM_ROOT}/scripts/install/common.sh" cd "$OPENIM_ROOT" -if command -v docker-compose &> /dev/null; then - docker-compose ps -else - docker compose ps -fi +openim::util::check_docker_and_compose_versions progress() { local _main_pid="$1" diff --git a/scripts/install-im-server.sh b/scripts/install-im-server.sh index 76d5120fe..26ab35b0d 100755 --- a/scripts/install-im-server.sh +++ b/scripts/install-im-server.sh @@ -30,6 +30,8 @@ openim::util::ensure_docker_daemon_connectivity DOCKER_COMPOSE_COMMAND= # Check if docker-compose command is available +openim::util::check_docker_and_compose_versions + if command -v docker compose &> /dev/null then openim::log::info "docker compose command is available" diff --git a/scripts/install_im_compose.sh b/scripts/install_im_compose.sh index 1f7dd7f52..06a573424 100755 --- a/scripts/install_im_compose.sh +++ b/scripts/install_im_compose.sh @@ -51,17 +51,7 @@ execute_scripts() { "${OPENIM_ROOT}"/scripts/env_check.sh } -# Start docker compose -start_docker_compose() { - openim::log::info "Checking if docker-compose command is available" - if command -v docker-compose &> /dev/null; then - docker-compose up -d - else - docker compose up -d - fi - - "${OPENIM_ROOT}"/scripts/docker-check-service.sh -} +openim::util::check_docker_and_compose_versions main() { load_env diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 0ad386e1a..d7c274c56 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -250,6 +250,39 @@ openim::util::host_arch() { echo "${host_arch}" } +# Define a bash function to check the versions of Docker and Docker Compose +openim::util::check_docker_and_compose_versions() { + # Define the required versions of Docker and Docker Compose + required_docker_version="20.10.0" + required_compose_version="2.0" + + # Get the currently installed Docker version + installed_docker_version=$(docker --version | awk '{print $3}' | sed 's/,//') + + # Check if the installed Docker version matches the required version + if [[ "$installed_docker_version" < "$required_docker_version" ]]; then + echo "Docker version mismatch. Installed: $installed_docker_version, Required: $required_docker_version" + return 1 + fi + + # Check if the docker compose sub-command is available + if ! docker compose version &> /dev/null; then + echo "Docker does not support the docker compose sub-command" + return 1 + fi + + # Get the currently installed Docker Compose version + installed_compose_version=$(docker compose version --short) + + # Check if the installed Docker Compose version matches the required version + if [[ "$installed_compose_version" < "$required_compose_version" ]]; then + echo "Docker Compose version mismatch. Installed: $installed_compose_version, Required: $required_compose_version" + return 1 + fi + +} + + # The `openim::util::check_ports` function analyzes the state of processes based on given ports. # It accepts multiple ports as arguments and prints: # 1. The state of the process (whether it's running or not). @@ -1321,3 +1354,7 @@ function openim::util::gen_os_arch() { exit 1 fi } + +if [[ "$*" =~ openim::util:: ]];then + eval $* +fi \ No newline at end of file diff --git a/scripts/make-rules/common.mk b/scripts/make-rules/common.mk index f72d22f73..99ceea7e0 100644 --- a/scripts/make-rules/common.mk +++ b/scripts/make-rules/common.mk @@ -167,6 +167,7 @@ define MAKEFILE_EXAMPLE # make install-deepcopy-gen Install deepcopy-gen tools if the license is missing. # make build BINS=openim-api V=1 DEBUG=1 Build debug binaries for only openim-api. # make multiarch -j PLATFORMS="linux_arm64 linux_amd64" V=1 Build binaries for both platforms. +# make image endef export MAKEFILE_EXAMPLE diff --git a/scripts/make-rules/golang.mk b/scripts/make-rules/golang.mk index 742016cbd..cc2c98544 100644 --- a/scripts/make-rules/golang.mk +++ b/scripts/make-rules/golang.mk @@ -17,7 +17,7 @@ # GO := go -GO_SUPPORTED_VERSIONS ?= 1.18|1.19|1.20|1.21|1.22 +GO_SUPPORTED_VERSIONS ?= 1.19|1.20|1.21|1.22 GO_LDFLAGS += -X $(VERSION_PACKAGE).gitVersion=$(GIT_TAG) \ -X $(VERSION_PACKAGE).gitCommit=$(GIT_COMMIT) \ @@ -45,7 +45,7 @@ ifeq ($(origin GOBIN), undefined) endif # COMMANDS is Specify all files under ${ROOT_DIR}/cmd/ and ${ROOT_DIR}/tools/ except those ending in.md -COMMANDS ?= $(filter-out %.md, $(wildcard ${ROOT_DIR}/cmd/* ${ROOT_DIR}/tools/* ${ROOT_DIR}/cmd/openim-rpc/*)) +COMMANDS ?= $(filter-out %.md, $(wildcard ${ROOT_DIR}/cmd/* ${ROOT_DIR}/tools/* ${ROOT_DIR}/tools/data-conversion/chat/cmd/* ${ROOT_DIR}/tools/data-conversion/openim/cmd/* ${ROOT_DIR}/cmd/openim-rpc/*)) ifeq (${COMMANDS},) $(error Could not determine COMMANDS, set ROOT_DIR or run in source dir) endif @@ -136,6 +136,7 @@ ifneq ($(shell $(GO) version | grep -q -E '\bgo($(GO_SUPPORTED_VERSIONS))\b' && $(error unsupported go version. Please make install one of the following supported version: '$(GO_SUPPORTED_VERSIONS)') endif +## go.build.%: Build binaries for a specific platform .PHONY: go.build.% go.build.%: $(eval COMMAND := $(word 2,$(subst ., ,$*))) @@ -159,6 +160,14 @@ go.build.%: CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \ $(BIN_TOOLS_DIR)/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/tools/$(COMMAND)/$(COMMAND).go; \ chmod +x $(BIN_TOOLS_DIR)/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT); \ + elif [ -f $(ROOT_DIR)/tools/data-conversion/openim/cmd/$(COMMAND)/$(COMMAND).go ]; then \ + CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \ + $(BIN_TOOLS_DIR)/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/tools/data-conversion/openim/cmd/$(COMMAND)/$(COMMAND).go; \ + chmod +x $(BIN_TOOLS_DIR)/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT); \ + elif [ -f $(ROOT_DIR)/tools/data-conversion/chat/cmd/$(COMMAND)/$(COMMAND).go ]; then \ + CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \ + $(BIN_TOOLS_DIR)/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/tools/data-conversion/chat/cmd/$(COMMAND)/$(COMMAND).go; \ + chmod +x $(BIN_TOOLS_DIR)/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT); \ fi \ fi diff --git a/scripts/make-rules/image.mk b/scripts/make-rules/image.mk index eea651d88..09e92126b 100644 --- a/scripts/make-rules/image.mk +++ b/scripts/make-rules/image.mk @@ -21,7 +21,6 @@ # DOCKER := docker -DOCKER_SUPPORTED_API_VERSION ?= 1.32|1.40|1.41|1.42 # read: https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/images.md REGISTRY_PREFIX ?= ghcr.io/openimsdk @@ -43,8 +42,8 @@ endif # Determine image files by looking into build/images/*/Dockerfile IMAGES_DIR ?= $(wildcard ${ROOT_DIR}/build/images/*) -# Determine images names by stripping out the dir names -IMAGES ?= $(filter-out tools,$(foreach image,${IMAGES_DIR},$(notdir ${image}))) +# Determine images names by stripping out the dir names, and filter out the undesired directories +IMAGES ?= $(filter-out Dockerfile openim-tools openim-cmdutils,$(foreach image,${IMAGES_DIR},$(notdir ${image}))) ifeq (${IMAGES},) $(error Could not determine IMAGES, set ROOT_DIR or run in source dir) @@ -75,22 +74,13 @@ image.docker-buildx: ## image.verify: Verify docker version .PHONY: image.verify image.verify: - $(eval API_VERSION := $(shell $(DOCKER) version | grep -E 'API version: {1,6}[0-9]' | head -n1 | awk '{print $$3} END { if (NR==0) print 0}' )) - $(eval PASS := $(shell echo "$(API_VERSION) > $(DOCKER_SUPPORTED_API_VERSION)" | bc)) - @if [ $(PASS) -ne 1 ]; then \ - $(DOCKER) -v ;\ - echo "Unsupported docker version. Docker API version should be greater than $(DOCKER_SUPPORTED_API_VERSION)"; \ - exit 1; \ - fi + @$(ROOT_DIR)/scripts/lib/util.sh openim::util::check_docker_and_compose_versions ## image.daemon.verify: Verify docker daemon experimental features .PHONY: image.daemon.verify image.daemon.verify: - $(eval PASS := $(shell $(DOCKER) version | grep -q -E 'Experimental: {1,5}true' && echo 1 || echo 0)) - @if [ $(PASS) -ne 1 ]; then \ - echo "Experimental features of Docker daemon is not enabled. Please add \"experimental\": true in '/etc/docker/daemon.json' and then restart Docker daemon."; \ - exit 1; \ - fi + @$(ROOT_DIR)/scripts/lib/util.sh openim::util::ensure_docker_daemon_connectivity + @$(ROOT_DIR)/scripts/lib/util.sh openim::util::ensure-docker-buildx # If you wish built the manager image targeting other platforms you can use the --platform flag. # (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it. @@ -110,10 +100,9 @@ image.build.%: go.build.% $(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM)))) @echo "===========> Building docker image $(IMAGE) $(VERSION) for $(IMAGE_PLAT)" @mkdir -p $(TMP_DIR)/$(IMAGE)/$(PLATFORM) - @awk '/FROM/ {c++; if (c==2) {print; next}} c>=2' $(ROOT_DIR)/build/images/$(IMAGE)/Dockerfile \ -| sed -e "s#BASE_IMAGE#$(BASE_IMAGE)#g" \ - -e 's/--from=builder //g' \ - -e 's#COPY /openim/openim-server/#COPY ./#g' > $(TMP_DIR)/$(IMAGE)/Dockerfile + @cat $(ROOT_DIR)/build/images/Dockerfile\ + | sed "s#BASE_IMAGE#$(BASE_IMAGE)#g" \ + | sed "s#BINARY_NAME#$(IMAGE)#g" >$(TMP_DIR)/$(IMAGE)/Dockerfile @cp $(BIN_DIR)/platforms/$(IMAGE_PLAT)/$(IMAGE) $(TMP_DIR)/$(IMAGE) $(eval BUILD_SUFFIX := $(_DOCKER_BUILD_EXTRA_ARGS) --pull -t $(REGISTRY_PREFIX)/$(IMAGE)-$(ARCH):$(VERSION) $(TMP_DIR)/$(IMAGE)) @if [ $(shell $(GO) env GOARCH) != $(ARCH) ] ; then \ @@ -124,7 +113,6 @@ image.build.%: go.build.% fi @rm -rf $(TMP_DIR)/$(IMAGE) - # https://docs.docker.com/build/building/multi-platform/ # busybox image supports amd64, arm32v5, arm32v6, arm32v7, arm64v8, i386, ppc64le, and s390x ## image.buildx.%: Build docker images with buildx diff --git a/test/typecheck/go.mod b/test/typecheck/go.mod index f644c5b25..9ef1b1da7 100644 --- a/test/typecheck/go.mod +++ b/test/typecheck/go.mod @@ -1,6 +1,6 @@ module github.com/openimsdk/open-im-server/test/typecheck -go 1.18 +go 1.19 require golang.org/x/tools v0.12.0 diff --git a/tests/group_test.go b/tests/group_test.go deleted file mode 100644 index 54878805e..000000000 --- a/tests/group_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package tests - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" -) - -func TestDeleteGroupMemberHash(t *testing.T) { - mockGroupDB := new(controller.MockGroupDatabase) - - testGroupMemberHash := "testGroupMemberHash" - - err := mockGroupDB.DeleteGroupMemberHash(testGroupMemberHash) - assert.Nil(t, err) - - nonExistentGroupMemberHash := "nonExistentGroupMemberHash" - - err = mockGroupDB.DeleteGroupMemberHash(nonExistentGroupMemberHash) - assert.NotNil(t, err) -} diff --git a/tests/user_test.go b/tests/user_test.go deleted file mode 100644 index 5278f27f1..000000000 --- a/tests/user_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package cache_test - -import ( - "reflect" - "testing" - - "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" -) - -func TestRemoveRepeatedElementsInList(t *testing.T) { - testCases := []struct { - name string - input []string - expected []string - }{ - { - name: "No duplicates", - input: []string{"a", "b", "c"}, - expected: []string{"a", "b", "c"}, - }, - { - name: "All duplicates", - input: []string{"a", "a", "a"}, - expected: []string{"a"}, - }, - { - name: "Some duplicates", - input: []string{"a", "b", "a", "c", "b"}, - expected: []string{"a", "b", "c"}, - }, - { - name: "Empty list", - input: []string{}, - expected: []string{}, - }, - { - name: "Single element", - input: []string{"a"}, - expected: []string{"a"}, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - result := cache.RemoveRepeatedElementsInList(tc.input) - if !reflect.DeepEqual(result, tc.expected) { - t.Errorf("expected %v, got %v", tc.expected, result) - } - }) - } -} diff --git a/tools/changelog/go.mod b/tools/changelog/go.mod index 924f6709a..b5a2272a4 100644 --- a/tools/changelog/go.mod +++ b/tools/changelog/go.mod @@ -1,3 +1,3 @@ module github.com/openimsdk/open-im-server/v3/tools/changelog -go 1.18 +go 1.19 diff --git a/tools/component/go.mod b/tools/component/go.mod index 8d5548ee8..05a27bcad 100644 --- a/tools/component/go.mod +++ b/tools/component/go.mod @@ -1,3 +1,3 @@ module github.com/openimsdk/open-im-server/v3/tools/component -go 1.18 +go 1.19 diff --git a/tools/data-conversion/README.md b/tools/data-conversion/README.md new file mode 100644 index 000000000..282fbdc03 --- /dev/null +++ b/tools/data-conversion/README.md @@ -0,0 +1,103 @@ + +在更新你的数据迁移README文档时,考虑到清晰、准确和专业的表述是非常重要的。这不仅可以帮助读者更好地理解迁移过程,还可以确保在迁移过程中减少可能出现的错误。以下是对你提供的README文档的一些改进和完善建议: + +------ + +# OpenIM V2 至 V3 数据迁移指南 + +该指南提供了从 OpenIM V2 迁移至 V3 的详细步骤。请确保在开始迁移过程之前,熟悉所有步骤,并按照指南准确执行。 + ++ [OpenIM Chat](https://github.com/OpenIMSDK/chat) ++ [OpenIM Server](https://github.com/OpenIMSDK/Open-IM-Server) + + + +### 1. 数据备份 + +在开始数据迁移之前,强烈建议备份所有相关的数据以防止任何可能的数据丢失。 + +### 2. 迁移 OpenIM MySQL 数据 + ++ 位置: `open-im-server/v3/tools/data-conversion/openim/mysql.go` ++ 配置 `mysql.go` 文件中的数据库信息。 ++ 手动创建 V3 版本的数据库,并确保字符集为 `utf8mb4`。 + +```bash +// V2 数据库配置 +var ( + usernameV2 = "root" + passwordV2 = "openIM" + addrV2 = "127.0.0.1:13306" + databaseV2 = "openIM_v2" +) + +// V3 数据库配置 +var ( + usernameV3 = "root" + passwordV3 = "openIM123" + addrV3 = "127.0.0.1:13306" + databaseV3 = "openIM_v3" +) +``` + +**执行数据迁移命令:** + +```bash +make build BINS="conversion-mysql" +``` + +启动的二进制在 `_output/bin/tools` 中 + + +### 3. 转换聊天消息(可选) + ++ 只支持转换存储在 Kafka 中的消息。 ++ 位置: `open-im-server/v3/tools/data-conversion/openim/msg.go` ++ 配置 `msg.go` 文件中的消息和服务器信息。 + +```bash +var ( + topic = "ws2ms_chat" // V2 版本 Kafka 主题 + kafkaAddr = "127.0.0.1:9092" // V2 版本 Kafka 地址 + rpcAddr = "127.0.0.1:10130" // V3 版本 RPC 地址 + adminUserID = "openIM123456" // V3 版本管理员用户ID + concurrency = 4 // 并发数量 +) +``` + +**执行数据迁移命令:** + +```bash +make build BINS="conversion-msg" +``` + +### 4. 转换业务服务器数据 + ++ 只支持转换存储在 Kafka 中的消息。 ++ 位置: `open-im-server/v3/tools/data-conversion/chat/chat.go` ++ 需要手动创建 V3 版本的数据库,并确保字符集为 `utf8mb4`。 ++ 配置 `main.go` 文件中的数据库信息。 + +```bash +// V2 数据库配置 +var ( + usernameV2 = "root" + passwordV2 = "openIM" + addrV2 = "127.0.0.1:13306" + databaseV2 = "admin_chat" +) + +// V3 数据库配置 +var ( + usernameV3 = "root" + passwordV3 = "openIM123" + addrV3 = "127.0.0.1:13306" + databaseV3 = "openim_enterprise" +) +``` + +**执行数据迁移命令:** + +```bash +make build BINS="conversion-chat" +``` \ No newline at end of file diff --git a/tools/data-conversion/chat/chat.go b/tools/data-conversion/chat/cmd/conversion-chat/chat.go similarity index 100% rename from tools/data-conversion/chat/chat.go rename to tools/data-conversion/chat/cmd/conversion-chat/chat.go diff --git a/tools/data-conversion/go.mod b/tools/data-conversion/go.mod index 51cc32f2e..b0d7aea13 100644 --- a/tools/data-conversion/go.mod +++ b/tools/data-conversion/go.mod @@ -1,6 +1,6 @@ module github.com/openimsdk/open-im-server/v3/tools/data-conversion -go 1.18 +go 1.19 require ( github.com/IBM/sarama v1.41.2 diff --git a/tools/data-conversion/info.md b/tools/data-conversion/info.md deleted file mode 100644 index 600b1d09c..000000000 --- a/tools/data-conversion/info.md +++ /dev/null @@ -1,68 +0,0 @@ -# v2数据迁移工具 - -### 转换前请做好数据备份!!! - -### 转换OPENIM MYSQL数据 - - open-im-server/v3/tools/data-conversion/openim/mysql.go - - 配置mysql.go数据库信息 - - 需要手动创建v3版本数据库,字符集`utf8mb4` - -```go -var ( - usernameV2 = "root" // v2版本mysql用户名 - passwordV2 = "openIM" // v2版本mysql密码 - addrV2 = "127.0.0.1:13306" // v2版本mysql地址 - databaseV2 = "openIM_v2" // v2版本mysql数据库名字 -) - -var ( - usernameV3 = "root" // v3版本mysql用户名 - passwordV3 = "openIM123" // v3版本mysql密码 - addrV3 = "127.0.0.1:13306" // v3版本mysql地址 - databaseV3 = "openIM_v3" // v3版本mysql数据库名字 -) -``` -```shell -go run mysql.go -``` - -### 转换聊天消息(可选) -- 目前只支持转换kafka中的消息 -- open-im-server/v3/tools/data-conversion/openim/msg.go -- 配置msg.go数据库信息 -```go -var ( - topic = "ws2ms_chat" // v2版本配置文件kafka.topic.ws2ms_chat - kafkaAddr = "127.0.0.1:9092" // v2版本配置文件kafka.topic.addr - rpcAddr = "127.0.0.1:10130" // v3版本配置文件rpcPort.openImMessagePort - adminUserID = "openIM123456" // v3版本管理员userID - concurrency = 4 // 并发数量 -) -``` -```shell -go run msg.go -``` - -### 转换业务服务器数据(使用官方业务服务器需要转换) -- 目前只支持转换kafka中的消息 -- open-im-server/v3/tools/data-conversion/chat/chat.go -- 需要手动创建v3版本数据库,字符集`utf8mb4` -- main.go数据库信息 -```go -var ( - usernameV2 = "root" // v2版本mysql用户名 - passwordV2 = "openIM" // v2版本mysql密码 - addrV2 = "127.0.0.1:13306" // v2版本mysql地址 - databaseV2 = "admin_chat" // v2版本mysql数据库名字 -) - -var ( - usernameV3 = "root" // v3版本mysql用户名 - passwordV3 = "openIM123" // v3版本mysql密码 - addrV3 = "127.0.0.1:13306" // v3版本mysql地址 - databaseV3 = "openim_enterprise" // v3版本mysql数据库名字 -) -``` -```shell -go run chat.go -``` \ No newline at end of file diff --git a/tools/data-conversion/openim/msg.go b/tools/data-conversion/openim/cmd/conversion-msg/conversion-msg.go similarity index 100% rename from tools/data-conversion/openim/msg.go rename to tools/data-conversion/openim/cmd/conversion-msg/conversion-msg.go diff --git a/tools/data-conversion/openim/mysql.go b/tools/data-conversion/openim/cmd/conversion-mysql/conversion-mysql.go similarity index 100% rename from tools/data-conversion/openim/mysql.go rename to tools/data-conversion/openim/cmd/conversion-mysql/conversion-mysql.go diff --git a/tools/imctl/go.mod b/tools/imctl/go.mod index 60b06b45b..e3a720b27 100644 --- a/tools/imctl/go.mod +++ b/tools/imctl/go.mod @@ -1,6 +1,6 @@ module github.com/openimsdk/open-im-server/v3/tools/imctl -go 1.18 +go 1.19 require ( github.com/MakeNowJust/heredoc/v2 v2.0.1 diff --git a/tools/infra/go.mod b/tools/infra/go.mod index 6ee10304b..a6cf16c77 100644 --- a/tools/infra/go.mod +++ b/tools/infra/go.mod @@ -1,3 +1,3 @@ module github.com/openimsdk/open-im-server/v3/tools/infra -go 1.18 +go 1.19 diff --git a/tools/ncpu/go.mod b/tools/ncpu/go.mod index dfccea27a..66697e350 100644 --- a/tools/ncpu/go.mod +++ b/tools/ncpu/go.mod @@ -1,5 +1,5 @@ module github.com/openimsdk/open-im-server/v3/tools/ncpu -go 1.18 +go 1.19 require go.uber.org/automaxprocs v1.5.3 diff --git a/tools/openim-web/go.mod b/tools/openim-web/go.mod index 6f30c673c..deb24f4e5 100644 --- a/tools/openim-web/go.mod +++ b/tools/openim-web/go.mod @@ -1,6 +1,6 @@ module github.com/openimsdk/open-im-server/v3/tools/openim-web -go 1.18 +go 1.19 require gopkg.in/yaml.v2 v2.4.0 diff --git a/tools/versionchecker/go.mod b/tools/versionchecker/go.mod index 5858649e8..619bcdb5b 100644 --- a/tools/versionchecker/go.mod +++ b/tools/versionchecker/go.mod @@ -1,3 +1,3 @@ module github.com/openimsdk/open-im-server/v3/tools/versionchecker -go 1.18 +go 1.19 diff --git a/tools/yamlfmt/go.mod b/tools/yamlfmt/go.mod index 5b3581efc..7c496a530 100644 --- a/tools/yamlfmt/go.mod +++ b/tools/yamlfmt/go.mod @@ -1,6 +1,6 @@ module github.com/openimsdk/open-im-server/v3/tools/yamlfmt -go 1.18 +go 1.19 require ( github.com/likexian/gokit v0.25.13 From 9cd8fb2b01b88cfce462eccea9ec3769c3afee47 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:41:06 +0800 Subject: [PATCH 13/21] feat(deps): bump the gomod-deps group with 2 updates (#1257) Bumps the gomod-deps group with 2 updates: [google.golang.org/api](https://github.com/googleapis/google-api-go-client) and [github.com/OpenIMSDK/protocol](https://github.com/OpenIMSDK/protocol). Updates `google.golang.org/api` from 0.147.0 to 0.148.0 - [Release notes](https://github.com/googleapis/google-api-go-client/releases) - [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md) - [Commits](https://github.com/googleapis/google-api-go-client/compare/v0.147.0...v0.148.0) Updates `github.com/OpenIMSDK/protocol` from 0.0.27 to 0.0.30 - [Release notes](https://github.com/OpenIMSDK/protocol/releases) - [Commits](https://github.com/OpenIMSDK/protocol/compare/v0.0.27...v0.0.30) --- updated-dependencies: - dependency-name: google.golang.org/api dependency-type: direct:production update-type: version-update:semver-minor dependency-group: gomod-deps - dependency-name: github.com/OpenIMSDK/protocol dependency-type: direct:production update-type: version-update:semver-patch dependency-group: gomod-deps ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 6 +++--- go.sum | 44 +++++++------------------------------------- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index c26bcad59..4bd7999b7 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/stretchr/testify v1.8.4 go.mongodb.org/mongo-driver v1.12.1 golang.org/x/image v0.13.0 - google.golang.org/api v0.147.0 + google.golang.org/api v0.148.0 google.golang.org/grpc v1.59.0 google.golang.org/protobuf v1.31.0 gopkg.in/yaml.v3 v3.0.1 @@ -37,7 +37,7 @@ require github.com/google/uuid v1.3.1 require ( github.com/IBM/sarama v1.41.3 - github.com/OpenIMSDK/protocol v0.0.27 + github.com/OpenIMSDK/protocol v0.0.30 github.com/OpenIMSDK/tools v0.0.14 github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible github.com/go-redis/redis v6.15.9+incompatible @@ -128,7 +128,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a // indirect ) require ( diff --git a/go.sum b/go.sum index 4bdc0b51b..0dd6cc940 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIw github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/IBM/sarama v1.41.3 h1:MWBEJ12vHC8coMjdEXFq/6ftO6DUZnQlFYcxtOJFa7c= github.com/IBM/sarama v1.41.3/go.mod h1:Xxho9HkHd4K/MDUo/T/sOqwtX/17D33++E9Wib6hUdQ= -github.com/OpenIMSDK/protocol v0.0.27 h1:ee1Y6idLahgJrTHkgWPboKIvCT9zvf7jitavVpffpPw= -github.com/OpenIMSDK/protocol v0.0.27/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= +github.com/OpenIMSDK/protocol v0.0.30 h1:MiHO6PyQMR9ojBHNnSFxCHLmsoE2xZqaiYj975JiZnM= +github.com/OpenIMSDK/protocol v0.0.30/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= github.com/OpenIMSDK/tools v0.0.14 h1:WLof/+WxyPyRST+QkoTKubYCiV73uCLiL8pgnpH/yKQ= github.com/OpenIMSDK/tools v0.0.14/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= @@ -44,21 +44,14 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= -github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y= -github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs= github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I= github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/cosiner/argv v0.1.0 h1:BVDiEL32lwHukgJKP87btEPenzrrHUjajs/8yzaqcXg= -github.com/cosiner/argv v0.1.0/go.mod h1:EusR6TucWKX+zFgtdUsKT2Cvg45K5rtpCcWz4hK06d8= -github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/derekparker/trie v0.0.0-20221213183930-4c74548207f4 h1:atN94qKNhLpy+9BwbE5nxvFj4rScJi6W3x/NfFmMDg4= -github.com/derekparker/trie v0.0.0-20221213183930-4c74548207f4/go.mod h1:C7Es+DLenIpPc9J6IYw4jrK0h7S9bKj4DNl8+KxGEXU= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= @@ -86,10 +79,6 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= -github.com/go-delve/delve v1.21.1 h1:oDpED8gvXPLS1VKSYzaMH/ihZtyk04H9jqQ9xpyFXl0= -github.com/go-delve/delve v1.21.1/go.mod h1:FgTAiRUe43RS5EexL06RPyMtP8AMZVL/t9Qqgy3qUe4= -github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d h1:pxjSLshkZJGLVm0wv20f/H0oTWiq/egkoJQ2ja6LEvo= -github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d/go.mod h1:biJCRbqp51wS+I92HMqn5H8/A0PAhxn2vyOT+JqhiGI= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= @@ -139,13 +128,10 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-dap v0.9.1 h1:d8dETjgHMR9/xs+Xza+NrZmB7jxIS5OtM2uRsyJVA/c= -github.com/google/go-dap v0.9.1/go.mod h1:HAeyoSd2WIfTfg+0GRXcFrb+RnojAtGNh+k+XTIxJDE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= @@ -175,8 +161,6 @@ github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9 github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= -github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -223,13 +207,8 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= github.com/lithammer/shortuuid v3.0.0+incompatible h1:NcD0xWW/MZYXEHa6ITy6kaXN5nwm/V115vj2YXfhS0w= github.com/lithammer/shortuuid v3.0.0+incompatible/go.mod h1:FR74pbAuElzOUuenUHTK2Tciko1/vKuIKS9dSkDrA4w= -github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= -github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= -github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= @@ -288,14 +267,11 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5X github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg= github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= @@ -340,8 +316,6 @@ go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecq go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= -go.starlark.net v0.0.0-20220816155156-cfacd8902214 h1:MqijAN3S61c7KWasOk+zIqIjHQPN6WUra/X3+YAkQxQ= -go.starlark.net v0.0.0-20220816155156-cfacd8902214/go.mod h1:VZcBMdr3cT3PnBoWunTabuSEXwVAH+ZJ5zxfs3AdASk= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= @@ -361,8 +335,6 @@ golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58 golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= -golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg= golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -418,12 +390,10 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= @@ -458,8 +428,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= -google.golang.org/api v0.147.0 h1:Can3FaQo9LlVqxJCodNmeZW/ib3/qKAY3rFeXiHo5gc= -google.golang.org/api v0.147.0/go.mod h1:pQ/9j83DcmPd/5C9e2nFOdjjNkDZ1G+zkbK2uvdkJMs= +google.golang.org/api v0.148.0 h1:HBq4TZlN4/1pNcu0geJZ/Q50vIwIXT532UIMYoo0vOs= +google.golang.org/api v0.148.0/go.mod h1:8/TBgwaKjfqTdacOJrOv2+2Q6fBDU1uHKK06oGSkxzU= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= @@ -471,8 +441,8 @@ google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 h1:SeZZZx0cP0fqUyA google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97/go.mod h1:t1VqOqqvce95G3hIDCT5FeO3YUc6Q4Oe24L/+rNMxRk= google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU= google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c h1:jHkCUWkseRf+W+edG5hMzr/Uh1xkDREY4caybAq4dpY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c/go.mod h1:4cYg8o5yUbm77w8ZX00LhMVNl/YVBFJRYWDc0uYWMs0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a h1:a2MQQVoTo96JC9PMGtGBymLp7+/RzpFc2yX/9WfFg1c= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a/go.mod h1:4cYg8o5yUbm77w8ZX00LhMVNl/YVBFJRYWDc0uYWMs0= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= From 088b14330044d18377c0aa1caafb668107df0d03 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Mon, 23 Oct 2023 10:30:11 +0800 Subject: [PATCH 14/21] Update README.md (#1265) --- tools/data-conversion/README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tools/data-conversion/README.md b/tools/data-conversion/README.md index 282fbdc03..8d1bf8629 100644 --- a/tools/data-conversion/README.md +++ b/tools/data-conversion/README.md @@ -1,8 +1,3 @@ - -在更新你的数据迁移README文档时,考虑到清晰、准确和专业的表述是非常重要的。这不仅可以帮助读者更好地理解迁移过程,还可以确保在迁移过程中减少可能出现的错误。以下是对你提供的README文档的一些改进和完善建议: - ------- - # OpenIM V2 至 V3 数据迁移指南 该指南提供了从 OpenIM V2 迁移至 V3 的详细步骤。请确保在开始迁移过程之前,熟悉所有步骤,并按照指南准确执行。 @@ -100,4 +95,4 @@ var ( ```bash make build BINS="conversion-chat" -``` \ No newline at end of file +``` From ba190fde13ad099f42af1402abcddf6f500053ee Mon Sep 17 00:00:00 2001 From: liuxuewen Date: Mon, 23 Oct 2023 11:27:15 +0800 Subject: [PATCH 15/21] fix: golint code format and error log trace (#1259) --- internal/msggateway/init.go | 5 ++++- pkg/common/cmd/root.go | 1 + pkg/common/startrpc/doc.go | 3 ++- pkg/common/startrpc/start.go | 24 +++++++++--------------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/internal/msggateway/init.go b/internal/msggateway/init.go index 7a4b6d6ad..ce63fb21a 100644 --- a/internal/msggateway/init.go +++ b/internal/msggateway/init.go @@ -18,9 +18,12 @@ import ( "fmt" "time" + "github.com/OpenIMSDK/tools/utils" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) +// RunWsAndServer run ws server func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { fmt.Println( "start rpc/msg_gateway server, port: ", @@ -42,7 +45,7 @@ func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { go func() { err := hubServer.Start() if err != nil { - panic(err.Error()) + panic(utils.Wrap1(err)) } }() return hubServer.LongConnServer.Run() diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index f220ab7c1..4ea36cfeb 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -23,6 +23,7 @@ import ( "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/log" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) diff --git a/pkg/common/startrpc/doc.go b/pkg/common/startrpc/doc.go index 1d5fd6787..206e7900f 100644 --- a/pkg/common/startrpc/doc.go +++ b/pkg/common/startrpc/doc.go @@ -12,4 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -package startrpc // import "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc" +// Package startrpc start rpc server. +package startrpc diff --git a/pkg/common/startrpc/start.go b/pkg/common/startrpc/start.go index 19da8a586..2aeff3cf0 100644 --- a/pkg/common/startrpc/start.go +++ b/pkg/common/startrpc/start.go @@ -16,11 +16,12 @@ package startrpc import ( "fmt" - "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "net" "strconv" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -32,6 +33,7 @@ import ( "github.com/OpenIMSDK/tools/utils" ) +// Start rpc server. func Start( rpcPort int, rpcRegisterName string, @@ -39,16 +41,8 @@ func Start( rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption, ) error { - fmt.Println( - "start", - rpcRegisterName, - "server, port: ", - rpcPort, - "prometheusPort:", - prometheusPort, - ", OpenIM version: ", - config.Version, - ) + fmt.Printf("start %s server, port: %d, prometheusPort: %d, OpenIM version: %s", + rpcRegisterName, rpcPort, prometheusPort, config.Version) listener, err := net.Listen( "tcp", net.JoinHostPort(network.GetListenIP(config.Config.Rpc.ListenIP), strconv.Itoa(rpcPort)), @@ -63,11 +57,10 @@ func Start( zookeeper.WithFreq(time.Hour), zookeeper.WithUserNameAndPassword( config.Config.Zookeeper.Username, config.Config.Zookeeper.Password, - ), zookeeper.WithRoundRobin(), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/ - if err != nil { + ), zookeeper.WithRoundRobin(), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/if err != nil { return utils.Wrap1(err) } - //defer zkClient.CloseZK() + // defer zkClient.CloseZK() zkClient.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) registerIP, err := network.GetRpcRegisterIP(config.Config.Rpc.RegisterIP) if err != nil { @@ -108,5 +101,6 @@ func Start( } } }() + return utils.Wrap1(srv.Serve(listener)) } From b1415473fffd46dcc28185be07fd2d92ef13ef17 Mon Sep 17 00:00:00 2001 From: CNCSMonster <99234657+CNCSMonster@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:24:55 +0800 Subject: [PATCH 16/21] fix: fix all lint warning in pkg (#1263) * fix:fix lint errors in internal * fix:fix lint error in interal/tools * fix: fix lint errros in pkg/statics * fix: fix lint erros in pkg/authverify,pkg/rpcclien * fix: fix lint erros in pkg/common/cmd * fix: fix lint erros in pkg/common/config,convert * fix: fix lint erros in pkg/common/db/cache * fix: fix lint errors in pkg/common/db/controller * fix:fix lint errors in pkg/common/db/relation and pkg/common/db/localcache * fix: fix rest lint errors in pkg/common/db * fix: fix rest lint errors in pkg * fix:set back go.work to use go 1.19 --- .golangci.yml | 2 +- go.work | 8 +- internal/msgtransfer/init.go | 9 +- internal/rpc/msg/as_read.go | 2 - internal/rpc/user/user.go | 9 +- internal/tools/msg.go | 8 +- pkg/authverify/token.go | 3 + pkg/callbackstruct/common.go | 1 + pkg/common/cmd/api.go | 9 +- pkg/common/cmd/cron_task.go | 2 + pkg/common/cmd/msg_gateway.go | 4 + pkg/common/cmd/msg_transfer.go | 2 + pkg/common/cmd/msg_utils.go | 27 +- pkg/common/cmd/root.go | 105 ++-- pkg/common/cmd/rpc.go | 9 +- pkg/common/config/config.go | 2 + pkg/common/config/parse.go | 11 +- pkg/common/convert/black.go | 8 +- pkg/common/convert/conversation.go | 4 + pkg/common/convert/friend.go | 22 +- pkg/common/convert/msg.go | 2 + pkg/common/convert/user.go | 2 + pkg/common/db/cache/black.go | 2 + pkg/common/db/cache/conversation.go | 32 +- pkg/common/db/cache/friend.go | 29 +- pkg/common/db/cache/group.go | 59 ++- pkg/common/db/cache/meta_cache.go | 10 + pkg/common/db/cache/msg.go | 61 ++- pkg/common/db/cache/user.go | 141 +++--- pkg/common/db/controller/auth.go | 7 +- pkg/common/db/controller/black.go | 4 + pkg/common/db/controller/conversation.go | 19 +- pkg/common/db/controller/friend.go | 51 +- pkg/common/db/controller/group.go | 26 + pkg/common/db/controller/msg.go | 448 +++++++++++------- pkg/common/db/controller/msg_test.go | 1 + pkg/common/db/controller/s3.go | 1 + pkg/common/db/controller/user.go | 20 +- pkg/common/db/localcache/conversation.go | 1 + pkg/common/db/localcache/group.go | 2 + pkg/common/db/relation/black_model.go | 5 +- pkg/common/db/relation/chat_log_model.go | 8 +- pkg/common/db/relation/conversation_model.go | 6 + pkg/common/db/relation/friend_model.go | 5 + .../db/relation/friend_request_model.go | 6 + pkg/common/db/relation/group_member_model.go | 7 + pkg/common/db/relation/group_model.go | 4 + pkg/common/db/relation/group_request_model.go | 2 + pkg/common/db/relation/log_model.go | 9 +- pkg/common/db/relation/meta_db.go | 1 + pkg/common/db/relation/mysql_init.go | 12 +- pkg/common/db/relation/object_model.go | 2 + pkg/common/db/relation/user_model.go | 10 +- pkg/common/db/s3/cont/controller.go | 84 ++-- pkg/common/db/s3/cont/id.go | 2 + pkg/common/db/s3/cos/cos.go | 124 +++-- pkg/common/db/s3/minio/image.go | 19 +- pkg/common/db/s3/minio/minio.go | 169 +++++-- pkg/common/db/s3/oss/oss.go | 119 ++--- pkg/common/db/table/relation/group.go | 2 +- pkg/common/db/table/relation/utils.go | 4 +- pkg/common/db/table/unrelation/msg.go | 3 + pkg/common/db/unrelation/mongo.go | 54 ++- pkg/common/db/unrelation/msg.go | 134 ++++-- pkg/common/db/unrelation/msg_convert.go | 68 +-- pkg/common/db/unrelation/super_group.go | 8 + pkg/common/db/unrelation/user.go | 21 +- .../k8s_discovery_register.go | 27 +- pkg/common/http/http_client.go | 20 +- pkg/common/kafka/consumer_group.go | 1 + pkg/common/kafka/producer.go | 7 +- pkg/common/locker/message_locker.go | 4 + pkg/common/prome/gather.go | 35 ++ pkg/common/prome/prometheus.go | 4 + pkg/common/tls/tls.go | 3 + pkg/msgprocessor/conversation.go | 23 +- pkg/msgprocessor/options.go | 4 + pkg/rpcclient/auth.go | 1 + pkg/rpcclient/conversation.go | 10 + pkg/rpcclient/friend.go | 5 + pkg/rpcclient/group.go | 14 + pkg/rpcclient/msg.go | 11 +- .../{conevrsation.go => conversation.go} | 3 + pkg/rpcclient/notification/friend.go | 33 +- pkg/rpcclient/notification/group.go | 118 +++-- pkg/rpcclient/notification/msg.go | 2 + pkg/rpcclient/notification/user.go | 52 +- pkg/rpcclient/push.go | 1 + pkg/rpcclient/third.go | 9 +- pkg/rpcclient/user.go | 12 + pkg/statistics/statistics.go | 6 +- 91 files changed, 1610 insertions(+), 848 deletions(-) mode change 100644 => 100755 pkg/common/db/s3/oss/oss.go mode change 100644 => 100755 pkg/common/db/unrelation/mongo.go mode change 100644 => 100755 pkg/common/db/unrelation/msg.go mode change 100644 => 100755 pkg/common/db/unrelation/user.go mode change 100644 => 100755 pkg/common/tls/tls.go rename pkg/rpcclient/notification/{conevrsation.go => conversation.go} (99%) mode change 100644 => 100755 pkg/rpcclient/notification/group.go mode change 100644 => 100755 pkg/rpcclient/third.go diff --git a/.golangci.yml b/.golangci.yml index 3dad0af30..050025b6e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -510,7 +510,7 @@ linters-settings: nestif: # minimal complexity of if statements to report, 5 by default - min-complexity: 4 + min-complexity: 6 nilnil: # By default, nilnil checks all returned types below. diff --git a/go.work b/go.work index 1c819212c..33faf5195 100644 --- a/go.work +++ b/go.work @@ -1,16 +1,18 @@ go 1.19 + use ( . ./test/typecheck ./tools/changelog + ./tools/component + ./tools/data-conversion + ./tools/imctl //./tools/imctl ./tools/infra ./tools/ncpu ./tools/openim-web + ./tools/url2im ./tools/versionchecker ./tools/yamlfmt - ./tools/component - ./tools/url2im - ./tools/data-conversion ) diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index 4487826ee..7babd9a07 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -16,10 +16,11 @@ package msgtransfer import ( "fmt" - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + + "sync" + "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "sync" "github.com/OpenIMSDK/tools/mw" @@ -29,6 +30,7 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/db/relation" relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "github.com/openimsdk/open-im-server/v3/pkg/common/prome" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient" ) @@ -63,8 +65,7 @@ func StartTransfer(prometheusPort int) error { /* client, err := openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, openkeeper.WithFreq(time.Hour), openkeeper.WithRoundRobin(), openkeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/ - if err != nil { + config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/if err != nil { return err } if err := client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil { diff --git a/internal/rpc/msg/as_read.go b/internal/rpc/msg/as_read.go index c31cd02dd..efa322774 100644 --- a/internal/rpc/msg/as_read.go +++ b/internal/rpc/msg/as_read.go @@ -165,7 +165,6 @@ func (m *msgServer) MarkConversationAsRead( m.conversationAndGetRecvID(conversation, req.UserID), seqs, hasReadSeq); err != nil { return nil, err } - } else if conversation.ConversationType == constant.SuperGroupChatType { if req.HasReadSeq > hasReadSeq { err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq) @@ -178,7 +177,6 @@ func (m *msgServer) MarkConversationAsRead( req.UserID, seqs, hasReadSeq); err != nil { return nil, err } - } return &msg.MarkConversationAsReadResp{}, nil diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index f2ceb3beb..83573eeef 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -290,7 +290,8 @@ func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbus // GetUserStatus Get the online status of the user. func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatusReq) (resp *pbuser.GetUserStatusResp, - err error) { + err error, +) { onlineStatusList, err := s.UserDatabase.GetUserStatus(ctx, req.UserIDs) if err != nil { return nil, err @@ -300,7 +301,8 @@ func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatu // SetUserStatus Synchronize user's online status. func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatusReq) (resp *pbuser.SetUserStatusResp, - err error) { + err error, +) { err = s.UserDatabase.SetUserStatus(ctx, req.UserID, req.Status, req.PlatformID) if err != nil { return nil, err @@ -324,7 +326,8 @@ func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatu // GetSubscribeUsersStatus Get the online status of subscribers. func (s *userServer) GetSubscribeUsersStatus(ctx context.Context, - req *pbuser.GetSubscribeUsersStatusReq) (*pbuser.GetSubscribeUsersStatusResp, error) { + req *pbuser.GetSubscribeUsersStatusReq, +) (*pbuser.GetSubscribeUsersStatusResp, error) { userList, err := s.UserDatabase.GetAllSubscribeList(ctx, req.UserID) if err != nil { return nil, err diff --git a/internal/tools/msg.go b/internal/tools/msg.go index 5397689b2..94ce2dec0 100644 --- a/internal/tools/msg.go +++ b/internal/tools/msg.go @@ -17,11 +17,11 @@ package tools import ( "context" "fmt" - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + "math" + "github.com/redis/go-redis/v9" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "math" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" @@ -35,6 +35,7 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" "github.com/openimsdk/open-im-server/v3/pkg/common/db/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient/notification" ) @@ -76,8 +77,7 @@ func InitMsgTool() (*MsgTool, error) { /* discov, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, zookeeper.WithFreq(time.Hour), zookeeper.WithRoundRobin(), zookeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/ - if err != nil { + config.Config.Zookeeper.Password), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/if err != nil { return nil, err } discov.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) diff --git a/pkg/authverify/token.go b/pkg/authverify/token.go index a8e577fde..724d4934f 100644 --- a/pkg/authverify/token.go +++ b/pkg/authverify/token.go @@ -41,6 +41,7 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) { if opUserID == ownerUserID { return nil } + return errs.ErrNoPermission.Wrap(utils.GetSelfFuncName()) } @@ -52,6 +53,7 @@ func CheckAdmin(ctx context.Context) error { if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.UserID) { return nil } + return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx))) } @@ -74,5 +76,6 @@ func WsVerifyToken(token, userID string, platformID int) error { if claim.PlatformID != platformID { return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID)) } + return nil } diff --git a/pkg/callbackstruct/common.go b/pkg/callbackstruct/common.go index ef84d52b9..8b320a04f 100644 --- a/pkg/callbackstruct/common.go +++ b/pkg/callbackstruct/common.go @@ -61,6 +61,7 @@ func (c CommonCallbackResp) Parse() error { if c.ActionCode != errs.NoError || c.ErrCode != errs.NoError { return errs.NewCodeError(int(c.ErrCode), c.ErrMsg).WithDetail(c.ErrDlt) } + return nil } diff --git a/pkg/common/cmd/api.go b/pkg/common/cmd/api.go index 7ce872fac..98a200f14 100644 --- a/pkg/common/cmd/api.go +++ b/pkg/common/cmd/api.go @@ -16,9 +16,11 @@ package cmd import ( "fmt" + "github.com/OpenIMSDK/protocol/constant" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/spf13/cobra" + + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) type ApiCmd struct { @@ -42,8 +44,7 @@ func (a *ApiCmd) GetPortFromConfig(portType string) int { fmt.Println("GetPortFromConfig:", portType) if portType == constant.FlagPort { return config2.Config.Api.OpenImApiPort[0] - } else { - - return 0 } + + return 0 } diff --git a/pkg/common/cmd/cron_task.go b/pkg/common/cmd/cron_task.go index 1b0e796ac..c1e18e5cf 100644 --- a/pkg/common/cmd/cron_task.go +++ b/pkg/common/cmd/cron_task.go @@ -23,6 +23,7 @@ type CronTaskCmd struct { func NewCronTaskCmd() *CronTaskCmd { ret := &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())} ret.SetRootCmdPt(ret) + return ret } @@ -34,5 +35,6 @@ func (c *CronTaskCmd) addRunE(f func() error) { func (c *CronTaskCmd) Exec(f func() error) error { c.addRunE(f) + return c.Execute() } diff --git a/pkg/common/cmd/msg_gateway.go b/pkg/common/cmd/msg_gateway.go index c96bbd7af..c9ecfa42b 100644 --- a/pkg/common/cmd/msg_gateway.go +++ b/pkg/common/cmd/msg_gateway.go @@ -31,6 +31,7 @@ type MsgGatewayCmd struct { func NewMsgGatewayCmd() *MsgGatewayCmd { ret := &MsgGatewayCmd{NewRootCmd("msgGateway")} ret.SetRootCmdPt(ret) + return ret } @@ -43,6 +44,7 @@ func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int { if port == 0 { port = m.PortFromConfig(constant.FlagWsPort) } + return port } @@ -54,8 +56,10 @@ func (m *MsgGatewayCmd) addRunE() { func (m *MsgGatewayCmd) Exec() error { m.addRunE() + return m.Execute() } + func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int { if portType == constant.FlagWsPort { return config2.Config.LongConnSvr.OpenImWsPort[0] diff --git a/pkg/common/cmd/msg_transfer.go b/pkg/common/cmd/msg_transfer.go index 20349ebbb..1e0449b4c 100644 --- a/pkg/common/cmd/msg_transfer.go +++ b/pkg/common/cmd/msg_transfer.go @@ -27,6 +27,7 @@ type MsgTransferCmd struct { func NewMsgTransferCmd() *MsgTransferCmd { ret := &MsgTransferCmd{NewRootCmd("msgTransfer")} ret.SetRootCmdPt(ret) + return ret } @@ -38,5 +39,6 @@ func (m *MsgTransferCmd) addRunE() { func (m *MsgTransferCmd) Exec() error { m.addRunE() + return m.Execute() } diff --git a/pkg/common/cmd/msg_utils.go b/pkg/common/cmd/msg_utils.go index cfaf631ec..82306da8c 100644 --- a/pkg/common/cmd/msg_utils.go +++ b/pkg/common/cmd/msg_utils.go @@ -22,7 +22,7 @@ import ( type MsgUtilsCmd struct { cobra.Command - msgTool *tools.MsgTool + // msgTool *tools.MsgTool } func (m *MsgUtilsCmd) AddUserIDFlag() { @@ -31,6 +31,7 @@ func (m *MsgUtilsCmd) AddUserIDFlag() { func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string { userID, _ := cmdLines.Flags().GetString("userID") + return userID } @@ -38,26 +39,17 @@ func (m *MsgUtilsCmd) AddFixAllFlag() { m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs") } -func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool { - fixAll, _ := cmdLines.Flags().GetBool("fixAll") - return fixAll -} - func (m *MsgUtilsCmd) AddClearAllFlag() { m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs") } -func (m *MsgUtilsCmd) getClearAllFlag(cmdLines *cobra.Command) bool { - clearAll, _ := cmdLines.Flags().GetBool("clearAll") - return clearAll -} - func (m *MsgUtilsCmd) AddSuperGroupIDFlag() { m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID") } func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string { superGroupID, _ := cmdLines.Flags().GetString("superGroupID") + return superGroupID } @@ -65,20 +57,10 @@ func (m *MsgUtilsCmd) AddBeginSeqFlag() { m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq") } -func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 { - beginSeq, _ := cmdLines.Flags().GetInt64("beginSeq") - return beginSeq -} - func (m *MsgUtilsCmd) AddLimitFlag() { m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit") } -func (m *MsgUtilsCmd) getLimitFlag(cmdLines *cobra.Command) int64 { - limit, _ := cmdLines.Flags().GetInt64("limit") - return limit -} - func (m *MsgUtilsCmd) Execute() error { return m.Command.Execute() } @@ -131,6 +113,7 @@ func NewSeqCmd() *SeqCmd { seqCmd := &SeqCmd{ NewMsgUtilsCmd("seq", "seq", nil), } + return seqCmd } @@ -158,6 +141,7 @@ func (s *SeqCmd) GetSeqCmd() *cobra.Command { // println(seq) } } + return &s.Command } @@ -173,6 +157,7 @@ func NewMsgCmd() *MsgCmd { msgCmd := &MsgCmd{ NewMsgUtilsCmd("msg", "msg", nil), } + return msgCmd } diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 4ea36cfeb..d1deb628c 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -17,8 +17,6 @@ package cmd import ( "fmt" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/spf13/cobra" "github.com/OpenIMSDK/protocol/constant" @@ -61,62 +59,81 @@ func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd { Short: fmt.Sprintf(`Start %s `, name), Long: fmt.Sprintf(`Start %s `, name), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return rootCmd.persistentPreRun(cmd, opts...) + if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil { + panic(err) + } + cmdOpts := &CmdOpts{} + for _, opt := range opts { + opt(cmdOpts) + } + if cmdOpts.loggerPrefixName == "" { + cmdOpts.loggerPrefixName = "OpenIM.log.all" + } + err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel, + config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, + config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime) + if err != nil { + panic(err) + } + + return nil }, } rootCmd.Command = cmd rootCmd.addConfFlag() + return rootCmd } -func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error { - if err := rc.initializeConfiguration(cmd); err != nil { - return fmt.Errorf("failed to get configuration from command: %w", err) - } +// func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error { +// if err := rc.initializeConfiguration(cmd); err != nil { +// return fmt.Errorf("failed to get configuration from command: %w", err) +// } - cmdOpts := rc.applyOptions(opts...) +// cmdOpts := rc.applyOptions(opts...) - if err := rc.initializeLogger(cmdOpts); err != nil { - return fmt.Errorf("failed to initialize from config: %w", err) - } +// if err := rc.initializeLogger(cmdOpts); err != nil { +// return fmt.Errorf("failed to initialize from config: %w", err) +// } - return nil -} +// return nil +// } +//nolint:unused //unused work wrongly func (rc *RootCmd) initializeConfiguration(cmd *cobra.Command) error { return rc.getConfFromCmdAndInit(cmd) } -func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts { - cmdOpts := defaultCmdOpts() - for _, opt := range opts { - opt(cmdOpts) - } +// func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts { +// cmdOpts := defaultCmdOpts() +// for _, opt := range opts { +// opt(cmdOpts) +// } - return cmdOpts -} +// return cmdOpts +// } -func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error { - logConfig := config.Config.Log - - return log.InitFromConfig( - - cmdOpts.loggerPrefixName, - rc.Name, - logConfig.RemainLogLevel, - logConfig.IsStdout, - logConfig.IsJson, - logConfig.StorageLocation, - logConfig.RemainRotationCount, - logConfig.RotationTime, - ) -} +// func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error { +// logConfig := config.Config.Log -func defaultCmdOpts() *CmdOpts { - return &CmdOpts{ - loggerPrefixName: "OpenIM.log.all", - } -} +// return log.InitFromConfig( + +// cmdOpts.loggerPrefixName, +// rc.Name, +// logConfig.RemainLogLevel, +// logConfig.IsStdout, +// logConfig.IsJson, +// logConfig.StorageLocation, +// logConfig.RemainRotationCount, +// logConfig.RotationTime, +// ) +// } + +// func defaultCmdOpts() *CmdOpts { +// return &CmdOpts{ +// loggerPrefixName: "OpenIM.log.all", +// } +// } func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) { r.cmdItf = cmdItf @@ -135,6 +152,7 @@ func (r *RootCmd) getPortFlag(cmd *cobra.Command) int { if port == 0 { port = r.PortFromConfig(constant.FlagPort) } + return port } @@ -151,6 +169,7 @@ func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int { if port == 0 { port = r.PortFromConfig(constant.FlagPrometheusPort) } + return port } @@ -161,7 +180,8 @@ func (r *RootCmd) GetPrometheusPortFlag() int { func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error { configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf) fmt.Println("configFolderPath:", configFolderPath) - return config2.InitConfig(configFolderPath) + + return config.InitConfig(configFolderPath) } func (r *RootCmd) Execute() error { @@ -174,9 +194,12 @@ func (r *RootCmd) AddCommand(cmds ...*cobra.Command) { func (r *RootCmd) GetPortFromConfig(portType string) int { fmt.Println("RootCmd.GetPortFromConfig:", portType) + return 0 } + func (r *RootCmd) PortFromConfig(portType string) int { fmt.Println("PortFromConfig:", portType) + return r.cmdItf.GetPortFromConfig(portType) } diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go index 224edc0a0..6d34c6603 100644 --- a/pkg/common/cmd/rpc.go +++ b/pkg/common/cmd/rpc.go @@ -16,11 +16,13 @@ package cmd import ( "errors" + "github.com/OpenIMSDK/protocol/constant" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/spf13/cobra" "google.golang.org/grpc" + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc" @@ -33,6 +35,7 @@ type RpcCmd struct { func NewRpcCmd(name string) *RpcCmd { ret := &RpcCmd{NewRootCmd(name)} ret.SetRootCmdPt(ret) + return ret } @@ -41,6 +44,7 @@ func (a *RpcCmd) Exec() error { a.port = a.getPortFlag(cmd) a.prometheusPort = a.getPrometheusPortFlag(cmd) } + return a.Execute() } @@ -51,8 +55,10 @@ func (a *RpcCmd) StartSvr( if a.GetPortFlag() == 0 { return errors.New("port is required") } + return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn) } + func (a *RpcCmd) GetPortFromConfig(portType string) int { switch a.Name { case RpcPushServer: @@ -88,5 +94,6 @@ func (a *RpcCmd) GetPortFromConfig(portType string) int { return config2.Config.RpcPort.OpenImUserPort[0] } } + return 0 } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 95f4a864e..d521fbd51 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -336,6 +336,7 @@ func (c *configStruct) RegisterConf2Registry(registry discoveryregistry.SvcDisco if err != nil { return err } + return registry.RegisterConf2Registry(ConfKey, data) } @@ -348,5 +349,6 @@ func (c *configStruct) EncodeConfig() []byte { if err := yaml.NewEncoder(buf).Encode(c); err != nil { panic(err) } + return buf.Bytes() } diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index e37514ecd..86292a597 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -21,8 +21,9 @@ import ( "path/filepath" "github.com/OpenIMSDK/protocol/constant" - "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" "gopkg.in/yaml.v3" + + "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" ) //go:embed version @@ -34,7 +35,7 @@ const ( DefaultFolderPath = "../config/" ) -// getProjectRoot returns the absolute path of the project root directory +// getProjectRoot returns the absolute path of the project root directory. func GetProjectRoot() string { b, _ := filepath.Abs(os.Args[0]) @@ -56,6 +57,7 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent()) } opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg)) + return opts } @@ -76,6 +78,7 @@ func initConfig(config interface{}, configName, configFolderPath string) error { return fmt.Errorf("unmarshal yaml error: %w", err) } fmt.Println("use config", configFolderPath) + return nil } @@ -92,6 +95,6 @@ func InitConfig(configFolderPath string) error { if err := initConfig(&Config, FileName, configFolderPath); err != nil { return err } - - return initConfig(&Config.Notification, NotificationFileName, configFolderPath) + + return nil } diff --git a/pkg/common/convert/black.go b/pkg/common/convert/black.go index 50c270dcb..9c862d5b7 100644 --- a/pkg/common/convert/black.go +++ b/pkg/common/convert/black.go @@ -18,7 +18,6 @@ import ( "context" "github.com/OpenIMSDK/protocol/sdkws" - sdk "github.com/OpenIMSDK/protocol/sdkws" "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" ) @@ -27,11 +26,11 @@ func BlackDB2Pb( ctx context.Context, blackDBs []*relation.BlackModel, f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), -) (blackPbs []*sdk.BlackInfo, err error) { +) (blackPbs []*sdkws.BlackInfo, err error) { if len(blackDBs) == 0 { return nil, nil } - var userIDs []string + userIDs := make([]string, 0, len(blackDBs)) for _, blackDB := range blackDBs { userIDs = append(userIDs, blackDB.BlockUserID) } @@ -40,7 +39,7 @@ func BlackDB2Pb( return nil, err } for _, blackDB := range blackDBs { - blackPb := &sdk.BlackInfo{ + blackPb := &sdkws.BlackInfo{ OwnerUserID: blackDB.OwnerUserID, CreateTime: blackDB.CreateTime.Unix(), AddSource: blackDB.AddSource, @@ -55,5 +54,6 @@ func BlackDB2Pb( } blackPbs = append(blackPbs, blackPb) } + return blackPbs, nil } diff --git a/pkg/common/convert/conversation.go b/pkg/common/convert/conversation.go index 165262b7f..295ff4c82 100644 --- a/pkg/common/convert/conversation.go +++ b/pkg/common/convert/conversation.go @@ -27,6 +27,7 @@ func ConversationDB2Pb(conversationDB *relation.ConversationModel) *conversation if err := utils.CopyStructFields(conversationPB, conversationDB); err != nil { return nil } + return conversationPB } @@ -39,6 +40,7 @@ func ConversationsDB2Pb(conversationsDB []*relation.ConversationModel) (conversa conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.Unix() conversationsPB = append(conversationsPB, conversationPB) } + return conversationsPB } @@ -47,6 +49,7 @@ func ConversationPb2DB(conversationPB *conversation.Conversation) *relation.Conv if err := utils.CopyStructFields(conversationDB, conversationPB); err != nil { return nil } + return conversationDB } @@ -58,5 +61,6 @@ func ConversationsPb2DB(conversationsPB []*conversation.Conversation) (conversat } conversationsDB = append(conversationsDB, conversationDB) } + return conversationsDB } diff --git a/pkg/common/convert/friend.go b/pkg/common/convert/friend.go index 7003c8aa6..41907d5ca 100644 --- a/pkg/common/convert/friend.go +++ b/pkg/common/convert/friend.go @@ -16,6 +16,7 @@ package convert import ( "context" + "fmt" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/tools/utils" @@ -25,9 +26,13 @@ import ( func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel { dbFriend := &relation.FriendModel{} - utils.CopyStructFields(dbFriend, friend) + err := utils.CopyStructFields(dbFriend, friend) + if err != nil { + panic(err) + } dbFriend.FriendUserID = friend.FriendUser.UserID dbFriend.CreateTime = utils.UnixSecondToTime(friend.CreateTime) + return dbFriend } @@ -37,7 +42,10 @@ func FriendDB2Pb( getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), ) (*sdkws.FriendInfo, error) { pbfriend := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}} - utils.CopyStructFields(pbfriend, friendDB) + err := utils.CopyStructFields(pbfriend, friendDB) + if err != nil { + panic(err) + } users, err := getUsers(ctx, []string{friendDB.FriendUserID}) if err != nil { return nil, err @@ -47,6 +55,7 @@ func FriendDB2Pb( pbfriend.FriendUser.FaceURL = users[friendDB.FriendUserID].FaceURL pbfriend.FriendUser.Ex = users[friendDB.FriendUserID].Ex pbfriend.CreateTime = friendDB.CreateTime.Unix() + return pbfriend, nil } @@ -58,7 +67,7 @@ func FriendsDB2Pb( if len(friendsDB) == 0 { return nil, nil } - var userID []string + userID := make([]string, 0, len(friendsDB)) for _, friendDB := range friendsDB { userID = append(userID, friendDB.FriendUserID) } @@ -68,7 +77,8 @@ func FriendsDB2Pb( } for _, friend := range friendsDB { friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}} - utils.CopyStructFields(friendPb, friend) + err2 := utils.CopyStructFields(friendPb, friend) + err = fmt.Errorf("%w, %w", err, err2) friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname friendPb.FriendUser.FaceURL = users[friend.FriendUserID].FaceURL @@ -76,7 +86,8 @@ func FriendsDB2Pb( friendPb.CreateTime = friend.CreateTime.Unix() friendsPb = append(friendsPb, friendPb) } - return friendsPb, nil + + return friendsPb, err } func FriendRequestDB2Pb( @@ -116,5 +127,6 @@ func FriendRequestDB2Pb( Ex: friendRequest.Ex, }) } + return res, nil } diff --git a/pkg/common/convert/msg.go b/pkg/common/convert/msg.go index 56f71f018..23bd5dfea 100644 --- a/pkg/common/convert/msg.go +++ b/pkg/common/convert/msg.go @@ -55,6 +55,7 @@ func MsgPb2DB(msg *sdkws.MsgData) *unrelation.MsgDataModel { msgDataModel.AtUserIDList = msg.AtUserIDList msgDataModel.AttachedInfo = msg.AttachedInfo msgDataModel.Ex = msg.Ex + return &msgDataModel } @@ -95,5 +96,6 @@ func MsgDB2Pb(msgModel *unrelation.MsgDataModel) *sdkws.MsgData { msg.AtUserIDList = msgModel.AtUserIDList msg.AttachedInfo = msgModel.AttachedInfo msg.Ex = msgModel.Ex + return &msg } diff --git a/pkg/common/convert/user.go b/pkg/common/convert/user.go index abb3a2144..38496515f 100644 --- a/pkg/common/convert/user.go +++ b/pkg/common/convert/user.go @@ -34,6 +34,7 @@ func UsersDB2Pb(users []*relationtb.UserModel) (result []*sdkws.UserInfo) { userPb.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt result = append(result, &userPb) } + return result } @@ -46,5 +47,6 @@ func UserPb2DB(user *sdkws.UserInfo) *relationtb.UserModel { userDB.CreateTime = time.UnixMilli(user.CreateTime) userDB.AppMangerLevel = user.AppMangerLevel userDB.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt + return &userDB } diff --git a/pkg/common/db/cache/black.go b/pkg/common/db/cache/black.go index 6da7d5d05..d1abe945c 100644 --- a/pkg/common/db/cache/black.go +++ b/pkg/common/db/cache/black.go @@ -52,6 +52,7 @@ func NewBlackCacheRedis( options rockscache.Options, ) BlackCache { rcClient := rockscache.NewClient(rdb, options) + return &BlackCacheRedis{ expireTime: blackExpireTime, rcClient: rcClient, @@ -88,5 +89,6 @@ func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (black func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache { cache := b.NewCache() cache.AddKeys(b.getBlackIDsKey(userID)) + return cache } diff --git a/pkg/common/db/cache/conversation.go b/pkg/common/db/cache/conversation.go index d755de645..890552160 100644 --- a/pkg/common/db/cache/conversation.go +++ b/pkg/common/db/cache/conversation.go @@ -89,6 +89,7 @@ func NewConversationRedis( db relationtb.ConversationModelInterface, ) ConversationCache { rcClient := rockscache.NewClient(rdb, opts) + return &ConversationRedisCache{ rcClient: rcClient, metaCache: NewMetaCacheRedis(rcClient), @@ -110,6 +111,7 @@ func NewNewConversationRedis( options rockscache.Options, ) ConversationCache { rcClient := rockscache.NewClient(rdb, options) + return &ConversationRedisCache{ rcClient: rcClient, metaCache: NewMetaCacheRedis(rcClient), @@ -168,12 +170,13 @@ func (c *ConversationRedisCache) GetUserConversationIDs(ctx context.Context, own } func (c *ConversationRedisCache) DelConversationIDs(userIDs ...string) ConversationCache { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, c.getConversationIDsKey(userID)) } cache := c.NewCache() cache.AddKeys(keys...) + return cache } @@ -198,18 +201,20 @@ func (c *ConversationRedisCache) GetUserConversationIDsHash( utils.Sort(conversationIDs, true) bi := big.NewInt(0) bi.SetString(utils.Md5(strings.Join(conversationIDs, ";"))[0:8], 16) + return bi.Uint64(), nil }, ) } func (c *ConversationRedisCache) DelUserConversationIDsHash(ownerUserIDs ...string) ConversationCache { - var keys []string + keys := make([]string, 0, len(ownerUserIDs)) for _, ownerUserID := range ownerUserIDs { keys = append(keys, c.getUserConversationIDsHashKey(ownerUserID)) } cache := c.NewCache() cache.AddKeys(keys...) + return cache } @@ -229,12 +234,13 @@ func (c *ConversationRedisCache) GetConversation( } func (c *ConversationRedisCache) DelConversations(ownerUserID string, conversationIDs ...string) ConversationCache { - var keys []string + keys := make([]string, 0, len(conversationIDs)) for _, conversationID := range conversationIDs { keys = append(keys, c.getConversationKey(ownerUserID, conversationID)) } cache := c.NewCache() cache.AddKeys(keys...) + return cache } @@ -248,6 +254,7 @@ func (c *ConversationRedisCache) getConversationIndex( return _i, nil } } + return 0, errors.New("not found key:" + key + " in keys") } @@ -256,10 +263,11 @@ func (c *ConversationRedisCache) GetConversations( ownerUserID string, conversationIDs []string, ) ([]*relationtb.ConversationModel, error) { - var keys []string + keys := make([]string, 0, len(conversationIDs)) for _, conversarionID := range conversationIDs { keys = append(keys, c.getConversationKey(ownerUserID, conversarionID)) } + return batchGetCache( ctx, c.rcClient, @@ -280,10 +288,11 @@ func (c *ConversationRedisCache) GetUserAllConversations( if err != nil { return nil, err } - var keys []string + keys := make([]string, 0, len(conversationIDs)) for _, conversarionID := range conversationIDs { keys = append(keys, c.getConversationKey(ownerUserID, conversarionID)) } + return batchGetCache( ctx, c.rcClient, @@ -327,24 +336,27 @@ func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDs( } func (c *ConversationRedisCache) DelUsersConversation(conversationID string, ownerUserIDs ...string) ConversationCache { - var keys []string + keys := make([]string, 0, len(ownerUserIDs)) for _, ownerUserID := range ownerUserIDs { keys = append(keys, c.getConversationKey(ownerUserID, conversationID)) } cache := c.NewCache() cache.AddKeys(keys...) + return cache } func (c *ConversationRedisCache) DelUserRecvMsgOpt(ownerUserID, conversationID string) ConversationCache { cache := c.NewCache() cache.AddKeys(c.getRecvMsgOptKey(ownerUserID, conversationID)) + return cache } func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDs(groupID string) ConversationCache { cache := c.NewCache() cache.AddKeys(c.getSuperGroupRecvNotNotifyUserIDsKey(groupID)) + return cache } @@ -365,6 +377,7 @@ func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash( utils.Sort(userIDs, true) bi := big.NewInt(0) bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16) + return bi.Uint64(), nil }, ) @@ -373,6 +386,7 @@ func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash( func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID string) ConversationCache { cache := c.NewCache() cache.AddKeys(c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID)) + return cache } @@ -385,6 +399,7 @@ func (c *ConversationRedisCache) getUserAllHasReadSeqsIndex( return _i, nil } } + return 0, errors.New("not found key:" + conversationID + " in keys") } @@ -396,10 +411,11 @@ func (c *ConversationRedisCache) GetUserAllHasReadSeqs( if err != nil { return nil, err } - var keys []string + keys := make([]string, 0, len(conversationIDs)) for _, conversarionID := range conversationIDs { keys = append(keys, c.getConversationHasReadSeqKey(ownerUserID, conversarionID)) } + return batchGetCacheMap( ctx, c.rcClient, @@ -420,6 +436,7 @@ func (c *ConversationRedisCache) DelUserAllHasReadSeqs(ownerUserID string, for _, conversationID := range conversationIDs { cache.AddKeys(c.getConversationHasReadSeqKey(ownerUserID, conversationID)) } + return cache } @@ -451,5 +468,6 @@ func (c *ConversationRedisCache) DelConversationNotReceiveMessageUserIDs(convers for _, conversationID := range conversationIDs { cache.AddKeys(c.getConversationNotReceiveMessageUserIDsKey(conversationID)) } + return cache } diff --git a/pkg/common/db/cache/friend.go b/pkg/common/db/cache/friend.go index fd8c1d3c0..37f5b0a98 100644 --- a/pkg/common/db/cache/friend.go +++ b/pkg/common/db/cache/friend.go @@ -59,6 +59,7 @@ func NewFriendCacheRedis( options rockscache.Options, ) FriendCache { rcClient := rockscache.NewClient(rdb, options) + return &FriendCacheRedis{ metaCache: NewMetaCacheRedis(rcClient), friendDB: friendDB, @@ -100,14 +101,15 @@ func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) ) } -func (f *FriendCacheRedis) DelFriendIDs(ownerUserID ...string) FriendCache { - new := f.NewCache() - var keys []string - for _, userID := range ownerUserID { +func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache { + newGroupCache := f.NewCache() + keys := make([]string, 0, len(ownerUserIDs)) + for _, userID := range ownerUserIDs { keys = append(keys, f.getFriendIDsKey(userID)) } - new.AddKeys(keys...) - return new + newGroupCache.AddKeys(keys...) + + return newGroupCache } // todo. @@ -128,13 +130,15 @@ func (f *FriendCacheRedis) GetTwoWayFriendIDs( twoWayFriendIDs = append(twoWayFriendIDs, ownerUserID) } } + return twoWayFriendIDs, nil } func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) FriendCache { - new := f.NewCache() - new.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID)) - return new + newFriendCache := f.NewCache() + newFriendCache.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID)) + + return newFriendCache } func (f *FriendCacheRedis) GetFriend( @@ -153,7 +157,8 @@ func (f *FriendCacheRedis) GetFriend( } func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) FriendCache { - new := f.NewCache() - new.AddKeys(f.getFriendKey(ownerUserID, friendUserID)) - return new + newFriendCache := f.NewCache() + newFriendCache.AddKeys(f.getFriendKey(ownerUserID, friendUserID)) + + return newFriendCache } diff --git a/pkg/common/db/cache/group.go b/pkg/common/db/cache/group.go index 7d4c2b043..0505241d0 100644 --- a/pkg/common/db/cache/group.go +++ b/pkg/common/db/cache/group.go @@ -109,6 +109,7 @@ func NewGroupCacheRedis( opts rockscache.Options, ) GroupCache { rcClient := rockscache.NewClient(rdb, opts) + return &GroupCacheRedis{ rcClient: rcClient, expireTime: groupExpireTime, groupDB: groupDB, groupMemberDB: groupMemberDB, groupRequestDB: groupRequestDB, @@ -169,6 +170,7 @@ func (g *GroupCacheRedis) GetGroupIndex(group *relationtb.GroupModel, keys []str return i, nil } } + return 0, errIndex } @@ -179,6 +181,7 @@ func (g *GroupCacheRedis) GetGroupMemberIndex(groupMember *relationtb.GroupMembe return i, nil } } + return 0, errIndex } @@ -187,10 +190,11 @@ func (g *GroupCacheRedis) GetGroupsInfo( ctx context.Context, groupIDs []string, ) (groups []*relationtb.GroupModel, err error) { - var keys []string + keys := make([]string, 0, len(groupIDs)) for _, group := range groupIDs { keys = append(keys, g.getGroupInfoKey(group)) } + return batchGetCache( ctx, g.rcClient, @@ -216,13 +220,14 @@ func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (gro } func (g *GroupCacheRedis) DelGroupsInfo(groupIDs ...string) GroupCache { - new := g.NewCache() - var keys []string + newGroupCache := g.NewCache() + keys := make([]string, 0, len(groupIDs)) for _, groupID := range groupIDs { keys = append(keys, g.getGroupInfoKey(groupID)) } - new.AddKeys(keys...) - return new + newGroupCache.AddKeys(keys...) + + return newGroupCache } func (g *GroupCacheRedis) GetJoinedSuperGroupIDs( @@ -239,6 +244,7 @@ func (g *GroupCacheRedis) GetJoinedSuperGroupIDs( if err != nil { return nil, err } + return userGroup.GroupIDs, nil }, ) @@ -248,10 +254,11 @@ func (g *GroupCacheRedis) GetSuperGroupMemberIDs( ctx context.Context, groupIDs ...string, ) (models []*unrelationtb.SuperGroupModel, err error) { - var keys []string + keys := make([]string, 0, len(groupIDs)) for _, group := range groupIDs { keys = append(keys, g.getSuperGroupMemberIDsKey(group)) } + return batchGetCache( ctx, g.rcClient, @@ -263,6 +270,7 @@ func (g *GroupCacheRedis) GetSuperGroupMemberIDs( return i, nil } } + return 0, errIndex }, func(ctx context.Context) ([]*unrelationtb.SuperGroupModel, error) { @@ -273,23 +281,25 @@ func (g *GroupCacheRedis) GetSuperGroupMemberIDs( // userJoinSuperGroup. func (g *GroupCacheRedis) DelJoinedSuperGroupIDs(userIDs ...string) GroupCache { - new := g.NewCache() - var keys []string + newGroupCache := g.NewCache() + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, g.getJoinedSuperGroupsIDKey(userID)) } - new.AddKeys(keys...) - return new + newGroupCache.AddKeys(keys...) + + return newGroupCache } func (g *GroupCacheRedis) DelSuperGroupMemberIDs(groupIDs ...string) GroupCache { - new := g.NewCache() - var keys []string + newGroupCache := g.NewCache() + keys := make([]string, 0, len(groupIDs)) for _, groupID := range groupIDs { keys = append(keys, g.getSuperGroupMemberIDsKey(groupID)) } - new.AddKeys(keys...) - return new + newGroupCache.AddKeys(keys...) + + return newGroupCache } // groupMembersHash. @@ -368,12 +378,14 @@ func (g *GroupCacheRedis) GetGroupMemberHashMap( } res[groupID] = &relationtb.GroupSimpleUserID{Hash: hash, MemberNum: uint32(num)} } + return res, nil } func (g *GroupCacheRedis) DelGroupMembersHash(groupID string) GroupCache { cache := g.NewCache() cache.AddKeys(g.getGroupMembersHashKey(groupID)) + return cache } @@ -399,12 +411,14 @@ func (g *GroupCacheRedis) GetGroupsMemberIDs(ctx context.Context, groupIDs []str } m[groupID] = userIDs } + return m, nil } func (g *GroupCacheRedis) DelGroupMemberIDs(groupID string) GroupCache { cache := g.NewCache() cache.AddKeys(g.getGroupMemberIDsKey(groupID)) + return cache } @@ -421,12 +435,13 @@ func (g *GroupCacheRedis) GetJoinedGroupIDs(ctx context.Context, userID string) } func (g *GroupCacheRedis) DelJoinedGroupID(userIDs ...string) GroupCache { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, g.getJoinedGroupsKey(userID)) } cache := g.NewCache() cache.AddKeys(keys...) + return cache } @@ -450,10 +465,11 @@ func (g *GroupCacheRedis) GetGroupMembersInfo( groupID string, userIDs []string, ) ([]*relationtb.GroupMemberModel, error) { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, g.getGroupMemberInfoKey(groupID, userID)) } + return batchGetCache( ctx, g.rcClient, @@ -482,6 +498,7 @@ func (g *GroupCacheRedis) GetGroupMembersPage( userIDs = groupMemberIDs } groupMembers, err = g.GetGroupMembersInfo(ctx, groupID, utils.Paginate(userIDs, int(showNumber), int(showNumber))) + return uint32(len(userIDs)), groupMembers, err } @@ -493,6 +510,7 @@ func (g *GroupCacheRedis) GetAllGroupMembersInfo( if err != nil { return nil, err } + return g.GetGroupMembersInfo(ctx, groupID, groupMemberIDs) } @@ -504,10 +522,11 @@ func (g *GroupCacheRedis) GetAllGroupMemberInfo( if err != nil { return nil, err } - var keys []string + keys := make([]string, 0, len(groupMemberIDs)) for _, groupMemberID := range groupMemberIDs { keys = append(keys, g.getGroupMemberInfoKey(groupID, groupMemberID)) } + return batchGetCache( ctx, g.rcClient, @@ -521,12 +540,13 @@ func (g *GroupCacheRedis) GetAllGroupMemberInfo( } func (g *GroupCacheRedis) DelGroupMembersInfo(groupID string, userIDs ...string) GroupCache { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, g.getGroupMemberInfoKey(groupID, userID)) } cache := g.NewCache() cache.AddKeys(keys...) + return cache } @@ -543,11 +563,12 @@ func (g *GroupCacheRedis) GetGroupMemberNum(ctx context.Context, groupID string) } func (g *GroupCacheRedis) DelGroupsMemberNum(groupID ...string) GroupCache { - var keys []string + keys := make([]string, 0, len(groupID)) for _, groupID := range groupID { keys = append(keys, g.getGroupMemberNumKey(groupID)) } cache := g.NewCache() cache.AddKeys(keys...) + return cache } diff --git a/pkg/common/db/cache/meta_cache.go b/pkg/common/db/cache/meta_cache.go index ca742d4a3..3d62255a7 100644 --- a/pkg/common/db/cache/meta_cache.go +++ b/pkg/common/db/cache/meta_cache.go @@ -72,6 +72,7 @@ func (m *metaCacheRedis) ExecDel(ctx context.Context) error { ), ) log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", m.keys) + return err } retryTimes++ @@ -80,6 +81,7 @@ func (m *metaCacheRedis) ExecDel(ctx context.Context) error { } } } + return nil } @@ -103,6 +105,7 @@ func GetDefaultOpt() rockscache.Options { opts := rockscache.NewDefaultOptions() opts.StrongConsistency = true opts.RandomExpireAdjustment = 0.2 + return opts } @@ -125,6 +128,7 @@ func getCache[T any]( return "", utils.Wrap(err, "") } write = true + return string(bs), nil }) if err != nil { @@ -139,8 +143,10 @@ func getCache[T any]( err = json.Unmarshal([]byte(v), &t) if err != nil { log.ZError(ctx, "cache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire) + return t, utils.Wrap(err, "") } + return t, nil } @@ -169,6 +175,7 @@ func batchGetCache[T any]( } values[index] = string(bs) } + return values, nil }) if err != nil { @@ -185,6 +192,7 @@ func batchGetCache[T any]( tArrays = append(tArrays, t) } } + return tArrays, nil } @@ -213,6 +221,7 @@ func batchGetCacheMap[T any]( } values[index] = string(bs) } + return values, nil }) if err != nil { @@ -229,5 +238,6 @@ func batchGetCacheMap[T any]( tMap[originKeys[i]] = t } } + return tMap, nil } diff --git a/pkg/common/db/cache/msg.go b/pkg/common/db/cache/msg.go index 65b8d63de..66161c424 100644 --- a/pkg/common/db/cache/msg.go +++ b/pkg/common/db/cache/msg.go @@ -16,13 +16,12 @@ package cache import ( "context" + "errors" "strconv" "time" "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" - "github.com/dtm-labs/rockscache" - "github.com/OpenIMSDK/tools/errs" "github.com/gogo/protobuf/jsonpb" @@ -33,7 +32,6 @@ import ( "github.com/OpenIMSDK/tools/utils" "github.com/openimsdk/open-im-server/v3/pkg/common/config" - unrelationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation" "github.com/redis/go-redis/v9" ) @@ -140,10 +138,10 @@ func NewMsgCacheModel(client redis.UniversalClient) MsgModel { type msgCache struct { metaCache - rdb redis.UniversalClient - expireTime time.Duration - rcClient *rockscache.Client - msgDocDatabase unrelationtb.MsgDocModelInterface + rdb redis.UniversalClient + // expireTime time.Duration + // rcClient *rockscache.Client + // msgDocDatabase unrelationtb.MsgDocModelInterface } func (c *msgCache) getMaxSeqKey(conversationID string) string { @@ -182,18 +180,20 @@ func (c *msgCache) getSeqs( ) (m map[string]int64, err error) { pipe := c.rdb.Pipeline() for _, v := range items { - if err := pipe.Get(ctx, getkey(v)).Err(); err != nil && err != redis.Nil { - return nil, errs.Wrap(err) + err2 := pipe.Get(ctx, getkey(v)).Err() + if err2 != nil && !errors.Is(err2, redis.Nil) { + return nil, errs.Wrap(err2) } } result, err := pipe.Exec(ctx) - if err != nil && err != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return nil, errs.Wrap(err) } m = make(map[string]int64, len(items)) for i, v := range result { seq := v.(*redis.StringCmd) - if seq.Err() != nil && seq.Err() != redis.Nil { + + if seq.Err() != nil && !errors.Is(seq.Err(), redis.Nil) { return nil, errs.Wrap(v.Err()) } val := utils.StringToInt64(seq.Val()) @@ -201,6 +201,7 @@ func (c *msgCache) getSeqs( m[items[i]] = val } } + return m, nil } @@ -229,6 +230,7 @@ func (c *msgCache) setSeqs(ctx context.Context, seqs map[string]int64, getkey fu } } _, err := pipe.Exec(ctx) + return err } @@ -319,6 +321,7 @@ func (c *msgCache) GetHasReadSeq(ctx context.Context, userID string, conversatio func (c *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error { key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID) + return errs.Wrap(c.rdb.HSet(ctx, key, token, flag).Err()) } @@ -332,6 +335,7 @@ func (c *msgCache) GetTokensWithoutError(ctx context.Context, userID string, pla for k, v := range m { mm[k] = utils.StringToInt(v) } + return mm, nil } @@ -341,11 +345,13 @@ func (c *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platf for k, v := range m { mm[k] = v } + return errs.Wrap(c.rdb.HSet(ctx, key, mm).Err()) } func (c *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platform int, fields []string) error { key := uidPidToken + userID + ":" + constant.PlatformIDToName(platform) + return errs.Wrap(c.rdb.HDel(ctx, key, fields...).Err()) } @@ -366,8 +372,9 @@ func (c *msgCache) GetMessagesBySeq( for _, v := range seqs { // MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1 key := c.getMessageCacheKey(conversationID, v) - if err := pipe.Get(ctx, key).Err(); err != nil && err != redis.Nil { - return nil, nil, err + err2 := pipe.Get(ctx, key).Err() + if err2 != nil && errors.Is(err2, redis.Nil) { + return nil, nil, err2 } } result, err := pipe.Exec(ctx) @@ -381,6 +388,7 @@ func (c *msgCache) GetMessagesBySeq( if err == nil { if msg.Status != constant.MsgDeleted { seqMsgs = append(seqMsgs, &msg) + continue } } else { @@ -389,6 +397,7 @@ func (c *msgCache) GetMessagesBySeq( failedSeqs = append(failedSeqs, seqs[i]) } } + return seqMsgs, failedSeqs, err } @@ -408,6 +417,7 @@ func (c *msgCache) SetMessageToCache(ctx context.Context, conversationID string, } } _, err := pipe.Exec(ctx) + return len(failedMsgs), err } @@ -440,6 +450,7 @@ func (c *msgCache) UserDeleteMsgs(ctx context.Context, conversationID string, se } } _, err := pipe.Exec(ctx) + return errs.Wrap(err) } @@ -452,6 +463,7 @@ func (c *msgCache) GetUserDelList(ctx context.Context, userID, conversationID st for i, v := range result { seqs[i] = utils.StringToInt64(v) } + return seqs, nil } @@ -460,6 +472,7 @@ func (c *msgCache) DelUserDeleteMsgsList(ctx context.Context, conversationID str delUsers, err := c.rdb.SMembers(ctx, c.getMessageDelUserListKey(conversationID, seq)).Result() if err != nil { log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq) + continue } if len(delUsers) > 0 { @@ -502,12 +515,13 @@ func (c *msgCache) DeleteMessages(ctx context.Context, conversationID string, se } } _, err := pipe.Exec(ctx) + return errs.Wrap(err) } func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversationID string) error { vals, err := c.rdb.Keys(ctx, c.allMessageCacheKey(conversationID)).Result() - if err == redis.Nil { + if errors.Is(err, redis.Nil) { return nil } if err != nil { @@ -515,11 +529,13 @@ func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversatio } pipe := c.rdb.Pipeline() for _, v := range vals { - if err := pipe.Del(ctx, v).Err(); err != nil { - return errs.Wrap(err) + err2 := pipe.Del(ctx, v).Err() + if err2 != nil { + return errs.Wrap(err2) } } _, err = pipe.Exec(ctx) + return errs.Wrap(err) } @@ -528,13 +544,15 @@ func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []in key := c.getMessageCacheKey(userID, seq) result, err := c.rdb.Get(ctx, key).Result() if err != nil { - if err == redis.Nil { + if errors.Is(err, redis.Nil) { continue } + return errs.Wrap(err) } var msg sdkws.MsgData - if err := jsonpb.UnmarshalString(result, &msg); err != nil { + err = jsonpb.UnmarshalString(result, &msg) + if err != nil { return err } msg.Status = constant.MsgDeleted @@ -546,6 +564,7 @@ func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []in return errs.Wrap(err) } } + return nil } @@ -571,6 +590,7 @@ func (c *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32 func (c *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) { result, err := c.rdb.Get(ctx, sendMsgFailedFlag+id).Int() + return int32(result), errs.Wrap(err) } @@ -597,6 +617,7 @@ func (c *msgCache) DelFcmToken(ctx context.Context, account string, platformID i func (c *msgCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) { seq, err := c.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result() + return int(seq), errs.Wrap(err) } @@ -610,11 +631,13 @@ func (c *msgCache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string func (c *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error { key := exTypeKeyLocker + clientMsgID + "_" + TypeKey + return errs.Wrap(c.rdb.SetNX(ctx, key, 1, time.Minute).Err()) } func (c *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error { key := exTypeKeyLocker + clientMsgID + "_" + TypeKey + return errs.Wrap(c.rdb.Del(ctx, key).Err()) } @@ -629,6 +652,7 @@ func (c *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType in case constant.NotificationChatType: return "EX_NOTIFICATION" + clientMsgID } + return "" } @@ -637,6 +661,7 @@ func (c *msgCache) JudgeMessageReactionExist(ctx context.Context, clientMsgID st if err != nil { return false, utils.Wrap(err, "") } + return n > 0, nil } diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index b821b4a52..0afbd595e 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -17,6 +17,7 @@ package cache import ( "context" "encoding/json" + "errors" "hash/crc32" "strconv" "time" @@ -70,6 +71,7 @@ func NewUserCacheRedis( options rockscache.Options, ) UserCache { rcClient := rockscache.NewClient(rdb, options) + return &UserCacheRedis{ rdb: rdb, metaCache: NewMetaCacheRedis(rcClient), @@ -97,10 +99,6 @@ func (u *UserCacheRedis) getUserGlobalRecvMsgOptKey(userID string) string { return userGlobalRecvMsgOptKey + userID } -func (u *UserCacheRedis) getUserStatusHashKey(userID string, Id int32) string { - return userID + "_" + string(Id) + platformID -} - func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userInfo *relationtb.UserModel, err error) { return getCache( ctx, @@ -114,10 +112,11 @@ func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userIn } func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, u.getUserInfoKey(userID)) } + return batchGetCache( ctx, u.rcClient, @@ -129,6 +128,7 @@ func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([] return i, nil } } + return 0, errIndex }, func(ctx context.Context) ([]*relationtb.UserModel, error) { @@ -138,12 +138,13 @@ func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([] } func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) UserCache { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, u.getUserInfoKey(userID)) } cache := u.NewCache() cache.AddKeys(keys...) + return cache } @@ -160,22 +161,19 @@ func (u *UserCacheRedis) GetUserGlobalRecvMsgOpt(ctx context.Context, userID str } func (u *UserCacheRedis) DelUsersGlobalRecvMsgOpt(userIDs ...string) UserCache { - var keys []string + keys := make([]string, 0, len(userIDs)) for _, userID := range userIDs { keys = append(keys, u.getUserGlobalRecvMsgOptKey(userID)) } cache := u.NewCache() cache.AddKeys(keys...) - return cache -} -func (u *UserCacheRedis) getOnlineStatusKey(userID string) string { - return olineStatusKey + userID + return cache } // GetUserStatus get user status. func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) { - var res []*user.OnlineStatus + userStatus := make([]*user.OnlineStatus, 0, len(userIDs)) for _, userID := range userIDs { UserIDNum := crc32.ChecksumIEEE([]byte(userID)) modKey := strconv.Itoa(int(UserIDNum % statusMod)) @@ -183,13 +181,14 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([ key := olineStatusKey + modKey result, err := u.rdb.HGet(ctx, key, userID).Result() if err != nil { - if err == redis.Nil { + if errors.Is(err, redis.Nil) { // key or field does not exist - res = append(res, &user.OnlineStatus{ + userStatus = append(userStatus, &user.OnlineStatus{ UserID: userID, Status: constant.Offline, PlatformIDs: nil, }) + continue } else { return nil, errs.Wrap(err) @@ -201,9 +200,10 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([ } onlineStatus.UserID = userID onlineStatus.Status = constant.Online - res = append(res, &onlineStatus) + userStatus = append(userStatus, &onlineStatus) } - return res, nil + + return userStatus, nil } // SetUserStatus Set the user status and save it in redis. @@ -224,15 +224,16 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu Status: constant.Online, PlatformIDs: []int32{platformID}, } - jsonData, err := json.Marshal(onlineStatus) - if err != nil { - return errs.Wrap(err) + jsonData, err2 := json.Marshal(&onlineStatus) + if err2 != nil { + return errs.Wrap(err2) } - _, err = u.rdb.HSet(ctx, key, userID, string(jsonData)).Result() - if err != nil { - return errs.Wrap(err) + _, err2 = u.rdb.HSet(ctx, key, userID, string(jsonData)).Result() + if err2 != nil { + return errs.Wrap(err2) } u.rdb.Expire(ctx, key, userOlineStatusExpireTime) + return nil } } @@ -240,7 +241,7 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu isNil := false result, err := u.rdb.HGet(ctx, key, userID).Result() if err != nil { - if err == redis.Nil { + if errors.Is(err, redis.Nil) { isNil = true } else { return errs.Wrap(err) @@ -248,51 +249,45 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu } if status == constant.Offline { - if isNil { - log.ZWarn(ctx, "this user not online,maybe trigger order not right", - err, "userStatus", status) - return nil + err = u.refreshStatusOffline(ctx, userID, status, platformID, isNil, err, result, key) + if err != nil { + return err } - var onlineStatus user.OnlineStatus - err = json.Unmarshal([]byte(result), &onlineStatus) + } else { + err = u.refreshStatusOnline(ctx, userID, platformID, isNil, err, result, key) if err != nil { return errs.Wrap(err) } - var newPlatformIDs []int32 - for _, val := range onlineStatus.PlatformIDs { - if val != platformID { - newPlatformIDs = append(newPlatformIDs, val) - } + } + + return nil +} + +func (u *UserCacheRedis) refreshStatusOffline(ctx context.Context, userID string, status, platformID int32, isNil bool, err error, result, key string) error { + if isNil { + log.ZWarn(ctx, "this user not online,maybe trigger order not right", + err, "userStatus", status) + + return nil + } + var onlineStatus user.OnlineStatus + err = json.Unmarshal([]byte(result), &onlineStatus) + if err != nil { + return errs.Wrap(err) + } + var newPlatformIDs []int32 + for _, val := range onlineStatus.PlatformIDs { + if val != platformID { + newPlatformIDs = append(newPlatformIDs, val) } - if newPlatformIDs == nil { - _, err = u.rdb.HDel(ctx, key, userID).Result() - if err != nil { - return errs.Wrap(err) - } - } else { - onlineStatus.PlatformIDs = newPlatformIDs - newjsonData, err := json.Marshal(&onlineStatus) - if err != nil { - return errs.Wrap(err) - } - _, err = u.rdb.HSet(ctx, key, userID, string(newjsonData)).Result() - if err != nil { - return errs.Wrap(err) - } + } + if newPlatformIDs == nil { + _, err = u.rdb.HDel(ctx, key, userID).Result() + if err != nil { + return errs.Wrap(err) } } else { - var onlineStatus user.OnlineStatus - if !isNil { - err = json.Unmarshal([]byte(result), &onlineStatus) - if err != nil { - return errs.Wrap(err) - } - onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID)) - } else { - onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID) - } - onlineStatus.Status = constant.Online - onlineStatus.UserID = userID + onlineStatus.PlatformIDs = newPlatformIDs newjsonData, err := json.Marshal(&onlineStatus) if err != nil { return errs.Wrap(err) @@ -301,7 +296,31 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu if err != nil { return errs.Wrap(err) } + } + + return nil +} +func (u *UserCacheRedis) refreshStatusOnline(ctx context.Context, userID string, platformID int32, isNil bool, err error, result, key string) error { + var onlineStatus user.OnlineStatus + if !isNil { + err2 := json.Unmarshal([]byte(result), &onlineStatus) + if err != nil { + return errs.Wrap(err2) + } + onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID)) + } else { + onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID) + } + onlineStatus.Status = constant.Online + onlineStatus.UserID = userID + newjsonData, err := json.Marshal(&onlineStatus) + if err != nil { + return errs.Wrap(err) + } + _, err = u.rdb.HSet(ctx, key, userID, string(newjsonData)).Result() + if err != nil { + return errs.Wrap(err) } return nil diff --git a/pkg/common/db/controller/auth.go b/pkg/common/db/controller/auth.go index 17b4a440d..13d06a964 100644 --- a/pkg/common/db/controller/auth.go +++ b/pkg/common/db/controller/auth.go @@ -69,9 +69,9 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI } } if len(deleteTokenKey) != 0 { - err := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey) - if err != nil { - return "", err + err2 := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey) + if err2 != nil { + return "", err2 } } claims := tokenverify.BuildClaims(userID, platformID, a.accessExpire) @@ -80,5 +80,6 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI if err != nil { return "", utils.Wrap(err, "") } + return tokenString, a.cache.AddTokenFlag(ctx, userID, platformID, tokenString, constant.NormalToken) } diff --git a/pkg/common/db/controller/black.go b/pkg/common/db/controller/black.go index 70e942a77..38147e4e9 100644 --- a/pkg/common/db/controller/black.go +++ b/pkg/common/db/controller/black.go @@ -55,6 +55,7 @@ func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackMode if err := b.black.Create(ctx, blacks); err != nil { return err } + return b.deleteBlackIDsCache(ctx, blacks) } @@ -63,6 +64,7 @@ func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackMode if err := b.black.Delete(ctx, blacks); err != nil { return err } + return b.deleteBlackIDsCache(ctx, blacks) } @@ -71,6 +73,7 @@ func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*relat for _, black := range blacks { cache = cache.DelBlackIDs(ctx, black.OwnerUserID) } + return cache.ExecDel(ctx) } @@ -97,6 +100,7 @@ func (b *blackDatabase) CheckIn( return } log.ZDebug(ctx, "blackIDs", "user1BlackIDs", userID1BlackIDs, "user2BlackIDs", userID2BlackIDs) + return utils.IsContain(userID2, userID1BlackIDs), utils.IsContain(userID1, userID2BlackIDs), nil } diff --git a/pkg/common/db/controller/conversation.go b/pkg/common/db/controller/conversation.go index c3dd6980e..e68eb25ba 100644 --- a/pkg/common/db/controller/conversation.go +++ b/pkg/common/db/controller/conversation.go @@ -99,8 +99,8 @@ func (c *conversationDatabase) SetUsersConversationFiledTx(ctx context.Context, now := time.Now() for _, v := range NotUserIDs { temp := new(relationtb.ConversationModel) - if err := utils.CopyStructFields(temp, conversation); err != nil { - return err + if err2 := utils.CopyStructFields(temp, conversation); err2 != nil { + return err2 } temp.OwnerUserID = v temp.CreateTime = now @@ -113,10 +113,12 @@ func (c *conversationDatabase) SetUsersConversationFiledTx(ctx context.Context, } cache = cache.DelConversationIDs(NotUserIDs...).DelUserConversationIDsHash(NotUserIDs...).DelConversations(conversation.ConversationID, NotUserIDs...) } + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -130,6 +132,7 @@ func (c *conversationDatabase) UpdateUsersConversationFiled(ctx context.Context, if _, ok := args["recv_msg_opt"]; ok { cache = cache.DelConversationNotReceiveMessageUserIDs(conversationID) } + return cache.ExecDel(ctx) } @@ -137,13 +140,14 @@ func (c *conversationDatabase) CreateConversation(ctx context.Context, conversat if err := c.conversationDB.Create(ctx, conversations); err != nil { return err } - var userIDs []string + userIDs := make([]string, 0, len(conversations)) cache := c.cache.NewCache() for _, conversation := range conversations { cache = cache.DelConversations(conversation.OwnerUserID, conversation.ConversationID) cache = cache.DelConversationNotReceiveMessageUserIDs(conversation.ConversationID) userIDs = append(userIDs, conversation.OwnerUserID) } + return cache.DelConversationIDs(userIDs...).DelUserConversationIDsHash(userIDs...).ExecDel(ctx) } @@ -178,10 +182,12 @@ func (c *conversationDatabase) SyncPeerUserPrivateConversationTx(ctx context.Con } } } + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -234,12 +240,15 @@ func (c *conversationDatabase) SetUserConversations(ctx context.Context, ownerUs if err != nil { return err } - cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID).DelConversationNotReceiveMessageUserIDs(utils.Slice(notExistConversations, func(e *relationtb.ConversationModel) string { return e.ConversationID })...) + cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID) + cache = cache.DelConversationNotReceiveMessageUserIDs(utils.Slice(notExistConversations, func(e *relationtb.ConversationModel) string { return e.ConversationID })...) } + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -276,10 +285,12 @@ func (c *conversationDatabase) CreateGroupChatConversation(ctx context.Context, for _, v := range existConversationUserIDs { cache = cache.DelConversations(v, conversationID) } + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index 7816ef935..f35d6728b 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -16,6 +16,7 @@ package controller import ( "context" + "errors" "time" "gorm.io/gorm" @@ -109,6 +110,7 @@ func (f *friendDatabase) CheckIn( if err != nil { return } + return utils.IsContain(userID2, userID1FriendIDs), utils.IsContain(userID1, userID2FriendIDs), nil } @@ -121,8 +123,8 @@ func (f *friendDatabase) AddFriendRequest( ) (err error) { return f.tx.Transaction(func(tx any) error { _, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID) - // 有db错误 - if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound { + // if there is a db error + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return err } // 无错误 则更新 @@ -136,12 +138,14 @@ func (f *friendDatabase) AddFriendRequest( if err := f.friendRequest.NewTx(tx).UpdateByMap(ctx, fromUserID, toUserID, m); err != nil { return err } + return nil } // gorm.ErrRecordNotFound 错误,则新增 if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil { return err } + return nil }) } @@ -154,11 +158,11 @@ func (f *friendDatabase) BecomeFriends( addSource int32, ) (err error) { cache := f.cache.NewCache() - if err := f.tx.Transaction(func(tx any) error { - // 先find 找出重复的 去掉重复的 - fs1, err := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs) - if err != nil { - return err + fn := func(tx any) error { + // first,find and drop delete ones + fs1, err2 := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs) + if err2 != nil { + return err2 } opUserID := mcontext.GetOperationID(ctx) for _, v := range friendUserIDs { @@ -168,13 +172,13 @@ func (f *friendDatabase) BecomeFriends( return e.FriendUserID }) - err = f.friend.NewTx(tx).Create(ctx, fs11) - if err != nil { - return err + err2 = f.friend.NewTx(tx).Create(ctx, fs11) + if err2 != nil { + return err2 } - fs2, err := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs) - if err != nil { - return err + fs2, err2 := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs) + if err2 != nil { + return err2 } var newFriendIDs []string for _, v := range friendUserIDs { @@ -184,16 +188,20 @@ func (f *friendDatabase) BecomeFriends( fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string { return e.OwnerUserID }) - err = f.friend.NewTx(tx).Create(ctx, fs22) - if err != nil { - return err + err2 = f.friend.NewTx(tx).Create(ctx, fs22) + if err2 != nil { + return err2 } newFriendIDs = append(newFriendIDs, ownerUserID) cache = cache.DelFriendIDs(newFriendIDs...) + return nil - }); err != nil { - return nil } + err = f.tx.Transaction(fn) + if err != nil { + return err + } + return cache.ExecDel(ctx) } @@ -216,6 +224,7 @@ func (f *friendDatabase) RefuseFriendRequest( if err != nil { return err } + return nil } @@ -251,7 +260,7 @@ func (f *friendDatabase) AgreeFriendRequest( if err != nil { return err } - } else if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound { + } else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return err } @@ -290,6 +299,7 @@ func (f *friendDatabase) AgreeFriendRequest( return err } } + return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).ExecDel(ctx) }) } @@ -299,6 +309,7 @@ func (f *friendDatabase) Delete(ctx context.Context, ownerUserID string, friendU if err := f.friend.Delete(ctx, ownerUserID, friendUserIDs); err != nil { return err } + return f.cache.DelFriendIDs(append(friendUserIDs, ownerUserID)...).ExecDel(ctx) } @@ -307,6 +318,7 @@ func (f *friendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUs if err := f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark); err != nil { return err } + return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx) } @@ -359,6 +371,7 @@ func (f *friendDatabase) FindFriendsWithError( if len(friends) != len(friendUserIDs) { err = errs.ErrRecordNotFound.Wrap() } + return } diff --git a/pkg/common/db/controller/group.go b/pkg/common/db/controller/group.go index 194f3e8b2..0788429a8 100644 --- a/pkg/common/db/controller/group.go +++ b/pkg/common/db/controller/group.go @@ -102,6 +102,7 @@ func NewGroupDatabase( cache: cache, mongoDB: superGroup, } + return database } @@ -109,6 +110,7 @@ func InitGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, database *mongo.D rcOptions := rockscache.NewDefaultOptions() rcOptions.StrongConsistency = true rcOptions.RandomExpireAdjustment = 0.2 + return NewGroupDatabase( relation.NewGroupDB(db), relation.NewGroupMemberDB(db), @@ -151,6 +153,7 @@ func (g *groupDatabase) FindGroupMemberNum(ctx context.Context, groupID string) if err != nil { return 0, err } + return uint32(num), nil } @@ -184,10 +187,12 @@ func (g *groupDatabase) CreateGroup( cache = cache.DelJoinedGroupID(groupMember.UserID).DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID) } cache = cache.DelGroupsInfo(createGroupIDs...) + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -211,6 +216,7 @@ func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data ma if err := g.groupDB.UpdateMap(ctx, groupID, data); err != nil { return err } + return g.cache.DelGroupsInfo(groupID).ExecDel(ctx) } @@ -231,10 +237,12 @@ func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string, delete cache = cache.DelJoinedGroupID(userIDs...).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelGroupMembersHash(groupID) } cache = cache.DelGroupsInfo(groupID) + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -276,6 +284,7 @@ func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string, } res = append(res, v) } + return res, nil } if len(roleLevels) == 0 { @@ -286,8 +295,10 @@ func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string, } totalGroupMembers = append(totalGroupMembers, groupMembers...) } + return totalGroupMembers, nil } + return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels) } @@ -307,6 +318,7 @@ func (g *groupDatabase) PageGetJoinGroup( } totalGroupMembers = append(totalGroupMembers, groupMembers...) } + return uint32(len(groupIDs)), totalGroupMembers, nil } @@ -327,6 +339,7 @@ func (g *groupDatabase) PageGetGroupMember( if err != nil { return 0, nil, err } + return uint32(len(groupMemberIDs)), members, nil } @@ -378,6 +391,7 @@ func (g *groupDatabase) HandlerGroupRequest( return err } } + return nil }) } @@ -386,6 +400,7 @@ func (g *groupDatabase) DeleteGroupMember(ctx context.Context, groupID string, u if err := g.groupMemberDB.Delete(ctx, groupID, userIDs); err != nil { return err } + return g.cache.DelGroupMembersHash(groupID). DelGroupMemberIDs(groupID). DelGroupsMemberNum(groupID). @@ -410,6 +425,7 @@ func (g *groupDatabase) MapGroupMemberNum(ctx context.Context, groupIDs []string } m[groupID] = uint32(num) } + return m, nil } @@ -429,6 +445,7 @@ func (g *groupDatabase) TransferGroupOwner(ctx context.Context, groupID string, if rowsAffected != 1 { return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "") } + return g.cache.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).DelGroupMembersHash(groupID).ExecDel(ctx) }) } @@ -442,6 +459,7 @@ func (g *groupDatabase) UpdateGroupMember( if err := g.groupMemberDB.Update(ctx, groupID, userID, data); err != nil { return err } + return g.cache.DelGroupMembersInfo(groupID, userID).ExecDel(ctx) } @@ -454,10 +472,12 @@ func (g *groupDatabase) UpdateGroupMembers(ctx context.Context, data []*relation } cache = cache.DelGroupMembersInfo(item.GroupID, item.UserID) } + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -469,6 +489,7 @@ func (g *groupDatabase) CreateGroupRequest(ctx context.Context, requests []*rela return err } } + return db.Create(ctx, requests) }) } @@ -504,6 +525,7 @@ func (g *groupDatabase) CreateSuperGroup(ctx context.Context, groupID string, in if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDs); err != nil { return err } + return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(initMemberIDs...).ExecDel(ctx) } @@ -521,10 +543,12 @@ func (g *groupDatabase) DeleteSuperGroup(ctx context.Context, groupID string) er if len(models) > 0 { cache = cache.DelJoinedSuperGroupIDs(models[0].MemberIDs...) } + return nil }); err != nil { return err } + return cache.ExecDel(ctx) } @@ -532,6 +556,7 @@ func (g *groupDatabase) DeleteSuperGroupMember(ctx context.Context, groupID stri if err := g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs); err != nil { return err } + return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx) } @@ -539,6 +564,7 @@ func (g *groupDatabase) CreateSuperGroupMember(ctx context.Context, groupID stri if err := g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs); err != nil { return err } + return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx) } diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index af678f92c..1bbf4cdf6 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -135,6 +135,7 @@ func InitCommonMsgDatabase(rdb redis.UniversalClient, database *mongo.Database) cacheModel := cache.NewMsgCacheModel(rdb) msgDocModel := unrelation.NewMsgMongoDriver(database) CommonMsgDatabase := NewCommonMsgDatabase(msgDocModel, cacheModel) + return CommonMsgDatabase } @@ -150,14 +151,17 @@ type commonMsgDatabase struct { func (db *commonMsgDatabase) MsgToMQ(ctx context.Context, key string, msg2mq *sdkws.MsgData) error { _, _, err := db.producer.SendMessage(ctx, key, msg2mq) + return err } func (db *commonMsgDatabase) MsgToModifyMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData) error { if len(messages) > 0 { _, _, err := db.producerToModify.SendMessage(ctx, key, &pbmsg.MsgDataToModifyByMQ{ConversationID: conversationID, Messages: messages}) + return err } + return nil } @@ -165,26 +169,26 @@ func (db *commonMsgDatabase) MsgToPushMQ(ctx context.Context, key, conversationI partition, offset, err := db.producerToPush.SendMessage(ctx, key, &pbmsg.PushMsgDataToMQ{MsgData: msg2mq, ConversationID: conversationID}) if err != nil { log.ZError(ctx, "MsgToPushMQ", err, "key", key, "msg2mq", msg2mq) + return 0, 0, err } + return partition, offset, nil } func (db *commonMsgDatabase) MsgToMongoMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData, lastSeq int64) error { if len(messages) > 0 { _, _, err := db.producerToMongo.SendMessage(ctx, key, &pbmsg.MsgDataToMongoByMQ{LastSeq: lastSeq, ConversationID: conversationID, MsgData: messages}) + return err } + return nil } -func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, firstSeq int64) error { - if len(fields) == 0 { - return nil - } - num := db.msg.GetSingleGocMsgNum() +func checkTypeForBatchInsertBlock(fields []any, key int8, firstSeq int64) error { // num = 100 - for i, field := range fields { // 检查类型 + for i, field := range fields { // check type var ok bool switch key { case updateKeyMsg: @@ -202,80 +206,106 @@ func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationI return errs.ErrInternalServer.Wrap("field type is invalid") } } - // 返回值为true表示数据库存在该文档,false表示数据库不存在该文档 - updateMsgModel := func(seq int64, i int) (bool, error) { - var ( - res *mongo.UpdateResult - err error - ) - docID := db.msg.GetDocID(conversationID, seq) - index := db.msg.GetMsgIndex(seq) - field := fields[i] + + return nil +} + +func (db *commonMsgDatabase) updateMsgModelForBatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, seq int64, i int) (bool, error) { + var ( + res *mongo.UpdateResult + err error + ) + docID := db.msg.GetDocID(conversationID, seq) + index := db.msg.GetMsgIndex(seq) + field := fields[i] + switch key { + case updateKeyMsg: + res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "msg", field) + case updateKeyRevoke: + res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "revoke", field) + } + if err != nil { + return false, err + } + + return res.MatchedCount > 0, nil +} + +func (db *commonMsgDatabase) newDocForBatchInsertBlock(conversationID string, fields []any, key int8, seq, firstSeq, num int64, i int) (unrelationtb.MsgDocModel, int) { + doc := unrelationtb.MsgDocModel{ + DocID: db.msg.GetDocID(conversationID, seq), + Msg: make([]*unrelationtb.MsgInfoModel, num), + } + var insert int // number of inserted + for j := i; j < len(fields); j++ { + seq = firstSeq + int64(j) + if db.msg.GetDocID(conversationID, seq) != doc.DocID { + break + } + insert++ switch key { case updateKeyMsg: - res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "msg", field) + doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ + Msg: fields[j].(*unrelationtb.MsgDataModel), + } case updateKeyRevoke: - res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "revoke", field) + doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ + Revoke: fields[j].(*unrelationtb.RevokeModel), + } } - if err != nil { - return false, err + } + for i, model := range doc.Msg { + if model == nil { + model = &unrelationtb.MsgInfoModel{} + doc.Msg[i] = model + } + if model.DelList == nil { + doc.Msg[i].DelList = []string{} } - return res.MatchedCount > 0, nil + } + + return doc, insert +} + +func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, firstSeq int64) error { + if len(fields) == 0 { + return nil + } + num := db.msg.GetSingleGocMsgNum() + // num = 100 + err := checkTypeForBatchInsertBlock(fields, key, firstSeq) + if err != nil { + return err } tryUpdate := true for i := 0; i < len(fields); i++ { - seq := firstSeq + int64(i) // 当前seq + seq := firstSeq + int64(i) // current seq + // try update if tryUpdate { - matched, err := updateMsgModel(seq, i) + matched, err := db.updateMsgModelForBatchInsertBlock(ctx, conversationID, fields, key, seq, i) if err != nil { return err } if matched { - continue // 匹配到了,继续下一个(不一定修改) - } - } - doc := unrelationtb.MsgDocModel{ - DocID: db.msg.GetDocID(conversationID, seq), - Msg: make([]*unrelationtb.MsgInfoModel, num), - } - var insert int // 插入的数量 - for j := i; j < len(fields); j++ { - seq = firstSeq + int64(j) - if db.msg.GetDocID(conversationID, seq) != doc.DocID { - break - } - insert++ - switch key { - case updateKeyMsg: - doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ - Msg: fields[j].(*unrelationtb.MsgDataModel), - } - case updateKeyRevoke: - doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ - Revoke: fields[j].(*unrelationtb.RevokeModel), - } - } - } - for i, model := range doc.Msg { - if model == nil { - model = &unrelationtb.MsgInfoModel{} - doc.Msg[i] = model - } - if model.DelList == nil { - doc.Msg[i].DelList = []string{} + continue // if matched,skip } } + doc, insert := db.newDocForBatchInsertBlock(conversationID, fields, key, seq, firstSeq, num, i) + // insert doc into db if err := db.msgDocDatabase.Create(ctx, &doc); err != nil { if mongo.IsDuplicateKeyError(err) { - i-- // 存在并发,重试当前数据 - tryUpdate = true // 以修改模式 + i-- // exists concurrent, + tryUpdate = true // try update + continue } + return err } - tryUpdate = false // 当前以插入成功,下一块优先插入模式 - i += insert - 1 // 跳过已插入的数据 + tryUpdate = false // if insert success,change to insert mode + i += insert - 1 // skip inserted data } + return nil } @@ -322,6 +352,7 @@ func (db *commonMsgDatabase) BatchInsertChat2DB(ctx context.Context, conversatio Ex: msg.Ex, } } + return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyMsg, msgList[0].Seq) } @@ -338,9 +369,11 @@ func (db *commonMsgDatabase) MarkSingleChatMsgsAsRead(ctx context.Context, userI log.ZDebug(ctx, "MarkSingleChatMsgsAsRead", "userID", userID, "docID", docID, "indexes", indexes) if err := db.msgDocDatabase.MarkSingleChatMsgsAsRead(ctx, userID, docID, indexes); err != nil { log.ZError(ctx, "MarkSingleChatMsgsAsRead", err, "userID", userID, "docID", docID, "indexes", indexes) + return err } } + return nil } @@ -354,8 +387,9 @@ func (db *commonMsgDatabase) DelUserDeleteMsgsList(ctx context.Context, conversa func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (seq int64, isNew bool, err error) { currentMaxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { prome.Inc(prome.SeqGetFailedCounter) + return 0, false, err } prome.Inc(prome.SeqGetSuccessCounter) @@ -366,7 +400,7 @@ func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversa if lenList < 1 { return 0, false, errors.New("too short as 0") } - if errs.Unwrap(err) == redis.Nil { + if errors.Is(err, redis.Nil) { isNew = true } lastMaxSeq := currentMaxSeq @@ -396,6 +430,7 @@ func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversa } else { prome.Inc(prome.SeqSetSuccessCounter) } + return lastMaxSeq, isNew, utils.Wrap(err, "") } @@ -410,6 +445,7 @@ func (db *commonMsgDatabase) getMsgBySeqs(ctx context.Context, userID, conversat totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg)) } } + return totalMsgs, nil } @@ -420,6 +456,7 @@ func (db *commonMsgDatabase) findMsgInfoBySeq(ctx context.Context, userID, docID msg.Msg.IsRead = true } } + return msgs, err } @@ -438,16 +475,76 @@ func (db *commonMsgDatabase) getMsgBySeqsRange(ctx context.Context, userID strin seqMsgs = append(seqMsgs, convert.MsgDB2Pb(msg.Msg)) } } + return seqMsgs, nil } +func (db *commonMsgDatabase) getCacheMsgForGetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin int64, seqs []int64) ([]*sdkws.MsgData, []int64, error) { + newBegin := seqs[0] + newEnd := seqs[len(seqs)-1] + log.ZDebug(ctx, "GetMsgBySeqsRange", "first seqs", seqs, "newBegin", newBegin, "newEnd", newEnd) + cachedMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs) + if err != nil { + if !errors.Is(err, redis.Nil) { + prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs)) + log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", seqs) + } + } + var successMsgs []*sdkws.MsgData + if len(cachedMsgs) == 0 { + return successMsgs, failedSeqs, err + } + // if len(cachedMsgs) > 0 + delSeqs, err2 := db.cache.GetUserDelList(ctx, userID, conversationID) + if err2 != nil && !errors.Is(err2, redis.Nil) { + return nil, nil, err2 + } + var cacheDelNum int + for _, msg := range cachedMsgs { + if !utils.Contain(msg.Seq, delSeqs...) { + successMsgs = append(successMsgs, msg) + } else { + cacheDelNum += 1 + } + } + log.ZDebug(ctx, "get delSeqs from redis", "delSeqs", delSeqs, "userID", userID, "conversationID", conversationID, "cacheDelNum", cacheDelNum) + var reGetSeqsCache []int64 + for i := 1; i <= cacheDelNum; { + newSeq := newBegin - int64(i) + if newSeq >= begin { + if !utils.Contain(newSeq, delSeqs...) { + log.ZDebug(ctx, "seq del in cache, a new seq in range append", "new seq", newSeq) + reGetSeqsCache = append(reGetSeqsCache, newSeq) + i++ + } + } else { + break + } + } + if len(reGetSeqsCache) > 0 { + log.ZDebug(ctx, "reGetSeqsCache", "reGetSeqsCache", reGetSeqsCache) + cachedMsgs, failedSeqs2, err2 := db.cache.GetMessagesBySeq(ctx, conversationID, reGetSeqsCache) + if err2 != nil { + if !errors.Is(err2, redis.Nil) { + prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs2)) + log.ZError(ctx, "get message from redis exception", err2, "conversationID", conversationID, "seqs", reGetSeqsCache) + } + } + failedSeqs = append(failedSeqs, failedSeqs2...) + successMsgs = append(successMsgs, cachedMsgs...) + } + + return successMsgs, failedSeqs, err +} + func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin, end, num, userMaxSeq int64) (int64, int64, []*sdkws.MsgData, error) { + // 从缓存中获取最小和最大序列号,并根据给定的范围值进行调整 userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return 0, 0, nil, err } minSeq, err := db.cache.GetMinSeq(ctx, conversationID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return 0, 0, nil, err } if userMinSeq > minSeq { @@ -455,18 +552,25 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin } if minSeq > end { log.ZInfo(ctx, "minSeq > end", "minSeq", minSeq, "end", end) + return 0, 0, nil, nil } maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return 0, 0, nil, err } + + // log out debug info log.ZDebug(ctx, "GetMsgBySeqsRange", "userMinSeq", userMinSeq, "conMinSeq", minSeq, "conMaxSeq", maxSeq, "userMaxSeq", userMaxSeq) + + // adjust maxSeq according to userMaxSeq if userMaxSeq != 0 { if userMaxSeq < maxSeq { maxSeq = userMaxSeq } } + + // adjust begin and end according to minSeq and maxSeq if begin < minSeq { begin = minSeq } @@ -476,6 +580,8 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin if end < begin { return 0, 0, nil, errs.ErrArgs.Wrap("seq end < begin") } + + // get seqs to search var seqs []int64 for i := end; i > end-num; i-- { if i >= begin { @@ -487,67 +593,24 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin if len(seqs) == 0 { return 0, 0, nil, nil } - newBegin := seqs[0] - newEnd := seqs[len(seqs)-1] - log.ZDebug(ctx, "GetMsgBySeqsRange", "first seqs", seqs, "newBegin", newBegin, "newEnd", newEnd) - cachedMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs) + + // get info from cache,and filter deleted msg + successMsgs, failedSeqs, err := db.getCacheMsgForGetMsgBySeqsRange(ctx, userID, conversationID, begin, seqs) if err != nil { - if err != redis.Nil { - prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs)) - log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", seqs) - } - } - var successMsgs []*sdkws.MsgData - if len(cachedMsgs) > 0 { - delSeqs, err := db.cache.GetUserDelList(ctx, userID, conversationID) - if err != nil && errs.Unwrap(err) != redis.Nil { - return 0, 0, nil, err - } - var cacheDelNum int - for _, msg := range cachedMsgs { - if !utils.Contain(msg.Seq, delSeqs...) { - successMsgs = append(successMsgs, msg) - } else { - cacheDelNum += 1 - } - } - log.ZDebug(ctx, "get delSeqs from redis", "delSeqs", delSeqs, "userID", userID, "conversationID", conversationID, "cacheDelNum", cacheDelNum) - var reGetSeqsCache []int64 - for i := 1; i <= cacheDelNum; { - newSeq := newBegin - int64(i) - if newSeq >= begin { - if !utils.Contain(newSeq, delSeqs...) { - log.ZDebug(ctx, "seq del in cache, a new seq in range append", "new seq", newSeq) - reGetSeqsCache = append(reGetSeqsCache, newSeq) - i++ - } - } else { - break - } - } - if len(reGetSeqsCache) > 0 { - log.ZDebug(ctx, "reGetSeqsCache", "reGetSeqsCache", reGetSeqsCache) - cachedMsgs, failedSeqs2, err := db.cache.GetMessagesBySeq(ctx, conversationID, reGetSeqsCache) - if err != nil { - if err != redis.Nil { - prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs2)) - log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", reGetSeqsCache) - } - } - failedSeqs = append(failedSeqs, failedSeqs2...) - successMsgs = append(successMsgs, cachedMsgs...) - } + return 0, 0, nil, err } + // log out debug info log.ZDebug(ctx, "get msgs from cache", "successMsgs", successMsgs) if len(failedSeqs) != 0 { log.ZDebug(ctx, "msgs not exist in redis", "seqs", failedSeqs) } - // get from cache or db + // if not found in cache,find in mongo prome.Add(prome.MsgPullFromRedisSuccessCounter, len(successMsgs)) if len(failedSeqs) > 0 { mongoMsgs, err := db.getMsgBySeqsRange(ctx, userID, conversationID, failedSeqs, begin, end) if err != nil { prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs)) + return 0, 0, nil, err } prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs)) @@ -559,15 +622,15 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) (int64, int64, []*sdkws.MsgData, error) { userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return 0, 0, nil, err } minSeq, err := db.cache.GetMinSeq(ctx, conversationID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return 0, 0, nil, err } maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && !errors.Is(err, redis.Nil) { return 0, 0, nil, err } if userMinSeq < minSeq { @@ -581,7 +644,7 @@ func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, co } successMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, newSeqs) if err != nil { - if err != redis.Nil { + if !errors.Is(err, redis.Nil) { prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs)) log.ZError(ctx, "get message from redis exception", err, "failedSeqs", failedSeqs, "conversationID", conversationID) } @@ -607,11 +670,13 @@ func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, co mongoMsgs, err := db.getMsgBySeqs(ctx, userID, conversationID, failedSeqs) if err != nil { prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs)) + return 0, 0, nil, err } prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs)) successMsgs = append(successMsgs, mongoMsgs...) } + return minSeq, maxSeq, successMsgs, nil } @@ -632,61 +697,74 @@ func (db *commonMsgDatabase) DeleteConversationMsgsAndSetMinSeq(ctx context.Cont log.ZWarn(ctx, "CleanUpOneUserAllMsg", err, "conversationID", conversationID) } } + return db.cache.SetMinSeq(ctx, conversationID, minSeq) } -func (db *commonMsgDatabase) UserMsgsDestruct(ctx context.Context, userID string, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) { +func processMsgDocModel(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, userID, conversationID string, index int64, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, over bool) { + if len(msgDocModel.Msg) > 0 { + i := 0 + for _, msg := range msgDocModel.Msg { + i++ + if msg != nil && msg.Msg != nil && msg.Msg.SendTime+destructTime*1000 <= time.Now().UnixMilli() { + if msg.Msg.SendTime+destructTime*1000 > lastMsgDestructTime.UnixMilli() && !utils.Contain(userID, msg.DelList...) { + seqs = append(seqs, msg.Msg.Seq) + } + } else { + log.ZDebug(ctx, "all msg need destruct is found", "conversationID", conversationID, "userID", userID, "index", index, "stop index", i) + over = true + + return seqs, over + } + } + } + + return seqs, over +} + +func (db *commonMsgDatabase) UserMsgsDestruct(ctx context.Context, userID, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) { var index int64 + + // refresh msg list for { - // from oldest 2 newest - msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1) - if err != nil || msgDocModel.DocID == "" { - if err != nil { - if err == unrelation.ErrMsgListNotExist { + // from oldest to newest + msgDocModel, err2 := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1) + if err2 != nil || msgDocModel.DocID == "" { + if err2 != nil { + if errors.Is(err2, unrelation.ErrMsgListNotExist) { log.ZDebug(ctx, "not doc find", "conversationID", conversationID, "userID", userID, "index", index) } else { - log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index) + log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err2, "conversationID", conversationID, "index", index) } } - // 获取报错,或者获取不到了,物理删除并且返回seq delMongoMsgsPhysical(delStruct.delDocIDList), 结束递归 + // If there is an error or no message document is found, delete the message physically and return the sequence number, then end the recursion. break } index++ //&& msgDocModel.Msg[0].Msg.SendTime > lastMsgDestructTime.UnixMilli() - if len(msgDocModel.Msg) > 0 { - i := 0 - var over bool - for _, msg := range msgDocModel.Msg { - i++ - if msg != nil && msg.Msg != nil && msg.Msg.SendTime+destructTime*1000 <= time.Now().UnixMilli() { - if msg.Msg.SendTime+destructTime*1000 > lastMsgDestructTime.UnixMilli() && !utils.Contain(userID, msg.DelList...) { - seqs = append(seqs, msg.Msg.Seq) - } - } else { - log.ZDebug(ctx, "all msg need destruct is found", "conversationID", conversationID, "userID", userID, "index", index, "stop index", i) - over = true - break - } - } - if over { - break - } + curSeqs, over := processMsgDocModel(ctx, msgDocModel, userID, conversationID, index, destructTime, lastMsgDestructTime) + seqs = append(seqs, curSeqs...) + if over { + break } } - + // Log the result of the function call. log.ZDebug(ctx, "UserMsgsDestruct", "conversationID", conversationID, "userID", userID, "seqs", seqs) - if len(seqs) > 0 { - userMinSeq := seqs[len(seqs)-1] + 1 - currentUserMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) - if err != nil && errs.Unwrap(err) != redis.Nil { + if len(seqs) == 0 { + return seqs, nil + } + // if len(seqs) > 0 + userMinSeq := seqs[len(seqs)-1] + 1 + currentUserMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) + if err != nil && !errors.Is(err, redis.Nil) { + return nil, err + } + if currentUserMinSeq < userMinSeq { + if err := db.cache.SetConversationUserMinSeq(ctx, conversationID, userID, userMinSeq); err != nil { return nil, err } - if currentUserMinSeq < userMinSeq { - if err := db.cache.SetConversationUserMinSeq(ctx, conversationID, userID, userMinSeq); err != nil { - return nil, err - } - } } + return seqs, nil } @@ -709,45 +787,58 @@ func (db *commonMsgDatabase) deleteMsgRecursion(ctx context.Context, conversatio msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1) if err != nil || msgDocModel.DocID == "" { if err != nil { - if err == unrelation.ErrMsgListNotExist { + if errors.Is(err, unrelation.ErrMsgListNotExist) { log.ZDebug(ctx, "deleteMsgRecursion ErrMsgListNotExist", "conversationID", conversationID, "index:", index) } else { log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index) } } - // 获取报错,或者获取不到了,物理删除并且返回seq delMongoMsgsPhysical(delStruct.delDocIDList), 结束递归 + // get error or miss content, delete physically and return minSeq,delMongoMsgsPhysical(delStruct.delDocIDList), end recursion err = db.msgDocDatabase.DeleteDocs(ctx, delStruct.delDocIDs) if err != nil { return 0, err } + return delStruct.getSetMinSeq() + 1, nil } + log.ZDebug(ctx, "doc info", "conversationID", conversationID, "index", index, "docID", msgDocModel.DocID, "len", len(msgDocModel.Msg)) if int64(len(msgDocModel.Msg)) > db.msg.GetSingleGocMsgNum() { log.ZWarn(ctx, "msgs too large", nil, "lenth", len(msgDocModel.Msg), "docID:", msgDocModel.DocID) } - if msgDocModel.IsFull() && msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill() { - log.ZDebug(ctx, "doc is full and all msg is expired", "docID", msgDocModel.DocID) - delStruct.delDocIDs = append(delStruct.delDocIDs, msgDocModel.DocID) - delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq + fullAndExpired := msgDocModel.IsFull() && msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill() + if fullAndExpired { + handleFullAndExpiredForDeleteMsgRecursion(ctx, msgDocModel, delStruct) } else { - var delMsgIndexs []int - for i, MsgInfoModel := range msgDocModel.Msg { - if MsgInfoModel != nil && MsgInfoModel.Msg != nil { - if utils.GetCurrentTimestampByMill() > MsgInfoModel.Msg.SendTime+(remainTime*1000) { - delMsgIndexs = append(delMsgIndexs, i) - } + handleNotFullAndExpiredForDeleteMsgRecursion(ctx, msgDocModel, remainTime, index, conversationID, delStruct, db) + } + seq, err := db.deleteMsgRecursion(ctx, conversationID, index+1, delStruct, remainTime) + + return seq, err +} + +func handleFullAndExpiredForDeleteMsgRecursion(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, delStruct *delMsgRecursionStruct) { + log.ZDebug(ctx, "doc is full and all msg is expired", "docID", msgDocModel.DocID) + delStruct.delDocIDs = append(delStruct.delDocIDs, msgDocModel.DocID) + delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq +} + +func handleNotFullAndExpiredForDeleteMsgRecursion(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, remainTime, index int64, conversationID string, delStruct *delMsgRecursionStruct, db *commonMsgDatabase) { + var delMsgIndexs []int + for i, MsgInfoModel := range msgDocModel.Msg { + if MsgInfoModel != nil && MsgInfoModel.Msg != nil { + if utils.GetCurrentTimestampByMill() > MsgInfoModel.Msg.SendTime+(remainTime*1000) { + delMsgIndexs = append(delMsgIndexs, i) } } - if len(delMsgIndexs) > 0 { - if err := db.msgDocDatabase.DeleteMsgsInOneDocByIndex(ctx, msgDocModel.DocID, delMsgIndexs); err != nil { - log.ZError(ctx, "deleteMsgRecursion DeleteMsgsInOneDocByIndex failed", err, "conversationID", conversationID, "index", index) - } - delStruct.minSeq = int64(msgDocModel.Msg[delMsgIndexs[len(delMsgIndexs)-1]].Msg.Seq) + } + if len(delMsgIndexs) > 0 { + err2 := db.msgDocDatabase.DeleteMsgsInOneDocByIndex(ctx, msgDocModel.DocID, delMsgIndexs) + if err2 != nil { + log.ZError(ctx, "deleteMsgRecursion DeleteMsgsInOneDocByIndex failed", err2, "conversationID", conversationID, "index", index) } + delStruct.minSeq = msgDocModel.Msg[delMsgIndexs[len(delMsgIndexs)-1]].Msg.Seq } - seq, err := db.deleteMsgRecursion(ctx, conversationID, index+1, delStruct, remainTime) - return seq, err } func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conversationID string, allSeqs []int64) error { @@ -763,13 +854,15 @@ func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conve return err } } + return nil } func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) error { cachedMsgs, _, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs) - if err != nil && errs.Unwrap(err) != redis.Nil { + if err != nil && errors.Is(err, redis.Nil) { log.ZWarn(ctx, "DeleteUserMsgsBySeqs", err, "conversationID", conversationID, "seqs", seqs) + return err } if len(cachedMsgs) > 0 { @@ -789,6 +882,7 @@ func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID st } } } + return nil } @@ -800,11 +894,12 @@ func (db *commonMsgDatabase) CleanUpUserConversationsMsgs(ctx context.Context, u for _, conversationID := range conversationIDs { maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) if err != nil { - if err == redis.Nil { + if errors.Is(err, redis.Nil) { log.ZInfo(ctx, "max seq is nil", "conversationID", conversationID) } else { log.ZError(ctx, "get max seq failed", err, "conversationID", conversationID) } + continue } if err := db.cache.SetMinSeq(ctx, conversationID, maxSeq+1); err != nil { @@ -898,6 +993,7 @@ func (db *commonMsgDatabase) GetConversationMinMaxSeqInMongoAndCache(ctx context if err != nil { return } + return } @@ -916,6 +1012,7 @@ func (db *commonMsgDatabase) GetMinMaxSeqMongo(ctx context.Context, conversation return } maxSeqMongo = newestMsgMongo.Msg.Seq + return } @@ -943,7 +1040,7 @@ func (db *commonMsgDatabase) RangeGroupSendCount( } func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.SearchMessageReq) (total int32, msgData []*sdkws.MsgData, err error) { - var totalMsgs []*sdkws.MsgData + totalMsgs := make([]*sdkws.MsgData, 0) total, msgs, err := db.msgDocDatabase.SearchMessage(ctx, req) if err != nil { return 0, nil, err @@ -954,6 +1051,7 @@ func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.Searc } totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg)) } + return total, totalMsgs, nil } diff --git a/pkg/common/db/controller/msg_test.go b/pkg/common/db/controller/msg_test.go index 80e2db122..15448674b 100644 --- a/pkg/common/db/controller/msg_test.go +++ b/pkg/common/db/controller/msg_test.go @@ -162,6 +162,7 @@ func GetDB() *commonMsgDatabase { if err != nil { panic(err) } + return &commonMsgDatabase{ msgDocDatabase: unrelation.NewMsgMongoDriver(mongo.GetDatabase()), } diff --git a/pkg/common/db/controller/s3.go b/pkg/common/db/controller/s3.go index 6ef3e73b3..f848f15a4 100644 --- a/pkg/common/db/controller/s3.go +++ b/pkg/common/db/controller/s3.go @@ -89,5 +89,6 @@ func (s *s3Database) AccessURL(ctx context.Context, name string, expire time.Dur if err != nil { return time.Time{}, "", err } + return expireTime, rawURL, nil } diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 9c6fdc5c4..d4a120f1c 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -90,6 +90,7 @@ func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel if len(miss) > 0 { _ = u.userDB.Create(ctx, miss) } + return nil } @@ -102,30 +103,35 @@ func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (use if len(users) != len(userIDs) { err = errs.ErrRecordNotFound.Wrap("userID not found") } + return } // Find Get the information of the specified user. If the userID is not found, no error will be returned. func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { users, err = u.cache.GetUsersInfo(ctx, userIDs) + return } // Create Insert multiple external guarantees that the userID is not repeated and does not exist in the db. func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) { - if err := u.tx.Transaction(func(tx any) error { + err = u.tx.Transaction(func(tx any) error { err = u.userDB.Create(ctx, users) if err != nil { return err } + return nil - }); err != nil { + }) + if err != nil { return err } - var userIDs []string + userIDs := make([]string, 0, len(users)) for _, user := range users { userIDs = append(userIDs, user.UserID) } + return u.cache.DelUsersInfo(userIDs...).ExecDel(ctx) } @@ -134,6 +140,7 @@ func (u *userDatabase) Update(ctx context.Context, user *relation.UserModel) (er if err := u.userDB.Update(ctx, user); err != nil { return err } + return u.cache.DelUsersInfo(user.UserID).ExecDel(ctx) } @@ -142,6 +149,7 @@ func (u *userDatabase) UpdateByMap(ctx context.Context, userID string, args map[ if err := u.userDB.UpdateByMap(ctx, userID, args); err != nil { return err } + return u.cache.DelUsersInfo(userID).ExecDel(ctx) } @@ -162,6 +170,7 @@ func (u *userDatabase) IsExist(ctx context.Context, userIDs []string) (exist boo if len(users) > 0 { return true, nil } + return false, nil } @@ -183,12 +192,14 @@ func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.T // SubscribeUsersStatus Subscribe or unsubscribe a user's presence status. func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error { err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs) + return err } // UnsubscribeUsersStatus unsubscribe a user's presence status. func (u *userDatabase) UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error { err := u.mongoDB.UnsubscriptionList(ctx, userID, userIDs) + return err } @@ -198,6 +209,7 @@ func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) ( if err != nil { return nil, err } + return list, nil } @@ -207,12 +219,14 @@ func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([] if err != nil { return nil, err } + return list, nil } // GetUserStatus get user status. func (u *userDatabase) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) { onlineStatusList, err := u.cache.GetUserStatus(ctx, userIDs) + return onlineStatusList, err } diff --git a/pkg/common/db/localcache/conversation.go b/pkg/common/db/localcache/conversation.go index c40bcdbce..b43e58257 100644 --- a/pkg/common/db/localcache/conversation.go +++ b/pkg/common/db/localcache/conversation.go @@ -50,6 +50,7 @@ func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, if err != nil { return nil, err } + return resp.UserIDs, nil } diff --git a/pkg/common/db/localcache/group.go b/pkg/common/db/localcache/group.go index 4958d91ee..140c3aeaf 100644 --- a/pkg/common/db/localcache/group.go +++ b/pkg/common/db/localcache/group.go @@ -57,6 +57,7 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) localHashInfo, ok := g.cache[groupID] if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash { g.lock.Unlock() + return localHashInfo.userIDs, nil } g.lock.Unlock() @@ -74,5 +75,6 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash, userIDs: groupMembersResp.UserIDs, } + return g.cache[groupID].userIDs, nil } diff --git a/pkg/common/db/relation/black_model.go b/pkg/common/db/relation/black_model.go index 34123c7a3..58dae3745 100644 --- a/pkg/common/db/relation/black_model.go +++ b/pkg/common/db/relation/black_model.go @@ -63,10 +63,11 @@ func (b *BlackGorm) Find( ctx context.Context, blacks []*relation.BlackModel, ) (blackList []*relation.BlackModel, err error) { - var where [][]interface{} + where := make([][]interface{}, 0, len(blacks)) for _, black := range blacks { where = append(where, []interface{}{black.OwnerUserID, black.BlockUserID}) } + return blackList, utils.Wrap( b.db(ctx).Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "", @@ -75,6 +76,7 @@ func (b *BlackGorm) Find( func (b *BlackGorm) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.BlackModel, err error) { black = &relation.BlackModel{} + return black, utils.Wrap( b.db(ctx).Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error, "", @@ -96,6 +98,7 @@ func (b *BlackGorm) FindOwnerBlacks( showNumber, ) total = int64(totalUint32) + return } diff --git a/pkg/common/db/relation/chat_log_model.go b/pkg/common/db/relation/chat_log_model.go index f183a543f..f474a2d34 100644 --- a/pkg/common/db/relation/chat_log_model.go +++ b/pkg/common/db/relation/chat_log_model.go @@ -15,6 +15,8 @@ package relation import ( + + //nolint:staticcheck //tofix: SA1019: "github.com/golang/protobuf/jsonpb" is deprecated: Use the "google.golang.org/protobuf/encoding/protojson" package instead. "github.com/golang/protobuf/jsonpb" "github.com/jinzhu/copier" "google.golang.org/protobuf/proto" @@ -38,7 +40,10 @@ func NewChatLogGorm(db *gorm.DB) relation.ChatLogModelInterface { func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error { chatLog := new(relation.ChatLogModel) - copier.Copy(chatLog, msg.MsgData) + err := copier.Copy(chatLog, msg.MsgData) + if err != nil { + return err + } switch msg.MsgData.SessionType { case constant.GroupChatType, constant.SuperGroupChatType: chatLog.RecvID = msg.MsgData.GroupID @@ -59,5 +64,6 @@ func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error { } chatLog.CreateTime = utils.UnixMillSecondToTime(msg.MsgData.CreateTime) chatLog.SendTime = utils.UnixMillSecondToTime(msg.MsgData.SendTime) + return c.DB.Create(chatLog).Error } diff --git a/pkg/common/db/relation/conversation_model.go b/pkg/common/db/relation/conversation_model.go index d5ca92ec2..37a4e02be 100644 --- a/pkg/common/db/relation/conversation_model.go +++ b/pkg/common/db/relation/conversation_model.go @@ -54,6 +54,7 @@ func (c *ConversationGorm) UpdateByMap( args map[string]interface{}, ) (rows int64, err error) { result := c.db(ctx).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args) + return result.RowsAffected, utils.Wrap(result.Error, "") } @@ -79,6 +80,7 @@ func (c *ConversationGorm) Find( Error, "", ) + return conversations, err } @@ -87,6 +89,7 @@ func (c *ConversationGorm) Take( userID, conversationID string, ) (conversation *relation.ConversationModel, err error) { cc := &relation.ConversationModel{} + return cc, utils.Wrap( c.db(ctx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error, "", @@ -169,6 +172,7 @@ func (c *ConversationGorm) GetUserRecvMsgOpt( ownerUserID, conversationID string, ) (opt int, err error) { var conversation relation.ConversationModel + return int( conversation.RecvMsgOpt, ), utils.Wrap( @@ -219,6 +223,7 @@ func (c *ConversationGorm) GetConversationIDsNeedDestruct( func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID string, conversationID string) (int32, error) { var recvMsgOpt int32 + return recvMsgOpt, errs.Wrap( c.db(ctx). Model(&relation.ConversationModel{}). @@ -230,6 +235,7 @@ func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID func (c *ConversationGorm) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) { var userIDs []string + return userIDs, errs.Wrap( c.db(ctx). Model(&relation.ConversationModel{}). diff --git a/pkg/common/db/relation/friend_model.go b/pkg/common/db/relation/friend_model.go index 869254455..103afd8aa 100644 --- a/pkg/common/db/relation/friend_model.go +++ b/pkg/common/db/relation/friend_model.go @@ -50,6 +50,7 @@ func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserI Error, "", ) + return err } @@ -84,6 +85,7 @@ func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID } m := make(map[string]interface{}, 1) m["remark"] = "" + return utils.Wrap(f.db(ctx).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "") } @@ -93,6 +95,7 @@ func (f *FriendGorm) Take( ownerUserID, friendUserID string, ) (friend *relation.FriendModel, err error) { friend = &relation.FriendModel{} + return friend, utils.Wrap( f.db(ctx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "", @@ -156,6 +159,7 @@ func (f *FriendGorm) FindOwnerFriends( Error, "", ) + return } @@ -178,6 +182,7 @@ func (f *FriendGorm) FindInWhoseFriends( Error, "", ) + return } diff --git a/pkg/common/db/relation/friend_request_model.go b/pkg/common/db/relation/friend_request_model.go index 5678f7b7b..4fd372c0c 100644 --- a/pkg/common/db/relation/friend_request_model.go +++ b/pkg/common/db/relation/friend_request_model.go @@ -74,6 +74,7 @@ func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation. fr2 := *friendRequest fr2.FromUserID = "" fr2.ToUserID = "" + return utils.Wrap( f.db(ctx). Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID). @@ -93,6 +94,7 @@ func (f *FriendRequestGorm) Find( f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error, "", ) + return friendRequest, err } @@ -105,6 +107,7 @@ func (f *FriendRequestGorm) Take( f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error, "", ) + return friendRequest, err } @@ -127,6 +130,7 @@ func (f *FriendRequestGorm) FindToUserID( Error, "", ) + return } @@ -149,6 +153,7 @@ func (f *FriendRequestGorm) FindFromUserID( Error, "", ) + return } @@ -160,5 +165,6 @@ func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUser Error, "", ) + return } diff --git a/pkg/common/db/relation/group_member_model.go b/pkg/common/db/relation/group_member_model.go index 312e32054..48baea61c 100644 --- a/pkg/common/db/relation/group_member_model.go +++ b/pkg/common/db/relation/group_member_model.go @@ -68,6 +68,7 @@ func (g *GroupMemberGorm) UpdateRoleLevel( db := g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{ "role_level": roleLevel, }) + return db.RowsAffected, utils.Wrap(db.Error, "") } @@ -87,6 +88,7 @@ func (g *GroupMemberGorm) Find( if len(roleLevels) > 0 { db = db.Where("role_level in (?)", roleLevels) } + return groupMembers, utils.Wrap(db.Find(&groupMembers).Error, "") } @@ -96,6 +98,7 @@ func (g *GroupMemberGorm) Take( userID string, ) (groupMember *relation.GroupMemberModel, err error) { groupMember = &relation.GroupMemberModel{} + return groupMember, utils.Wrap( g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "", @@ -107,6 +110,7 @@ func (g *GroupMemberGorm) TakeOwner( groupID string, ) (groupMember *relation.GroupMemberModel, err error) { groupMember = &relation.GroupMemberModel{} + return groupMember, utils.Wrap( g.db(ctx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "", @@ -125,6 +129,7 @@ func (g *GroupMemberGorm) SearchMember( ormutil.GormIn(&db, "group_id", groupIDs) ormutil.GormIn(&db, "user_id", userIDs) ormutil.GormIn(&db, "role_level", roleLevels) + return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber) } @@ -152,6 +157,7 @@ func (g *GroupMemberGorm) FindJoinUserID( groupUsers[item.GroupID] = append(v, item.UserID) } } + return groupUsers, nil } @@ -182,6 +188,7 @@ func (g *GroupMemberGorm) FindUsersJoinedGroupID(ctx context.Context, userIDs [] result[groupMember.UserID] = append(v, groupMember.GroupID) } } + return result, nil } diff --git a/pkg/common/db/relation/group_model.go b/pkg/common/db/relation/group_model.go index 7a8eee9f0..508a86f5f 100644 --- a/pkg/common/db/relation/group_model.go +++ b/pkg/common/db/relation/group_model.go @@ -61,12 +61,14 @@ func (g *GroupGorm) Find(ctx context.Context, groupIDs []string) (groups []*rela func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.GroupModel, err error) { group = &relation.GroupModel{} + return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "") } func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) { db := g.DB db = db.WithContext(ctx).Where("status!=?", constant.GroupStatusDismissed) + return ormutil.GormSearch[relation.GroupModel](db, []string{"name"}, keyword, pageNumber, showNumber) } @@ -82,6 +84,7 @@ func (g *GroupGorm) CountTotal(ctx context.Context, before *time.Time) (count in if err := db.Count(&count).Error; err != nil { return 0, err } + return count, nil } @@ -98,6 +101,7 @@ func (g *GroupGorm) CountRangeEverydayTotal(ctx context.Context, start time.Time for _, r := range res { v[r.Date.Format("2006-01-02")] = r.Count } + return v, nil } diff --git a/pkg/common/db/relation/group_request_model.go b/pkg/common/db/relation/group_request_model.go index af3f277e8..691a83bb0 100644 --- a/pkg/common/db/relation/group_request_model.go +++ b/pkg/common/db/relation/group_request_model.go @@ -80,6 +80,7 @@ func (g *GroupRequestGorm) Take( userID string, ) (groupRequest *relation.GroupRequestModel, err error) { groupRequest = &relation.GroupRequestModel{} + return groupRequest, utils.Wrap( g.DB.WithContext(ctx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error, utils.GetSelfFuncName(), @@ -114,5 +115,6 @@ func (g *GroupRequestGorm) PageGroup( func (g *GroupRequestGorm) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (total int64, groupRequests []*relation.GroupRequestModel, err error) { err = g.DB.WithContext(ctx).Where("group_id = ? and user_id in ?", groupID, userIDs).Find(&groupRequests).Error + return int64(len(groupRequests)), groupRequests, utils.Wrap(err, utils.GetSelfFuncName()) } diff --git a/pkg/common/db/relation/log_model.go b/pkg/common/db/relation/log_model.go index 53365ca5b..fc1a82cdb 100644 --- a/pkg/common/db/relation/log_model.go +++ b/pkg/common/db/relation/log_model.go @@ -25,6 +25,7 @@ func (l *LogGorm) Search(ctx context.Context, keyword string, start time.Time, e db = l.db.WithContext(ctx).Where("create_time <= ?", end) } db = db.Order("create_time desc") + return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber) } @@ -32,6 +33,7 @@ func (l *LogGorm) Delete(ctx context.Context, logIDs []string, userID string) er if userID == "" { return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Delete(&relationtb.Log{}).Error) } + return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Delete(&relationtb.Log{}).Error) } @@ -40,10 +42,15 @@ func (l *LogGorm) Get(ctx context.Context, logIDs []string, userID string) ([]*r if userID == "" { return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Find(&logs).Error) } + return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error) } func NewLogGorm(db *gorm.DB) relationtb.LogInterface { - db.AutoMigrate(&relationtb.Log{}) + err := db.AutoMigrate(&relationtb.Log{}) + if err != nil { + panic(err) + } + return &LogGorm{db: db} } diff --git a/pkg/common/db/relation/meta_db.go b/pkg/common/db/relation/meta_db.go index 6ab980120..00c1b76bc 100644 --- a/pkg/common/db/relation/meta_db.go +++ b/pkg/common/db/relation/meta_db.go @@ -34,5 +34,6 @@ func NewMetaDB(db *gorm.DB, table any) *MetaDB { func (g *MetaDB) db(ctx context.Context) *gorm.DB { db := g.DB.WithContext(ctx).Model(g.table) + return db } diff --git a/pkg/common/db/relation/mysql_init.go b/pkg/common/db/relation/mysql_init.go index 0e5ea5e43..550053ea2 100644 --- a/pkg/common/db/relation/mysql_init.go +++ b/pkg/common/db/relation/mysql_init.go @@ -15,6 +15,7 @@ package relation import ( + "errors" "fmt" "time" @@ -82,6 +83,7 @@ func newMysqlGormDB() (*gorm.DB, error) { sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.MaxLifeTime)) sqlDB.SetMaxOpenConns(config.Config.Mysql.MaxOpenConn) sqlDB.SetMaxIdleConns(config.Config.Mysql.MaxIdleConn) + return db, nil } @@ -94,11 +96,13 @@ func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) { if err == nil { return db, nil } - if mysqlErr, ok := err.(*mysqldriver.MySQLError); ok && mysqlErr.Number == 1045 { + var mysqlErr *mysqldriver.MySQLError + if errors.As(err, &mysqlErr) && mysqlErr.Number == 1045 { return nil, err } time.Sleep(time.Duration(1) * time.Second) } + return nil, err } @@ -106,6 +110,7 @@ func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) { func NewGormDB() (*gorm.DB, error) { specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound) specialerror.AddErrHandler(replaceDuplicateKey) + return newMysqlGormDB() } @@ -113,12 +118,15 @@ func replaceDuplicateKey(err error) errs.CodeError { if IsMysqlDuplicateKey(err) { return errs.ErrDuplicateKey } + return nil } func IsMysqlDuplicateKey(err error) bool { - if mysqlErr, ok := err.(*mysqldriver.MySQLError); ok { + var mysqlErr *mysqldriver.MySQLError + if errors.As(err, &mysqlErr) { return mysqlErr.Number == 1062 } + return false } diff --git a/pkg/common/db/relation/object_model.go b/pkg/common/db/relation/object_model.go index c5624a8d4..34b511c6a 100644 --- a/pkg/common/db/relation/object_model.go +++ b/pkg/common/db/relation/object_model.go @@ -44,10 +44,12 @@ func (o *ObjectInfoGorm) SetObject(ctx context.Context, obj *relation.ObjectMode if err := o.DB.WithContext(ctx).Where("name = ?", obj.Name).FirstOrCreate(obj).Error; err != nil { return errs.Wrap(err) } + return nil } func (o *ObjectInfoGorm) Take(ctx context.Context, name string) (info *relation.ObjectModel, err error) { info = &relation.ObjectModel{} + return info, errs.Wrap(o.DB.WithContext(ctx).Where("name = ?", name).Take(info).Error) } diff --git a/pkg/common/db/relation/user_model.go b/pkg/common/db/relation/user_model.go index b04c29816..ef605abd9 100644 --- a/pkg/common/db/relation/user_model.go +++ b/pkg/common/db/relation/user_model.go @@ -53,6 +53,7 @@ func (u *UserGorm) Update(ctx context.Context, user *relation.UserModel) (err er // 获取指定用户信息 不存在,也不返回错误. func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "") + return users, err } @@ -60,6 +61,7 @@ func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relatio func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) { user = &relation.UserModel{} err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "") + return user, err } @@ -81,6 +83,7 @@ func (u *UserGorm) Page( Error, "", ) + return } @@ -88,13 +91,14 @@ func (u *UserGorm) Page( func (u *UserGorm) GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) { if pageNumber == 0 || showNumber == 0 { return userIDs, errs.Wrap(u.db(ctx).Pluck("user_id", &userIDs).Error) - } else { - return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error) } + + return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error) } func (u *UserGorm) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) { err = u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Pluck("global_recv_msg_opt", &opt).Error + return opt, err } @@ -106,6 +110,7 @@ func (u *UserGorm) CountTotal(ctx context.Context, before *time.Time) (count int if err := db.Count(&count).Error; err != nil { return 0, err } + return count, nil } @@ -132,5 +137,6 @@ func (u *UserGorm) CountRangeEverydayTotal( for _, r := range res { v[r.Date.Format("2006-01-02")] = r.Count } + return v, nil } diff --git a/pkg/common/db/s3/cont/controller.go b/pkg/common/db/s3/cont/controller.go index 6faa997a9..7ff6fa755 100644 --- a/pkg/common/db/s3/cont/controller.go +++ b/pkg/common/db/s3/cont/controller.go @@ -46,6 +46,7 @@ func (c *Controller) HashPath(md5 string) string { func (c *Controller) NowPath() string { now := time.Now() + return path.Join( fmt.Sprintf("%04d", now.Year()), fmt.Sprintf("%02d", now.Month()), @@ -58,6 +59,7 @@ func (c *Controller) NowPath() string { func (c *Controller) UUID() string { id := uuid.New() + return hex.EncodeToString(id[:]) } @@ -92,20 +94,24 @@ func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64 partNumber++ } if maxParts > 0 && partNumber > 0 && partNumber < maxParts { - return nil, errors.New(fmt.Sprintf("too many parts: %d", partNumber)) + return nil, fmt.Errorf("too few parts: %d", partNumber) } - if info, err := c.impl.StatObject(ctx, c.HashPath(hash)); err == nil { + info, err := c.impl.StatObject(ctx, c.HashPath(hash)) + if err == nil { return nil, &HashAlreadyExistsError{Object: info} - } else if !c.impl.IsNotFound(err) { + } + if !c.impl.IsNotFound(err) { return nil, err } + if size <= partSize { // 预签名上传 key := path.Join(tempPath, c.NowPath(), fmt.Sprintf("%s_%d_%s.presigned", hash, size, c.UUID())) - rawURL, err := c.impl.PresignedPutObject(ctx, key, expire) - if err != nil { - return nil, err + rawURL, err2 := c.impl.PresignedPutObject(ctx, key, expire) + if err2 != nil { + return nil, err2 } + return &InitiateUploadResult{ UploadID: newMultipartUploadID(multipartUploadID{ Type: UploadTypePresigned, @@ -124,38 +130,39 @@ func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64 }, }, }, nil - } else { - // 分片上传 - upload, err := c.impl.InitiateMultipartUpload(ctx, c.HashPath(hash)) + } + + // 分片上传 + upload, err := c.impl.InitiateMultipartUpload(ctx, c.HashPath(hash)) + if err != nil { + return nil, err + } + if maxParts < 0 { + maxParts = partNumber + } + var authSign *s3.AuthSignResult + if maxParts > 0 { + partNumbers := make([]int, partNumber) + for i := 0; i < maxParts; i++ { + partNumbers[i] = i + 1 + } + authSign, err = c.impl.AuthSign(ctx, upload.UploadID, upload.Key, time.Hour*24, partNumbers) if err != nil { return nil, err } - if maxParts < 0 { - maxParts = partNumber - } - var authSign *s3.AuthSignResult - if maxParts > 0 { - partNumbers := make([]int, partNumber) - for i := 0; i < maxParts; i++ { - partNumbers[i] = i + 1 - } - authSign, err = c.impl.AuthSign(ctx, upload.UploadID, upload.Key, time.Hour*24, partNumbers) - if err != nil { - return nil, err - } - } - return &InitiateUploadResult{ - UploadID: newMultipartUploadID(multipartUploadID{ - Type: UploadTypeMultipart, - ID: upload.UploadID, - Key: upload.Key, - Size: size, - Hash: hash, - }), - PartSize: partSize, - Sign: authSign, - }, nil } + + return &InitiateUploadResult{ + UploadID: newMultipartUploadID(multipartUploadID{ + Type: UploadTypeMultipart, + ID: upload.UploadID, + Key: upload.Key, + Size: size, + Hash: hash, + }), + PartSize: partSize, + Sign: authSign, + }, nil } func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHashs []string) (*UploadResult, error) { @@ -164,8 +171,10 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa if err != nil { return nil, err } + //nolint:gosec //tofix G401: Use of weak cryptographic primitive if md5Sum := md5.Sum([]byte(strings.Join(partHashs, partSeparator))); hex.EncodeToString(md5Sum[:]) != upload.Hash { fmt.Println("CompleteUpload sum:", hex.EncodeToString(md5Sum[:]), "upload hash:", upload.Hash) + return nil, errors.New("md5 mismatching") } if info, err := c.impl.StatObject(ctx, c.HashPath(upload.Hash)); err == nil { @@ -193,7 +202,7 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa ETag: part, } } - // todo: 验证大小 + // todo: verify size result, err := c.impl.CompleteMultipartUpload(ctx, upload.ID, upload.Key, parts) if err != nil { return nil, err @@ -208,11 +217,12 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa if uploadInfo.Size != upload.Size { return nil, errors.New("upload size mismatching") } + //nolint:gosec //G401: Use of weak cryptographic primitive md5Sum := md5.Sum([]byte(strings.Join([]string{uploadInfo.ETag}, partSeparator))) if md5val := hex.EncodeToString(md5Sum[:]); md5val != upload.Hash { return nil, errs.ErrArgs.Wrap(fmt.Sprintf("md5 mismatching %s != %s", md5val, upload.Hash)) } - // 防止在这个时候,并发操作,导致文件被覆盖 + // Prevent concurrent operations at this time to avoid file overwrite copyInfo, err := c.impl.CopyObject(ctx, uploadInfo.Key, upload.Key+"."+c.UUID()) if err != nil { return nil, err @@ -230,6 +240,7 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa default: return nil, errors.New("invalid upload id type") } + return &UploadResult{ Key: targetKey, Size: upload.Size, @@ -261,5 +272,6 @@ func (c *Controller) AccessURL(ctx context.Context, name string, expire time.Dur opt.Filename = "" opt.ContentType = "" } + return c.impl.AccessURL(ctx, name, expire, opt) } diff --git a/pkg/common/db/s3/cont/id.go b/pkg/common/db/s3/cont/id.go index 47f37d4aa..a2b723b83 100644 --- a/pkg/common/db/s3/cont/id.go +++ b/pkg/common/db/s3/cont/id.go @@ -33,6 +33,7 @@ func newMultipartUploadID(id multipartUploadID) string { if err != nil { panic(err) } + return base64.StdEncoding.EncodeToString(data) } @@ -45,5 +46,6 @@ func parseMultipartUploadID(id string) (*multipartUploadID, error) { if err := json.Unmarshal(data, &upload); err != nil { return nil, fmt.Errorf("invalid multipart upload id: %w", err) } + return &upload, nil } diff --git a/pkg/common/db/s3/cos/cos.go b/pkg/common/db/s3/cos/cos.go index 7add88487..5484778a5 100644 --- a/pkg/common/db/s3/cos/cos.go +++ b/pkg/common/db/s3/cos/cos.go @@ -44,11 +44,6 @@ const ( imageWebp = "webp" ) -const ( - videoSnapshotImagePng = "png" - videoSnapshotImageJpg = "jpg" -) - func NewCos() (s3.Interface, error) { conf := config.Config.Object.Cos u, err := url.Parse(conf.BucketURL) @@ -62,6 +57,7 @@ func NewCos() (s3.Interface, error) { SessionToken: conf.SessionToken, }, }) + return &Cos{ copyURL: u.Host + "/", client: client, @@ -92,6 +88,7 @@ func (c *Cos) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini if err != nil { return nil, err } + return &s3.InitiateMultipartUploadResult{ UploadID: result.UploadID, Bucket: result.Bucket, @@ -113,6 +110,7 @@ func (c *Cos) CompleteMultipartUpload(ctx context.Context, uploadID string, name if err != nil { return nil, err } + return &s3.CompleteMultipartUploadResult{ Location: result.Location, Bucket: result.Bucket, @@ -135,6 +133,7 @@ func (c *Cos) PartSize(ctx context.Context, size int64) (int64, error) { if size%maxNumSize != 0 { partSize++ } + return partSize, nil } @@ -157,6 +156,7 @@ func (c *Cos) AuthSign(ctx context.Context, uploadID string, name string, expire Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}}, } } + return &result, nil } @@ -165,11 +165,13 @@ func (c *Cos) PresignedPutObject(ctx context.Context, name string, expire time.D if err != nil { return "", err } + return rawURL.String(), nil } func (c *Cos) DeleteObject(ctx context.Context, name string) error { _, err := c.client.Object.Delete(ctx, name) + return err } @@ -185,25 +187,26 @@ func (c *Cos) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, erro if res.ETag = strings.ToLower(strings.ReplaceAll(info.Header.Get("ETag"), `"`, "")); res.ETag == "" { return nil, errors.New("StatObject etag not found") } - if contentLengthStr := info.Header.Get("Content-Length"); contentLengthStr == "" { + contentLengthStr := info.Header.Get("Content-Length") + if contentLengthStr == "" { return nil, errors.New("StatObject content-length not found") - } else { - res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) - if err != nil { - return nil, fmt.Errorf("StatObject content-length parse error: %w", err) - } - if res.Size < 0 { - return nil, errors.New("StatObject content-length must be greater than 0") - } } - if lastModified := info.Header.Get("Last-Modified"); lastModified == "" { + res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) + if err != nil { + return nil, fmt.Errorf("StatObject content-length parse error: %w", err) + } + if res.Size < 0 { + return nil, errors.New("StatObject content-length must be greater than 0") + } + lastModified := info.Header.Get("Last-Modified") + if lastModified == "" { return nil, errors.New("StatObject last-modified not found") - } else { - res.LastModified, err = time.Parse(http.TimeFormat, lastModified) - if err != nil { - return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) - } } + res.LastModified, err = time.Parse(http.TimeFormat, lastModified) + if err != nil { + return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) + } + return res, nil } @@ -213,6 +216,7 @@ func (c *Cos) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO if err != nil { return nil, err } + return &s3.CopyObjectInfo{ Key: dst, ETag: strings.ReplaceAll(result.ETag, `"`, ``), @@ -220,16 +224,17 @@ func (c *Cos) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO } func (c *Cos) IsNotFound(err error) bool { - switch e := err.(type) { - case *cos.ErrorResponse: - return e.Response.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" - default: - return false + var cosErr *cos.ErrorResponse + if errors.As(err, &cosErr) { + return cosErr.Response.StatusCode == http.StatusNotFound || cosErr.Code == "NoSuchKey" } + + return false } func (c *Cos) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error { _, err := c.client.Object.AbortMultipartUpload(ctx, name, uploadID) + return err } @@ -257,46 +262,59 @@ func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name strin Size: part.Size, } } + return res, nil } func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) { var imageMogr string var option cos.PresignedURLOptions - if opt != nil { - query := make(url.Values) - if opt.Image != nil { - // https://cloud.tencent.com/document/product/436/44880 - style := make([]string, 0, 2) - wh := make([]string, 2) - if opt.Image.Width > 0 { - wh[0] = strconv.Itoa(opt.Image.Width) - } - if opt.Image.Height > 0 { - wh[1] = strconv.Itoa(opt.Image.Height) - } - if opt.Image.Width > 0 || opt.Image.Height > 0 { - style = append(style, strings.Join(wh, "x")) - } - switch opt.Image.Format { - case - imagePng, - imageJpg, - imageJpeg, - imageGif, - imageWebp: - style = append(style, "format/"+opt.Image.Format) - } - if len(style) > 0 { - imageMogr = "imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1" - } + getImageMogr := func(opt *s3.AccessURLOption) (imageMogr string) { + if opt.Image == nil { + return imageMogr + } + // https://cloud.tencent.com/document/product/436/44880 + style := make([]string, 0, 2) + wh := make([]string, 2) + if opt.Image.Width > 0 { + wh[0] = strconv.Itoa(opt.Image.Width) + } + if opt.Image.Height > 0 { + wh[1] = strconv.Itoa(opt.Image.Height) + } + if opt.Image.Width > 0 || opt.Image.Height > 0 { + style = append(style, strings.Join(wh, "x")) + } + switch opt.Image.Format { + case + imagePng, + imageJpg, + imageJpeg, + imageGif, + imageWebp: + style = append(style, "format/"+opt.Image.Format) } + if len(style) > 0 { + imageMogr = "imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1" + } + + return imageMogr + } + getQuery := func(opt *s3.AccessURLOption) (query url.Values) { + query = make(url.Values) if opt.ContentType != "" { query.Set("response-content-type", opt.ContentType) } if opt.Filename != "" { query.Set("response-content-disposition", `attachment; filename=`+strconv.Quote(opt.Filename)) } + + return query + } + + if opt != nil { + imageMogr = getImageMogr(opt) + query := getQuery(opt) if len(query) > 0 { option.Query = &query } @@ -317,6 +335,7 @@ func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, rawURL.RawQuery = rawURL.RawQuery + "&" + imageMogr } } + return rawURL.String(), nil } @@ -324,5 +343,6 @@ func (c *Cos) getPresignedURL(ctx context.Context, name string, expire time.Dura if !config.Config.Object.Cos.PublicRead { return c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, opt) } + return c.client.Object.GetObjectURL(name), nil } diff --git a/pkg/common/db/s3/minio/image.go b/pkg/common/db/s3/minio/image.go index 71db1ea51..4812f47ca 100644 --- a/pkg/common/db/s3/minio/image.go +++ b/pkg/common/db/s3/minio/image.go @@ -39,6 +39,7 @@ func ImageStat(reader io.Reader) (image.Image, string, error) { func ImageWidthHeight(img image.Image) (int, int) { bounds := img.Bounds().Max + return bounds.X, bounds.Y } @@ -47,27 +48,27 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { imgWidth := bounds.Max.X imgHeight := bounds.Max.Y - // 计算缩放比例 + // Calculate scaling ratio scaleWidth := float64(maxWidth) / float64(imgWidth) scaleHeight := float64(maxHeight) / float64(imgHeight) - // 如果都为0,则不缩放,返回原始图片 + // If both maxWidth and maxHeight are 0, return the original image if maxWidth == 0 && maxHeight == 0 { return img } - // 如果宽度和高度都大于0,则选择较小的缩放比例,以保持宽高比 + // If both maxWidth and maxHeight are greater than 0, choose the smaller scaling ratio to maintain aspect ratio if maxWidth > 0 && maxHeight > 0 { scale := scaleWidth if scaleHeight < scaleWidth { scale = scaleHeight } - // 计算缩略图尺寸 + // Calculate thumbnail size thumbnailWidth := int(float64(imgWidth) * scale) thumbnailHeight := int(float64(imgHeight) * scale) - // 使用"image"库的Resample方法生成缩略图 + // Generate thumbnail using the Resample method of the "image" library thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight)) for y := 0; y < thumbnailHeight; y++ { for x := 0; x < thumbnailWidth; x++ { @@ -80,12 +81,12 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { return thumbnail } - // 如果只指定了宽度或高度,则根据最大不超过的规则生成缩略图 + // If only maxWidth or maxHeight is specified, generate thumbnail according to the "max not exceed" rule if maxWidth > 0 { thumbnailWidth := maxWidth thumbnailHeight := int(float64(imgHeight) * scaleWidth) - // 使用"image"库的Resample方法生成缩略图 + // Generate thumbnail using the Resample method of the "image" library thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight)) for y := 0; y < thumbnailHeight; y++ { for x := 0; x < thumbnailWidth; x++ { @@ -102,7 +103,7 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { thumbnailWidth := int(float64(imgWidth) * scaleHeight) thumbnailHeight := maxHeight - // 使用"image"库的Resample方法生成缩略图 + // Generate thumbnail using the Resample method of the "image" library thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight)) for y := 0; y < thumbnailHeight; y++ { for x := 0; x < thumbnailWidth; x++ { @@ -115,6 +116,6 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { return thumbnail } - // 默认情况下,返回原始图片 + // By default, return the original image return img } diff --git a/pkg/common/db/s3/minio/minio.go b/pkg/common/db/s3/minio/minio.go index 7984df5a0..a84b8c3f7 100644 --- a/pkg/common/db/s3/minio/minio.go +++ b/pkg/common/db/s3/minio/minio.go @@ -111,6 +111,7 @@ func NewMinio() (s3.Interface, error) { if err := m.initMinio(ctx); err != nil { fmt.Println("init minio error:", err) } + return m, nil } @@ -141,8 +142,9 @@ func (m *Minio) initMinio(ctx context.Context) error { return fmt.Errorf("check bucket exists error: %w", err) } if !exists { - if err := m.core.Client.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{}); err != nil { - return fmt.Errorf("make bucket error: %w", err) + err2 := m.core.Client.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{}) + if err2 != nil { + return fmt.Errorf("make bucket error: %w", err2) } } if conf.PublicRead { @@ -150,8 +152,9 @@ func (m *Minio) initMinio(ctx context.Context) error { `{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject","s3:PutObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::%s/*"],"Sid": ""}]}`, conf.Bucket, ) - if err := m.core.Client.SetBucketPolicy(ctx, conf.Bucket, policy); err != nil { - return err + err2 := m.core.Client.SetBucketPolicy(ctx, conf.Bucket, policy) + if err2 != nil { + return err2 } } m.location, err = m.core.Client.GetBucketLocation(ctx, conf.Bucket) @@ -182,6 +185,7 @@ func (m *Minio) initMinio(ctx context.Context) error { vblc.Elem().Elem().Interface().(interface{ Set(string, string) }).Set(conf.Bucket, m.location) }() m.init = true + return nil } @@ -205,6 +209,7 @@ func (m *Minio) InitiateMultipartUpload(ctx context.Context, name string) (*s3.I if err != nil { return nil, err } + return &s3.InitiateMultipartUploadResult{ Bucket: m.bucket, Key: name, @@ -227,6 +232,7 @@ func (m *Minio) CompleteMultipartUpload(ctx context.Context, uploadID string, na if err != nil { return nil, err } + return &s3.CompleteMultipartUploadResult{ Location: upload.Location, Bucket: upload.Bucket, @@ -249,6 +255,7 @@ func (m *Minio) PartSize(ctx context.Context, size int64) (int64, error) { if size%maxNumSize != 0 { partSize++ } + return partSize, nil } @@ -282,6 +289,7 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi if m.prefix != "" { result.URL = m.signEndpoint + m.prefix + "/" + m.bucket + "/" + name } + return &result, nil } @@ -296,6 +304,7 @@ func (m *Minio) PresignedPutObject(ctx context.Context, name string, expire time if m.prefix != "" { rawURL.Path = path.Join(m.prefix, rawURL.Path) } + return rawURL.String(), nil } @@ -303,6 +312,7 @@ func (m *Minio) DeleteObject(ctx context.Context, name string) error { if err := m.initMinio(ctx); err != nil { return err } + return m.core.Client.RemoveObject(ctx, m.bucket, name, minio.RemoveObjectOptions{}) } @@ -314,6 +324,7 @@ func (m *Minio) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, er if err != nil { return nil, err } + return &s3.ObjectInfo{ ETag: strings.ToLower(info.ETag), Key: info.Key, @@ -336,6 +347,7 @@ func (m *Minio) CopyObject(ctx context.Context, src string, dst string) (*s3.Cop if err != nil { return nil, err } + return &s3.CopyObjectInfo{ Key: dst, ETag: strings.ToLower(result.ETag), @@ -346,20 +358,23 @@ func (m *Minio) IsNotFound(err error) bool { if err == nil { return false } - switch e := err.(type) { - case minio.ErrorResponse: - return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" - case *minio.ErrorResponse: - return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" - default: - return false + var minioErr minio.ErrorResponse + if errors.As(err, &minio.ErrorResponse{}) { + return minioErr.StatusCode == http.StatusNotFound || minioErr.Code == "NoSuchKey" + } + var minioErr2 *minio.ErrorResponse + if errors.As(err, &minioErr2) { + return minioErr2.StatusCode == http.StatusNotFound || minioErr2.Code == "NoSuchKey" } + + return false } func (m *Minio) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error { if err := m.initMinio(ctx); err != nil { return err } + return m.core.AbortMultipartUpload(ctx, m.bucket, name, uploadID) } @@ -386,6 +401,7 @@ func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name str Size: part.Size, } } + return res, nil } @@ -410,14 +426,11 @@ func (m *Minio) presignedGetObject(ctx context.Context, name string, expire time if m.prefix != "" { rawURL.Path = path.Join(m.prefix, rawURL.Path) } + return rawURL.String(), nil } -func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) { - if err := m.initMinio(ctx); err != nil { - return "", err - } - reqParams := make(url.Values) +func (m *Minio) getImageInfoForAccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption, reqParams url.Values) (fileInfo *s3.ObjectInfo, objectInfoPath, msg string, err error) { if opt != nil { if opt.ContentType != "" { reqParams.Set("response-content-type", opt.ContentType) @@ -427,35 +440,47 @@ func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration } } if opt.Image == nil || (opt.Image.Width < 0 && opt.Image.Height < 0 && opt.Image.Format == "") || (opt.Image.Width > maxImageWidth || opt.Image.Height > maxImageHeight) { - return m.presignedGetObject(ctx, name, expire, reqParams) + msg, err = m.presignedGetObject(ctx, name, expire, reqParams) + + return nil, "", msg, err } - fileInfo, err := m.StatObject(ctx, name) + fileInfo, err = m.StatObject(ctx, name) + objectInfoPath = path.Join(pathInfo, fileInfo.ETag, "image.json") if err != nil { - return "", err + return nil, "", msg, err } if fileInfo.Size > maxImageSize { - return "", errors.New("file size too large") + return nil, "", "", errors.New("file size too large") } - objectInfoPath := path.Join(pathInfo, fileInfo.ETag, "image.json") - var ( - img image.Image - info minioImageInfo - ) - data, err := m.getObjectData(ctx, objectInfoPath, 1024) + + return fileInfo, objectInfoPath, "", nil +} + +func (m *Minio) loadImgDataForAccessURL(objectInfoPath string, ctx context.Context, name string, info *minioImageInfo) (img image.Image, msg string, err error) { + var data []byte + data, err = m.getObjectData(ctx, objectInfoPath, 1024) + + //nolint:nestif //easy enough to understand if err == nil { - if err := json.Unmarshal(data, &info); err != nil { - return "", fmt.Errorf("unmarshal minio image info.json error: %w", err) + err = json.Unmarshal(data, &info) + if err != nil { + return nil, "", fmt.Errorf("unmarshal minio image info.json error: %w", err) } if info.NotImage { - return "", errors.New("not image") + return nil, "", errors.New("not image") } } else if m.IsNotFound(err) { - reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) + var reader *minio.Object + reader, err = m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) if err != nil { - return "", err + return img, msg, err } defer reader.Close() - imageInfo, format, err := ImageStat(reader) + var ( + imageInfo image.Image + format string + ) + imageInfo, format, err = ImageStat(reader) if err == nil { info.NotImage = false info.Format = format @@ -464,16 +489,22 @@ func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration } else { info.NotImage = true } - data, err := json.Marshal(&info) + + data, err = json.Marshal(&info) if err != nil { - return "", err + return img, msg, err } - if _, err := m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil { - return "", err + + _, err = m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}) + if err != nil { + return img, msg, err } - } else { - return "", err } + + return img, msg, err +} + +func (m *Minio) formatImgInfoForAccessURL(opt *s3.AccessURLOption, info *minioImageInfo, reqParams url.Values) { if opt.Image.Width > info.Width || opt.Image.Width <= 0 { opt.Image.Width = info.Width } @@ -496,24 +527,24 @@ func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration } } reqParams.Set("response-content-type", "image/"+opt.Image.Format) - if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format { - return m.presignedGetObject(ctx, name, expire, reqParams) - } - cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format)) - if _, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{}); err == nil { +} + +func (m *Minio) cacheImgInfoForAccessURL(ctx context.Context, name, cacheKey string, img image.Image, expire time.Duration, opt *s3.AccessURLOption, reqParams url.Values) (string, error) { + _, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{}) + if err == nil { return m.presignedGetObject(ctx, cacheKey, expire, reqParams) } else if !m.IsNotFound(err) { return "", err } if img == nil { - reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) - if err != nil { - return "", err + reader, err2 := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) + if err2 != nil { + return "", err2 } defer reader.Close() - img, _, err = ImageStat(reader) - if err != nil { - return "", err + img, _, err2 = ImageStat(reader) + if err2 != nil { + return "", err2 } } thumbnail := resizeImage(img, opt.Image.Width, opt.Image.Height) @@ -526,9 +557,48 @@ func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration case formatGif: err = gif.Encode(buf, thumbnail, nil) } + if err != nil { + return "", err + } if _, err := m.core.Client.PutObject(ctx, m.bucket, cacheKey, buf, int64(buf.Len()), minio.PutObjectOptions{}); err != nil { return "", err } + + return "", nil +} + +func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) { + errInit := m.initMinio(ctx) + if errInit != nil { + return "", errInit + } + reqParams := make(url.Values) + fileInfo, objectInfoPath, msg, err := m.getImageInfoForAccessURL(ctx, name, expire, opt, reqParams) + if err != nil { + return msg, err + } + // load-cache img data + var ( + img image.Image + info minioImageInfo + ) + img, msg, err = m.loadImgDataForAccessURL(objectInfoPath, ctx, name, &info) + if err != nil { + return msg, err + } + // format img info + m.formatImgInfoForAccessURL(opt, &info, reqParams) + // no need resize + if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format { + return m.presignedGetObject(ctx, name, expire, reqParams) + } + // cache img + cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format)) + msg, err = m.cacheImgInfoForAccessURL(ctx, name, cacheKey, img, expire, opt, reqParams) + if err != nil { + return msg, err + } + // return cache img return m.presignedGetObject(ctx, cacheKey, expire, reqParams) } @@ -541,5 +611,6 @@ func (m *Minio) getObjectData(ctx context.Context, name string, limit int64) ([] if limit < 0 { return io.ReadAll(object) } + return io.ReadAll(io.LimitReader(object, 1024)) } diff --git a/pkg/common/db/s3/oss/oss.go b/pkg/common/db/s3/oss/oss.go old mode 100644 new mode 100755 index 6a728127b..4f7f37497 --- a/pkg/common/db/s3/oss/oss.go +++ b/pkg/common/db/s3/oss/oss.go @@ -45,11 +45,6 @@ const ( imageWebp = "webp" ) -const ( - videoSnapshotImagePng = "png" - videoSnapshotImageJpg = "jpg" -) - func NewOSS() (s3.Interface, error) { conf := config.Config.Object.Oss if conf.BucketURL == "" { @@ -66,6 +61,7 @@ func NewOSS() (s3.Interface, error) { if conf.BucketURL[len(conf.BucketURL)-1] != '/' { conf.BucketURL += "/" } + return &OSS{ bucketURL: conf.BucketURL, bucket: bucket, @@ -98,6 +94,7 @@ func (o *OSS) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini if err != nil { return nil, err } + return &s3.InitiateMultipartUploadResult{ UploadID: result.UploadID, Bucket: result.Bucket, @@ -121,6 +118,7 @@ func (o *OSS) CompleteMultipartUpload(ctx context.Context, uploadID string, name if err != nil { return nil, err } + return &s3.CompleteMultipartUploadResult{ Location: result.Location, Bucket: result.Bucket, @@ -143,6 +141,7 @@ func (o *OSS) PartSize(ctx context.Context, size int64) (int64, error) { if size%maxNumSize != 0 { partSize++ } + return partSize, nil } @@ -155,7 +154,7 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire } for i, partNumber := range partNumbers { rawURL := fmt.Sprintf(`%s%s?partNumber=%d&uploadId=%s`, o.bucketURL, name, partNumber, uploadID) - request, err := http.NewRequest(http.MethodPut, rawURL, nil) + request, err := http.NewRequestWithContext(context.Background(), http.MethodPut, rawURL, nil) if err != nil { return nil, err } @@ -175,6 +174,7 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire Header: request.Header, } } + return &result, nil } @@ -191,25 +191,26 @@ func (o *OSS) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, erro if res.ETag = strings.ToLower(strings.ReplaceAll(header.Get("ETag"), `"`, ``)); res.ETag == "" { return nil, errors.New("StatObject etag not found") } - if contentLengthStr := header.Get("Content-Length"); contentLengthStr == "" { + contentLengthStr := header.Get("Content-Length") + if contentLengthStr == "" { return nil, errors.New("StatObject content-length not found") - } else { - res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) - if err != nil { - return nil, fmt.Errorf("StatObject content-length parse error: %w", err) - } - if res.Size < 0 { - return nil, errors.New("StatObject content-length must be greater than 0") - } } - if lastModified := header.Get("Last-Modified"); lastModified == "" { + res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) + if err != nil { + return nil, fmt.Errorf("StatObject content-length parse error: %w", err) + } + if res.Size < 0 { + return nil, errors.New("StatObject content-length must be greater than 0") + } + lastModified := header.Get("Last-Modified") + if lastModified == "" { return nil, errors.New("StatObject last-modified not found") - } else { - res.LastModified, err = time.Parse(http.TimeFormat, lastModified) - if err != nil { - return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) - } } + res.LastModified, err = time.Parse(http.TimeFormat, lastModified) + if err != nil { + return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) + } + return res, nil } @@ -222,6 +223,7 @@ func (o *OSS) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO if err != nil { return nil, err } + return &s3.CopyObjectInfo{ Key: dst, ETag: strings.ToLower(strings.ReplaceAll(result.ETag, `"`, ``)), @@ -229,6 +231,7 @@ func (o *OSS) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO } func (o *OSS) IsNotFound(err error) bool { + //nolint:errorlint //this is exactly what we want,there is no risk for no wrapped errors switch e := err.(type) { case oss.ServiceError: return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" @@ -271,6 +274,7 @@ func (o *OSS) ListUploadedParts(ctx context.Context, uploadID string, name strin Size: int64(part.Size), } } + return res, nil } @@ -278,39 +282,7 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration, publicRead := config.Config.Object.Oss.PublicRead var opts []oss.Option if opt != nil { - if opt.Image != nil { - // 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji - var format string - switch opt.Image.Format { - case - imagePng, - imageJpg, - imageJpeg, - imageGif, - imageWebp: - format = opt.Image.Format - default: - opt.Image.Format = imageJpg - } - // https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit - process := "image/resize,m_lfit" - if opt.Image.Width > 0 { - process += ",w_" + strconv.Itoa(opt.Image.Width) - } - if opt.Image.Height > 0 { - process += ",h_" + strconv.Itoa(opt.Image.Height) - } - process += ",format," + format - opts = append(opts, oss.Process(process)) - } - if !publicRead { - if opt.ContentType != "" { - opts = append(opts, oss.ResponseContentType(opt.ContentType)) - } - if opt.Filename != "" { - opts = append(opts, oss.ResponseContentDisposition(`attachment; filename=`+strconv.Quote(opt.Filename))) - } - } + opts = optsForAccessURL(opt, opts, publicRead) } if expire <= 0 { expire = time.Hour * 24 * 365 * 99 // 99 years @@ -325,5 +297,44 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration, return "", err } params := getURLParams(*o.bucket.Client.Conn, rawParams) + return getURL(o.um, o.bucket.BucketName, name, params).String(), nil } + +func optsForAccessURL(opt *s3.AccessURLOption, opts []oss.Option, publicRead bool) []oss.Option { + if opt.Image != nil { + // 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji + var format string + switch opt.Image.Format { + case + imagePng, + imageJpg, + imageJpeg, + imageGif, + imageWebp: + format = opt.Image.Format + default: + opt.Image.Format = imageJpg + } + // https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit + process := "image/resize,m_lfit" + if opt.Image.Width > 0 { + process += ",w_" + strconv.Itoa(opt.Image.Width) + } + if opt.Image.Height > 0 { + process += ",h_" + strconv.Itoa(opt.Image.Height) + } + process += ",format," + format + opts = append(opts, oss.Process(process)) + } + if !publicRead { + if opt.ContentType != "" { + opts = append(opts, oss.ResponseContentType(opt.ContentType)) + } + if opt.Filename != "" { + opts = append(opts, oss.ResponseContentDisposition(`attachment; filename=`+strconv.Quote(opt.Filename))) + } + } + + return opts +} diff --git a/pkg/common/db/table/relation/group.go b/pkg/common/db/table/relation/group.go index 6759e0d35..24a75173d 100644 --- a/pkg/common/db/table/relation/group.go +++ b/pkg/common/db/table/relation/group.go @@ -30,7 +30,7 @@ type GroupModel struct { Introduction string `gorm:"column:introduction;size:255" json:"introduction"` FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"` CreateTime time.Time `gorm:"column:create_time;index:create_time;autoCreateTime"` - Ex string `gorm:"column:ex" json:"ex;size:1024"` + Ex string `gorm:"column:ex;size:1024" json:"ex"` Status int32 `gorm:"column:status"` CreatorUserID string `gorm:"column:creator_user_id;size:64"` GroupType int32 `gorm:"column:group_type"` diff --git a/pkg/common/db/table/relation/utils.go b/pkg/common/db/table/relation/utils.go index c944eae8b..bc2639e1a 100644 --- a/pkg/common/db/table/relation/utils.go +++ b/pkg/common/db/table/relation/utils.go @@ -15,6 +15,8 @@ package relation import ( + "errors" + "gorm.io/gorm" "github.com/OpenIMSDK/tools/utils" @@ -32,5 +34,5 @@ type GroupSimpleUserID struct { } func IsNotFound(err error) bool { - return utils.Unwrap(err) == gorm.ErrRecordNotFound + return errors.Is(utils.Unwrap(err), gorm.ErrRecordNotFound) } diff --git a/pkg/common/db/table/unrelation/msg.go b/pkg/common/db/table/unrelation/msg.go index c95b211a8..542f318ad 100644 --- a/pkg/common/db/table/unrelation/msg.go +++ b/pkg/common/db/table/unrelation/msg.go @@ -150,6 +150,7 @@ func (m *MsgDocModel) IsFull() bool { func (m MsgDocModel) GetDocID(conversationID string, seq int64) string { seqSuffix := (seq - 1) / singleGocMsgNum + return m.indexGen(conversationID, seqSuffix) } @@ -164,6 +165,7 @@ func (m MsgDocModel) GetDocIDSeqsMap(conversationID string, seqs []int64) map[st t[docID] = append(value, seqs[i]) } } + return t } @@ -181,5 +183,6 @@ func (MsgDocModel) GenExceptionMessageBySeqs(seqs []int64) (exceptionMsg []*sdkw msgModel.Seq = v exceptionMsg = append(exceptionMsg, msgModel) } + return exceptionMsg } diff --git a/pkg/common/db/unrelation/mongo.go b/pkg/common/db/unrelation/mongo.go old mode 100644 new mode 100755 index 09e3e904e..8a90a2a2c --- a/pkg/common/db/unrelation/mongo.go +++ b/pkg/common/db/unrelation/mongo.go @@ -16,6 +16,7 @@ package unrelation import ( "context" + "errors" "fmt" "strings" "time" @@ -44,27 +45,12 @@ type Mongo struct { // NewMongo Initialize MongoDB connection. func NewMongo() (*Mongo, error) { specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound) - uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" + // uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" + var uri string if config.Config.Mongo.Uri != "" { uri = config.Config.Mongo.Uri } else { - mongodbHosts := "" - for i, v := range config.Config.Mongo.Address { - if i == len(config.Config.Mongo.Address)-1 { - mongodbHosts += v - } else { - mongodbHosts += v + "," - } - } - if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" { - uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", - config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts, - config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize) - } else { - uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", - mongodbHosts, config.Config.Mongo.Database, - config.Config.Mongo.MaxPoolSize) - } + uri = defaultMongoUriForNewMongo() } fmt.Println("mongo:", uri) var mongoClient *mongo.Client @@ -76,17 +62,41 @@ func NewMongo() (*Mongo, error) { if err == nil { return &Mongo{db: mongoClient}, nil } - if cmdErr, ok := err.(mongo.CommandError); ok { + var cmdErr mongo.CommandError + if errors.As(err, &cmdErr) { if cmdErr.Code == 13 || cmdErr.Code == 18 { return nil, err - } else { - fmt.Printf("Failed to connect to MongoDB: %s\n", err) } + fmt.Printf("Failed to connect to MongoDB: %s\n", err) } } + return nil, err } +func defaultMongoUriForNewMongo() string { + var uri string + mongodbHosts := "" + for i, v := range config.Config.Mongo.Address { + if i == len(config.Config.Mongo.Address)-1 { + mongodbHosts += v + } else { + mongodbHosts += v + "," + } + } + if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" { + uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", + config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts, + config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize) + } else { + uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", + mongodbHosts, config.Config.Mongo.Database, + config.Config.Mongo.MaxPoolSize) + } + + return uri +} + func (m *Mongo) GetClient() *mongo.Client { return m.db } @@ -106,6 +116,7 @@ func (m *Mongo) CreateSuperGroupIndex() error { if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil { return err } + return nil } @@ -139,5 +150,6 @@ func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...strin if err != nil { return utils.Wrap(err, result) } + return nil } diff --git a/pkg/common/db/unrelation/msg.go b/pkg/common/db/unrelation/msg.go old mode 100644 new mode 100755 index 9b461dd1f..afa2f81e4 --- a/pkg/common/db/unrelation/msg.go +++ b/pkg/common/db/unrelation/msg.go @@ -49,6 +49,7 @@ type MsgMongoDriver struct { func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface { collection := database.Collection(table.MsgDocModel{}.TableName()) + return &MsgMongoDriver{MsgCollection: collection} } @@ -59,6 +60,7 @@ func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsTo func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error { _, err := m.MsgCollection.InsertOne(ctx, model) + return err } @@ -81,6 +83,7 @@ func (m *MsgMongoDriver) UpdateMsg( if err != nil { return nil, utils.Wrap(err, "") } + return res, nil } @@ -108,6 +111,7 @@ func (m *MsgMongoDriver) PushUnique( if err != nil { return nil, utils.Wrap(err, "") } + return res, nil } @@ -120,6 +124,7 @@ func (m *MsgMongoDriver) UpdateMsgContent(ctx context.Context, docID string, ind if err != nil { return utils.Wrap(err, "") } + return nil } @@ -143,12 +148,14 @@ func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc( if err != nil { return utils.Wrap(err, "") } + return nil } func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) { doc := &table.MsgDocModel{} err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc) + return doc, err } @@ -177,6 +184,7 @@ func (m *MsgMongoDriver) GetMsgDocModelByIndex( if len(msgs) > 0 { return &msgs[0], nil } + return nil, ErrMsgListNotExist } @@ -225,6 +233,7 @@ func (m *MsgMongoDriver) DeleteMsgsInOneDocByIndex(ctx context.Context, docID st if err != nil { return utils.Wrap(err, "") } + return nil } @@ -233,6 +242,7 @@ func (m *MsgMongoDriver) DeleteDocs(ctx context.Context, docIDs []string) error return nil } _, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}}) + return err } @@ -246,6 +256,7 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc( for _, seq := range seqs { indexs = append(indexs, m.model.GetMsgIndex(seq)) } + //nolint:govet //This is already the officially recommended standard practice. pipeline := mongo.Pipeline{ { {"$match", bson.D{ @@ -336,6 +347,7 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc( } msgs = append(msgs, msg) } + return msgs, nil } @@ -344,6 +356,7 @@ func (m *MsgMongoDriver) IsExistDocID(ctx context.Context, docID string) (bool, if err != nil { return false, errs.Wrap(err) } + return count > 0, nil } @@ -372,6 +385,7 @@ func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead( updates = append(updates, updateModel) } _, err := m.MsgCollection.BulkWrite(ctx, updates) + return err } @@ -611,7 +625,39 @@ func (m *MsgMongoDriver) RangeUserSendCount( }, ) } - pipeline := bson.A{ + pipeline := buildPiplineForRangeUserSendCount(or, start, end, sort, pageNumber, showNumber) + cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) + if err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + defer cur.Close(ctx) + var result []Result + if err = cur.All(ctx, &result); err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + if len(result) == 0 { + return 0, 0, nil, nil, errs.Wrap(err) + } + users = make([]*table.UserCount, len(result[0].Users)) + for i, r := range result[0].Users { + users[i] = &table.UserCount{ + UserID: r.UserID, + Count: r.Count, + } + } + dateCount = make(map[string]int64) + for _, r := range result[0].Dates { + dateCount[r.Date] = r.Count + } + + return result[0].MsgCount, result[0].UserCount, users, dateCount, nil +} + +//nolint:funlen // it need to be such long +func buildPiplineForRangeUserSendCount(or bson.A, start time.Time, + end time.Time, sort int, pageNumber, showNumber int32, +) bson.A { + return bson.A{ bson.M{ "$match": bson.M{ "$and": bson.A{ @@ -795,30 +841,6 @@ func (m *MsgMongoDriver) RangeUserSendCount( }, }, } - cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) - if err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - defer cur.Close(ctx) - var result []Result - if err := cur.All(ctx, &result); err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - if len(result) == 0 { - return 0, 0, nil, nil, errs.Wrap(err) - } - users = make([]*table.UserCount, len(result[0].Users)) - for i, r := range result[0].Users { - users[i] = &table.UserCount{ - UserID: r.UserID, - Count: r.Count, - } - } - dateCount = make(map[string]int64) - for _, r := range result[0].Dates { - dateCount[r.Date] = r.Count - } - return result[0].MsgCount, result[0].UserCount, users, dateCount, nil } func (m *MsgMongoDriver) RangeGroupSendCount( @@ -847,7 +869,39 @@ func (m *MsgMongoDriver) RangeGroupSendCount( Count int64 `bson:"count"` } `bson:"dates"` } - pipeline := bson.A{ + pipeline := buildPiplineForRangeGroupSendCount(start, end, sort, pageNumber, showNumber) + cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) + if err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + defer cur.Close(ctx) + var result []Result + if err = cur.All(ctx, &result); err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + if len(result) == 0 { + return 0, 0, nil, nil, errs.Wrap(err) + } + groups = make([]*table.GroupCount, len(result[0].Groups)) + for i, r := range result[0].Groups { + groups[i] = &table.GroupCount{ + GroupID: r.GroupID, + Count: r.Count, + } + } + dateCount = make(map[string]int64) + for _, r := range result[0].Dates { + dateCount[r.Date] = r.Count + } + + return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil +} + +//nolint:funlen //it need to has such length +func buildPiplineForRangeGroupSendCount(start time.Time, + end time.Time, sort int, pageNumber, showNumber int32, +) bson.A { + return bson.A{ bson.M{ "$match": bson.M{ "$and": bson.A{ @@ -1044,30 +1098,6 @@ func (m *MsgMongoDriver) RangeGroupSendCount( }, }, } - cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) - if err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - defer cur.Close(ctx) - var result []Result - if err := cur.All(ctx, &result); err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - if len(result) == 0 { - return 0, 0, nil, nil, errs.Wrap(err) - } - groups = make([]*table.GroupCount, len(result[0].Groups)) - for i, r := range result[0].Groups { - groups[i] = &table.GroupCount{ - GroupID: r.GroupID, - Count: r.Count, - } - } - dateCount = make(map[string]int64) - for _, r := range result[0].Dates { - dateCount[r.Date] = r.Count - } - return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil } func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) { @@ -1075,6 +1105,7 @@ func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessa if err != nil { return 0, nil, err } + return total, msgs, nil } @@ -1119,7 +1150,7 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa }, }, ) - + //nolint:govet //this is already standard pipe = mongo.Pipeline{ { {"$match", bson.D{ @@ -1214,5 +1245,6 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa } else { msgs = msgs[start:] } + return n, msgs, nil } diff --git a/pkg/common/db/unrelation/msg_convert.go b/pkg/common/db/unrelation/msg_convert.go index 810b4f419..a5b28a5c7 100644 --- a/pkg/common/db/unrelation/msg_convert.go +++ b/pkg/common/db/unrelation/msg_convert.go @@ -31,12 +31,14 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": regex}) if err != nil { log.ZError(ctx, "convertAll find msg doc failed", err, "conversationID", conversationID) + continue } var msgDocs []table.MsgDocModel err = cursor.All(ctx, &msgDocs) if err != nil { log.ZError(ctx, "convertAll cursor all failed", err, "conversationID", conversationID) + continue } if len(msgDocs) < 1 { @@ -44,39 +46,45 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs } log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(msgDocs)", len(msgDocs)) if len(msgDocs[0].Msg) == int(m.model.GetSingleGocMsgNum5000()) { - if _, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": regex}); err != nil { - log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID) - continue - } - var newMsgDocs []interface{} - for _, msgDoc := range msgDocs { - if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() { - continue - } - var index int64 - for index < int64(len(msgDoc.Msg)) { - msg := msgDoc.Msg[index] - if msg != nil && msg.Msg != nil { - msgDocModel := table.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)} - end := index + m.model.GetSingleGocMsgNum() - if int(end) >= len(msgDoc.Msg) { - msgDocModel.Msg = msgDoc.Msg[index:] - } else { - msgDocModel.Msg = msgDoc.Msg[index:end] - } - newMsgDocs = append(newMsgDocs, msgDocModel) - index = end - } else { - break - } + convertMsgDocs(m, ctx, msgDocs, conversationID, regex) + } + } +} + +func convertMsgDocs(m *MsgMongoDriver, ctx context.Context, msgDocs []table.MsgDocModel, conversationID string, regex primitive.Regex) { + var err error + if _, err = m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": regex}); err != nil { + log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID) + + return + } + var newMsgDocs []interface{} + for _, msgDoc := range msgDocs { + if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() { + continue + } + var index int64 + for index < int64(len(msgDoc.Msg)) { + msg := msgDoc.Msg[index] + if msg != nil && msg.Msg != nil { + msgDocModel := table.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)} + end := index + m.model.GetSingleGocMsgNum() + if int(end) >= len(msgDoc.Msg) { + msgDocModel.Msg = msgDoc.Msg[index:] + } else { + msgDocModel.Msg = msgDoc.Msg[index:end] } - } - _, err = m.MsgCollection.InsertMany(ctx, newMsgDocs) - if err != nil { - log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) + newMsgDocs = append(newMsgDocs, msgDocModel) + index = end } else { - log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) + break } } } + _, err = m.MsgCollection.InsertMany(ctx, newMsgDocs) + if err != nil { + log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) + } else { + log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) + } } diff --git a/pkg/common/db/unrelation/super_group.go b/pkg/common/db/unrelation/super_group.go index c762140a2..7f9aecfd6 100644 --- a/pkg/common/db/unrelation/super_group.go +++ b/pkg/common/db/unrelation/super_group.go @@ -59,6 +59,7 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st return err } } + return nil } @@ -69,6 +70,7 @@ func (s *SuperGroupMongoDriver) TakeSuperGroup( if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil { return nil, utils.Wrap(err, "") } + return group, nil } @@ -86,6 +88,7 @@ func (s *SuperGroupMongoDriver) FindSuperGroup( if err := cursor.All(ctx, &groups); err != nil { return nil, utils.Wrap(err, "") } + return groups, nil } @@ -113,6 +116,7 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID return utils.Wrap(err, "transaction failed") } } + return nil } @@ -129,6 +133,7 @@ func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, g if err != nil { return err } + return nil } @@ -138,6 +143,7 @@ func (s *SuperGroupMongoDriver) GetSuperGroupByUserID( ) (*unrelation.UserToSuperGroupModel, error) { var user unrelation.UserToSuperGroupModel err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user) + return &user, utils.Wrap(err, "") } @@ -149,6 +155,7 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil { return utils.Wrap(err, "") } + return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs) } @@ -158,5 +165,6 @@ func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}}, ) + return utils.Wrap(err, "") } diff --git a/pkg/common/db/unrelation/user.go b/pkg/common/db/unrelation/user.go old mode 100644 new mode 100755 index 4b4a78c79..ad02968bd --- a/pkg/common/db/unrelation/user.go +++ b/pkg/common/db/unrelation/user.go @@ -16,6 +16,7 @@ package unrelation import ( "context" + "errors" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/utils" @@ -50,6 +51,7 @@ type UserMongoDriver struct { // AddSubscriptionList Subscriber's handling of thresholds. func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error { // Check the number of lists in the key. + //nolint:govet //this has already been the standard format for mongo.Pipeline pipeline := mongo.Pipeline{ {{"$match", bson.D{{"user_id", SubscriptionPrefix + userID}}}}, {{"$project", bson.D{{"count", bson.D{{"$size", "$user_id_list"}}}}}}, @@ -65,7 +67,7 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string } // iterate over aggregated results for cursor.Next(ctx) { - err := cursor.Decode(&cnt) + err = cursor.Decode(&cnt) if err != nil { return errs.Wrap(err) } @@ -122,6 +124,7 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string return utils.Wrap(err, "transaction failed") } } + return nil } @@ -139,6 +142,7 @@ func (u *UserMongoDriver) UnsubscriptionList(ctx context.Context, userID string, if err != nil { return errs.Wrap(err) } + return nil } @@ -152,6 +156,7 @@ func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, user bson.M{"$pull": bson.M{"user_id_list": userID}}, ) } + return errs.Wrap(err) } @@ -163,12 +168,13 @@ func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string bson.M{"user_id": SubscriptionPrefix + userID}) err = cursor.Decode(&user) if err != nil { - if err == mongo.ErrNoDocuments { + if errors.Is(err, mongo.ErrNoDocuments) { return []string{}, nil - } else { - return nil, errs.Wrap(err) } + + return nil, errs.Wrap(err) } + return user.UserIDList, nil } @@ -180,11 +186,12 @@ func (u *UserMongoDriver) GetSubscribedList(ctx context.Context, userID string) bson.M{"user_id": SubscribedPrefix + userID}) err = cursor.Decode(&user) if err != nil { - if err == mongo.ErrNoDocuments { + if errors.Is(err, mongo.ErrNoDocuments) { return []string{}, nil - } else { - return nil, errs.Wrap(err) } + + return nil, errs.Wrap(err) } + return user.UserIDList, nil } diff --git a/pkg/common/discovery_register/k8s_discovery_register.go b/pkg/common/discovery_register/k8s_discovery_register.go index 70f9f39f3..72179fdbd 100644 --- a/pkg/common/discovery_register/k8s_discovery_register.go +++ b/pkg/common/discovery_register/k8s_discovery_register.go @@ -4,12 +4,14 @@ import ( "context" "errors" "fmt" + "time" + "github.com/OpenIMSDK/tools/discoveryregistry" openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" - "github.com/openimsdk/open-im-server/v3/pkg/common/config" "google.golang.org/grpc" - "time" + + "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) { @@ -28,6 +30,7 @@ func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistr client = nil err = errors.New("envType not correct") } + return client, err } @@ -42,47 +45,51 @@ func NewK8sDiscoveryRegister() (discoveryregistry.SvcDiscoveryRegistry, error) { func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error { cli.rpcRegisterAddr = serviceName + return nil } -func (cli *K8sDR) UnRegister() error { +func (cli *K8sDR) UnRegister() error { return nil } -func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error { +func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error { return nil } -func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error { +func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error { return nil } func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) { - return nil, nil } -func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) { +func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) { conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) + return []*grpc.ClientConn{conn}, err } -func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { +func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) } -func (cli *K8sDR) GetSelfConnTarget() string { +func (cli *K8sDR) GetSelfConnTarget() string { return cli.rpcRegisterAddr } + func (cli *K8sDR) AddOption(opts ...grpc.DialOption) { cli.options = append(cli.options, opts...) } + func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) { conn.Close() } -// do not use this method for call rpc +// do not use this method for call rpc. func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn { fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!") + return nil } diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index 2d7c24c77..579643964 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -34,16 +34,21 @@ import ( var client http.Client func Get(url string) (response []byte, err error) { - client := http.Client{Timeout: 5 * time.Second} - resp, err := client.Get(url) + clientGet := http.Client{Timeout: 5 * time.Second} + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) if err != nil { return nil, err } + resp, err2 := clientGet.Do(req) + if err2 != nil { + return nil, err + } defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { + body, err3 := io.ReadAll(resp.Body) + if err3 != nil { return nil, err } + return body, nil } @@ -83,6 +88,7 @@ func Post( if err != nil { return nil, err } + return result, nil } @@ -98,6 +104,7 @@ func PostReturn( return err } err = json.Unmarshal(b, output) + return err } @@ -116,17 +123,22 @@ func callBackPostReturn( if err != nil { if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue { log.ZWarn(ctx, "callback failed but continue", err, "url", url) + return errs.ErrCallbackContinue } + return errs.ErrNetwork.Wrap(err.Error()) } if err = json.Unmarshal(b, output); err != nil { if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue { log.ZWarn(ctx, "callback failed but continue", err, "url", url) + return errs.ErrCallbackContinue } + return errs.ErrData.Wrap(err.Error()) } + return output.Parse() } diff --git a/pkg/common/kafka/consumer_group.go b/pkg/common/kafka/consumer_group.go index 1eb7b522a..c5ec69533 100644 --- a/pkg/common/kafka/consumer_group.go +++ b/pkg/common/kafka/consumer_group.go @@ -51,6 +51,7 @@ func NewMConsumerGroup(consumerConfig *MConsumerGroupConfig, topics, addrs []str if err != nil { panic(err.Error()) } + return &MConsumerGroup{ consumerGroup, groupID, diff --git a/pkg/common/kafka/producer.go b/pkg/common/kafka/producer.go index 4a52d2bef..b4545af9b 100644 --- a/pkg/common/kafka/producer.go +++ b/pkg/common/kafka/producer.go @@ -67,6 +67,7 @@ func NewKafkaProducer(addr []string, topic string) *Producer { producer, err = sarama.NewSyncProducer(p.addr, p.config) // Initialize the client if err == nil { p.producer = producer + return &p } //TODO If the password is wrong, exit directly @@ -83,6 +84,7 @@ func NewKafkaProducer(addr []string, topic string) *Producer { panic(err.Error()) } p.producer = producer + return &p } @@ -91,6 +93,7 @@ func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error) if err != nil { return nil, err } + return []sarama.RecordHeader{ {Key: []byte(constant.OperationID), Value: []byte(operationID)}, {Key: []byte(constant.OpUserID), Value: []byte(opUserID)}, @@ -100,10 +103,11 @@ func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error) } func GetContextWithMQHeader(header []*sarama.RecordHeader) context.Context { - var values []string + values := make([]string, 0, len(header)) for _, recordHeader := range header { values = append(values, string(recordHeader.Value)) } + return mcontext.WithMustInfoCtx(values) // TODO } @@ -134,5 +138,6 @@ func (p *Producer) SendMessage(ctx context.Context, key string, msg proto.Messag if err == nil { prome.Inc(prome.SendMsgCounter) } + return partition, offset, utils.Wrap(err, "") } diff --git a/pkg/common/locker/message_locker.go b/pkg/common/locker/message_locker.go index 55241eb5f..108dbbd48 100644 --- a/pkg/common/locker/message_locker.go +++ b/pkg/common/locker/message_locker.go @@ -42,11 +42,13 @@ func (l *LockerMessage) LockMessageTypeKey(ctx context.Context, clientMsgID, typ err = l.cache.LockMessageTypeKey(ctx, clientMsgID, typeKey) if err != nil { time.Sleep(time.Millisecond * 100) + continue } else { break } } + return err } @@ -55,11 +57,13 @@ func (l *LockerMessage) LockGlobalMessage(ctx context.Context, clientMsgID strin err = l.cache.LockMessageTypeKey(ctx, clientMsgID, GlOBALLOCK) if err != nil { time.Sleep(time.Millisecond * 100) + continue } else { break } } + return err } diff --git a/pkg/common/prome/gather.go b/pkg/common/prome/gather.go index eb4bc6c3b..184034e25 100644 --- a/pkg/common/prome/gather.go +++ b/pkg/common/prome/gather.go @@ -79,6 +79,7 @@ var ( ConversationCreateFailedCounter prometheus.Counter ) +//nolint:promlinter //no idea to fix it func NewUserLoginCounter() { if UserLoginCounter != nil { return @@ -89,6 +90,7 @@ func NewUserLoginCounter() { }) } +//nolint:promlinter //no idea to fix it func NewUserRegisterCounter() { if UserRegisterCounter != nil { return @@ -99,6 +101,7 @@ func NewUserRegisterCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSeqGetSuccessCounter() { if SeqGetSuccessCounter != nil { return @@ -109,6 +112,7 @@ func NewSeqGetSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSeqGetFailedCounter() { if SeqGetFailedCounter != nil { return @@ -119,6 +123,7 @@ func NewSeqGetFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSeqSetSuccessCounter() { if SeqSetSuccessCounter != nil { return @@ -129,6 +134,7 @@ func NewSeqSetSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSeqSetFailedCounter() { if SeqSetFailedCounter != nil { return @@ -139,6 +145,7 @@ func NewSeqSetFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewApiRequestCounter() { if ApiRequestCounter != nil { return @@ -149,6 +156,7 @@ func NewApiRequestCounter() { }) } +//nolint:promlinter //no idea to fix it func NewApiRequestSuccessCounter() { if ApiRequestSuccessCounter != nil { return @@ -159,6 +167,7 @@ func NewApiRequestSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewApiRequestFailedCounter() { if ApiRequestFailedCounter != nil { return @@ -169,6 +178,7 @@ func NewApiRequestFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewGrpcRequestCounter() { if GrpcRequestCounter != nil { return @@ -179,6 +189,7 @@ func NewGrpcRequestCounter() { }) } +//nolint:promlinter //no idea to fix it func NewGrpcRequestSuccessCounter() { if GrpcRequestSuccessCounter != nil { return @@ -189,6 +200,7 @@ func NewGrpcRequestSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewGrpcRequestFailedCounter() { if GrpcRequestFailedCounter != nil { return @@ -199,6 +211,7 @@ func NewGrpcRequestFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSendMsgCount() { if SendMsgCounter != nil { return @@ -209,6 +222,7 @@ func NewSendMsgCount() { }) } +//nolint:promlinter //no idea to fix it func NewMsgInsertRedisSuccessCounter() { if MsgInsertRedisSuccessCounter != nil { return @@ -219,6 +233,7 @@ func NewMsgInsertRedisSuccessCounter() { }) } +//nolint:promlinter //no idea to fix its func NewMsgInsertRedisFailedCounter() { if MsgInsertRedisFailedCounter != nil { return @@ -229,6 +244,7 @@ func NewMsgInsertRedisFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgInsertMongoSuccessCounter() { if MsgInsertMongoSuccessCounter != nil { return @@ -239,6 +255,7 @@ func NewMsgInsertMongoSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgInsertMongoFailedCounter() { if MsgInsertMongoFailedCounter != nil { return @@ -249,6 +266,7 @@ func NewMsgInsertMongoFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgPullFromRedisSuccessCounter() { if MsgPullFromRedisSuccessCounter != nil { return @@ -259,6 +277,7 @@ func NewMsgPullFromRedisSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgPullFromRedisFailedCounter() { if MsgPullFromRedisFailedCounter != nil { return @@ -269,6 +288,7 @@ func NewMsgPullFromRedisFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgPullFromMongoSuccessCounter() { if MsgPullFromMongoSuccessCounter != nil { return @@ -279,6 +299,7 @@ func NewMsgPullFromMongoSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgPullFromMongoFailedCounter() { if MsgPullFromMongoFailedCounter != nil { return @@ -319,6 +340,7 @@ func NewPullMsgBySeqListTotalCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSingleChatMsgRecvSuccessCounter() { if SingleChatMsgRecvSuccessCounter != nil { return @@ -329,6 +351,7 @@ func NewSingleChatMsgRecvSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewGroupChatMsgRecvSuccessCounter() { if GroupChatMsgRecvSuccessCounter != nil { return @@ -339,6 +362,7 @@ func NewGroupChatMsgRecvSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewWorkSuperGroupChatMsgRecvSuccessCounter() { if WorkSuperGroupChatMsgRecvSuccessCounter != nil { return @@ -359,6 +383,7 @@ func NewOnlineUserGauges() { }) } +//nolint:promlinter //no idea to fix it func NewSingleChatMsgProcessSuccessCounter() { if SingleChatMsgProcessSuccessCounter != nil { return @@ -369,6 +394,7 @@ func NewSingleChatMsgProcessSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewSingleChatMsgProcessFailedCounter() { if SingleChatMsgProcessFailedCounter != nil { return @@ -379,6 +405,7 @@ func NewSingleChatMsgProcessFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewGroupChatMsgProcessSuccessCounter() { if GroupChatMsgProcessSuccessCounter != nil { return @@ -389,6 +416,7 @@ func NewGroupChatMsgProcessSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewGroupChatMsgProcessFailedCounter() { if GroupChatMsgProcessFailedCounter != nil { return @@ -399,6 +427,7 @@ func NewGroupChatMsgProcessFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewWorkSuperGroupChatMsgProcessSuccessCounter() { if WorkSuperGroupChatMsgProcessSuccessCounter != nil { return @@ -409,6 +438,7 @@ func NewWorkSuperGroupChatMsgProcessSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewWorkSuperGroupChatMsgProcessFailedCounter() { if WorkSuperGroupChatMsgProcessFailedCounter != nil { return @@ -419,6 +449,7 @@ func NewWorkSuperGroupChatMsgProcessFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgOnlinePushSuccessCounter() { if MsgOnlinePushSuccessCounter != nil { return @@ -429,6 +460,7 @@ func NewMsgOnlinePushSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgOfflinePushSuccessCounter() { if MsgOfflinePushSuccessCounter != nil { return @@ -439,6 +471,7 @@ func NewMsgOfflinePushSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewMsgOfflinePushFailedCounter() { if MsgOfflinePushFailedCounter != nil { return @@ -449,6 +482,7 @@ func NewMsgOfflinePushFailedCounter() { }) } +//nolint:promlinter //no idea to fix it func NewConversationCreateSuccessCounter() { if ConversationCreateSuccessCounter != nil { return @@ -459,6 +493,7 @@ func NewConversationCreateSuccessCounter() { }) } +//nolint:promlinter //no idea to fix it func NewConversationCreateFailedCounter() { if ConversationCreateFailedCounter != nil { return diff --git a/pkg/common/prome/prometheus.go b/pkg/common/prome/prometheus.go index 254a6c9ea..60df5b0af 100644 --- a/pkg/common/prome/prometheus.go +++ b/pkg/common/prome/prometheus.go @@ -30,13 +30,16 @@ func StartPrometheusSrv(prometheusPort int) error { if config.Config.Prometheus.Enable { http.Handle("/metrics", promhttp.Handler()) err := http.ListenAndServe(":"+strconv.Itoa(prometheusPort), nil) + return err } + return nil } func PrometheusHandler() gin.HandlerFunc { h := promhttp.Handler() + return func(c *gin.Context) { h.ServeHTTP(c.Writer, c.Request) } @@ -49,6 +52,7 @@ type responseBodyWriter struct { func (r responseBodyWriter) Write(b []byte) (int, error) { r.body.Write(b) + return r.ResponseWriter.Write(b) } diff --git a/pkg/common/tls/tls.go b/pkg/common/tls/tls.go old mode 100644 new mode 100755 index 3bf91beb9..7b3e9033e --- a/pkg/common/tls/tls.go +++ b/pkg/common/tls/tls.go @@ -24,6 +24,7 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) +//nolint:staticcheck //we have not time looking for a replacement for x509 to fix the security valnerability func decryptPEM(data []byte, passphrase []byte) ([]byte, error) { if len(passphrase) == 0 { return data, nil @@ -33,6 +34,7 @@ func decryptPEM(data []byte, passphrase []byte) ([]byte, error) { if err != nil { return nil, err } + return pem.EncodeToMemory(&pem.Block{ Type: b.Type, Bytes: d, @@ -44,6 +46,7 @@ func readEncryptablePEMBlock(path string, pwd []byte) ([]byte, error) { if err != nil { return nil, err } + return decryptPEM(data, pwd) } diff --git a/pkg/msgprocessor/conversation.go b/pkg/msgprocessor/conversation.go index ca77438ea..559994eaf 100644 --- a/pkg/msgprocessor/conversation.go +++ b/pkg/msgprocessor/conversation.go @@ -28,6 +28,7 @@ func GetNotificationConversationIDByMsg(msg *sdkws.MsgData) string { case constant.SingleChatType: l := []string{msg.SendID, msg.RecvID} sort.Strings(l) + return "n_" + strings.Join(l, "_") case constant.GroupChatType: return "n_" + msg.GroupID @@ -36,6 +37,7 @@ func GetNotificationConversationIDByMsg(msg *sdkws.MsgData) string { case constant.NotificationChatType: return "n_" + msg.SendID + "_" + msg.RecvID } + return "" } @@ -44,6 +46,7 @@ func GetChatConversationIDByMsg(msg *sdkws.MsgData) string { case constant.SingleChatType: l := []string{msg.SendID, msg.RecvID} sort.Strings(l) + return "si_" + strings.Join(l, "_") case constant.GroupChatType: return "g_" + msg.GroupID @@ -52,6 +55,7 @@ func GetChatConversationIDByMsg(msg *sdkws.MsgData) string { case constant.NotificationChatType: return "sn_" + msg.SendID + "_" + msg.RecvID } + return "" } @@ -60,10 +64,12 @@ func GenConversationUniqueKey(msg *sdkws.MsgData) string { case constant.SingleChatType, constant.NotificationChatType: l := []string{msg.SendID, msg.RecvID} sort.Strings(l) + return strings.Join(l, "_") case constant.SuperGroupChatType: return msg.GroupID } + return "" } @@ -76,23 +82,28 @@ func GetConversationIDByMsg(msg *sdkws.MsgData) string { if !options.IsNotNotification() { return "n_" + strings.Join(l, "_") } + return "si_" + strings.Join(l, "_") // single chat case constant.GroupChatType: if !options.IsNotNotification() { return "n_" + msg.GroupID // group chat } + return "g_" + msg.GroupID // group chat case constant.SuperGroupChatType: if !options.IsNotNotification() { return "n_" + msg.GroupID // super group chat } + return "sg_" + msg.GroupID // super group chat case constant.NotificationChatType: if !options.IsNotNotification() { return "n_" + msg.SendID + "_" + msg.RecvID // super group chat } + return "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat } + return "" } @@ -111,6 +122,7 @@ func GetConversationIDBySessionType(sessionType int, ids ...string) string { case constant.NotificationChatType: return "sn_" + ids[0] // server notification chat } + return "" } @@ -118,10 +130,11 @@ func GetNotificationConversationIDByConversationID(conversationID string) string l := strings.Split(conversationID, "_") if len(l) > 1 { l[0] = "n" + return strings.Join(l, "_") - } else { - return "" } + + return "" } func GetNotificationConversationID(sessionType int, ids ...string) string { @@ -135,6 +148,7 @@ func GetNotificationConversationID(sessionType int, ids ...string) string { case constant.SuperGroupChatType: return "n_" + ids[0] // super group chat } + return "" } @@ -155,18 +169,22 @@ func ParseConversationID(msg *sdkws.MsgData) (isNotification bool, conversationI if !options.IsNotNotification() { return true, "n_" + strings.Join(l, "_") } + return false, "si_" + strings.Join(l, "_") // single chat case constant.SuperGroupChatType: if !options.IsNotNotification() { return true, "n_" + msg.GroupID // super group chat } + return false, "sg_" + msg.GroupID // super group chat case constant.NotificationChatType: if !options.IsNotNotification() { return true, "n_" + msg.SendID + "_" + msg.RecvID // super group chat } + return false, "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat } + return false, "" } @@ -189,6 +207,7 @@ func Pb2String(pb proto.Message) (string, error) { if err != nil { return "", err } + return string(s), nil } diff --git a/pkg/msgprocessor/options.go b/pkg/msgprocessor/options.go index c17c7cb05..27bbb839a 100644 --- a/pkg/msgprocessor/options.go +++ b/pkg/msgprocessor/options.go @@ -38,12 +38,14 @@ func NewOptions(opts ...OptionsOpt) Options { for _, opt := range opts { opt(options) } + return options } func NewMsgOptions() Options { options := make(map[string]bool, 11) options[constant.IsOfflinePush] = false + return make(map[string]bool) } @@ -51,6 +53,7 @@ func WithOptions(options Options, opts ...OptionsOpt) Options { for _, opt := range opts { opt(options) } + return options } @@ -131,6 +134,7 @@ func (o Options) Is(notification string) bool { if !ok || v { return true } + return false } diff --git a/pkg/rpcclient/auth.go b/pkg/rpcclient/auth.go index 0ee021de1..4859e541d 100644 --- a/pkg/rpcclient/auth.go +++ b/pkg/rpcclient/auth.go @@ -31,6 +31,7 @@ func NewAuth(discov discoveryregistry.SvcDiscoveryRegistry) *Auth { panic(err) } client := auth.NewAuthClient(conn) + return &Auth{discov: discov, conn: conn, Client: client} } diff --git a/pkg/rpcclient/conversation.go b/pkg/rpcclient/conversation.go index 60ca53351..30b0b4b77 100644 --- a/pkg/rpcclient/conversation.go +++ b/pkg/rpcclient/conversation.go @@ -39,6 +39,7 @@ func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversatio panic(err) } client := pbconversation.NewConversationClient(conn) + return &Conversation{discov: discov, conn: conn, Client: client} } @@ -56,26 +57,31 @@ func (c *ConversationRpcClient) GetSingleConversationRecvMsgOpt(ctx context.Cont if err != nil { return 0, err } + return conversation.GetConversation().RecvMsgOpt, err } func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error { _, err := c.Client.CreateSingleChatConversations(ctx, &pbconversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID}) + return err } func (c *ConversationRpcClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error { _, err := c.Client.CreateGroupChatConversations(ctx, &pbconversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID}) + return err } func (c *ConversationRpcClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error { _, err := c.Client.SetConversationMaxSeq(ctx, &pbconversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq}) + return err } func (c *ConversationRpcClient) SetConversations(ctx context.Context, userIDs []string, conversation *pbconversation.ConversationReq) error { _, err := c.Client.SetConversations(ctx, &pbconversation.SetConversationsReq{UserIDs: userIDs, Conversation: conversation}) + return err } @@ -84,6 +90,7 @@ func (c *ConversationRpcClient) GetConversationIDs(ctx context.Context, ownerUse if err != nil { return nil, err } + return resp.ConversationIDs, nil } @@ -92,6 +99,7 @@ func (c *ConversationRpcClient) GetConversation(ctx context.Context, ownerUserID if err != nil { return nil, err } + return resp.Conversation, nil } @@ -106,6 +114,7 @@ func (c *ConversationRpcClient) GetConversationsByConversationID(ctx context.Con if len(resp.Conversations) == 0 { return nil, errs.ErrRecordNotFound.Wrap(fmt.Sprintf("conversationIDs: %v not found", conversationIDs)) } + return resp.Conversations, nil } @@ -124,5 +133,6 @@ func (c *ConversationRpcClient) GetConversations( if err != nil { return nil, err } + return resp.Conversations, nil } diff --git a/pkg/rpcclient/friend.go b/pkg/rpcclient/friend.go index b84db40d4..6b214aaf2 100644 --- a/pkg/rpcclient/friend.go +++ b/pkg/rpcclient/friend.go @@ -38,6 +38,7 @@ func NewFriend(discov discoveryregistry.SvcDiscoveryRegistry) *Friend { panic(err) } client := friend.NewFriendClient(conn) + return &Friend{discov: discov, conn: conn, Client: client} } @@ -59,6 +60,7 @@ func (f *FriendRpcClient) GetFriendsInfo( return nil, err } resp = r.FriendsInfo[0] + return } @@ -68,6 +70,7 @@ func (f *FriendRpcClient) IsFriend(ctx context.Context, possibleFriendUserID, us if err != nil { return false, err } + return resp.InUser1Friends, nil } @@ -77,6 +80,7 @@ func (f *FriendRpcClient) GetFriendIDs(ctx context.Context, ownerUserID string) if err != nil { return nil, err } + return resp.FriendIDs, nil } @@ -85,5 +89,6 @@ func (b *FriendRpcClient) IsBlocked(ctx context.Context, possibleBlackUserID, us if err != nil { return false, err } + return r.InUser2Blacks, nil } diff --git a/pkg/rpcclient/group.go b/pkg/rpcclient/group.go index bf0efe60c..5a340875b 100644 --- a/pkg/rpcclient/group.go +++ b/pkg/rpcclient/group.go @@ -42,6 +42,7 @@ func NewGroup(discov discoveryregistry.SvcDiscoveryRegistry) *Group { panic(err) } client := group.NewGroupClient(conn) + return &Group{discov: discov, conn: conn, Client: client} } @@ -69,6 +70,7 @@ func (g *GroupRpcClient) GetGroupInfos( return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ",")) } } + return resp.GroupInfos, nil } @@ -77,6 +79,7 @@ func (g *GroupRpcClient) GetGroupInfo(ctx context.Context, groupID string) (*sdk if err != nil { return nil, err } + return groups[0], nil } @@ -89,6 +92,7 @@ func (g *GroupRpcClient) GetGroupInfoMap( if err != nil { return nil, err } + return utils.SliceToMap(groups, func(e *sdkws.GroupInfo) string { return e.GroupID }), nil @@ -114,6 +118,7 @@ func (g *GroupRpcClient) GetGroupMemberInfos( return nil, errs.ErrNotInGroupYet.Wrap(strings.Join(ids, ",")) } } + return resp.Members, nil } @@ -126,6 +131,7 @@ func (g *GroupRpcClient) GetGroupMemberInfo( if err != nil { return nil, err } + return members[0], nil } @@ -139,6 +145,7 @@ func (g *GroupRpcClient) GetGroupMemberInfoMap( if err != nil { return nil, err } + return utils.SliceToMap(members, func(e *sdkws.GroupMemberFullInfo) string { return e.UserID }), nil @@ -155,6 +162,7 @@ func (g *GroupRpcClient) GetOwnerAndAdminInfos( if err != nil { return nil, err } + return resp.Members, nil } @@ -163,6 +171,7 @@ func (g *GroupRpcClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdk GroupID: groupID, RoleLevels: []int32{constant.GroupOwner}, }) + return resp.Members[0], err } @@ -173,6 +182,7 @@ func (g *GroupRpcClient) GetGroupMemberIDs(ctx context.Context, groupID string) if err != nil { return nil, err } + return resp.UserIDs, nil } @@ -183,6 +193,7 @@ func (g *GroupRpcClient) GetGroupInfoCache(ctx context.Context, groupID string) if err != nil { return nil, err } + return resp.GroupInfo, nil } @@ -198,6 +209,7 @@ func (g *GroupRpcClient) GetGroupMemberCache( if err != nil { return nil, err } + return resp.Member, nil } @@ -206,6 +218,7 @@ func (g *GroupRpcClient) DismissGroup(ctx context.Context, groupID string) error GroupID: groupID, DeleteMember: true, }) + return err } @@ -213,5 +226,6 @@ func (g *GroupRpcClient) NotificationUserInfoUpdate(ctx context.Context, userID _, err := g.Client.NotificationUserInfoUpdate(ctx, &group.NotificationUserInfoUpdateReq{ UserID: userID, }) + return err } diff --git a/pkg/rpcclient/msg.go b/pkg/rpcclient/msg.go index e3dad874e..51e29c7d8 100644 --- a/pkg/rpcclient/msg.go +++ b/pkg/rpcclient/msg.go @@ -136,6 +136,7 @@ func NewMessage(discov discoveryregistry.SvcDiscoveryRegistry) *Message { panic(err) } client := msg.NewMsgClient(conn) + return &Message{discov: discov, conn: conn, Client: client} } @@ -147,16 +148,19 @@ func NewMessageRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) MessageR func (m *MessageRpcClient) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) { resp, err := m.Client.SendMsg(ctx, req) + return resp, err } func (m *MessageRpcClient) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) { resp, err := m.Client.GetMaxSeq(ctx, req) + return resp, err } func (m *MessageRpcClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMessageBySeqsReq) (*sdkws.PullMessageBySeqsResp, error) { resp, err := m.Client.PullMessageBySeqs(ctx, req) + return resp, err } @@ -165,6 +169,7 @@ func (m *MessageRpcClient) GetConversationMaxSeq(ctx context.Context, conversati if err != nil { return 0, err } + return resp.MaxSeq, nil } @@ -200,6 +205,7 @@ func NewNotificationSender(opts ...NotificationSenderOptions) *NotificationSende for _, opt := range opts { opt(notificationSender) } + return notificationSender } @@ -220,6 +226,7 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s content, err := json.Marshal(&n) if err != nil { log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m) + return err } notificationOpt := ¬ificationOpt{} @@ -229,7 +236,8 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s var req msg.SendMsgReq var msg sdkws.MsgData if notificationOpt.WithRpcGetUsername && s.getUserInfo != nil { - userInfo, err := s.getUserInfo(ctx, sendID) + var userInfo *sdkws.UserInfo + userInfo, err = s.getUserInfo(ctx, sendID) if err != nil { log.ZWarn(ctx, "getUserInfo failed", err, "sendID", sendID) } else { @@ -267,6 +275,7 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s } else { log.ZError(ctx, "MsgClient Notification SendMsg failed", err, "req", &req) } + return err } diff --git a/pkg/rpcclient/notification/conevrsation.go b/pkg/rpcclient/notification/conversation.go similarity index 99% rename from pkg/rpcclient/notification/conevrsation.go rename to pkg/rpcclient/notification/conversation.go index 77fc623e4..0fefb147e 100644 --- a/pkg/rpcclient/notification/conevrsation.go +++ b/pkg/rpcclient/notification/conversation.go @@ -41,6 +41,7 @@ func (c *ConversationNotificationSender) ConversationSetPrivateNotification(ctx IsPrivate: isPrivateChat, ConversationID: conversationID, } + return c.Notification(ctx, sendID, recvID, constant.ConversationPrivateChatNotification, tips) } @@ -50,6 +51,7 @@ func (c *ConversationNotificationSender) ConversationChangeNotification(ctx cont UserID: userID, ConversationIDList: conversationIDs, } + return c.Notification(ctx, userID, userID, constant.ConversationChangeNotification, tips) } @@ -65,5 +67,6 @@ func (c *ConversationNotificationSender) ConversationUnreadChangeNotification( HasReadSeq: hasReadSeq, UnreadCountTime: unreadCountTime, } + return c.Notification(ctx, userID, userID, constant.ConversationUnreadNotification, tips) } diff --git a/pkg/rpcclient/notification/friend.go b/pkg/rpcclient/notification/friend.go index b061a24ae..9dae27c6e 100644 --- a/pkg/rpcclient/notification/friend.go +++ b/pkg/rpcclient/notification/friend.go @@ -57,6 +57,7 @@ func WithDBFunc( for _, user := range users { result = append(result, user) } + return result, nil } s.getUsersInfo = f @@ -75,6 +76,7 @@ func WithRpcFunc( for _, user := range users { result = append(result, user) } + return result, err } s.getUsersInfo = f @@ -91,6 +93,7 @@ func NewFriendNotificationSender( for _, opt := range opts { opt(f) } + return f } @@ -106,22 +109,13 @@ func (f *FriendNotificationSender) getUsersInfoMap( for _, user := range users { result[user.GetUserID()] = user.(*sdkws.UserInfo) } - return result, nil -} -func (f *FriendNotificationSender) getFromToUserNickname( - ctx context.Context, - fromUserID, toUserID string, -) (string, string, error) { - users, err := f.getUsersInfoMap(ctx, []string{fromUserID, toUserID}) - if err != nil { - return "", "", nil - } - return users[fromUserID].Nickname, users[toUserID].Nickname, nil + return result, nil } func (f *FriendNotificationSender) UserInfoUpdatedNotification(ctx context.Context, changedUserID string) error { tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} + return f.Notification(ctx, mcontext.GetOpUserID(ctx), changedUserID, constant.UserInfoUpdatedNotification, &tips) } @@ -133,6 +127,7 @@ func (f *FriendNotificationSender) FriendApplicationAddNotification( FromUserID: req.FromUserID, ToUserID: req.ToUserID, }} + return f.Notification(ctx, req.FromUserID, req.ToUserID, constant.FriendApplicationNotification, &tips) } @@ -144,6 +139,7 @@ func (f *FriendNotificationSender) FriendApplicationAgreedNotification( FromUserID: req.FromUserID, ToUserID: req.ToUserID, }, HandleMsg: req.HandleMsg} + return f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &tips) } @@ -155,6 +151,7 @@ func (f *FriendNotificationSender) FriendApplicationRefusedNotification( FromUserID: req.FromUserID, ToUserID: req.ToUserID, }, HandleMsg: req.HandleMsg} + return f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationRejectedNotification, &tips) } @@ -179,6 +176,7 @@ func (f *FriendNotificationSender) FriendAddedNotification( if err != nil { return err } + return f.Notification(ctx, fromUserID, toUserID, constant.FriendAddedNotification, &tips) } @@ -187,6 +185,7 @@ func (f *FriendNotificationSender) FriendDeletedNotification(ctx context.Context FromUserID: req.OwnerUserID, ToUserID: req.FriendUserID, }} + return f.Notification(ctx, req.OwnerUserID, req.FriendUserID, constant.FriendDeletedNotification, &tips) } @@ -194,6 +193,7 @@ func (f *FriendNotificationSender) FriendRemarkSetNotification(ctx context.Conte tips := sdkws.FriendInfoChangedTips{FromToUserID: &sdkws.FromToUserID{}} tips.FromToUserID.FromUserID = fromUserID tips.FromToUserID.ToUserID = toUserID + return f.Notification(ctx, fromUserID, toUserID, constant.FriendRemarkSetNotification, &tips) } @@ -201,6 +201,7 @@ func (f *FriendNotificationSender) BlackAddedNotification(ctx context.Context, r tips := sdkws.BlackAddedTips{FromToUserID: &sdkws.FromToUserID{}} tips.FromToUserID.FromUserID = req.OwnerUserID tips.FromToUserID.ToUserID = req.BlackUserID + return f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackAddedNotification, &tips) } @@ -209,7 +210,10 @@ func (f *FriendNotificationSender) BlackDeletedNotification(ctx context.Context, FromUserID: req.OwnerUserID, ToUserID: req.BlackUserID, }} - f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips) + err := f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips) + if err != nil { + panic(err) + } } func (f *FriendNotificationSender) FriendInfoUpdatedNotification( @@ -218,5 +222,8 @@ func (f *FriendNotificationSender) FriendInfoUpdatedNotification( needNotifiedUserID string, ) { tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} - f.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips) + err := f.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips) + if err != nil { + panic(err) + } } diff --git a/pkg/rpcclient/notification/group.go b/pkg/rpcclient/notification/group.go old mode 100644 new mode 100755 index 8e71f61c3..23341af70 --- a/pkg/rpcclient/notification/group.go +++ b/pkg/rpcclient/notification/group.go @@ -60,6 +60,7 @@ func (g *GroupNotificationSender) getUser(ctx context.Context, userID string) (* if len(users) == 0 { return nil, errs.ErrUserIDNotFound.Wrap(fmt.Sprintf("user %s not found", userID)) } + return &sdkws.PublicUserInfo{ UserID: users[0].GetUserID(), Nickname: users[0].GetNickname(), @@ -68,6 +69,23 @@ func (g *GroupNotificationSender) getUser(ctx context.Context, userID string) (* }, nil } +func (g *GroupNotificationSender) groupMemberDB2PB(member *relation.GroupMemberModel, appMangerLevel int32) *sdkws.GroupMemberFullInfo { + return &sdkws.GroupMemberFullInfo{ + GroupID: member.GroupID, + UserID: member.UserID, + RoleLevel: member.RoleLevel, + JoinTime: member.JoinTime.UnixMilli(), + Nickname: member.Nickname, + FaceURL: member.FaceURL, + AppMangerLevel: appMangerLevel, + JoinSource: member.JoinSource, + OperatorUserID: member.OperatorUserID, + Ex: member.Ex, + MuteEndTime: member.MuteEndTime.UnixMilli(), + InviterUserID: member.InviterUserID, + } +} + func (g *GroupNotificationSender) getGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { gm, err := g.db.TakeGroup(ctx, groupID) if err != nil { @@ -81,6 +99,7 @@ func (g *GroupNotificationSender) getGroupInfo(ctx context.Context, groupID stri if err != nil { return nil, err } + return &sdkws.GroupInfo{ GroupID: gm.GroupID, GroupName: gm.GroupName, @@ -148,6 +167,7 @@ func (g *GroupNotificationSender) getGroupMemberMap(ctx context.Context, groupID for i, member := range members { m[member.UserID] = members[i] } + return m, nil } @@ -159,6 +179,7 @@ func (g *GroupNotificationSender) getGroupMember(ctx context.Context, groupID st if len(members) == 0 { return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("group %s member %s not found", groupID, userID)) } + return members[0], nil } @@ -168,46 +189,8 @@ func (g *GroupNotificationSender) getGroupOwnerAndAdminUserID(ctx context.Contex return nil, err } fn := func(e *relation.GroupMemberModel) string { return e.UserID } - return utils.Slice(members, fn), nil -} - -func (g *GroupNotificationSender) groupDB2PB(group *relation.GroupModel, ownerUserID string, memberCount uint32) *sdkws.GroupInfo { - return &sdkws.GroupInfo{ - GroupID: group.GroupID, - GroupName: group.GroupName, - Notification: group.Notification, - Introduction: group.Introduction, - FaceURL: group.FaceURL, - OwnerUserID: ownerUserID, - CreateTime: group.CreateTime.UnixMilli(), - MemberCount: memberCount, - Ex: group.Ex, - Status: group.Status, - CreatorUserID: group.CreatorUserID, - GroupType: group.GroupType, - NeedVerification: group.NeedVerification, - LookMemberInfo: group.LookMemberInfo, - ApplyMemberFriend: group.ApplyMemberFriend, - NotificationUpdateTime: group.NotificationUpdateTime.UnixMilli(), - NotificationUserID: group.NotificationUserID, - } -} -func (g *GroupNotificationSender) groupMemberDB2PB(member *relation.GroupMemberModel, appMangerLevel int32) *sdkws.GroupMemberFullInfo { - return &sdkws.GroupMemberFullInfo{ - GroupID: member.GroupID, - UserID: member.UserID, - RoleLevel: member.RoleLevel, - JoinTime: member.JoinTime.UnixMilli(), - Nickname: member.Nickname, - FaceURL: member.FaceURL, - AppMangerLevel: appMangerLevel, - JoinSource: member.JoinSource, - OperatorUserID: member.OperatorUserID, - Ex: member.Ex, - MuteEndTime: member.MuteEndTime.UnixMilli(), - InviterUserID: member.InviterUserID, - } + return utils.Slice(members, fn), nil } func (g *GroupNotificationSender) getUsersInfoMap(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error) { @@ -219,6 +202,7 @@ func (g *GroupNotificationSender) getUsersInfoMap(ctx context.Context, userIDs [ for _, user := range users { result[user.GetUserID()] = user.(*sdkws.UserInfo) } + return result, nil } @@ -236,21 +220,31 @@ func (g *GroupNotificationSender) fillOpUser(ctx context.Context, opUser **sdkws return nil } userID := mcontext.GetOpUserID(ctx) - if groupID != "" { + getOpUser := func(g *GroupNotificationSender, groupID, userID string) (opUser *sdkws.GroupMemberFullInfo, err error) { if authverify.IsManagerUserID(userID) { - *opUser = &sdkws.GroupMemberFullInfo{ + opUser = &sdkws.GroupMemberFullInfo{ GroupID: groupID, UserID: userID, RoleLevel: constant.GroupAdmin, AppMangerLevel: constant.AppAdmin, } - } else { - member, err := g.db.TakeGroupMember(ctx, groupID, userID) - if err == nil { - *opUser = g.groupMemberDB2PB(member, 0) - } else if !errs.ErrRecordNotFound.Is(err) { - return err - } + + return opUser, nil + } + var member *relation.GroupMemberModel + member, err = g.db.TakeGroupMember(ctx, groupID, userID) + if err == nil { + opUser = g.groupMemberDB2PB(member, 0) + } else if !errs.ErrRecordNotFound.Is(err) { + return nil, err + } + + return opUser, nil + } + if groupID != "" { + *opUser, err = getOpUser(g, groupID, userID) + if err != nil { + return err } } user, err := g.getUser(ctx, userID) @@ -273,6 +267,7 @@ func (g *GroupNotificationSender) fillOpUser(ctx context.Context, opUser **sdkws (*opUser).FaceURL = user.FaceURL } } + return nil } @@ -286,6 +281,7 @@ func (g *GroupNotificationSender) GroupCreatedNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupCreatedNotification, tips) } @@ -299,6 +295,7 @@ func (g *GroupNotificationSender) GroupInfoSetNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetNotification, tips, rpcclient.WithRpcGetUserName()) } @@ -312,6 +309,7 @@ func (g *GroupNotificationSender) GroupInfoSetNameNotification(ctx context.Conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetNameNotification, tips) } @@ -325,6 +323,7 @@ func (g *GroupNotificationSender) GroupInfoSetAnnouncementNotification(ctx conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetAnnouncementNotification, tips, rpcclient.WithRpcGetUserName()) } @@ -355,6 +354,7 @@ func (g *GroupNotificationSender) JoinGroupApplicationNotification(ctx context.C log.ZError(ctx, "JoinGroupApplicationNotification failed", err, "group", req.GroupID, "userID", userID) } } + return nil } @@ -370,6 +370,7 @@ func (g *GroupNotificationSender) MemberQuitNotification(ctx context.Context, me return err } tips := &sdkws.MemberQuitTips{Group: group, QuitUser: member} + return g.Notification(ctx, mcontext.GetOpUserID(ctx), member.GroupID, constant.MemberQuitNotification, tips) } @@ -389,7 +390,8 @@ func (g *GroupNotificationSender) GroupApplicationAcceptedNotification(ctx conte return err } tips := &sdkws.GroupApplicationAcceptedTips{Group: group, HandleMsg: req.HandledMsg, ReceiverAs: 1} - if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { + err = g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID) + if err != nil { return err } for _, userID := range append(userIDs, mcontext.GetOpUserID(ctx)) { @@ -398,6 +400,7 @@ func (g *GroupNotificationSender) GroupApplicationAcceptedNotification(ctx conte log.ZError(ctx, "failed", err) } } + return nil } @@ -417,7 +420,8 @@ func (g *GroupNotificationSender) GroupApplicationRejectedNotification(ctx conte return err } tips := &sdkws.GroupApplicationRejectedTips{Group: group, HandleMsg: req.HandledMsg} - if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { + err = g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID) + if err != nil { return err } for _, userID := range append(userIDs, mcontext.GetOpUserID(ctx)) { @@ -426,6 +430,7 @@ func (g *GroupNotificationSender) GroupApplicationRejectedNotification(ctx conte log.ZError(ctx, "failed", err) } } + return nil } @@ -449,6 +454,7 @@ func (g *GroupNotificationSender) GroupOwnerTransferredNotification(ctx context. if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupOwnerTransferredNotification, tips) } @@ -462,6 +468,7 @@ func (g *GroupNotificationSender) MemberKickedNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.MemberKickedNotification, tips) } @@ -487,6 +494,7 @@ func (g *GroupNotificationSender) MemberInvitedNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.MemberInvitedNotification, tips) } @@ -506,6 +514,7 @@ func (g *GroupNotificationSender) MemberEnterNotification(ctx context.Context, g return err } tips := &sdkws.MemberEnterTips{Group: group, EntrantUser: user} + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.MemberEnterNotification, tips) } @@ -519,6 +528,7 @@ func (g *GroupNotificationSender) GroupDismissedNotification(ctx context.Context if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupDismissedNotification, tips) } @@ -544,6 +554,7 @@ func (g *GroupNotificationSender) GroupMemberMutedNotification(ctx context.Conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberMutedNotification, tips) } @@ -566,6 +577,7 @@ func (g *GroupNotificationSender) GroupMemberCancelMutedNotification(ctx context if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberCancelMutedNotification, tips) } @@ -591,6 +603,7 @@ func (g *GroupNotificationSender) GroupMutedNotification(ctx context.Context, gr if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMutedNotification, tips) } @@ -616,6 +629,7 @@ func (g *GroupNotificationSender) GroupCancelMutedNotification(ctx context.Conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupCancelMutedNotification, tips) } @@ -638,6 +652,7 @@ func (g *GroupNotificationSender) GroupMemberInfoSetNotification(ctx context.Con if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberInfoSetNotification, tips) } @@ -660,6 +675,7 @@ func (g *GroupNotificationSender) GroupMemberSetToAdminNotification(ctx context. if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberSetToAdminNotification, tips) } @@ -682,6 +698,7 @@ func (g *GroupNotificationSender) GroupMemberSetToOrdinaryUserNotification(ctx c if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } + return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberSetToOrdinaryUserNotification, tips) } @@ -693,5 +710,6 @@ func (g *GroupNotificationSender) SuperGroupNotification(ctx context.Context, se } }() err = g.Notification(ctx, sendID, recvID, constant.SuperGroupUpdateNotification, nil) + return err } diff --git a/pkg/rpcclient/notification/msg.go b/pkg/rpcclient/notification/msg.go index 60fa64f40..6e367ac04 100644 --- a/pkg/rpcclient/notification/msg.go +++ b/pkg/rpcclient/notification/msg.go @@ -37,6 +37,7 @@ func (m *MsgNotificationSender) UserDeleteMsgsNotification(ctx context.Context, ConversationID: conversationID, Seqs: seqs, } + return m.Notification(ctx, userID, userID, constant.DeleteMsgsNotification, &tips) } @@ -47,5 +48,6 @@ func (m *MsgNotificationSender) MarkAsReadNotification(ctx context.Context, conv Seqs: seqs, HasReadSeq: hasReadSeq, } + return m.NotificationWithSesstionType(ctx, sendID, recvID, constant.HasReadReceipt, sesstionType, tips) } diff --git a/pkg/rpcclient/notification/user.go b/pkg/rpcclient/notification/user.go index 4feebf7b9..f6e592d18 100644 --- a/pkg/rpcclient/notification/user.go +++ b/pkg/rpcclient/notification/user.go @@ -52,6 +52,7 @@ func WithUserFunc( for _, user := range users { result = append(result, user) } + return result, nil } u.getUsersInfo = f @@ -68,34 +69,37 @@ func NewUserNotificationSender( for _, opt := range opts { opt(f) } + return f } -func (u *UserNotificationSender) getUsersInfoMap( - ctx context.Context, - userIDs []string, -) (map[string]*sdkws.UserInfo, error) { - users, err := u.getUsersInfo(ctx, userIDs) - if err != nil { - return nil, err - } - result := make(map[string]*sdkws.UserInfo) - for _, user := range users { - result[user.GetUserID()] = user.(*sdkws.UserInfo) - } - return result, nil -} +// func (u *UserNotificationSender) getUsersInfoMap( +// ctx context.Context, +// userIDs []string, +// ) (map[string]*sdkws.UserInfo, error) { +// users, err := u.getUsersInfo(ctx, userIDs) +// if err != nil { +// return nil, err +// } +// result := make(map[string]*sdkws.UserInfo) +// for _, user := range users { +// result[user.GetUserID()] = user.(*sdkws.UserInfo) +// } -func (u *UserNotificationSender) getFromToUserNickname( - ctx context.Context, - fromUserID, toUserID string, -) (string, string, error) { - users, err := u.getUsersInfoMap(ctx, []string{fromUserID, toUserID}) - if err != nil { - return "", "", nil - } - return users[fromUserID].Nickname, users[toUserID].Nickname, nil -} +// return result, nil +// } + +// func (u *UserNotificationSender) getFromToUserNickname( +// ctx context.Context, +// fromUserID, toUserID string, +// ) (string, string, error) { +// users, err := u.getUsersInfoMap(ctx, []string{fromUserID, toUserID}) +// if err != nil { +// return "", "", err +// } + +// return users[fromUserID].Nickname, users[toUserID].Nickname, nil +// } func (u *UserNotificationSender) UserStatusChangeNotification( ctx context.Context, diff --git a/pkg/rpcclient/push.go b/pkg/rpcclient/push.go index 6d0876972..7733572bf 100644 --- a/pkg/rpcclient/push.go +++ b/pkg/rpcclient/push.go @@ -36,6 +36,7 @@ func NewPush(discov discoveryregistry.SvcDiscoveryRegistry) *Push { if err != nil { panic(err) } + return &Push{ discov: discov, conn: conn, diff --git a/pkg/rpcclient/third.go b/pkg/rpcclient/third.go old mode 100644 new mode 100755 index 48a537112..2bb761450 --- a/pkg/rpcclient/third.go +++ b/pkg/rpcclient/third.go @@ -42,14 +42,13 @@ func NewThird(discov discoveryregistry.SvcDiscoveryRegistry) *Third { panic(err) } client := third.NewThirdClient(conn) - minioClient, err := minioInit() + minioClient, _ := minioInit() + return &Third{discov: discov, Client: client, conn: conn, MinioClient: minioClient} } func minioInit() (*minio.Client, error) { - minioClient := &minio.Client{} - var initUrl string - initUrl = config.Config.Object.Minio.Endpoint + initUrl := config.Config.Object.Minio.Endpoint minioUrl, err := url.Parse(initUrl) if err != nil { return nil, err @@ -63,9 +62,11 @@ func minioInit() (*minio.Client, error) { } else if minioUrl.Scheme == "https" { opts.Secure = true } + var minioClient *minio.Client minioClient, err = minio.New(minioUrl.Host, opts) if err != nil { return nil, err } + return minioClient, nil } diff --git a/pkg/rpcclient/user.go b/pkg/rpcclient/user.go index c40d95727..dfd93fb0b 100644 --- a/pkg/rpcclient/user.go +++ b/pkg/rpcclient/user.go @@ -45,6 +45,7 @@ func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User { panic(err) } client := user.NewUserClient(conn) + return &User{Discov: discov, Client: client, conn: conn} } @@ -54,6 +55,7 @@ type UserRpcClient User // NewUserRpcClientByUser initializes a UserRpcClient based on the provided User instance. func NewUserRpcClientByUser(user *User) *UserRpcClient { rpc := UserRpcClient(*user) + return &rpc } @@ -75,6 +77,7 @@ func (u *UserRpcClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]* })); len(ids) > 0 { return nil, errs.ErrUserIDNotFound.Wrap(strings.Join(ids, ",")) } + return resp.UsersInfo, nil } @@ -84,6 +87,7 @@ func (u *UserRpcClient) GetUserInfo(ctx context.Context, userID string) (*sdkws. if err != nil { return nil, err } + return users[0], nil } @@ -93,6 +97,7 @@ func (u *UserRpcClient) GetUsersInfoMap(ctx context.Context, userIDs []string) ( if err != nil { return nil, err } + return utils.SliceToMap(users, func(e *sdkws.UserInfo) string { return e.UserID }), nil @@ -108,6 +113,7 @@ func (u *UserRpcClient) GetPublicUserInfos( if err != nil { return nil, err } + return utils.Slice(users, func(e *sdkws.UserInfo) *sdkws.PublicUserInfo { return &sdkws.PublicUserInfo{ UserID: e.UserID, @@ -124,6 +130,7 @@ func (u *UserRpcClient) GetPublicUserInfo(ctx context.Context, userID string) (* if err != nil { return nil, err } + return users[0], nil } @@ -137,6 +144,7 @@ func (u *UserRpcClient) GetPublicUserInfoMap( if err != nil { return nil, err } + return utils.SliceToMap(users, func(e *sdkws.PublicUserInfo) string { return e.UserID }), nil @@ -150,6 +158,7 @@ func (u *UserRpcClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID stri if err != nil { return 0, err } + return resp.GlobalRecvMsgOpt, nil } @@ -159,6 +168,7 @@ func (u *UserRpcClient) Access(ctx context.Context, ownerUserID string) error { if err != nil { return err } + return authverify.CheckAccessV3(ctx, ownerUserID) } @@ -168,6 +178,7 @@ func (u *UserRpcClient) GetAllUserIDs(ctx context.Context, pageNumber, showNumbe if err != nil { return nil, err } + return resp.UserIDs, nil } @@ -177,5 +188,6 @@ func (u *UserRpcClient) SetUserStatus(ctx context.Context, userID string, status UserID: userID, Status: status, PlatformID: int32(platformID), }) + return err } diff --git a/pkg/statistics/statistics.go b/pkg/statistics/statistics.go index de6d04fec..080933c73 100644 --- a/pkg/statistics/statistics.go +++ b/pkg/statistics/statistics.go @@ -36,9 +36,8 @@ func (s *Statistics) output() { var timeIntervalNum uint64 for { sum = *s.AllCount - select { - case <-t.C: - } + <-t.C + if *s.AllCount-sum <= 0 { intervalCount = 0 } else { @@ -66,5 +65,6 @@ func (s *Statistics) output() { func NewStatistics(allCount *uint64, moduleName, printArgs string, sleepTime int) *Statistics { p := &Statistics{AllCount: allCount, ModuleName: moduleName, SleepTime: uint64(sleepTime), PrintArgs: printArgs} go p.output() + return p } From 6ffcfbe68e358191407147dd257786cef68192e3 Mon Sep 17 00:00:00 2001 From: withchao <48119764+withchao@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:24:22 -0500 Subject: [PATCH 17/21] fix: not support redis cluster. CROSSSLOT Keys in request don't hash to the same slot (#1258) * feat: v2 to v3 data conversion * feat: v2 to v3 data conversion * fix: CallbackBeforeCreateGroup * fix: NotificationUserInfoUpdate * fix: NotificationUserInfoUpdate * chore: update pkg github.com/OpenIMSDK/protocol v0.0.26 * chore: code format * update pkg * feat: QuitGroup support administrator operations * feat: QuitGroup support administrator operations * fix: checkMongo uri * fix: k8s minio prefix * fix: k8s minio prefix * fix: k8s minio prefix * fix: k8s minio prefix test * fix: k8s minio prefix test * fix: GetUsersInfo cache * fix: redis cache * fix: redis remove pipeline * fix: redis remove pipeline --- pkg/common/db/cache/black.go | 19 +- pkg/common/db/cache/conversation.go | 266 ++++++---------- pkg/common/db/cache/friend.go | 40 +-- pkg/common/db/cache/group.go | 266 ++++++---------- pkg/common/db/cache/meta_cache.go | 229 +++++++------- pkg/common/db/cache/msg.go | 374 +++++++++++++---------- pkg/common/db/cache/user.go | 49 +-- pkg/common/db/controller/conversation.go | 8 +- 8 files changed, 550 insertions(+), 701 deletions(-) diff --git a/pkg/common/db/cache/black.go b/pkg/common/db/cache/black.go index d1abe945c..5a70097ed 100644 --- a/pkg/common/db/cache/black.go +++ b/pkg/common/db/cache/black.go @@ -62,12 +62,7 @@ func NewBlackCacheRedis( } func (b *BlackCacheRedis) NewCache() BlackCache { - return &BlackCacheRedis{ - expireTime: b.expireTime, - rcClient: b.rcClient, - blackDB: b.blackDB, - metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...), - } + return &BlackCacheRedis{expireTime: b.expireTime, rcClient: b.rcClient, blackDB: b.blackDB, metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...)} } func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string { @@ -75,15 +70,9 @@ func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string { } func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) { - return getCache( - ctx, - b.rcClient, - b.getBlackIDsKey(userID), - b.expireTime, - func(ctx context.Context) ([]string, error) { - return b.blackDB.FindBlackUserIDs(ctx, userID) - }, - ) + return getCache(ctx, b.rcClient, b.getBlackIDsKey(userID), b.expireTime, func(ctx context.Context) ([]string, error) { + return b.blackDB.FindBlackUserIDs(ctx, userID) + }) } func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache { diff --git a/pkg/common/db/cache/conversation.go b/pkg/common/db/cache/conversation.go index 890552160..9c0bcfae4 100644 --- a/pkg/common/db/cache/conversation.go +++ b/pkg/common/db/cache/conversation.go @@ -73,7 +73,7 @@ type ConversationCache interface { GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint64, err error) DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID string) ConversationCache - GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) + //GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) DelUserAllHasReadSeqs(ownerUserID string, conversationIDs ...string) ConversationCache GetConversationsByConversationID(ctx context.Context, @@ -83,11 +83,7 @@ type ConversationCache interface { DelConversationNotReceiveMessageUserIDs(conversationIDs ...string) ConversationCache } -func NewConversationRedis( - rdb redis.UniversalClient, - opts rockscache.Options, - db relationtb.ConversationModelInterface, -) ConversationCache { +func NewConversationRedis(rdb redis.UniversalClient, opts rockscache.Options, db relationtb.ConversationModelInterface) ConversationCache { rcClient := rockscache.NewClient(rdb, opts) return &ConversationRedisCache{ @@ -158,15 +154,9 @@ func (c *ConversationRedisCache) getConversationNotReceiveMessageUserIDsKey(conv } func (c *ConversationRedisCache) GetUserConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) { - return getCache( - ctx, - c.rcClient, - c.getConversationIDsKey(ownerUserID), - c.expireTime, - func(ctx context.Context) ([]string, error) { - return c.conversationDB.FindUserIDAllConversationID(ctx, ownerUserID) - }, - ) + return getCache(ctx, c.rcClient, c.getConversationIDsKey(ownerUserID), c.expireTime, func(ctx context.Context) ([]string, error) { + return c.conversationDB.FindUserIDAllConversationID(ctx, ownerUserID) + }) } func (c *ConversationRedisCache) DelConversationIDs(userIDs ...string) ConversationCache { @@ -184,10 +174,7 @@ func (c *ConversationRedisCache) getUserConversationIDsHashKey(ownerUserID strin return conversationIDsHashKey + ownerUserID } -func (c *ConversationRedisCache) GetUserConversationIDsHash( - ctx context.Context, - ownerUserID string, -) (hash uint64, err error) { +func (c *ConversationRedisCache) GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error) { return getCache( ctx, c.rcClient, @@ -201,7 +188,6 @@ func (c *ConversationRedisCache) GetUserConversationIDsHash( utils.Sort(conversationIDs, true) bi := big.NewInt(0) bi.SetString(utils.Md5(strings.Join(conversationIDs, ";"))[0:8], 16) - return bi.Uint64(), nil }, ) @@ -218,19 +204,10 @@ func (c *ConversationRedisCache) DelUserConversationIDsHash(ownerUserIDs ...stri return cache } -func (c *ConversationRedisCache) GetConversation( - ctx context.Context, - ownerUserID, conversationID string, -) (*relationtb.ConversationModel, error) { - return getCache( - ctx, - c.rcClient, - c.getConversationKey(ownerUserID, conversationID), - c.expireTime, - func(ctx context.Context) (*relationtb.ConversationModel, error) { - return c.conversationDB.Take(ctx, ownerUserID, conversationID) - }, - ) +func (c *ConversationRedisCache) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*relationtb.ConversationModel, error) { + return getCache(ctx, c.rcClient, c.getConversationKey(ownerUserID, conversationID), c.expireTime, func(ctx context.Context) (*relationtb.ConversationModel, error) { + return c.conversationDB.Take(ctx, ownerUserID, conversationID) + }) } func (c *ConversationRedisCache) DelConversations(ownerUserID string, conversationIDs ...string) ConversationCache { @@ -244,10 +221,7 @@ func (c *ConversationRedisCache) DelConversations(ownerUserID string, conversati return cache } -func (c *ConversationRedisCache) getConversationIndex( - convsation *relationtb.ConversationModel, - keys []string, -) (int, error) { +func (c *ConversationRedisCache) getConversationIndex(convsation *relationtb.ConversationModel, keys []string) (int, error) { key := c.getConversationKey(convsation.OwnerUserID, convsation.ConversationID) for _i, _key := range keys { if _key == key { @@ -258,81 +232,60 @@ func (c *ConversationRedisCache) getConversationIndex( return 0, errors.New("not found key:" + key + " in keys") } -func (c *ConversationRedisCache) GetConversations( - ctx context.Context, - ownerUserID string, - conversationIDs []string, -) ([]*relationtb.ConversationModel, error) { - keys := make([]string, 0, len(conversationIDs)) - for _, conversarionID := range conversationIDs { - keys = append(keys, c.getConversationKey(ownerUserID, conversarionID)) - } - - return batchGetCache( - ctx, - c.rcClient, - keys, - c.expireTime, - c.getConversationIndex, - func(ctx context.Context) ([]*relationtb.ConversationModel, error) { - return c.conversationDB.Find(ctx, ownerUserID, conversationIDs) - }, - ) -} - -func (c *ConversationRedisCache) GetUserAllConversations( - ctx context.Context, - ownerUserID string, -) ([]*relationtb.ConversationModel, error) { +func (c *ConversationRedisCache) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationtb.ConversationModel, error) { + //var keys []string + //for _, conversarionID := range conversationIDs { + // keys = append(keys, c.getConversationKey(ownerUserID, conversarionID)) + //} + //return batchGetCache( + // ctx, + // c.rcClient, + // keys, + // c.expireTime, + // c.getConversationIndex, + // func(ctx context.Context) ([]*relationtb.ConversationModel, error) { + // return c.conversationDB.Find(ctx, ownerUserID, conversationIDs) + // }, + //) + return batchGetCache2(ctx, c.rcClient, c.expireTime, conversationIDs, func(conversationID string) string { + return c.getConversationKey(ownerUserID, conversationID) + }, func(ctx context.Context, conversationID string) (*relationtb.ConversationModel, error) { + return c.conversationDB.Take(ctx, ownerUserID, conversationID) + }) +} + +func (c *ConversationRedisCache) GetUserAllConversations(ctx context.Context, ownerUserID string) ([]*relationtb.ConversationModel, error) { conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID) if err != nil { return nil, err } - keys := make([]string, 0, len(conversationIDs)) - for _, conversarionID := range conversationIDs { - keys = append(keys, c.getConversationKey(ownerUserID, conversarionID)) - } - - return batchGetCache( - ctx, - c.rcClient, - keys, - c.expireTime, - c.getConversationIndex, - func(ctx context.Context) ([]*relationtb.ConversationModel, error) { - return c.conversationDB.FindUserIDAllConversations(ctx, ownerUserID) - }, - ) -} - -func (c *ConversationRedisCache) GetUserRecvMsgOpt( - ctx context.Context, - ownerUserID, conversationID string, -) (opt int, err error) { - return getCache( - ctx, - c.rcClient, - c.getRecvMsgOptKey(ownerUserID, conversationID), - c.expireTime, - func(ctx context.Context) (opt int, err error) { - return c.conversationDB.GetUserRecvMsgOpt(ctx, ownerUserID, conversationID) - }, - ) -} - -func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDs( - ctx context.Context, - groupID string, -) (userIDs []string, err error) { - return getCache( - ctx, - c.rcClient, - c.getSuperGroupRecvNotNotifyUserIDsKey(groupID), - c.expireTime, - func(ctx context.Context) (userIDs []string, err error) { - return c.conversationDB.FindSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID) - }, - ) + //var keys []string + //for _, conversarionID := range conversationIDs { + // keys = append(keys, c.getConversationKey(ownerUserID, conversarionID)) + //} + //return batchGetCache( + // ctx, + // c.rcClient, + // keys, + // c.expireTime, + // c.getConversationIndex, + // func(ctx context.Context) ([]*relationtb.ConversationModel, error) { + // return c.conversationDB.FindUserIDAllConversations(ctx, ownerUserID) + // }, + //) + return c.GetConversations(ctx, ownerUserID, conversationIDs) +} + +func (c *ConversationRedisCache) GetUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) (opt int, err error) { + return getCache(ctx, c.rcClient, c.getRecvMsgOptKey(ownerUserID, conversationID), c.expireTime, func(ctx context.Context) (opt int, err error) { + return c.conversationDB.GetUserRecvMsgOpt(ctx, ownerUserID, conversationID) + }) +} + +func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) (userIDs []string, err error) { + return getCache(ctx, c.rcClient, c.getSuperGroupRecvNotNotifyUserIDsKey(groupID), c.expireTime, func(ctx context.Context) (userIDs []string, err error) { + return c.conversationDB.FindSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID) + }) } func (c *ConversationRedisCache) DelUsersConversation(conversationID string, ownerUserIDs ...string) ConversationCache { @@ -360,26 +313,17 @@ func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDs(groupID st return cache } -func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash( - ctx context.Context, - groupID string, -) (hash uint64, err error) { - return getCache( - ctx, - c.rcClient, - c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID), - c.expireTime, - func(ctx context.Context) (hash uint64, err error) { - userIDs, err := c.GetSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID) - if err != nil { - return 0, err - } - utils.Sort(userIDs, true) - bi := big.NewInt(0) - bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16) - - return bi.Uint64(), nil - }, +func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint64, err error) { + return getCache(ctx, c.rcClient, c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID), c.expireTime, func(ctx context.Context) (hash uint64, err error) { + userIDs, err := c.GetSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID) + if err != nil { + return 0, err + } + utils.Sort(userIDs, true) + bi := big.NewInt(0) + bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16) + return bi.Uint64(), nil + }, ) } @@ -390,10 +334,7 @@ func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupI return cache } -func (c *ConversationRedisCache) getUserAllHasReadSeqsIndex( - conversationID string, - conversationIDs []string, -) (int, error) { +func (c *ConversationRedisCache) getUserAllHasReadSeqsIndex(conversationID string, conversationIDs []string) (int, error) { for _i, _conversationID := range conversationIDs { if _conversationID == conversationID { return _i, nil @@ -403,35 +344,21 @@ func (c *ConversationRedisCache) getUserAllHasReadSeqsIndex( return 0, errors.New("not found key:" + conversationID + " in keys") } -func (c *ConversationRedisCache) GetUserAllHasReadSeqs( - ctx context.Context, - ownerUserID string, -) (map[string]int64, error) { - conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID) - if err != nil { - return nil, err - } - keys := make([]string, 0, len(conversationIDs)) - for _, conversarionID := range conversationIDs { - keys = append(keys, c.getConversationHasReadSeqKey(ownerUserID, conversarionID)) - } - - return batchGetCacheMap( - ctx, - c.rcClient, - keys, - conversationIDs, - c.expireTime, - c.getUserAllHasReadSeqsIndex, - func(ctx context.Context) (map[string]int64, error) { - return c.conversationDB.GetUserAllHasReadSeqs(ctx, ownerUserID) - }, - ) -} - -func (c *ConversationRedisCache) DelUserAllHasReadSeqs(ownerUserID string, - conversationIDs ...string, -) ConversationCache { +//func (c *ConversationRedisCache) GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) { +// conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID) +// if err != nil { +// return nil, err +// } +// var keys []string +// for _, conversarionID := range conversationIDs { +// keys = append(keys, c.getConversationHasReadSeqKey(ownerUserID, conversarionID)) +// } +// return batchGetCacheMap(ctx, c.rcClient, keys, conversationIDs, c.expireTime, c.getUserAllHasReadSeqsIndex, func(ctx context.Context) (map[string]int64, error) { +// return c.conversationDB.GetUserAllHasReadSeqs(ctx, ownerUserID) +// }) +//} + +func (c *ConversationRedisCache) DelUserAllHasReadSeqs(ownerUserID string, conversationIDs ...string) ConversationCache { cache := c.NewCache() for _, conversationID := range conversationIDs { cache.AddKeys(c.getConversationHasReadSeqKey(ownerUserID, conversationID)) @@ -440,10 +367,7 @@ func (c *ConversationRedisCache) DelUserAllHasReadSeqs(ownerUserID string, return cache } -func (c *ConversationRedisCache) GetConversationsByConversationID( - ctx context.Context, - conversationIDs []string, -) ([]*relationtb.ConversationModel, error) { +func (c *ConversationRedisCache) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error) { panic("implement me") } @@ -452,15 +376,9 @@ func (c *ConversationRedisCache) DelConversationByConversationID(conversationIDs } func (c *ConversationRedisCache) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) { - return getCache( - ctx, - c.rcClient, - c.getConversationNotReceiveMessageUserIDsKey(conversationID), - c.expireTime, - func(ctx context.Context) ([]string, error) { - return c.conversationDB.GetConversationNotReceiveMessageUserIDs(ctx, conversationID) - }, - ) + return getCache(ctx, c.rcClient, c.getConversationNotReceiveMessageUserIDsKey(conversationID), c.expireTime, func(ctx context.Context) ([]string, error) { + return c.conversationDB.GetConversationNotReceiveMessageUserIDs(ctx, conversationID) + }) } func (c *ConversationRedisCache) DelConversationNotReceiveMessageUserIDs(conversationIDs ...string) ConversationCache { diff --git a/pkg/common/db/cache/friend.go b/pkg/common/db/cache/friend.go index 37f5b0a98..28bb971b4 100644 --- a/pkg/common/db/cache/friend.go +++ b/pkg/common/db/cache/friend.go @@ -53,11 +53,7 @@ type FriendCacheRedis struct { rcClient *rockscache.Client } -func NewFriendCacheRedis( - rdb redis.UniversalClient, - friendDB relationtb.FriendModelInterface, - options rockscache.Options, -) FriendCache { +func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface, options rockscache.Options) FriendCache { rcClient := rockscache.NewClient(rdb, options) return &FriendCacheRedis{ @@ -90,15 +86,9 @@ func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string } func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) { - return getCache( - ctx, - f.rcClient, - f.getFriendIDsKey(ownerUserID), - f.expireTime, - func(ctx context.Context) ([]string, error) { - return f.friendDB.FindFriendUserIDs(ctx, ownerUserID) - }, - ) + return getCache(ctx, f.rcClient, f.getFriendIDsKey(ownerUserID), f.expireTime, func(ctx context.Context) ([]string, error) { + return f.friendDB.FindFriendUserIDs(ctx, ownerUserID) + }) } func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache { @@ -113,10 +103,7 @@ func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache { } // todo. -func (f *FriendCacheRedis) GetTwoWayFriendIDs( - ctx context.Context, - ownerUserID string, -) (twoWayFriendIDs []string, err error) { +func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) { friendIDs, err := f.GetFriendIDs(ctx, ownerUserID) if err != nil { return nil, err @@ -141,19 +128,10 @@ func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID s return newFriendCache } -func (f *FriendCacheRedis) GetFriend( - ctx context.Context, - ownerUserID, friendUserID string, -) (friend *relationtb.FriendModel, err error) { - return getCache( - ctx, - f.rcClient, - f.getFriendKey(ownerUserID, friendUserID), - f.expireTime, - func(ctx context.Context) (*relationtb.FriendModel, error) { - return f.friendDB.Take(ctx, ownerUserID, friendUserID) - }, - ) +func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error) { + return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID, friendUserID), f.expireTime, func(ctx context.Context) (*relationtb.FriendModel, error) { + return f.friendDB.Take(ctx, ownerUserID, friendUserID) + }) } func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) FriendCache { diff --git a/pkg/common/db/cache/group.go b/pkg/common/db/cache/group.go index 0505241d0..d505772eb 100644 --- a/pkg/common/db/cache/group.go +++ b/pkg/common/db/cache/group.go @@ -65,22 +65,10 @@ type GroupCache interface { GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error) DelJoinedGroupID(userID ...string) GroupCache - GetGroupMemberInfo( - ctx context.Context, - groupID, userID string, - ) (groupMember *relationtb.GroupMemberModel, err error) - GetGroupMembersInfo( - ctx context.Context, - groupID string, - userID []string, - ) (groupMembers []*relationtb.GroupMemberModel, err error) + GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relationtb.GroupMemberModel, err error) + GetGroupMembersInfo(ctx context.Context, groupID string, userID []string) (groupMembers []*relationtb.GroupMemberModel, err error) GetAllGroupMembersInfo(ctx context.Context, groupID string) (groupMembers []*relationtb.GroupMemberModel, err error) - GetGroupMembersPage( - ctx context.Context, - groupID string, - userID []string, - showNumber, pageNumber int32, - ) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) + GetGroupMembersPage(ctx context.Context, groupID string, userID []string, showNumber, pageNumber int32) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) DelGroupMembersInfo(groupID string, userID ...string) GroupCache @@ -186,37 +174,25 @@ func (g *GroupCacheRedis) GetGroupMemberIndex(groupMember *relationtb.GroupMembe } // / groupInfo. -func (g *GroupCacheRedis) GetGroupsInfo( - ctx context.Context, - groupIDs []string, -) (groups []*relationtb.GroupModel, err error) { - keys := make([]string, 0, len(groupIDs)) - for _, group := range groupIDs { - keys = append(keys, g.getGroupInfoKey(group)) - } - - return batchGetCache( - ctx, - g.rcClient, - keys, - g.expireTime, - g.GetGroupIndex, - func(ctx context.Context) ([]*relationtb.GroupModel, error) { - return g.groupDB.Find(ctx, groupIDs) - }, - ) +func (g *GroupCacheRedis) GetGroupsInfo(ctx context.Context, groupIDs []string) (groups []*relationtb.GroupModel, err error) { + //var keys []string + //for _, group := range groupIDs { + // keys = append(keys, g.getGroupInfoKey(group)) + //} + //return batchGetCache(ctx, g.rcClient, keys, g.expireTime, g.GetGroupIndex, func(ctx context.Context) ([]*relationtb.GroupModel, error) { + // return g.groupDB.Find(ctx, groupIDs) + //}) + return batchGetCache2(ctx, g.rcClient, g.expireTime, groupIDs, func(groupID string) string { + return g.getGroupInfoKey(groupID) + }, func(ctx context.Context, groupID string) (*relationtb.GroupModel, error) { + return g.groupDB.Take(ctx, groupID) + }) } func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (group *relationtb.GroupModel, err error) { - return getCache( - ctx, - g.rcClient, - g.getGroupInfoKey(groupID), - g.expireTime, - func(ctx context.Context) (*relationtb.GroupModel, error) { - return g.groupDB.Take(ctx, groupID) - }, - ) + return getCache(ctx, g.rcClient, g.getGroupInfoKey(groupID), g.expireTime, func(ctx context.Context) (*relationtb.GroupModel, error) { + return g.groupDB.Take(ctx, groupID) + }) } func (g *GroupCacheRedis) DelGroupsInfo(groupIDs ...string) GroupCache { @@ -230,53 +206,38 @@ func (g *GroupCacheRedis) DelGroupsInfo(groupIDs ...string) GroupCache { return newGroupCache } -func (g *GroupCacheRedis) GetJoinedSuperGroupIDs( - ctx context.Context, - userID string, -) (joinedSuperGroupIDs []string, err error) { - return getCache( - ctx, - g.rcClient, - g.getJoinedSuperGroupsIDKey(userID), - g.expireTime, - func(ctx context.Context) ([]string, error) { - userGroup, err := g.mongoDB.GetSuperGroupByUserID(ctx, userID) - if err != nil { - return nil, err - } - - return userGroup.GroupIDs, nil - }, +func (g *GroupCacheRedis) GetJoinedSuperGroupIDs(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error) { + return getCache(ctx, g.rcClient, g.getJoinedSuperGroupsIDKey(userID), g.expireTime, func(ctx context.Context) ([]string, error) { + userGroup, err := g.mongoDB.GetSuperGroupByUserID(ctx, userID) + if err != nil { + return nil, err + } + return userGroup.GroupIDs, nil + }, ) } -func (g *GroupCacheRedis) GetSuperGroupMemberIDs( - ctx context.Context, - groupIDs ...string, -) (models []*unrelationtb.SuperGroupModel, err error) { - keys := make([]string, 0, len(groupIDs)) - for _, group := range groupIDs { - keys = append(keys, g.getSuperGroupMemberIDsKey(group)) - } - - return batchGetCache( - ctx, - g.rcClient, - keys, - g.expireTime, - func(model *unrelationtb.SuperGroupModel, keys []string) (int, error) { - for i, key := range keys { - if g.getSuperGroupMemberIDsKey(model.GroupID) == key { - return i, nil - } - } - - return 0, errIndex - }, - func(ctx context.Context) ([]*unrelationtb.SuperGroupModel, error) { - return g.mongoDB.FindSuperGroup(ctx, groupIDs) - }, - ) +func (g *GroupCacheRedis) GetSuperGroupMemberIDs(ctx context.Context, groupIDs ...string) (models []*unrelationtb.SuperGroupModel, err error) { + //var keys []string + //for _, group := range groupIDs { + // keys = append(keys, g.getSuperGroupMemberIDsKey(group)) + //} + //return batchGetCache(ctx, g.rcClient, keys, g.expireTime, func(model *unrelationtb.SuperGroupModel, keys []string) (int, error) { + // for i, key := range keys { + // if g.getSuperGroupMemberIDsKey(model.GroupID) == key { + // return i, nil + // } + // } + // return 0, errIndex + //}, + // func(ctx context.Context) ([]*unrelationtb.SuperGroupModel, error) { + // return g.mongoDB.FindSuperGroup(ctx, groupIDs) + // }) + return batchGetCache2(ctx, g.rcClient, g.expireTime, groupIDs, func(groupID string) string { + return g.getSuperGroupMemberIDsKey(groupID) + }, func(ctx context.Context, groupID string) (*unrelationtb.SuperGroupModel, error) { + return g.mongoDB.TakeSuperGroup(ctx, groupID) + }) } // userJoinSuperGroup. @@ -361,10 +322,7 @@ func (g *GroupCacheRedis) GetGroupMembersHash(ctx context.Context, groupID strin //) } -func (g *GroupCacheRedis) GetGroupMemberHashMap( - ctx context.Context, - groupIDs []string, -) (map[string]*relationtb.GroupSimpleUserID, error) { +func (g *GroupCacheRedis) GetGroupMemberHashMap(ctx context.Context, groupIDs []string) (map[string]*relationtb.GroupSimpleUserID, error) { res := make(map[string]*relationtb.GroupSimpleUserID) for _, groupID := range groupIDs { hash, err := g.GetGroupMembersHash(ctx, groupID) @@ -391,15 +349,9 @@ func (g *GroupCacheRedis) DelGroupMembersHash(groupID string) GroupCache { // groupMemberIDs. func (g *GroupCacheRedis) GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) { - return getCache( - ctx, - g.rcClient, - g.getGroupMemberIDsKey(groupID), - g.expireTime, - func(ctx context.Context) ([]string, error) { - return g.groupMemberDB.FindMemberUserID(ctx, groupID) - }, - ) + return getCache(ctx, g.rcClient, g.getGroupMemberIDsKey(groupID), g.expireTime, func(ctx context.Context) ([]string, error) { + return g.groupMemberDB.FindMemberUserID(ctx, groupID) + }) } func (g *GroupCacheRedis) GetGroupsMemberIDs(ctx context.Context, groupIDs []string) (map[string][]string, error) { @@ -423,15 +375,9 @@ func (g *GroupCacheRedis) DelGroupMemberIDs(groupID string) GroupCache { } func (g *GroupCacheRedis) GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error) { - return getCache( - ctx, - g.rcClient, - g.getJoinedGroupsKey(userID), - g.expireTime, - func(ctx context.Context) ([]string, error) { - return g.groupMemberDB.FindUserJoinedGroupID(ctx, userID) - }, - ) + return getCache(ctx, g.rcClient, g.getJoinedGroupsKey(userID), g.expireTime, func(ctx context.Context) ([]string, error) { + return g.groupMemberDB.FindUserJoinedGroupID(ctx, userID) + }) } func (g *GroupCacheRedis) DelJoinedGroupID(userIDs ...string) GroupCache { @@ -445,49 +391,28 @@ func (g *GroupCacheRedis) DelJoinedGroupID(userIDs ...string) GroupCache { return cache } -func (g *GroupCacheRedis) GetGroupMemberInfo( - ctx context.Context, - groupID, userID string, -) (groupMember *relationtb.GroupMemberModel, err error) { - return getCache( - ctx, - g.rcClient, - g.getGroupMemberInfoKey(groupID, userID), - g.expireTime, - func(ctx context.Context) (*relationtb.GroupMemberModel, error) { - return g.groupMemberDB.Take(ctx, groupID, userID) - }, - ) +func (g *GroupCacheRedis) GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relationtb.GroupMemberModel, err error) { + return getCache(ctx, g.rcClient, g.getGroupMemberInfoKey(groupID, userID), g.expireTime, func(ctx context.Context) (*relationtb.GroupMemberModel, error) { + return g.groupMemberDB.Take(ctx, groupID, userID) + }) } -func (g *GroupCacheRedis) GetGroupMembersInfo( - ctx context.Context, - groupID string, - userIDs []string, -) ([]*relationtb.GroupMemberModel, error) { - keys := make([]string, 0, len(userIDs)) - for _, userID := range userIDs { - keys = append(keys, g.getGroupMemberInfoKey(groupID, userID)) - } - - return batchGetCache( - ctx, - g.rcClient, - keys, - g.expireTime, - g.GetGroupMemberIndex, - func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) { - return g.groupMemberDB.Find(ctx, []string{groupID}, userIDs, nil) - }, - ) +func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, groupID string, userIDs []string) ([]*relationtb.GroupMemberModel, error) { + //var keys []string + //for _, userID := range userIDs { + // keys = append(keys, g.getGroupMemberInfoKey(groupID, userID)) + //} + //return batchGetCache(ctx, g.rcClient, keys, g.expireTime, g.GetGroupMemberIndex, func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) { + // return g.groupMemberDB.Find(ctx, []string{groupID}, userIDs, nil) + //}) + return batchGetCache2(ctx, g.rcClient, g.expireTime, userIDs, func(userID string) string { + return g.getGroupMemberInfoKey(groupID, userID) + }, func(ctx context.Context, userID string) (*relationtb.GroupMemberModel, error) { + return g.groupMemberDB.Take(ctx, groupID, userID) + }) } -func (g *GroupCacheRedis) GetGroupMembersPage( - ctx context.Context, - groupID string, - userIDs []string, - showNumber, pageNumber int32, -) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) { +func (g *GroupCacheRedis) GetGroupMembersPage(ctx context.Context, groupID string, userIDs []string, showNumber, pageNumber int32) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) { groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID) if err != nil { return 0, nil, err @@ -502,10 +427,7 @@ func (g *GroupCacheRedis) GetGroupMembersPage( return uint32(len(userIDs)), groupMembers, err } -func (g *GroupCacheRedis) GetAllGroupMembersInfo( - ctx context.Context, - groupID string, -) (groupMembers []*relationtb.GroupMemberModel, err error) { +func (g *GroupCacheRedis) GetAllGroupMembersInfo(ctx context.Context, groupID string) (groupMembers []*relationtb.GroupMemberModel, err error) { groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID) if err != nil { return nil, err @@ -514,29 +436,19 @@ func (g *GroupCacheRedis) GetAllGroupMembersInfo( return g.GetGroupMembersInfo(ctx, groupID, groupMemberIDs) } -func (g *GroupCacheRedis) GetAllGroupMemberInfo( - ctx context.Context, - groupID string, -) ([]*relationtb.GroupMemberModel, error) { +func (g *GroupCacheRedis) GetAllGroupMemberInfo(ctx context.Context, groupID string) ([]*relationtb.GroupMemberModel, error) { groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID) if err != nil { return nil, err } - keys := make([]string, 0, len(groupMemberIDs)) - for _, groupMemberID := range groupMemberIDs { - keys = append(keys, g.getGroupMemberInfoKey(groupID, groupMemberID)) - } - - return batchGetCache( - ctx, - g.rcClient, - keys, - g.expireTime, - g.GetGroupMemberIndex, - func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) { - return g.groupMemberDB.Find(ctx, []string{groupID}, groupMemberIDs, nil) - }, - ) + //var keys []string + //for _, groupMemberID := range groupMemberIDs { + // keys = append(keys, g.getGroupMemberInfoKey(groupID, groupMemberID)) + //} + //return batchGetCache(ctx, g.rcClient, keys, g.expireTime, g.GetGroupMemberIndex, func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) { + // return g.groupMemberDB.Find(ctx, []string{groupID}, groupMemberIDs, nil) + //}) + return g.GetGroupMembersInfo(ctx, groupID, groupMemberIDs) } func (g *GroupCacheRedis) DelGroupMembersInfo(groupID string, userIDs ...string) GroupCache { @@ -551,15 +463,9 @@ func (g *GroupCacheRedis) DelGroupMembersInfo(groupID string, userIDs ...string) } func (g *GroupCacheRedis) GetGroupMemberNum(ctx context.Context, groupID string) (memberNum int64, err error) { - return getCache( - ctx, - g.rcClient, - g.getGroupMemberNumKey(groupID), - g.expireTime, - func(ctx context.Context) (int64, error) { - return g.groupMemberDB.TakeGroupMemberNum(ctx, groupID) - }, - ) + return getCache(ctx, g.rcClient, g.getGroupMemberNumKey(groupID), g.expireTime, func(ctx context.Context) (int64, error) { + return g.groupMemberDB.TakeGroupMemberNum(ctx, groupID) + }) } func (g *GroupCacheRedis) DelGroupsMemberNum(groupID ...string) GroupCache { diff --git a/pkg/common/db/cache/meta_cache.go b/pkg/common/db/cache/meta_cache.go index 3d62255a7..5cff3df7f 100644 --- a/pkg/common/db/cache/meta_cache.go +++ b/pkg/common/db/cache/meta_cache.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "errors" - "fmt" "time" "github.com/dtm-labs/rockscache" @@ -59,26 +58,37 @@ type metaCacheRedis struct { func (m *metaCacheRedis) ExecDel(ctx context.Context) error { if len(m.keys) > 0 { log.ZDebug(ctx, "delete cache", "keys", m.keys) - retryTimes := 0 - for { - if err := m.rcClient.TagAsDeletedBatch2(ctx, m.keys); err != nil { - if retryTimes >= m.maxRetryTimes { - err = errs.ErrInternalServer.Wrap( - fmt.Sprintf( - "delete cache error: %v, keys: %v, retry times %d, please check redis server", - err, - m.keys, - retryTimes, - ), - ) - log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", m.keys) - - return err + for _, key := range m.keys { + for i := 0; i < m.maxRetryTimes; i++ { + if err := m.rcClient.TagAsDeleted(key); err != nil { + log.ZError(ctx, "delete cache failed", err, "key", key) + time.Sleep(m.retryInterval) + continue } - retryTimes++ - } else { break } + + //retryTimes := 0 + //for { + // m.rcClient.TagAsDeleted() + // if err := m.rcClient.TagAsDeletedBatch2(ctx, []string{key}); err != nil { + // if retryTimes >= m.maxRetryTimes { + // err = errs.ErrInternalServer.Wrap( + // fmt.Sprintf( + // "delete cache error: %v, keys: %v, retry times %d, please check redis server", + // err, + // key, + // retryTimes, + // ), + // ) + // log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", key) + // return err + // } + // retryTimes++ + // } else { + // break + // } + //} } } @@ -109,13 +119,7 @@ func GetDefaultOpt() rockscache.Options { return opts } -func getCache[T any]( - ctx context.Context, - rcClient *rockscache.Client, - key string, - expire time.Duration, - fn func(ctx context.Context) (T, error), -) (T, error) { +func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) { var t T var write bool v, err := rcClient.Fetch2(ctx, key, expire, func() (s string, err error) { @@ -150,94 +154,101 @@ func getCache[T any]( return t, nil } -func batchGetCache[T any]( - ctx context.Context, - rcClient *rockscache.Client, - keys []string, - expire time.Duration, - keyIndexFn func(t T, keys []string) (int, error), - fn func(ctx context.Context) ([]T, error), -) ([]T, error) { - batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { - values := make(map[int]string) - tArrays, err := fn(ctx) - if err != nil { - return nil, err - } - for _, v := range tArrays { - index, err := keyIndexFn(v, keys) - if err != nil { - continue - } - bs, err := json.Marshal(v) - if err != nil { - return nil, utils.Wrap(err, "marshal failed") - } - values[index] = string(bs) - } - - return values, nil - }) - if err != nil { - return nil, err - } - var tArrays []T - for _, v := range batchMap { - if v != "" { - var t T - err = json.Unmarshal([]byte(v), &t) - if err != nil { - return nil, utils.Wrap(err, "unmarshal failed") - } - tArrays = append(tArrays, t) - } +//func batchGetCache[T any](ctx context.Context, rcClient *rockscache.Client, keys []string, expire time.Duration, keyIndexFn func(t T, keys []string) (int, error), fn func(ctx context.Context) ([]T, error)) ([]T, error) { +// batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { +// values := make(map[int]string) +// tArrays, err := fn(ctx) +// if err != nil { +// return nil, err +// } +// for _, v := range tArrays { +// index, err := keyIndexFn(v, keys) +// if err != nil { +// continue +// } +// bs, err := json.Marshal(v) +// if err != nil { +// return nil, utils.Wrap(err, "marshal failed") +// } +// values[index] = string(bs) +// } +// return values, nil +// }) +// if err != nil { +// return nil, err +// } +// var tArrays []T +// for _, v := range batchMap { +// if v != "" { +// var t T +// err = json.Unmarshal([]byte(v), &t) +// if err != nil { +// return nil, utils.Wrap(err, "unmarshal failed") +// } +// tArrays = append(tArrays, t) +// } +// } +// return tArrays, nil +//} + +func batchGetCache2[T any, K comparable](ctx context.Context, rcClient *rockscache.Client, expire time.Duration, keys []K, keyFn func(key K) string, fns func(ctx context.Context, key K) (T, error)) ([]T, error) { + if len(keys) == 0 { + return nil, nil } - - return tArrays, nil -} - -func batchGetCacheMap[T any]( - ctx context.Context, - rcClient *rockscache.Client, - keys, originKeys []string, - expire time.Duration, - keyIndexFn func(s string, keys []string) (int, error), - fn func(ctx context.Context) (map[string]T, error), -) (map[string]T, error) { - batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { - tArrays, err := fn(ctx) + res := make([]T, 0, len(keys)) + for _, key := range keys { + val, err := getCache(ctx, rcClient, keyFn(key), expire, func(ctx context.Context) (T, error) { + return fns(ctx, key) + }) if err != nil { return nil, err } - values := make(map[int]string) - for k, v := range tArrays { - index, err := keyIndexFn(k, originKeys) - if err != nil { - continue - } - bs, err := json.Marshal(v) - if err != nil { - return nil, utils.Wrap(err, "marshal failed") - } - values[index] = string(bs) - } - - return values, nil - }) - if err != nil { - return nil, err - } - tMap := make(map[string]T) - for i, v := range batchMap { - if v != "" { - var t T - err = json.Unmarshal([]byte(v), &t) - if err != nil { - return nil, utils.Wrap(err, "unmarshal failed") - } - tMap[originKeys[i]] = t - } + res = append(res, val) } - return tMap, nil + return res, nil } + +//func batchGetCacheMap[T any]( +// ctx context.Context, +// rcClient *rockscache.Client, +// keys, originKeys []string, +// expire time.Duration, +// keyIndexFn func(s string, keys []string) (int, error), +// fn func(ctx context.Context) (map[string]T, error), +//) (map[string]T, error) { +// batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { +// tArrays, err := fn(ctx) +// if err != nil { +// return nil, err +// } +// values := make(map[int]string) +// for k, v := range tArrays { +// index, err := keyIndexFn(k, originKeys) +// if err != nil { +// continue +// } +// bs, err := json.Marshal(v) +// if err != nil { +// return nil, utils.Wrap(err, "marshal failed") +// } +// values[index] = string(bs) +// } +// return values, nil +// }) +// if err != nil { +// return nil, err +// } +// tMap := make(map[string]T) +// for i, v := range batchMap { +// if v != "" { +// var t T +// err = json.Unmarshal([]byte(v), &t) +// if err != nil { +// return nil, utils.Wrap(err, "unmarshal failed") +// } +// tMap[originKeys[i]] = t +// } +// } +// return tMap, nil +//} diff --git a/pkg/common/db/cache/msg.go b/pkg/common/db/cache/msg.go index 66161c424..b55a76f62 100644 --- a/pkg/common/db/cache/msg.go +++ b/pkg/common/db/cache/msg.go @@ -17,6 +17,8 @@ package cache import ( "context" "errors" + "github.com/dtm-labs/rockscache" + unrelationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation" "strconv" "time" @@ -103,11 +105,7 @@ type MsgModel interface { GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error) SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error - GetMessagesBySeq( - ctx context.Context, - conversationID string, - seqs []int64, - ) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) + GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error DelUserDeleteMsgsList(ctx context.Context, conversationID string, seqs []int64) @@ -120,12 +118,7 @@ type MsgModel interface { JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error - SetMessageReactionExpire( - ctx context.Context, - clientMsgID string, - sessionType int32, - expiration time.Duration, - ) (bool, error) + SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error @@ -138,10 +131,10 @@ func NewMsgCacheModel(client redis.UniversalClient) MsgModel { type msgCache struct { metaCache - rdb redis.UniversalClient - // expireTime time.Duration - // rcClient *rockscache.Client - // msgDocDatabase unrelationtb.MsgDocModelInterface + rdb redis.UniversalClient + expireTime time.Duration + rcClient *rockscache.Client + msgDocDatabase unrelationtb.MsgDocModelInterface } func (c *msgCache) getMaxSeqKey(conversationID string) string { @@ -156,53 +149,51 @@ func (c *msgCache) getHasReadSeqKey(conversationID string, userID string) string return hasReadSeq + userID + ":" + conversationID } -func (c *msgCache) setSeq( - ctx context.Context, - conversationID string, - seq int64, - getkey func(conversationID string) string, -) error { +func (c *msgCache) setSeq(ctx context.Context, conversationID string, seq int64, getkey func(conversationID string) string) error { return utils.Wrap1(c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err()) } -func (c *msgCache) getSeq( - ctx context.Context, - conversationID string, - getkey func(conversationID string) string, -) (int64, error) { +func (c *msgCache) getSeq(ctx context.Context, conversationID string, getkey func(conversationID string) string) (int64, error) { return utils.Wrap2(c.rdb.Get(ctx, getkey(conversationID)).Int64()) } -func (c *msgCache) getSeqs( - ctx context.Context, - items []string, - getkey func(s string) string, -) (m map[string]int64, err error) { - pipe := c.rdb.Pipeline() - for _, v := range items { - err2 := pipe.Get(ctx, getkey(v)).Err() - if err2 != nil && !errors.Is(err2, redis.Nil) { - return nil, errs.Wrap(err2) - } - } - result, err := pipe.Exec(ctx) - if err != nil && !errors.Is(err, redis.Nil) { - return nil, errs.Wrap(err) - } +func (c *msgCache) getSeqs(ctx context.Context, items []string, getkey func(s string) string) (m map[string]int64, err error) { m = make(map[string]int64, len(items)) - for i, v := range result { - seq := v.(*redis.StringCmd) - - if seq.Err() != nil && !errors.Is(seq.Err(), redis.Nil) { - return nil, errs.Wrap(v.Err()) + for i, v := range items { + res, err := c.rdb.Get(ctx, getkey(v)).Result() + if err != nil && err != redis.Nil { + return nil, errs.Wrap(err) } - val := utils.StringToInt64(seq.Val()) + val := utils.StringToInt64(res) if val != 0 { m[items[i]] = val } } return m, nil + + //pipe := c.rdb.Pipeline() + //for _, v := range items { + // if err := pipe.Get(ctx, getkey(v)).Err(); err != nil && err != redis.Nil { + // return nil, errs.Wrap(err) + // } + //} + //result, err := pipe.Exec(ctx) + //if err != nil && err != redis.Nil { + // return nil, errs.Wrap(err) + //} + //m = make(map[string]int64, len(items)) + //for i, v := range result { + // seq := v.(*redis.StringCmd) + // if seq.Err() != nil && seq.Err() != redis.Nil { + // return nil, errs.Wrap(v.Err()) + // } + // val := utils.StringToInt64(seq.Val()) + // if val != 0 { + // m[items[i]] = val + // } + //} + //return m, nil } func (c *msgCache) SetMaxSeq(ctx context.Context, conversationID string, maxSeq int64) error { @@ -222,16 +213,21 @@ func (c *msgCache) SetMinSeq(ctx context.Context, conversationID string, minSeq } func (c *msgCache) setSeqs(ctx context.Context, seqs map[string]int64, getkey func(key string) string) error { - pipe := c.rdb.Pipeline() - for k, seq := range seqs { - err := pipe.Set(ctx, getkey(k), seq, 0).Err() - if err != nil { + for conversationID, seq := range seqs { + if err := c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err(); err != nil { return errs.Wrap(err) } } - _, err := pipe.Exec(ctx) - - return err + return nil + //pipe := c.rdb.Pipeline() + //for k, seq := range seqs { + // err := pipe.Set(ctx, getkey(k), seq, 0).Err() + // if err != nil { + // return errs.Wrap(err) + // } + //} + //_, err := pipe.Exec(ctx) + //return err } func (c *msgCache) SetMinSeqs(ctx context.Context, seqs map[string]int64) error { @@ -254,30 +250,17 @@ func (c *msgCache) GetConversationUserMinSeq(ctx context.Context, conversationID return utils.Wrap2(c.rdb.Get(ctx, c.getConversationUserMinSeqKey(conversationID, userID)).Int64()) } -func (c *msgCache) GetConversationUserMinSeqs( - ctx context.Context, - conversationID string, - userIDs []string, -) (m map[string]int64, err error) { +func (c *msgCache) GetConversationUserMinSeqs(ctx context.Context, conversationID string, userIDs []string) (m map[string]int64, err error) { return c.getSeqs(ctx, userIDs, func(userID string) string { return c.getConversationUserMinSeqKey(conversationID, userID) }) } -func (c *msgCache) SetConversationUserMinSeq( - ctx context.Context, - conversationID string, - userID string, - minSeq int64, -) error { +func (c *msgCache) SetConversationUserMinSeq(ctx context.Context, conversationID string, userID string, minSeq int64) error { return utils.Wrap1(c.rdb.Set(ctx, c.getConversationUserMinSeqKey(conversationID, userID), minSeq, 0).Err()) } -func (c *msgCache) SetConversationUserMinSeqs( - ctx context.Context, - conversationID string, - seqs map[string]int64, -) (err error) { +func (c *msgCache) SetConversationUserMinSeqs(ctx context.Context, conversationID string, seqs map[string]int64) (err error) { return c.setSeqs(ctx, seqs, func(userID string) string { return c.getConversationUserMinSeqKey(conversationID, userID) }) @@ -305,11 +288,7 @@ func (c *msgCache) UserSetHasReadSeqs(ctx context.Context, userID string, hasRea }) } -func (c *msgCache) GetHasReadSeqs( - ctx context.Context, - userID string, - conversationIDs []string, -) (map[string]int64, error) { +func (c *msgCache) GetHasReadSeqs(ctx context.Context, userID string, conversationIDs []string) (map[string]int64, error) { return c.getSeqs(ctx, conversationIDs, func(conversationID string) string { return c.getHasReadSeqKey(conversationID, userID) }) @@ -363,62 +342,86 @@ func (c *msgCache) allMessageCacheKey(conversationID string) string { return messageCache + conversationID + "_*" } -func (c *msgCache) GetMessagesBySeq( - ctx context.Context, - conversationID string, - seqs []int64, -) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) { - pipe := c.rdb.Pipeline() - for _, v := range seqs { - // MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1 - key := c.getMessageCacheKey(conversationID, v) - err2 := pipe.Get(ctx, key).Err() - if err2 != nil && errors.Is(err2, redis.Nil) { - return nil, nil, err2 +func (c *msgCache) GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) { + for _, seq := range seqs { + res, err := c.rdb.Get(ctx, c.getMessageCacheKey(conversationID, seq)).Result() + if err != nil { + log.ZError(ctx, "GetMessagesBySeq failed", err, "conversationID", conversationID, "seq", seq) + failedSeqs = append(failedSeqs, seq) + continue } - } - result, err := pipe.Exec(ctx) - for i, v := range result { - cmd := v.(*redis.StringCmd) - if cmd.Err() != nil { - failedSeqs = append(failedSeqs, seqs[i]) - } else { - msg := sdkws.MsgData{} - err = msgprocessor.String2Pb(cmd.Val(), &msg) - if err == nil { - if msg.Status != constant.MsgDeleted { - seqMsgs = append(seqMsgs, &msg) - - continue - } - } else { - log.ZWarn(ctx, "UnmarshalString failed", err, "conversationID", conversationID, "seq", seqs[i], "msg", cmd.Val()) - } - failedSeqs = append(failedSeqs, seqs[i]) + msg := sdkws.MsgData{} + if err = msgprocessor.String2Pb(res, &msg); err != nil { + log.ZError(ctx, "GetMessagesBySeq Unmarshal failed", err, "res", res, "conversationID", conversationID, "seq", seq) + failedSeqs = append(failedSeqs, seq) + continue } + if msg.Status == constant.MsgDeleted { + failedSeqs = append(failedSeqs, seq) + continue + } + seqMsgs = append(seqMsgs, &msg) } - return seqMsgs, failedSeqs, err + return + //pipe := c.rdb.Pipeline() + //for _, v := range seqs { + // // MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1 + // key := c.getMessageCacheKey(conversationID, v) + // if err := pipe.Get(ctx, key).Err(); err != nil && err != redis.Nil { + // return nil, nil, err + // } + //} + //result, err := pipe.Exec(ctx) + //for i, v := range result { + // cmd := v.(*redis.StringCmd) + // if cmd.Err() != nil { + // failedSeqs = append(failedSeqs, seqs[i]) + // } else { + // msg := sdkws.MsgData{} + // err = msgprocessor.String2Pb(cmd.Val(), &msg) + // if err == nil { + // if msg.Status != constant.MsgDeleted { + // seqMsgs = append(seqMsgs, &msg) + // continue + // } + // } else { + // log.ZWarn(ctx, "UnmarshalString failed", err, "conversationID", conversationID, "seq", seqs[i], "msg", cmd.Val()) + // } + // failedSeqs = append(failedSeqs, seqs[i]) + // } + //} + //return seqMsgs, failedSeqs, err } func (c *msgCache) SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) { - pipe := c.rdb.Pipeline() - var failedMsgs []*sdkws.MsgData for _, msg := range msgs { - key := c.getMessageCacheKey(conversationID, msg.Seq) s, err := msgprocessor.Pb2String(msg) if err != nil { return 0, errs.Wrap(err) } - err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err() - if err != nil { - failedMsgs = append(failedMsgs, msg) - log.ZWarn(ctx, "set msg 2 cache failed", err, "msg", failedMsgs) + key := c.getMessageCacheKey(conversationID, msg.Seq) + if err := c.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { + return 0, errs.Wrap(err) } } - _, err := pipe.Exec(ctx) - - return len(failedMsgs), err + return len(msgs), nil + //pipe := c.rdb.Pipeline() + //var failedMsgs []*sdkws.MsgData + //for _, msg := range msgs { + // key := c.getMessageCacheKey(conversationID, msg.Seq) + // s, err := msgprocessor.Pb2String(msg) + // if err != nil { + // return 0, errs.Wrap(err) + // } + // err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err() + // if err != nil { + // failedMsgs = append(failedMsgs, msg) + // log.ZWarn(ctx, "set msg 2 cache failed", err, "msg", failedMsgs) + // } + //} + //_, err := pipe.Exec(ctx) + //return len(failedMsgs), err } func (c *msgCache) getMessageDelUserListKey(conversationID string, seq int64) string { @@ -430,28 +433,47 @@ func (c *msgCache) getUserDelList(conversationID, userID string) string { } func (c *msgCache) UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error { - pipe := c.rdb.Pipeline() for _, seq := range seqs { delUserListKey := c.getMessageDelUserListKey(conversationID, seq) userDelListKey := c.getUserDelList(conversationID, userID) - err := pipe.SAdd(ctx, delUserListKey, userID).Err() + err := c.rdb.SAdd(ctx, delUserListKey, userID).Err() if err != nil { return errs.Wrap(err) } - err = pipe.SAdd(ctx, userDelListKey, seq).Err() + err = c.rdb.SAdd(ctx, userDelListKey, seq).Err() if err != nil { return errs.Wrap(err) } - if err := pipe.Expire(ctx, delUserListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { + if err := c.rdb.Expire(ctx, delUserListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { return errs.Wrap(err) } - if err := pipe.Expire(ctx, userDelListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { + if err := c.rdb.Expire(ctx, userDelListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { return errs.Wrap(err) } } - _, err := pipe.Exec(ctx) - return errs.Wrap(err) + return nil + //pipe := c.rdb.Pipeline() + //for _, seq := range seqs { + // delUserListKey := c.getMessageDelUserListKey(conversationID, seq) + // userDelListKey := c.getUserDelList(conversationID, userID) + // err := pipe.SAdd(ctx, delUserListKey, userID).Err() + // if err != nil { + // return errs.Wrap(err) + // } + // err = pipe.SAdd(ctx, userDelListKey, seq).Err() + // if err != nil { + // return errs.Wrap(err) + // } + // if err := pipe.Expire(ctx, delUserListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { + // return errs.Wrap(err) + // } + // if err := pipe.Expire(ctx, userDelListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil { + // return errs.Wrap(err) + // } + //} + //_, err := pipe.Exec(ctx) + //return errs.Wrap(err) } func (c *msgCache) GetUserDelList(ctx context.Context, userID, conversationID string) (seqs []int64, err error) { @@ -476,47 +498,74 @@ func (c *msgCache) DelUserDeleteMsgsList(ctx context.Context, conversationID str continue } if len(delUsers) > 0 { - pipe := c.rdb.Pipeline() var failedFlag bool for _, userID := range delUsers { - err = pipe.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err() + err = c.rdb.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err() if err != nil { failedFlag = true - log.ZWarn( - ctx, - "DelUserDeleteMsgsList failed", - err, - "conversationID", - conversationID, - "seq", - seq, - "userID", - userID, - ) + log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq, "userID", userID) } } if !failedFlag { - if err := pipe.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil { + if err := c.rdb.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil { log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq) } } - if _, err := pipe.Exec(ctx); err != nil { - log.ZError(ctx, "pipe exec failed", err, "conversationID", conversationID, "seq", seq) - } } } + //for _, seq := range seqs { + // delUsers, err := c.rdb.SMembers(ctx, c.getMessageDelUserListKey(conversationID, seq)).Result() + // if err != nil { + // log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq) + // continue + // } + // if len(delUsers) > 0 { + // pipe := c.rdb.Pipeline() + // var failedFlag bool + // for _, userID := range delUsers { + // err = pipe.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err() + // if err != nil { + // failedFlag = true + // log.ZWarn( + // ctx, + // "DelUserDeleteMsgsList failed", + // err, + // "conversationID", + // conversationID, + // "seq", + // seq, + // "userID", + // userID, + // ) + // } + // } + // if !failedFlag { + // if err := pipe.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil { + // log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq) + // } + // } + // if _, err := pipe.Exec(ctx); err != nil { + // log.ZError(ctx, "pipe exec failed", err, "conversationID", conversationID, "seq", seq) + // } + // } + //} } func (c *msgCache) DeleteMessages(ctx context.Context, conversationID string, seqs []int64) error { - pipe := c.rdb.Pipeline() for _, seq := range seqs { - if err := pipe.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err(); err != nil { + if err := c.rdb.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err(); err != nil { return errs.Wrap(err) } } - _, err := pipe.Exec(ctx) - - return errs.Wrap(err) + return nil + //pipe := c.rdb.Pipeline() + //for _, seq := range seqs { + // if err := pipe.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err(); err != nil { + // return errs.Wrap(err) + // } + //} + //_, err := pipe.Exec(ctx) + //return errs.Wrap(err) } func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversationID string) error { @@ -527,16 +576,20 @@ func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversatio if err != nil { return errs.Wrap(err) } - pipe := c.rdb.Pipeline() for _, v := range vals { - err2 := pipe.Del(ctx, v).Err() - if err2 != nil { - return errs.Wrap(err2) + if err := c.rdb.Del(ctx, v).Err(); err != nil { + return errs.Wrap(err) } } - _, err = pipe.Exec(ctx) - - return errs.Wrap(err) + return nil + //pipe := c.rdb.Pipeline() + //for _, v := range vals { + // if err := pipe.Del(ctx, v).Err(); err != nil { + // return errs.Wrap(err) + // } + //} + //_, err = pipe.Exec(ctx) + //return errs.Wrap(err) } func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []int64) error { @@ -594,17 +647,8 @@ func (c *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, erro return int32(result), errs.Wrap(err) } -func (c *msgCache) SetFcmToken( - ctx context.Context, - account string, - platformID int, - fcmToken string, - expireTime int64, -) (err error) { - return errs.Wrap( - c.rdb.Set(ctx, fcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second). - Err(), - ) +func (c *msgCache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) { + return errs.Wrap(c.rdb.Set(ctx, fcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err()) } func (c *msgCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) { diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index 0afbd595e..d1164f2c0 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -112,29 +112,32 @@ func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userIn } func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) { - keys := make([]string, 0, len(userIDs)) - for _, userID := range userIDs { - keys = append(keys, u.getUserInfoKey(userID)) - } - - return batchGetCache( - ctx, - u.rcClient, - keys, - u.expireTime, - func(user *relationtb.UserModel, keys []string) (int, error) { - for i, key := range keys { - if key == u.getUserInfoKey(user.UserID) { - return i, nil - } - } - - return 0, errIndex - }, - func(ctx context.Context) ([]*relationtb.UserModel, error) { - return u.userDB.Find(ctx, userIDs) - }, - ) + //var keys []string + //for _, userID := range userIDs { + // keys = append(keys, u.getUserInfoKey(userID)) + //} + //return batchGetCache( + // ctx, + // u.rcClient, + // keys, + // u.expireTime, + // func(user *relationtb.UserModel, keys []string) (int, error) { + // for i, key := range keys { + // if key == u.getUserInfoKey(user.UserID) { + // return i, nil + // } + // } + // return 0, errIndex + // }, + // func(ctx context.Context) ([]*relationtb.UserModel, error) { + // return u.userDB.Find(ctx, userIDs) + // }, + //) + return batchGetCache2(ctx, u.rcClient, u.expireTime, userIDs, func(userID string) string { + return u.getUserInfoKey(userID) + }, func(ctx context.Context, userID string) (*relationtb.UserModel, error) { + return u.userDB.Take(ctx, userID) + }) } func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) UserCache { diff --git a/pkg/common/db/controller/conversation.go b/pkg/common/db/controller/conversation.go index e68eb25ba..29cd0d152 100644 --- a/pkg/common/db/controller/conversation.go +++ b/pkg/common/db/controller/conversation.go @@ -50,7 +50,7 @@ type ConversationDatabase interface { GetConversationIDs(ctx context.Context, userID string) ([]string, error) GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error) GetAllConversationIDs(ctx context.Context) ([]string, error) - GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) + //GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error) GetConversationIDsNeedDestruct(ctx context.Context) ([]*relationtb.ConversationModel, error) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) @@ -306,9 +306,9 @@ func (c *conversationDatabase) GetAllConversationIDs(ctx context.Context) ([]str return c.conversationDB.GetAllConversationIDs(ctx) } -func (c *conversationDatabase) GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) { - return c.cache.GetUserAllHasReadSeqs(ctx, ownerUserID) -} +//func (c *conversationDatabase) GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) { +// return c.cache.GetUserAllHasReadSeqs(ctx, ownerUserID) +//} func (c *conversationDatabase) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error) { return c.conversationDB.GetConversationsByConversationID(ctx, conversationIDs) From e4f3e34249eace95af2290148e7e626c455b3b81 Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:26:35 +0800 Subject: [PATCH 18/21] fix: api send messages for notification conversation. (#1254) * fix: to start im or chat, ZooKeeper must be started first. * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: go mod update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: get all userID Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: msggateway add online status call Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * chore: network mode change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * feat: add api of get server time Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: remove go work sum Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: pull message add isRead field Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: check msg-transfer script Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: start don't kill old process Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: check component Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: pull message set isRead only message come from single. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add ex field to update group info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: message log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: send message test log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * test: remove info log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: api of send message add sendTime field. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add callback for update user's info. * cicd: robot automated Change * fix: change callback command name. * cicd: robot automated Change * fix: single chat unread status change. * fix: single chat unread status change. * fix: single chat unread status change. * fix: user status change. * cicd: robot automated Change * fix: user status change. * fix: user status change. * fix: user status change. * cicd: robot automated Change * fix: ws close when user logout. * fix: remove repeat platform on online status. * cicd: robot automated Change * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. --------- Signed-off-by: Gordon <1432970085@qq.com> Signed-off-by: withchao <993506633@qq.com> Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: withchao <993506633@qq.com> Co-authored-by: Xinwei Xiong <3293172751NSS@gmail.com> Co-authored-by: FGadvancer --- .golangci.yml | 2 +- cmd/openim-api/main.go | 1 - internal/api/msg.go | 32 +++------ internal/api/route.go | 1 - internal/api/user.go | 5 -- internal/msgtransfer/init.go | 4 +- .../msgtransfer/online_history_msg_handler.go | 26 ++++--- .../online_msg_to_mongo_handler.go | 2 +- internal/push/push_handler.go | 1 + internal/push/push_to_client.go | 9 ++- internal/rpc/conversation/conversaion.go | 72 ++++++++++++------- internal/rpc/friend/friend.go | 3 +- internal/rpc/msg/as_read.go | 5 +- internal/tools/msg.go | 6 +- pkg/apistruct/manage.go | 2 +- pkg/rpcclient/conversation.go | 13 ++-- pkg/rpcclient/msg.go | 11 +-- 17 files changed, 104 insertions(+), 91 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 050025b6e..8785b72d8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -171,7 +171,7 @@ linters-settings: # exclude_godoc_examples: false funlen: lines: 150 - statements: 50 + statements: 80 gci: # put imports beginning with prefix after 3rd-party packages; # only support one prefix diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index f6db87353..d1f5cb3f8 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -32,7 +32,6 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" ) - func main() { apiCmd := cmd.NewApiCmd() apiCmd.AddPortFlag() diff --git a/internal/api/msg.go b/internal/api/msg.go index b9b10e98e..028d77ca4 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -57,6 +57,10 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg) var newContent string options := make(map[string]bool, 5) switch params.ContentType { + case constant.OANotification: + notification := sdkws.NotificationElem{} + notification.Detail = utils.StructToJsonString(params.Content) + newContent = utils.StructToJsonString(¬ification) case constant.Text: fallthrough case constant.Picture: @@ -69,10 +73,6 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg) fallthrough case constant.File: fallthrough - case constant.CustomNotTriggerConversation: - fallthrough - case constant.CustomOnlineOnly: - fallthrough default: newContent = utils.StructToJsonString(params.Content) } @@ -82,11 +82,6 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg) if params.NotOfflinePush { utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false) } - if params.ContentType == constant.CustomOnlineOnly { - m.SetOptions(options, false) - } else if params.ContentType == constant.CustomNotTriggerConversation { - utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, false) - } pbData := msg.SendMsgReq{ MsgData: &sdkws.MsgData{ SendID: params.SendID, @@ -105,14 +100,6 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg) OfflinePushInfo: params.OfflinePushInfo, }, } - //if params.ContentType == constant.OANotification { - // var tips sdkws.TipsComm - // tips.JsonDetail = utils.StructToJsonString(params.Content) - // pbData.MsgData.Content, err = proto.Marshal(&tips) - // if err != nil { - // log.ZError(c, "Marshal failed ", err, "tips", tips.String()) - // } - //} return &pbData } @@ -180,15 +167,13 @@ func (m *MessageApi) getSendMsgReq(c *gin.Context, req apistruct.SendMsg) (sendM data = apistruct.FileElem{} case constant.Custom: data = apistruct.CustomElem{} - case constant.Revoke: - data = apistruct.RevokeElem{} case constant.OANotification: data = apistruct.OANotificationElem{} req.SessionType = constant.NotificationChatType - case constant.CustomNotTriggerConversation: - data = apistruct.CustomElem{} - case constant.CustomOnlineOnly: - data = apistruct.CustomElem{} + if !authverify.IsManagerUserID(req.SendID) { + return nil, errs.ErrNoPermission. + Wrap("only app manager can as sender send OANotificationElem") + } default: return nil, errs.ErrArgs.WithDetail("not support err contentType") } @@ -212,7 +197,6 @@ func (m *MessageApi) SendMessage(c *gin.Context) { apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message")) return } - sendMsgReq, err := m.getSendMsgReq(c, req.SendMsg) if err != nil { log.ZError(c, "decodeData failed", err) diff --git a/internal/api/route.go b/internal/api/route.go index 9a639a2de..d714270b4 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -83,7 +83,6 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive userRouterGroup.POST("/get_users_online_status", ParseToken, u.GetUsersOnlineStatus) userRouterGroup.POST("/get_users_online_token_detail", ParseToken, u.GetUsersOnlineTokenDetail) userRouterGroup.POST("/subscribe_users_status", ParseToken, u.SubscriberStatus) - userRouterGroup.POST("/unsubscribe_users_status", ParseToken, u.UnSubscriberStatus) userRouterGroup.POST("/get_users_status", ParseToken, u.GetUserStatus) userRouterGroup.POST("/get_subscribe_users_status", ParseToken, u.GetSubscribeUsersStatus) } diff --git a/internal/api/user.go b/internal/api/user.go index 0a5a12698..86b7c0b0b 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -190,11 +190,6 @@ func (u *UserApi) SubscriberStatus(c *gin.Context) { a2r.Call(user.UserClient.SubscribeOrCancelUsersStatus, u.Client, c) } -// UnSubscriberStatus Unsubscribe a user's presence. -func (u *UserApi) UnSubscriberStatus(c *gin.Context) { - a2r.Call(user.UserClient.SubscribeOrCancelUsersStatus, u.Client, c) -} - // GetUserStatus Get the online status of the user. func (u *UserApi) GetUserStatus(c *gin.Context) { a2r.Call(user.UserClient.GetUserStatus, u.Client, c) diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index 7babd9a07..e5066633a 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -16,12 +16,13 @@ package msgtransfer import ( "fmt" - "sync" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + "github.com/OpenIMSDK/tools/mw" "github.com/openimsdk/open-im-server/v3/pkg/common/config" @@ -30,7 +31,6 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/db/relation" relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation" - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "github.com/openimsdk/open-im-server/v3/pkg/common/prome" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient" ) diff --git a/internal/msgtransfer/online_history_msg_handler.go b/internal/msgtransfer/online_history_msg_handler.go index e83939a4c..b4556634c 100644 --- a/internal/msgtransfer/online_history_msg_handler.go +++ b/internal/msgtransfer/online_history_msg_handler.go @@ -283,20 +283,30 @@ func (och *OnlineHistoryRedisConsumerHandler) handleMsg( return } if isNewConversation { - if storageList[0].SessionType == constant.SuperGroupChatType { - log.ZInfo(ctx, "group chat first create conversation", "conversationID", conversationID) + switch storageList[0].SessionType { + case constant.SuperGroupChatType: + log.ZInfo(ctx, "group chat first create conversation", "conversationID", + conversationID) userIDs, err := och.groupRpcClient.GetGroupMemberIDs(ctx, storageList[0].GroupID) if err != nil { - log.ZWarn(ctx, "get group member ids error", err, "conversationID", conversationID) + log.ZWarn(ctx, "get group member ids error", err, "conversationID", + conversationID) } else { - if err := och.conversationRpcClient.GroupChatFirstCreateConversation(ctx, storageList[0].GroupID, userIDs); err != nil { - log.ZWarn(ctx, "single chat first create conversation error", err, "conversationID", conversationID) + if err := och.conversationRpcClient.GroupChatFirstCreateConversation(ctx, + storageList[0].GroupID, userIDs); err != nil { + log.ZWarn(ctx, "single chat first create conversation error", err, + "conversationID", conversationID) } } - } else { - if err := och.conversationRpcClient.SingleChatFirstCreateConversation(ctx, storageList[0].RecvID, storageList[0].SendID); err != nil { - log.ZWarn(ctx, "single chat first create conversation error", err, "conversationID", conversationID) + case constant.SingleChatType, constant.NotificationChatType: + if err := och.conversationRpcClient.SingleChatFirstCreateConversation(ctx, storageList[0].RecvID, + storageList[0].SendID, conversationID, storageList[0].SessionType); err != nil { + log.ZWarn(ctx, "single chat or notification first create conversation error", err, + "conversationID", conversationID, "sessionType", storageList[0].SessionType) } + default: + log.ZWarn(ctx, "unknown session type", nil, "sessionType", + storageList[0].SessionType) } } diff --git a/internal/msgtransfer/online_msg_to_mongo_handler.go b/internal/msgtransfer/online_msg_to_mongo_handler.go index 8099d39d7..bfea6c433 100644 --- a/internal/msgtransfer/online_msg_to_mongo_handler.go +++ b/internal/msgtransfer/online_msg_to_mongo_handler.go @@ -62,7 +62,7 @@ func (mc *OnlineHistoryMongoConsumerHandler) handleChatWs2Mongo( log.ZError(ctx, "msgFromMQ.MsgData is empty", nil, "cMsg", cMsg) return } - log.ZInfo(ctx, "mongo consumer recv msg", "msgs", msgFromMQ.MsgData) + log.ZInfo(ctx, "mongo consumer recv msg", "msgs", msgFromMQ.String()) err = mc.msgDatabase.BatchInsertChat2DB(ctx, msgFromMQ.ConversationID, msgFromMQ.MsgData, msgFromMQ.LastSeq) if err != nil { log.ZError( diff --git a/internal/push/push_handler.go b/internal/push/push_handler.go index 91363282f..a1a9ff08e 100644 --- a/internal/push/push_handler.go +++ b/internal/push/push_handler.go @@ -58,6 +58,7 @@ func (c *ConsumerHandler) handleMs2PsChat(ctx context.Context, msg []byte) { } sec := msgFromMQ.MsgData.SendTime / 1000 nowSec := utils.GetCurrentTimestampBySecond() + log.ZDebug(ctx, "push msg", "msg", pbData.String(), "sec", sec, "nowSec", nowSec) if nowSec-sec > 10 { return } diff --git a/internal/push/push_to_client.go b/internal/push/push_to_client.go index 66b003eaa..ba0d65b39 100644 --- a/internal/push/push_to_client.go +++ b/internal/push/push_to_client.go @@ -126,13 +126,12 @@ func (p *Pusher) Push2User(ctx context.Context, userIDs []string, msg *sdkws.Msg } func (p *Pusher) UnmarshalNotificationElem(bytes []byte, t interface{}) error { - var notificationElem struct { - Detail string `json:"detail,omitempty"` - } - if err := json.Unmarshal(bytes, ¬ificationElem); err != nil { + var notification sdkws.NotificationElem + if err := json.Unmarshal(bytes, ¬ification); err != nil { return err } - return json.Unmarshal([]byte(notificationElem.Detail), t) + + return json.Unmarshal([]byte(notification.Detail), t) } func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws.MsgData) (err error) { diff --git a/internal/rpc/conversation/conversaion.go b/internal/rpc/conversation/conversaion.go index e76f83008..0ea7d54be 100644 --- a/internal/rpc/conversation/conversaion.go +++ b/internal/rpc/conversation/conversaion.go @@ -17,8 +17,6 @@ package conversation import ( "context" - "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" - "google.golang.org/grpc" "github.com/OpenIMSDK/protocol/constant" @@ -114,7 +112,10 @@ func (c *conversationServer) SetConversation(ctx context.Context, req *pbconvers return resp, nil } -func (c *conversationServer) SetConversations(ctx context.Context, req *pbconversation.SetConversationsReq) (*pbconversation.SetConversationsResp, error) { +//nolint +func (c *conversationServer) SetConversations(ctx context.Context, + req *pbconversation.SetConversationsReq, +) (*pbconversation.SetConversationsResp, error) { if req.Conversation == nil { return nil, errs.ErrArgs.Wrap("conversation must not be nil") } @@ -124,14 +125,8 @@ func (c *conversationServer) SetConversations(ctx context.Context, req *pbconver return nil, err } if groupInfo.Status == constant.GroupStatusDismissed { - return nil, err + return nil, errs.ErrDismissedAlready.Wrap("group dismissed") } - // for _, userID := range req.UserIDs { - // if _, err := c.groupRpcClient.GetGroupMemberCache(ctx, req.Conversation.GroupID, userID); err != nil { - // log.ZError(ctx, "user not in group", err, "userID", userID, "groupID", req.Conversation.GroupID) - // return nil, err - // } - // } } var unequal int var conv tablerelation.ConversationModel @@ -205,7 +200,14 @@ func (c *conversationServer) SetConversations(ctx context.Context, req *pbconver return nil, err } for _, userID := range req.UserIDs { - c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, userID, req.Conversation.UserID, req.Conversation.IsPrivateChat.Value, req.Conversation.ConversationID) + err := c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, userID, req.Conversation.UserID, + req.Conversation.IsPrivateChat.Value, req.Conversation.ConversationID) + if err != nil { + log.ZWarn(ctx, "send conversation set private notification failed", err, + "userID", userID, "conversationID", req.Conversation.ConversationID) + + continue + } } } if req.Conversation.BurnDuration != nil { @@ -235,24 +237,40 @@ func (c *conversationServer) GetRecvMsgNotNotifyUserIDs(ctx context.Context, req } // create conversation without notification for msg redis transfer. -func (c *conversationServer) CreateSingleChatConversations(ctx context.Context, req *pbconversation.CreateSingleChatConversationsReq) (*pbconversation.CreateSingleChatConversationsResp, error) { - var conversation tablerelation.ConversationModel - conversation.ConversationID = msgprocessor.GetConversationIDBySessionType(constant.SingleChatType, req.RecvID, req.SendID) - conversation.ConversationType = constant.SingleChatType - conversation.OwnerUserID = req.SendID - conversation.UserID = req.RecvID - err := c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation}) - if err != nil { - log.ZWarn(ctx, "create conversation failed", err, "conversation", conversation) - } +func (c *conversationServer) CreateSingleChatConversations(ctx context.Context, + req *pbconversation.CreateSingleChatConversationsReq, +) (*pbconversation.CreateSingleChatConversationsResp, error) { + switch req.ConversationType { + case constant.SingleChatType: + var conversation tablerelation.ConversationModel + conversation.ConversationID = req.ConversationID + conversation.ConversationType = req.ConversationType + conversation.OwnerUserID = req.SendID + conversation.UserID = req.RecvID + err := c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation}) + if err != nil { + log.ZWarn(ctx, "create conversation failed", err, "conversation", conversation) + } - conversation2 := conversation - conversation2.OwnerUserID = req.RecvID - conversation2.UserID = req.SendID - err = c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation2}) - if err != nil { - log.ZWarn(ctx, "create conversation failed", err, "conversation2", conversation) + conversation2 := conversation + conversation2.OwnerUserID = req.RecvID + conversation2.UserID = req.SendID + err = c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation2}) + if err != nil { + log.ZWarn(ctx, "create conversation failed", err, "conversation2", conversation) + } + case constant.NotificationChatType: + var conversation tablerelation.ConversationModel + conversation.ConversationID = req.ConversationID + conversation.ConversationType = req.ConversationType + conversation.OwnerUserID = req.RecvID + conversation.UserID = req.SendID + err := c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation}) + if err != nil { + log.ZWarn(ctx, "create conversation failed", err, "conversation2", conversation) + } } + return &pbconversation.CreateSingleChatConversationsResp{}, nil } diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 1524a7f27..c563f77fe 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -252,7 +252,8 @@ func (s *friendServer) GetDesignatedFriends( return resp, nil } -func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context, req *pbfriend.GetDesignatedFriendsApplyReq) (resp *pbfriend.GetDesignatedFriendsApplyResp, err error) { +func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context, + req *pbfriend.GetDesignatedFriendsApplyReq) (resp *pbfriend.GetDesignatedFriendsApplyResp, err error) { friendRequests, err := s.friendDatabase.FindBothFriendRequests(ctx, req.FromUserID, req.ToUserID) if err != nil { return nil, err diff --git a/internal/rpc/msg/as_read.go b/internal/rpc/msg/as_read.go index efa322774..55c3ba088 100644 --- a/internal/rpc/msg/as_read.go +++ b/internal/rpc/msg/as_read.go @@ -147,7 +147,6 @@ func (m *msgServer) MarkConversationAsRead( for i := hasReadSeq + 1; i <= req.HasReadSeq; i++ { seqs = append(seqs, i) } - if len(seqs) > 0 { log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID) if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil { @@ -165,7 +164,9 @@ func (m *msgServer) MarkConversationAsRead( m.conversationAndGetRecvID(conversation, req.UserID), seqs, hasReadSeq); err != nil { return nil, err } - } else if conversation.ConversationType == constant.SuperGroupChatType { + + } else if conversation.ConversationType == constant.SuperGroupChatType || + conversation.ConversationType == constant.NotificationChatType { if req.HasReadSeq > hasReadSeq { err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq) if err != nil { diff --git a/internal/tools/msg.go b/internal/tools/msg.go index 94ce2dec0..ca095051c 100644 --- a/internal/tools/msg.go +++ b/internal/tools/msg.go @@ -23,6 +23,8 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" @@ -35,7 +37,6 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" "github.com/openimsdk/open-im-server/v3/pkg/common/db/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation" - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient/notification" ) @@ -77,7 +78,8 @@ func InitMsgTool() (*MsgTool, error) { /* discov, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, zookeeper.WithFreq(time.Hour), zookeeper.WithRoundRobin(), zookeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/if err != nil { + config.Config.Zookeeper.Password), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/ + if err != nil { return nil, err } discov.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) diff --git a/pkg/apistruct/manage.go b/pkg/apistruct/manage.go index 7d30d2151..1238b4757 100644 --- a/pkg/apistruct/manage.go +++ b/pkg/apistruct/manage.go @@ -41,7 +41,7 @@ type SendMsgReq struct { type BatchSendMsgReq struct { SendMsg IsSendAll bool `json:"isSendAll"` - RecvIDs []string `json:"recvIDs"` + RecvIDs []string `json:"recvIDs" binding:"required"` } type BatchSendMsgResp struct { diff --git a/pkg/rpcclient/conversation.go b/pkg/rpcclient/conversation.go index 30b0b4b77..df01bcb8f 100644 --- a/pkg/rpcclient/conversation.go +++ b/pkg/rpcclient/conversation.go @@ -39,7 +39,6 @@ func NewConversation(discov discoveryregistry.SvcDiscoveryRegistry) *Conversatio panic(err) } client := pbconversation.NewConversationClient(conn) - return &Conversation{discov: discov, conn: conn, Client: client} } @@ -57,13 +56,17 @@ func (c *ConversationRpcClient) GetSingleConversationRecvMsgOpt(ctx context.Cont if err != nil { return 0, err } - return conversation.GetConversation().RecvMsgOpt, err } -func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID string) error { - _, err := c.Client.CreateSingleChatConversations(ctx, &pbconversation.CreateSingleChatConversationsReq{RecvID: recvID, SendID: sendID}) - +func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID, + conversationID string, conversationType int32, +) error { + _, err := c.Client.CreateSingleChatConversations(ctx, + &pbconversation.CreateSingleChatConversationsReq{ + RecvID: recvID, SendID: sendID, ConversationID: conversationID, + ConversationType: conversationType, + }) return err } diff --git a/pkg/rpcclient/msg.go b/pkg/rpcclient/msg.go index 51e29c7d8..00b0fa3f1 100644 --- a/pkg/rpcclient/msg.go +++ b/pkg/rpcclient/msg.go @@ -221,12 +221,13 @@ func WithRpcGetUserName() NotificationOptions { } } -func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, sendID, recvID string, contentType, sesstionType int32, m proto.Message, opts ...NotificationOptions) (err error) { +func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, sendID, recvID string, + contentType, sesstionType int32, m proto.Message, opts ...NotificationOptions) (err error) { n := sdkws.NotificationElem{Detail: utils.StructToJsonString(m)} content, err := json.Marshal(&n) if err != nil { - log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m) - + log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", + sendID, "recvID", recvID, "contentType", contentType, "msg", m) return err } notificationOpt := ¬ificationOpt{} @@ -235,8 +236,8 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s } var req msg.SendMsgReq var msg sdkws.MsgData + var userInfo *sdkws.UserInfo if notificationOpt.WithRpcGetUsername && s.getUserInfo != nil { - var userInfo *sdkws.UserInfo userInfo, err = s.getUserInfo(ctx, sendID) if err != nil { log.ZWarn(ctx, "getUserInfo failed", err, "sendID", sendID) @@ -259,7 +260,7 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s msg.CreateTime = utils.GetCurrentTimestampByMill() msg.ClientMsgID = utils.GetMsgID(sendID) optionsConfig := s.contentTypeConf[contentType] - if sesstionType == constant.SuperGroupChatType && contentType == constant.HasReadReceipt { + if sendID == recvID && contentType == constant.HasReadReceipt { optionsConfig.ReliabilityLevel = constant.UnreliableNotification } options := config.GetOptionsByNotification(optionsConfig) From 8a13017665ac0203a06f65aaf2d7a020e3e3ade8 Mon Sep 17 00:00:00 2001 From: a3d21 <93191329+a3d21@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:27:27 +0800 Subject: [PATCH 19/21] fix update user.FaceURL do not trigger GroupMemberInfoSetNotification (#1267) * fix update user.FaceURL do not trigger GroupMemberInfoSetNotification * cicd: robot automated Change --------- Co-authored-by: a3d21 --- cmd/openim-api/main.go | 1 + internal/rpc/group/group.go | 51 +++++++++++++++++---------- pkg/common/db/controller/msg.go | 18 ++++++++-- pkg/common/db/s3/minio/minio.go | 8 ++++- pkg/common/db/table/relation/group.go | 2 +- 5 files changed, 58 insertions(+), 22 deletions(-) diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index d1f5cb3f8..174300dc7 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -26,6 +26,7 @@ import ( "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" + "github.com/openimsdk/open-im-server/v3/internal/api" "github.com/openimsdk/open-im-server/v3/pkg/common/cmd" "github.com/openimsdk/open-im-server/v3/pkg/common/config" diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index de08d2eac..38dd0c32c 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -154,16 +154,16 @@ func (s *groupServer) CheckGroupAdmin(ctx context.Context, groupID string) error return nil } -func (s *groupServer) GetUsernameMap(ctx context.Context, userIDs []string, complete bool) (map[string]string, error) { +func (s *groupServer) GetPublicUserInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.PublicUserInfo, error) { if len(userIDs) == 0 { - return map[string]string{}, nil + return map[string]*sdkws.PublicUserInfo{}, nil } users, err := s.User.GetPublicUserInfos(ctx, userIDs, complete) if err != nil { return nil, err } - return utils.SliceToMapAny(users, func(e *sdkws.PublicUserInfo) (string, string) { - return e.UserID, e.Nickname + return utils.SliceToMapAny(users, func(e *sdkws.PublicUserInfo) (string, *sdkws.PublicUserInfo) { + return e.UserID, e }), nil } @@ -468,15 +468,18 @@ func (s *groupServer) GetGroupAllMember(ctx context.Context, req *pbgroup.GetGro if err != nil { return nil, err } - nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { - return e.UserID, e.Nickname == "" + publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { + return e.UserID, e.Nickname == "" || e.FaceURL == "" }), true) if err != nil { return nil, err } resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo { if e.Nickname == "" { - e.Nickname = nameMap[e.UserID] + e.Nickname = publicUserInfoMap[e.UserID].Nickname + } + if e.FaceURL == "" { + e.FaceURL = publicUserInfoMap[e.UserID].FaceURL } return convert.Db2PbGroupMember(e) }) @@ -616,15 +619,18 @@ func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbgroup.GetG if err != nil { return nil, err } - nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { - return e.UserID, e.Nickname == "" + publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { + return e.UserID, e.Nickname == "" || e.FaceURL == "" }), true) if err != nil { return nil, err } resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo { if e.Nickname == "" { - e.Nickname = nameMap[e.UserID] + e.Nickname = publicUserInfoMap[e.UserID].Nickname + } + if e.FaceURL == "" { + e.FaceURL = publicUserInfoMap[e.UserID].FaceURL } return convert.Db2PbGroupMember(e) }) @@ -1067,15 +1073,18 @@ func (s *groupServer) GetGroupMembersCMS(ctx context.Context, req *pbgroup.GetGr return nil, err } resp.Total = total - nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { - return e.UserID, e.Nickname == "" + nameMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { + return e.UserID, e.Nickname == "" || e.FaceURL == "" }), true) if err != nil { return nil, err } resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo { if e.Nickname == "" { - e.Nickname = nameMap[e.UserID] + e.Nickname = nameMap[e.UserID].Nickname + } + if e.FaceURL == "" { + e.FaceURL = nameMap[e.UserID].FaceURL } return convert.Db2PbGroupMember(e) }) @@ -1461,7 +1470,7 @@ func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbgroup.Ge if err != nil { return nil, err } - nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { + publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { return e.UserID, e.Nickname == "" }), true) if err != nil { @@ -1469,7 +1478,10 @@ func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbgroup.Ge } resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo { if e.Nickname == "" { - e.Nickname = nameMap[e.UserID] + e.Nickname = publicUserInfoMap[e.UserID].Nickname + } + if e.FaceURL == "" { + e.FaceURL = publicUserInfoMap[e.UserID].FaceURL } return convert.Db2PbGroupMember(e) }) @@ -1494,15 +1506,18 @@ func (s *groupServer) GetGroupMemberRoleLevel(ctx context.Context, req *pbgroup. if err != nil { return nil, err } - nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { - return e.UserID, e.Nickname == "" + publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) { + return e.UserID, e.Nickname == "" || e.FaceURL == "" }), true) if err != nil { return nil, err } resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo { if e.Nickname == "" { - e.Nickname = nameMap[e.UserID] + e.Nickname = publicUserInfoMap[e.UserID].Nickname + } + if e.FaceURL == "" { + e.FaceURL = publicUserInfoMap[e.UserID].FaceURL } return convert.Db2PbGroupMember(e) }) diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index 1bbf4cdf6..cda660afe 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -701,7 +701,14 @@ func (db *commonMsgDatabase) DeleteConversationMsgsAndSetMinSeq(ctx context.Cont return db.cache.SetMinSeq(ctx, conversationID, minSeq) } -func processMsgDocModel(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, userID, conversationID string, index int64, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, over bool) { +func processMsgDocModel( + ctx context.Context, + msgDocModel *unrelationtb.MsgDocModel, + userID, conversationID string, + index int64, + destructTime int64, + lastMsgDestructTime time.Time, +) (seqs []int64, over bool) { if len(msgDocModel.Msg) > 0 { i := 0 for _, msg := range msgDocModel.Msg { @@ -823,7 +830,14 @@ func handleFullAndExpiredForDeleteMsgRecursion(ctx context.Context, msgDocModel delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq } -func handleNotFullAndExpiredForDeleteMsgRecursion(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, remainTime, index int64, conversationID string, delStruct *delMsgRecursionStruct, db *commonMsgDatabase) { +func handleNotFullAndExpiredForDeleteMsgRecursion( + ctx context.Context, + msgDocModel *unrelationtb.MsgDocModel, + remainTime, index int64, + conversationID string, + delStruct *delMsgRecursionStruct, + db *commonMsgDatabase, +) { var delMsgIndexs []int for i, MsgInfoModel := range msgDocModel.Msg { if MsgInfoModel != nil && MsgInfoModel.Msg != nil { diff --git a/pkg/common/db/s3/minio/minio.go b/pkg/common/db/s3/minio/minio.go index a84b8c3f7..78ed381d5 100644 --- a/pkg/common/db/s3/minio/minio.go +++ b/pkg/common/db/s3/minio/minio.go @@ -430,7 +430,13 @@ func (m *Minio) presignedGetObject(ctx context.Context, name string, expire time return rawURL.String(), nil } -func (m *Minio) getImageInfoForAccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption, reqParams url.Values) (fileInfo *s3.ObjectInfo, objectInfoPath, msg string, err error) { +func (m *Minio) getImageInfoForAccessURL( + ctx context.Context, + name string, + expire time.Duration, + opt *s3.AccessURLOption, + reqParams url.Values, +) (fileInfo *s3.ObjectInfo, objectInfoPath, msg string, err error) { if opt != nil { if opt.ContentType != "" { reqParams.Set("response-content-type", opt.ContentType) diff --git a/pkg/common/db/table/relation/group.go b/pkg/common/db/table/relation/group.go index 24a75173d..309114ee2 100644 --- a/pkg/common/db/table/relation/group.go +++ b/pkg/common/db/table/relation/group.go @@ -30,7 +30,7 @@ type GroupModel struct { Introduction string `gorm:"column:introduction;size:255" json:"introduction"` FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"` CreateTime time.Time `gorm:"column:create_time;index:create_time;autoCreateTime"` - Ex string `gorm:"column:ex;size:1024" json:"ex"` + Ex string `gorm:"column:ex;size:1024" json:"ex"` Status int32 `gorm:"column:status"` CreatorUserID string `gorm:"column:creator_user_id;size:64"` GroupType int32 `gorm:"column:group_type"` From cb0394392b84e617e851d55ab55221e887e09bbc Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Tue, 24 Oct 2023 20:28:22 +0800 Subject: [PATCH 20/21] fix: reset branch. (#1277) * fix: to start im or chat, ZooKeeper must be started first. * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: go mod update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: get all userID Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: msggateway add online status call Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * chore: network mode change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * feat: add api of get server time Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: remove go work sum Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: pull message add isRead field Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: check msg-transfer script Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: start don't kill old process Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: check component Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: pull message set isRead only message come from single. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add ex field to update group info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: message log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: send message test log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * test: remove info log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: api of send message add sendTime field. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add callback for update user's info. * cicd: robot automated Change * fix: change callback command name. * cicd: robot automated Change * fix: single chat unread status change. * fix: single chat unread status change. * fix: single chat unread status change. * fix: user status change. * cicd: robot automated Change * fix: user status change. * fix: user status change. * fix: user status change. * cicd: robot automated Change * fix: ws close when user logout. * fix: remove repeat platform on online status. * cicd: robot automated Change * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * fix: reset branch * fix: not support redis cluster. CROSSSLOT Keys in request don't hash to the same slot * fix: update user.FaceURL do not trigger GroupMemberInfoSetNotification * cicd: robot automated Change * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. --------- Signed-off-by: Gordon <1432970085@qq.com> Signed-off-by: withchao <993506633@qq.com> Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: withchao <993506633@qq.com> Co-authored-by: Xinwei Xiong <3293172751NSS@gmail.com> Co-authored-by: FGadvancer Co-authored-by: withchao --- .golangci.yml | 2 +- cmd/openim-api/main.go | 1 - go.work | 8 +- internal/msgtransfer/init.go | 9 +- internal/rpc/msg/as_read.go | 3 +- internal/rpc/user/user.go | 9 +- internal/tools/msg.go | 6 +- pkg/authverify/token.go | 3 - pkg/callbackstruct/common.go | 1 - pkg/common/cmd/api.go | 9 +- pkg/common/cmd/cron_task.go | 2 - pkg/common/cmd/msg_gateway.go | 4 - pkg/common/cmd/msg_transfer.go | 2 - pkg/common/cmd/msg_utils.go | 27 +- pkg/common/cmd/root.go | 101 ++-- pkg/common/cmd/rpc.go | 9 +- pkg/common/config/config.go | 2 - pkg/common/config/parse.go | 9 +- pkg/common/convert/black.go | 8 +- pkg/common/convert/conversation.go | 4 - pkg/common/convert/friend.go | 22 +- pkg/common/convert/msg.go | 2 - pkg/common/convert/user.go | 2 - pkg/common/db/cache/black.go | 19 +- pkg/common/db/cache/friend.go | 20 +- pkg/common/db/cache/group.go | 7 +- pkg/common/db/cache/meta_cache.go | 12 +- pkg/common/db/cache/msg.go | 20 +- pkg/common/db/controller/auth.go | 7 +- pkg/common/db/controller/black.go | 4 - pkg/common/db/controller/conversation.go | 19 +- pkg/common/db/controller/friend.go | 51 +- pkg/common/db/controller/group.go | 26 - pkg/common/db/controller/msg.go | 462 +++++++----------- pkg/common/db/controller/msg_test.go | 1 - pkg/common/db/controller/s3.go | 1 - pkg/common/db/controller/user.go | 20 +- pkg/common/db/localcache/conversation.go | 1 - pkg/common/db/localcache/group.go | 2 - pkg/common/db/relation/black_model.go | 5 +- pkg/common/db/relation/chat_log_model.go | 8 +- pkg/common/db/relation/conversation_model.go | 6 - pkg/common/db/relation/friend_model.go | 5 - .../db/relation/friend_request_model.go | 6 - pkg/common/db/relation/group_member_model.go | 7 - pkg/common/db/relation/group_model.go | 4 - pkg/common/db/relation/group_request_model.go | 2 - pkg/common/db/relation/log_model.go | 9 +- pkg/common/db/relation/meta_db.go | 1 - pkg/common/db/relation/mysql_init.go | 12 +- pkg/common/db/relation/object_model.go | 2 - pkg/common/db/relation/user_model.go | 10 +- pkg/common/db/s3/cont/controller.go | 84 ++-- pkg/common/db/s3/cont/id.go | 2 - pkg/common/db/s3/cos/cos.go | 124 ++--- pkg/common/db/s3/minio/image.go | 19 +- pkg/common/db/s3/minio/minio.go | 175 ++----- pkg/common/db/s3/oss/oss.go | 119 ++--- pkg/common/db/table/relation/group.go | 2 +- pkg/common/db/table/relation/utils.go | 4 +- pkg/common/db/table/unrelation/msg.go | 3 - pkg/common/db/unrelation/mongo.go | 54 +- pkg/common/db/unrelation/msg.go | 134 ++--- pkg/common/db/unrelation/msg_convert.go | 68 ++- pkg/common/db/unrelation/super_group.go | 8 - pkg/common/db/unrelation/user.go | 21 +- .../k8s_discovery_register.go | 27 +- pkg/common/http/http_client.go | 20 +- pkg/common/kafka/consumer_group.go | 1 - pkg/common/kafka/producer.go | 7 +- pkg/common/locker/message_locker.go | 4 - pkg/common/prome/gather.go | 35 -- pkg/common/prome/prometheus.go | 4 - pkg/common/tls/tls.go | 3 - pkg/msgprocessor/conversation.go | 23 +- pkg/msgprocessor/options.go | 4 - pkg/rpcclient/auth.go | 1 - pkg/rpcclient/conversation.go | 10 +- pkg/rpcclient/friend.go | 5 - pkg/rpcclient/group.go | 14 - pkg/rpcclient/msg.go | 13 +- pkg/rpcclient/notification/friend.go | 33 +- pkg/rpcclient/notification/group.go | 118 ++--- pkg/rpcclient/notification/msg.go | 2 - pkg/rpcclient/notification/user.go | 52 +- pkg/rpcclient/push.go | 1 - pkg/rpcclient/third.go | 9 +- pkg/rpcclient/user.go | 12 - pkg/statistics/statistics.go | 6 +- tools/url2im/pkg/buffer.go | 2 +- tools/yamlfmt/yamlfmt.go | 2 +- 91 files changed, 778 insertions(+), 1441 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8785b72d8..dd68ce5a6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -510,7 +510,7 @@ linters-settings: nestif: # minimal complexity of if statements to report, 5 by default - min-complexity: 6 + min-complexity: 4 nilnil: # By default, nilnil checks all returned types below. diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index 174300dc7..d1f5cb3f8 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -26,7 +26,6 @@ import ( "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" - "github.com/openimsdk/open-im-server/v3/internal/api" "github.com/openimsdk/open-im-server/v3/pkg/common/cmd" "github.com/openimsdk/open-im-server/v3/pkg/common/config" diff --git a/go.work b/go.work index 33faf5195..1c819212c 100644 --- a/go.work +++ b/go.work @@ -1,18 +1,16 @@ go 1.19 - use ( . ./test/typecheck ./tools/changelog - ./tools/component - ./tools/data-conversion - ./tools/imctl //./tools/imctl ./tools/infra ./tools/ncpu ./tools/openim-web - ./tools/url2im ./tools/versionchecker ./tools/yamlfmt + ./tools/component + ./tools/url2im + ./tools/data-conversion ) diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index e5066633a..4487826ee 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -16,12 +16,10 @@ package msgtransfer import ( "fmt" - "sync" - + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + "sync" "github.com/OpenIMSDK/tools/mw" @@ -65,7 +63,8 @@ func StartTransfer(prometheusPort int) error { /* client, err := openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, openkeeper.WithFreq(time.Hour), openkeeper.WithRoundRobin(), openkeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/if err != nil { + config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/ + if err != nil { return err } if err := client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil { diff --git a/internal/rpc/msg/as_read.go b/internal/rpc/msg/as_read.go index 55c3ba088..6e3bbe987 100644 --- a/internal/rpc/msg/as_read.go +++ b/internal/rpc/msg/as_read.go @@ -147,6 +147,7 @@ func (m *msgServer) MarkConversationAsRead( for i := hasReadSeq + 1; i <= req.HasReadSeq; i++ { seqs = append(seqs, i) } + if len(seqs) > 0 { log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID) if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil { @@ -164,7 +165,6 @@ func (m *msgServer) MarkConversationAsRead( m.conversationAndGetRecvID(conversation, req.UserID), seqs, hasReadSeq); err != nil { return nil, err } - } else if conversation.ConversationType == constant.SuperGroupChatType || conversation.ConversationType == constant.NotificationChatType { if req.HasReadSeq > hasReadSeq { @@ -178,6 +178,7 @@ func (m *msgServer) MarkConversationAsRead( req.UserID, seqs, hasReadSeq); err != nil { return nil, err } + } return &msg.MarkConversationAsReadResp{}, nil diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 83573eeef..f2ceb3beb 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -290,8 +290,7 @@ func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbus // GetUserStatus Get the online status of the user. func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatusReq) (resp *pbuser.GetUserStatusResp, - err error, -) { + err error) { onlineStatusList, err := s.UserDatabase.GetUserStatus(ctx, req.UserIDs) if err != nil { return nil, err @@ -301,8 +300,7 @@ func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatu // SetUserStatus Synchronize user's online status. func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatusReq) (resp *pbuser.SetUserStatusResp, - err error, -) { + err error) { err = s.UserDatabase.SetUserStatus(ctx, req.UserID, req.Status, req.PlatformID) if err != nil { return nil, err @@ -326,8 +324,7 @@ func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatu // GetSubscribeUsersStatus Get the online status of subscribers. func (s *userServer) GetSubscribeUsersStatus(ctx context.Context, - req *pbuser.GetSubscribeUsersStatusReq, -) (*pbuser.GetSubscribeUsersStatusResp, error) { + req *pbuser.GetSubscribeUsersStatusReq) (*pbuser.GetSubscribeUsersStatusResp, error) { userList, err := s.UserDatabase.GetAllSubscribeList(ctx, req.UserID) if err != nil { return nil, err diff --git a/internal/tools/msg.go b/internal/tools/msg.go index ca095051c..5397689b2 100644 --- a/internal/tools/msg.go +++ b/internal/tools/msg.go @@ -17,13 +17,11 @@ package tools import ( "context" "fmt" - "math" - + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" "github.com/redis/go-redis/v9" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - - "github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register" + "math" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" diff --git a/pkg/authverify/token.go b/pkg/authverify/token.go index 724d4934f..a8e577fde 100644 --- a/pkg/authverify/token.go +++ b/pkg/authverify/token.go @@ -41,7 +41,6 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) { if opUserID == ownerUserID { return nil } - return errs.ErrNoPermission.Wrap(utils.GetSelfFuncName()) } @@ -53,7 +52,6 @@ func CheckAdmin(ctx context.Context) error { if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.UserID) { return nil } - return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx))) } @@ -76,6 +74,5 @@ func WsVerifyToken(token, userID string, platformID int) error { if claim.PlatformID != platformID { return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID)) } - return nil } diff --git a/pkg/callbackstruct/common.go b/pkg/callbackstruct/common.go index 8b320a04f..ef84d52b9 100644 --- a/pkg/callbackstruct/common.go +++ b/pkg/callbackstruct/common.go @@ -61,7 +61,6 @@ func (c CommonCallbackResp) Parse() error { if c.ActionCode != errs.NoError || c.ErrCode != errs.NoError { return errs.NewCodeError(int(c.ErrCode), c.ErrMsg).WithDetail(c.ErrDlt) } - return nil } diff --git a/pkg/common/cmd/api.go b/pkg/common/cmd/api.go index 98a200f14..7ce872fac 100644 --- a/pkg/common/cmd/api.go +++ b/pkg/common/cmd/api.go @@ -16,11 +16,9 @@ package cmd import ( "fmt" - "github.com/OpenIMSDK/protocol/constant" - "github.com/spf13/cobra" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/spf13/cobra" ) type ApiCmd struct { @@ -44,7 +42,8 @@ func (a *ApiCmd) GetPortFromConfig(portType string) int { fmt.Println("GetPortFromConfig:", portType) if portType == constant.FlagPort { return config2.Config.Api.OpenImApiPort[0] - } + } else { - return 0 + return 0 + } } diff --git a/pkg/common/cmd/cron_task.go b/pkg/common/cmd/cron_task.go index c1e18e5cf..1b0e796ac 100644 --- a/pkg/common/cmd/cron_task.go +++ b/pkg/common/cmd/cron_task.go @@ -23,7 +23,6 @@ type CronTaskCmd struct { func NewCronTaskCmd() *CronTaskCmd { ret := &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())} ret.SetRootCmdPt(ret) - return ret } @@ -35,6 +34,5 @@ func (c *CronTaskCmd) addRunE(f func() error) { func (c *CronTaskCmd) Exec(f func() error) error { c.addRunE(f) - return c.Execute() } diff --git a/pkg/common/cmd/msg_gateway.go b/pkg/common/cmd/msg_gateway.go index c9ecfa42b..c96bbd7af 100644 --- a/pkg/common/cmd/msg_gateway.go +++ b/pkg/common/cmd/msg_gateway.go @@ -31,7 +31,6 @@ type MsgGatewayCmd struct { func NewMsgGatewayCmd() *MsgGatewayCmd { ret := &MsgGatewayCmd{NewRootCmd("msgGateway")} ret.SetRootCmdPt(ret) - return ret } @@ -44,7 +43,6 @@ func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int { if port == 0 { port = m.PortFromConfig(constant.FlagWsPort) } - return port } @@ -56,10 +54,8 @@ func (m *MsgGatewayCmd) addRunE() { func (m *MsgGatewayCmd) Exec() error { m.addRunE() - return m.Execute() } - func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int { if portType == constant.FlagWsPort { return config2.Config.LongConnSvr.OpenImWsPort[0] diff --git a/pkg/common/cmd/msg_transfer.go b/pkg/common/cmd/msg_transfer.go index 1e0449b4c..20349ebbb 100644 --- a/pkg/common/cmd/msg_transfer.go +++ b/pkg/common/cmd/msg_transfer.go @@ -27,7 +27,6 @@ type MsgTransferCmd struct { func NewMsgTransferCmd() *MsgTransferCmd { ret := &MsgTransferCmd{NewRootCmd("msgTransfer")} ret.SetRootCmdPt(ret) - return ret } @@ -39,6 +38,5 @@ func (m *MsgTransferCmd) addRunE() { func (m *MsgTransferCmd) Exec() error { m.addRunE() - return m.Execute() } diff --git a/pkg/common/cmd/msg_utils.go b/pkg/common/cmd/msg_utils.go index 82306da8c..cfaf631ec 100644 --- a/pkg/common/cmd/msg_utils.go +++ b/pkg/common/cmd/msg_utils.go @@ -22,7 +22,7 @@ import ( type MsgUtilsCmd struct { cobra.Command - // msgTool *tools.MsgTool + msgTool *tools.MsgTool } func (m *MsgUtilsCmd) AddUserIDFlag() { @@ -31,7 +31,6 @@ func (m *MsgUtilsCmd) AddUserIDFlag() { func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string { userID, _ := cmdLines.Flags().GetString("userID") - return userID } @@ -39,17 +38,26 @@ func (m *MsgUtilsCmd) AddFixAllFlag() { m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs") } +func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool { + fixAll, _ := cmdLines.Flags().GetBool("fixAll") + return fixAll +} + func (m *MsgUtilsCmd) AddClearAllFlag() { m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs") } +func (m *MsgUtilsCmd) getClearAllFlag(cmdLines *cobra.Command) bool { + clearAll, _ := cmdLines.Flags().GetBool("clearAll") + return clearAll +} + func (m *MsgUtilsCmd) AddSuperGroupIDFlag() { m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID") } func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string { superGroupID, _ := cmdLines.Flags().GetString("superGroupID") - return superGroupID } @@ -57,10 +65,20 @@ func (m *MsgUtilsCmd) AddBeginSeqFlag() { m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq") } +func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 { + beginSeq, _ := cmdLines.Flags().GetInt64("beginSeq") + return beginSeq +} + func (m *MsgUtilsCmd) AddLimitFlag() { m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit") } +func (m *MsgUtilsCmd) getLimitFlag(cmdLines *cobra.Command) int64 { + limit, _ := cmdLines.Flags().GetInt64("limit") + return limit +} + func (m *MsgUtilsCmd) Execute() error { return m.Command.Execute() } @@ -113,7 +131,6 @@ func NewSeqCmd() *SeqCmd { seqCmd := &SeqCmd{ NewMsgUtilsCmd("seq", "seq", nil), } - return seqCmd } @@ -141,7 +158,6 @@ func (s *SeqCmd) GetSeqCmd() *cobra.Command { // println(seq) } } - return &s.Command } @@ -157,7 +173,6 @@ func NewMsgCmd() *MsgCmd { msgCmd := &MsgCmd{ NewMsgUtilsCmd("msg", "msg", nil), } - return msgCmd } diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index d1deb628c..7bff0f798 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -17,6 +17,8 @@ package cmd import ( "fmt" + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/spf13/cobra" "github.com/OpenIMSDK/protocol/constant" @@ -59,81 +61,62 @@ func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd { Short: fmt.Sprintf(`Start %s `, name), Long: fmt.Sprintf(`Start %s `, name), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil { - panic(err) - } - cmdOpts := &CmdOpts{} - for _, opt := range opts { - opt(cmdOpts) - } - if cmdOpts.loggerPrefixName == "" { - cmdOpts.loggerPrefixName = "OpenIM.log.all" - } - err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel, - config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, - config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime) - if err != nil { - panic(err) - } - - return nil + return rootCmd.persistentPreRun(cmd, opts...) }, } rootCmd.Command = cmd rootCmd.addConfFlag() - return rootCmd } -// func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error { -// if err := rc.initializeConfiguration(cmd); err != nil { -// return fmt.Errorf("failed to get configuration from command: %w", err) -// } +func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error { + if err := rc.initializeConfiguration(cmd); err != nil { + return fmt.Errorf("failed to get configuration from command: %w", err) + } -// cmdOpts := rc.applyOptions(opts...) + cmdOpts := rc.applyOptions(opts...) -// if err := rc.initializeLogger(cmdOpts); err != nil { -// return fmt.Errorf("failed to initialize from config: %w", err) -// } + if err := rc.initializeLogger(cmdOpts); err != nil { + return fmt.Errorf("failed to initialize from config: %w", err) + } -// return nil -// } + return nil +} -//nolint:unused //unused work wrongly func (rc *RootCmd) initializeConfiguration(cmd *cobra.Command) error { return rc.getConfFromCmdAndInit(cmd) } -// func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts { -// cmdOpts := defaultCmdOpts() -// for _, opt := range opts { -// opt(cmdOpts) -// } +func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts { + cmdOpts := defaultCmdOpts() + for _, opt := range opts { + opt(cmdOpts) + } -// return cmdOpts -// } + return cmdOpts +} -// func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error { -// logConfig := config.Config.Log +func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error { + logConfig := config.Config.Log -// return log.InitFromConfig( + return log.InitFromConfig( -// cmdOpts.loggerPrefixName, -// rc.Name, -// logConfig.RemainLogLevel, -// logConfig.IsStdout, -// logConfig.IsJson, -// logConfig.StorageLocation, -// logConfig.RemainRotationCount, -// logConfig.RotationTime, -// ) -// } + cmdOpts.loggerPrefixName, + rc.Name, + logConfig.RemainLogLevel, + logConfig.IsStdout, + logConfig.IsJson, + logConfig.StorageLocation, + logConfig.RemainRotationCount, + logConfig.RotationTime, + ) +} -// func defaultCmdOpts() *CmdOpts { -// return &CmdOpts{ -// loggerPrefixName: "OpenIM.log.all", -// } -// } +func defaultCmdOpts() *CmdOpts { + return &CmdOpts{ + loggerPrefixName: "OpenIM.log.all", + } +} func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) { r.cmdItf = cmdItf @@ -152,7 +135,6 @@ func (r *RootCmd) getPortFlag(cmd *cobra.Command) int { if port == 0 { port = r.PortFromConfig(constant.FlagPort) } - return port } @@ -169,7 +151,6 @@ func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int { if port == 0 { port = r.PortFromConfig(constant.FlagPrometheusPort) } - return port } @@ -180,8 +161,7 @@ func (r *RootCmd) GetPrometheusPortFlag() int { func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error { configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf) fmt.Println("configFolderPath:", configFolderPath) - - return config.InitConfig(configFolderPath) + return config2.InitConfig(configFolderPath) } func (r *RootCmd) Execute() error { @@ -194,12 +174,9 @@ func (r *RootCmd) AddCommand(cmds ...*cobra.Command) { func (r *RootCmd) GetPortFromConfig(portType string) int { fmt.Println("RootCmd.GetPortFromConfig:", portType) - return 0 } - func (r *RootCmd) PortFromConfig(portType string) int { fmt.Println("PortFromConfig:", portType) - return r.cmdItf.GetPortFromConfig(portType) } diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go index 6d34c6603..224edc0a0 100644 --- a/pkg/common/cmd/rpc.go +++ b/pkg/common/cmd/rpc.go @@ -16,13 +16,11 @@ package cmd import ( "errors" - "github.com/OpenIMSDK/protocol/constant" + config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/spf13/cobra" "google.golang.org/grpc" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc" @@ -35,7 +33,6 @@ type RpcCmd struct { func NewRpcCmd(name string) *RpcCmd { ret := &RpcCmd{NewRootCmd(name)} ret.SetRootCmdPt(ret) - return ret } @@ -44,7 +41,6 @@ func (a *RpcCmd) Exec() error { a.port = a.getPortFlag(cmd) a.prometheusPort = a.getPrometheusPortFlag(cmd) } - return a.Execute() } @@ -55,10 +51,8 @@ func (a *RpcCmd) StartSvr( if a.GetPortFlag() == 0 { return errors.New("port is required") } - return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn) } - func (a *RpcCmd) GetPortFromConfig(portType string) int { switch a.Name { case RpcPushServer: @@ -94,6 +88,5 @@ func (a *RpcCmd) GetPortFromConfig(portType string) int { return config2.Config.RpcPort.OpenImUserPort[0] } } - return 0 } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index d521fbd51..95f4a864e 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -336,7 +336,6 @@ func (c *configStruct) RegisterConf2Registry(registry discoveryregistry.SvcDisco if err != nil { return err } - return registry.RegisterConf2Registry(ConfKey, data) } @@ -349,6 +348,5 @@ func (c *configStruct) EncodeConfig() []byte { if err := yaml.NewEncoder(buf).Encode(c); err != nil { panic(err) } - return buf.Bytes() } diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 86292a597..2c20f1a98 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -21,9 +21,8 @@ import ( "path/filepath" "github.com/OpenIMSDK/protocol/constant" - "gopkg.in/yaml.v3" - "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" + "gopkg.in/yaml.v3" ) //go:embed version @@ -35,7 +34,7 @@ const ( DefaultFolderPath = "../config/" ) -// getProjectRoot returns the absolute path of the project root directory. +// getProjectRoot returns the absolute path of the project root directory func GetProjectRoot() string { b, _ := filepath.Abs(os.Args[0]) @@ -57,7 +56,6 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent()) } opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg)) - return opts } @@ -78,7 +76,6 @@ func initConfig(config interface{}, configName, configFolderPath string) error { return fmt.Errorf("unmarshal yaml error: %w", err) } fmt.Println("use config", configFolderPath) - return nil } @@ -96,5 +93,5 @@ func InitConfig(configFolderPath string) error { return err } - return nil + return initConfig(&Config.Notification, NotificationFileName, configFolderPath) } diff --git a/pkg/common/convert/black.go b/pkg/common/convert/black.go index 9c862d5b7..50c270dcb 100644 --- a/pkg/common/convert/black.go +++ b/pkg/common/convert/black.go @@ -18,6 +18,7 @@ import ( "context" "github.com/OpenIMSDK/protocol/sdkws" + sdk "github.com/OpenIMSDK/protocol/sdkws" "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" ) @@ -26,11 +27,11 @@ func BlackDB2Pb( ctx context.Context, blackDBs []*relation.BlackModel, f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), -) (blackPbs []*sdkws.BlackInfo, err error) { +) (blackPbs []*sdk.BlackInfo, err error) { if len(blackDBs) == 0 { return nil, nil } - userIDs := make([]string, 0, len(blackDBs)) + var userIDs []string for _, blackDB := range blackDBs { userIDs = append(userIDs, blackDB.BlockUserID) } @@ -39,7 +40,7 @@ func BlackDB2Pb( return nil, err } for _, blackDB := range blackDBs { - blackPb := &sdkws.BlackInfo{ + blackPb := &sdk.BlackInfo{ OwnerUserID: blackDB.OwnerUserID, CreateTime: blackDB.CreateTime.Unix(), AddSource: blackDB.AddSource, @@ -54,6 +55,5 @@ func BlackDB2Pb( } blackPbs = append(blackPbs, blackPb) } - return blackPbs, nil } diff --git a/pkg/common/convert/conversation.go b/pkg/common/convert/conversation.go index 295ff4c82..165262b7f 100644 --- a/pkg/common/convert/conversation.go +++ b/pkg/common/convert/conversation.go @@ -27,7 +27,6 @@ func ConversationDB2Pb(conversationDB *relation.ConversationModel) *conversation if err := utils.CopyStructFields(conversationPB, conversationDB); err != nil { return nil } - return conversationPB } @@ -40,7 +39,6 @@ func ConversationsDB2Pb(conversationsDB []*relation.ConversationModel) (conversa conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.Unix() conversationsPB = append(conversationsPB, conversationPB) } - return conversationsPB } @@ -49,7 +47,6 @@ func ConversationPb2DB(conversationPB *conversation.Conversation) *relation.Conv if err := utils.CopyStructFields(conversationDB, conversationPB); err != nil { return nil } - return conversationDB } @@ -61,6 +58,5 @@ func ConversationsPb2DB(conversationsPB []*conversation.Conversation) (conversat } conversationsDB = append(conversationsDB, conversationDB) } - return conversationsDB } diff --git a/pkg/common/convert/friend.go b/pkg/common/convert/friend.go index 41907d5ca..7003c8aa6 100644 --- a/pkg/common/convert/friend.go +++ b/pkg/common/convert/friend.go @@ -16,7 +16,6 @@ package convert import ( "context" - "fmt" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/tools/utils" @@ -26,13 +25,9 @@ import ( func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel { dbFriend := &relation.FriendModel{} - err := utils.CopyStructFields(dbFriend, friend) - if err != nil { - panic(err) - } + utils.CopyStructFields(dbFriend, friend) dbFriend.FriendUserID = friend.FriendUser.UserID dbFriend.CreateTime = utils.UnixSecondToTime(friend.CreateTime) - return dbFriend } @@ -42,10 +37,7 @@ func FriendDB2Pb( getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), ) (*sdkws.FriendInfo, error) { pbfriend := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}} - err := utils.CopyStructFields(pbfriend, friendDB) - if err != nil { - panic(err) - } + utils.CopyStructFields(pbfriend, friendDB) users, err := getUsers(ctx, []string{friendDB.FriendUserID}) if err != nil { return nil, err @@ -55,7 +47,6 @@ func FriendDB2Pb( pbfriend.FriendUser.FaceURL = users[friendDB.FriendUserID].FaceURL pbfriend.FriendUser.Ex = users[friendDB.FriendUserID].Ex pbfriend.CreateTime = friendDB.CreateTime.Unix() - return pbfriend, nil } @@ -67,7 +58,7 @@ func FriendsDB2Pb( if len(friendsDB) == 0 { return nil, nil } - userID := make([]string, 0, len(friendsDB)) + var userID []string for _, friendDB := range friendsDB { userID = append(userID, friendDB.FriendUserID) } @@ -77,8 +68,7 @@ func FriendsDB2Pb( } for _, friend := range friendsDB { friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}} - err2 := utils.CopyStructFields(friendPb, friend) - err = fmt.Errorf("%w, %w", err, err2) + utils.CopyStructFields(friendPb, friend) friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname friendPb.FriendUser.FaceURL = users[friend.FriendUserID].FaceURL @@ -86,8 +76,7 @@ func FriendsDB2Pb( friendPb.CreateTime = friend.CreateTime.Unix() friendsPb = append(friendsPb, friendPb) } - - return friendsPb, err + return friendsPb, nil } func FriendRequestDB2Pb( @@ -127,6 +116,5 @@ func FriendRequestDB2Pb( Ex: friendRequest.Ex, }) } - return res, nil } diff --git a/pkg/common/convert/msg.go b/pkg/common/convert/msg.go index 23bd5dfea..56f71f018 100644 --- a/pkg/common/convert/msg.go +++ b/pkg/common/convert/msg.go @@ -55,7 +55,6 @@ func MsgPb2DB(msg *sdkws.MsgData) *unrelation.MsgDataModel { msgDataModel.AtUserIDList = msg.AtUserIDList msgDataModel.AttachedInfo = msg.AttachedInfo msgDataModel.Ex = msg.Ex - return &msgDataModel } @@ -96,6 +95,5 @@ func MsgDB2Pb(msgModel *unrelation.MsgDataModel) *sdkws.MsgData { msg.AtUserIDList = msgModel.AtUserIDList msg.AttachedInfo = msgModel.AttachedInfo msg.Ex = msgModel.Ex - return &msg } diff --git a/pkg/common/convert/user.go b/pkg/common/convert/user.go index 38496515f..abb3a2144 100644 --- a/pkg/common/convert/user.go +++ b/pkg/common/convert/user.go @@ -34,7 +34,6 @@ func UsersDB2Pb(users []*relationtb.UserModel) (result []*sdkws.UserInfo) { userPb.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt result = append(result, &userPb) } - return result } @@ -47,6 +46,5 @@ func UserPb2DB(user *sdkws.UserInfo) *relationtb.UserModel { userDB.CreateTime = time.UnixMilli(user.CreateTime) userDB.AppMangerLevel = user.AppMangerLevel userDB.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt - return &userDB } diff --git a/pkg/common/db/cache/black.go b/pkg/common/db/cache/black.go index 5a70097ed..d1abe945c 100644 --- a/pkg/common/db/cache/black.go +++ b/pkg/common/db/cache/black.go @@ -62,7 +62,12 @@ func NewBlackCacheRedis( } func (b *BlackCacheRedis) NewCache() BlackCache { - return &BlackCacheRedis{expireTime: b.expireTime, rcClient: b.rcClient, blackDB: b.blackDB, metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...)} + return &BlackCacheRedis{ + expireTime: b.expireTime, + rcClient: b.rcClient, + blackDB: b.blackDB, + metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...), + } } func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string { @@ -70,9 +75,15 @@ func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string { } func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) { - return getCache(ctx, b.rcClient, b.getBlackIDsKey(userID), b.expireTime, func(ctx context.Context) ([]string, error) { - return b.blackDB.FindBlackUserIDs(ctx, userID) - }) + return getCache( + ctx, + b.rcClient, + b.getBlackIDsKey(userID), + b.expireTime, + func(ctx context.Context) ([]string, error) { + return b.blackDB.FindBlackUserIDs(ctx, userID) + }, + ) } func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache { diff --git a/pkg/common/db/cache/friend.go b/pkg/common/db/cache/friend.go index 28bb971b4..64a358984 100644 --- a/pkg/common/db/cache/friend.go +++ b/pkg/common/db/cache/friend.go @@ -53,9 +53,9 @@ type FriendCacheRedis struct { rcClient *rockscache.Client } -func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface, options rockscache.Options) FriendCache { +func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface, + options rockscache.Options) FriendCache { rcClient := rockscache.NewClient(rdb, options) - return &FriendCacheRedis{ metaCache: NewMetaCacheRedis(rcClient), friendDB: friendDB, @@ -64,12 +64,12 @@ func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendMo } } -func (c *FriendCacheRedis) NewCache() FriendCache { +func (f *FriendCacheRedis) NewCache() FriendCache { return &FriendCacheRedis{ - rcClient: c.rcClient, - metaCache: NewMetaCacheRedis(c.rcClient, c.metaCache.GetPreDelKeys()...), - friendDB: c.friendDB, - expireTime: c.expireTime, + rcClient: f.rcClient, + metaCache: NewMetaCacheRedis(f.rcClient, f.metaCache.GetPreDelKeys()...), + friendDB: f.friendDB, + expireTime: f.expireTime, } } @@ -128,8 +128,10 @@ func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID s return newFriendCache } -func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error) { - return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID, friendUserID), f.expireTime, func(ctx context.Context) (*relationtb.FriendModel, error) { +func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, + friendUserID string) (friend *relationtb.FriendModel, err error) { + return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID, + friendUserID), f.expireTime, func(ctx context.Context) (*relationtb.FriendModel, error) { return f.friendDB.Take(ctx, ownerUserID, friendUserID) }) } diff --git a/pkg/common/db/cache/group.go b/pkg/common/db/cache/group.go index d505772eb..6a4b57813 100644 --- a/pkg/common/db/cache/group.go +++ b/pkg/common/db/cache/group.go @@ -412,7 +412,12 @@ func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, groupID strin }) } -func (g *GroupCacheRedis) GetGroupMembersPage(ctx context.Context, groupID string, userIDs []string, showNumber, pageNumber int32) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) { +func (g *GroupCacheRedis) GetGroupMembersPage( + ctx context.Context, + groupID string, + userIDs []string, + showNumber, pageNumber int32, +) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) { groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID) if err != nil { return 0, nil, err diff --git a/pkg/common/db/cache/meta_cache.go b/pkg/common/db/cache/meta_cache.go index 5cff3df7f..549a0ea69 100644 --- a/pkg/common/db/cache/meta_cache.go +++ b/pkg/common/db/cache/meta_cache.go @@ -154,7 +154,8 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin return t, nil } -//func batchGetCache[T any](ctx context.Context, rcClient *rockscache.Client, keys []string, expire time.Duration, keyIndexFn func(t T, keys []string) (int, error), fn func(ctx context.Context) ([]T, error)) ([]T, error) { +// func batchGetCache[T any](ctx context.Context, rcClient *rockscache.Client, keys []string, expire time.Duration, keyIndexFn func(t T, keys []string) (int, error), fn func(ctx context.Context) ([]T, +// error)) ([]T, error) { // batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { // values := make(map[int]string) // tArrays, err := fn(ctx) @@ -191,7 +192,14 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin // return tArrays, nil //} -func batchGetCache2[T any, K comparable](ctx context.Context, rcClient *rockscache.Client, expire time.Duration, keys []K, keyFn func(key K) string, fns func(ctx context.Context, key K) (T, error)) ([]T, error) { +func batchGetCache2[T any, K comparable]( + ctx context.Context, + rcClient *rockscache.Client, + expire time.Duration, + keys []K, + keyFn func(key K) string, + fns func(ctx context.Context, key K) (T, error), +) ([]T, error) { if len(keys) == 0 { return nil, nil } diff --git a/pkg/common/db/cache/msg.go b/pkg/common/db/cache/msg.go index b55a76f62..2c869befb 100644 --- a/pkg/common/db/cache/msg.go +++ b/pkg/common/db/cache/msg.go @@ -17,11 +17,13 @@ package cache import ( "context" "errors" - "github.com/dtm-labs/rockscache" - unrelationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation" "strconv" "time" + "github.com/dtm-labs/rockscache" + + unrelationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation" + "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" "github.com/OpenIMSDK/tools/errs" @@ -718,21 +720,11 @@ func (c *msgCache) SetMessageTypeKeyValue( return errs.Wrap(c.rdb.HSet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err()) } -func (c *msgCache) SetMessageReactionExpire( - ctx context.Context, - clientMsgID string, - sessionType int32, - expiration time.Duration, -) (bool, error) { +func (c *msgCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) { return utils.Wrap2(c.rdb.Expire(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result()) } -func (c *msgCache) GetMessageTypeKeyValue( - ctx context.Context, - clientMsgID string, - sessionType int32, - typeKey string, -) (string, error) { +func (c *msgCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) { return utils.Wrap2(c.rdb.HGet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result()) } diff --git a/pkg/common/db/controller/auth.go b/pkg/common/db/controller/auth.go index 13d06a964..17b4a440d 100644 --- a/pkg/common/db/controller/auth.go +++ b/pkg/common/db/controller/auth.go @@ -69,9 +69,9 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI } } if len(deleteTokenKey) != 0 { - err2 := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey) - if err2 != nil { - return "", err2 + err := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey) + if err != nil { + return "", err } } claims := tokenverify.BuildClaims(userID, platformID, a.accessExpire) @@ -80,6 +80,5 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI if err != nil { return "", utils.Wrap(err, "") } - return tokenString, a.cache.AddTokenFlag(ctx, userID, platformID, tokenString, constant.NormalToken) } diff --git a/pkg/common/db/controller/black.go b/pkg/common/db/controller/black.go index 38147e4e9..70e942a77 100644 --- a/pkg/common/db/controller/black.go +++ b/pkg/common/db/controller/black.go @@ -55,7 +55,6 @@ func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackMode if err := b.black.Create(ctx, blacks); err != nil { return err } - return b.deleteBlackIDsCache(ctx, blacks) } @@ -64,7 +63,6 @@ func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackMode if err := b.black.Delete(ctx, blacks); err != nil { return err } - return b.deleteBlackIDsCache(ctx, blacks) } @@ -73,7 +71,6 @@ func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*relat for _, black := range blacks { cache = cache.DelBlackIDs(ctx, black.OwnerUserID) } - return cache.ExecDel(ctx) } @@ -100,7 +97,6 @@ func (b *blackDatabase) CheckIn( return } log.ZDebug(ctx, "blackIDs", "user1BlackIDs", userID1BlackIDs, "user2BlackIDs", userID2BlackIDs) - return utils.IsContain(userID2, userID1BlackIDs), utils.IsContain(userID1, userID2BlackIDs), nil } diff --git a/pkg/common/db/controller/conversation.go b/pkg/common/db/controller/conversation.go index 29cd0d152..b93f0bf06 100644 --- a/pkg/common/db/controller/conversation.go +++ b/pkg/common/db/controller/conversation.go @@ -99,8 +99,8 @@ func (c *conversationDatabase) SetUsersConversationFiledTx(ctx context.Context, now := time.Now() for _, v := range NotUserIDs { temp := new(relationtb.ConversationModel) - if err2 := utils.CopyStructFields(temp, conversation); err2 != nil { - return err2 + if err := utils.CopyStructFields(temp, conversation); err != nil { + return err } temp.OwnerUserID = v temp.CreateTime = now @@ -113,12 +113,10 @@ func (c *conversationDatabase) SetUsersConversationFiledTx(ctx context.Context, } cache = cache.DelConversationIDs(NotUserIDs...).DelUserConversationIDsHash(NotUserIDs...).DelConversations(conversation.ConversationID, NotUserIDs...) } - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -132,7 +130,6 @@ func (c *conversationDatabase) UpdateUsersConversationFiled(ctx context.Context, if _, ok := args["recv_msg_opt"]; ok { cache = cache.DelConversationNotReceiveMessageUserIDs(conversationID) } - return cache.ExecDel(ctx) } @@ -140,14 +137,13 @@ func (c *conversationDatabase) CreateConversation(ctx context.Context, conversat if err := c.conversationDB.Create(ctx, conversations); err != nil { return err } - userIDs := make([]string, 0, len(conversations)) + var userIDs []string cache := c.cache.NewCache() for _, conversation := range conversations { cache = cache.DelConversations(conversation.OwnerUserID, conversation.ConversationID) cache = cache.DelConversationNotReceiveMessageUserIDs(conversation.ConversationID) userIDs = append(userIDs, conversation.OwnerUserID) } - return cache.DelConversationIDs(userIDs...).DelUserConversationIDsHash(userIDs...).ExecDel(ctx) } @@ -182,12 +178,10 @@ func (c *conversationDatabase) SyncPeerUserPrivateConversationTx(ctx context.Con } } } - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -240,15 +234,12 @@ func (c *conversationDatabase) SetUserConversations(ctx context.Context, ownerUs if err != nil { return err } - cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID) - cache = cache.DelConversationNotReceiveMessageUserIDs(utils.Slice(notExistConversations, func(e *relationtb.ConversationModel) string { return e.ConversationID })...) + cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID).DelConversationNotReceiveMessageUserIDs(utils.Slice(notExistConversations, func(e *relationtb.ConversationModel) string { return e.ConversationID })...) } - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -285,12 +276,10 @@ func (c *conversationDatabase) CreateGroupChatConversation(ctx context.Context, for _, v := range existConversationUserIDs { cache = cache.DelConversations(v, conversationID) } - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index f35d6728b..7816ef935 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -16,7 +16,6 @@ package controller import ( "context" - "errors" "time" "gorm.io/gorm" @@ -110,7 +109,6 @@ func (f *friendDatabase) CheckIn( if err != nil { return } - return utils.IsContain(userID2, userID1FriendIDs), utils.IsContain(userID1, userID2FriendIDs), nil } @@ -123,8 +121,8 @@ func (f *friendDatabase) AddFriendRequest( ) (err error) { return f.tx.Transaction(func(tx any) error { _, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID) - // if there is a db error - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + // 有db错误 + if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound { return err } // 无错误 则更新 @@ -138,14 +136,12 @@ func (f *friendDatabase) AddFriendRequest( if err := f.friendRequest.NewTx(tx).UpdateByMap(ctx, fromUserID, toUserID, m); err != nil { return err } - return nil } // gorm.ErrRecordNotFound 错误,则新增 if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil { return err } - return nil }) } @@ -158,11 +154,11 @@ func (f *friendDatabase) BecomeFriends( addSource int32, ) (err error) { cache := f.cache.NewCache() - fn := func(tx any) error { - // first,find and drop delete ones - fs1, err2 := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs) - if err2 != nil { - return err2 + if err := f.tx.Transaction(func(tx any) error { + // 先find 找出重复的 去掉重复的 + fs1, err := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs) + if err != nil { + return err } opUserID := mcontext.GetOperationID(ctx) for _, v := range friendUserIDs { @@ -172,13 +168,13 @@ func (f *friendDatabase) BecomeFriends( return e.FriendUserID }) - err2 = f.friend.NewTx(tx).Create(ctx, fs11) - if err2 != nil { - return err2 + err = f.friend.NewTx(tx).Create(ctx, fs11) + if err != nil { + return err } - fs2, err2 := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs) - if err2 != nil { - return err2 + fs2, err := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs) + if err != nil { + return err } var newFriendIDs []string for _, v := range friendUserIDs { @@ -188,20 +184,16 @@ func (f *friendDatabase) BecomeFriends( fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string { return e.OwnerUserID }) - err2 = f.friend.NewTx(tx).Create(ctx, fs22) - if err2 != nil { - return err2 + err = f.friend.NewTx(tx).Create(ctx, fs22) + if err != nil { + return err } newFriendIDs = append(newFriendIDs, ownerUserID) cache = cache.DelFriendIDs(newFriendIDs...) - + return nil + }); err != nil { return nil } - err = f.tx.Transaction(fn) - if err != nil { - return err - } - return cache.ExecDel(ctx) } @@ -224,7 +216,6 @@ func (f *friendDatabase) RefuseFriendRequest( if err != nil { return err } - return nil } @@ -260,7 +251,7 @@ func (f *friendDatabase) AgreeFriendRequest( if err != nil { return err } - } else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + } else if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound { return err } @@ -299,7 +290,6 @@ func (f *friendDatabase) AgreeFriendRequest( return err } } - return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).ExecDel(ctx) }) } @@ -309,7 +299,6 @@ func (f *friendDatabase) Delete(ctx context.Context, ownerUserID string, friendU if err := f.friend.Delete(ctx, ownerUserID, friendUserIDs); err != nil { return err } - return f.cache.DelFriendIDs(append(friendUserIDs, ownerUserID)...).ExecDel(ctx) } @@ -318,7 +307,6 @@ func (f *friendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUs if err := f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark); err != nil { return err } - return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx) } @@ -371,7 +359,6 @@ func (f *friendDatabase) FindFriendsWithError( if len(friends) != len(friendUserIDs) { err = errs.ErrRecordNotFound.Wrap() } - return } diff --git a/pkg/common/db/controller/group.go b/pkg/common/db/controller/group.go index 0788429a8..194f3e8b2 100644 --- a/pkg/common/db/controller/group.go +++ b/pkg/common/db/controller/group.go @@ -102,7 +102,6 @@ func NewGroupDatabase( cache: cache, mongoDB: superGroup, } - return database } @@ -110,7 +109,6 @@ func InitGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, database *mongo.D rcOptions := rockscache.NewDefaultOptions() rcOptions.StrongConsistency = true rcOptions.RandomExpireAdjustment = 0.2 - return NewGroupDatabase( relation.NewGroupDB(db), relation.NewGroupMemberDB(db), @@ -153,7 +151,6 @@ func (g *groupDatabase) FindGroupMemberNum(ctx context.Context, groupID string) if err != nil { return 0, err } - return uint32(num), nil } @@ -187,12 +184,10 @@ func (g *groupDatabase) CreateGroup( cache = cache.DelJoinedGroupID(groupMember.UserID).DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID) } cache = cache.DelGroupsInfo(createGroupIDs...) - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -216,7 +211,6 @@ func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data ma if err := g.groupDB.UpdateMap(ctx, groupID, data); err != nil { return err } - return g.cache.DelGroupsInfo(groupID).ExecDel(ctx) } @@ -237,12 +231,10 @@ func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string, delete cache = cache.DelJoinedGroupID(userIDs...).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelGroupMembersHash(groupID) } cache = cache.DelGroupsInfo(groupID) - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -284,7 +276,6 @@ func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string, } res = append(res, v) } - return res, nil } if len(roleLevels) == 0 { @@ -295,10 +286,8 @@ func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string, } totalGroupMembers = append(totalGroupMembers, groupMembers...) } - return totalGroupMembers, nil } - return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels) } @@ -318,7 +307,6 @@ func (g *groupDatabase) PageGetJoinGroup( } totalGroupMembers = append(totalGroupMembers, groupMembers...) } - return uint32(len(groupIDs)), totalGroupMembers, nil } @@ -339,7 +327,6 @@ func (g *groupDatabase) PageGetGroupMember( if err != nil { return 0, nil, err } - return uint32(len(groupMemberIDs)), members, nil } @@ -391,7 +378,6 @@ func (g *groupDatabase) HandlerGroupRequest( return err } } - return nil }) } @@ -400,7 +386,6 @@ func (g *groupDatabase) DeleteGroupMember(ctx context.Context, groupID string, u if err := g.groupMemberDB.Delete(ctx, groupID, userIDs); err != nil { return err } - return g.cache.DelGroupMembersHash(groupID). DelGroupMemberIDs(groupID). DelGroupsMemberNum(groupID). @@ -425,7 +410,6 @@ func (g *groupDatabase) MapGroupMemberNum(ctx context.Context, groupIDs []string } m[groupID] = uint32(num) } - return m, nil } @@ -445,7 +429,6 @@ func (g *groupDatabase) TransferGroupOwner(ctx context.Context, groupID string, if rowsAffected != 1 { return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "") } - return g.cache.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).DelGroupMembersHash(groupID).ExecDel(ctx) }) } @@ -459,7 +442,6 @@ func (g *groupDatabase) UpdateGroupMember( if err := g.groupMemberDB.Update(ctx, groupID, userID, data); err != nil { return err } - return g.cache.DelGroupMembersInfo(groupID, userID).ExecDel(ctx) } @@ -472,12 +454,10 @@ func (g *groupDatabase) UpdateGroupMembers(ctx context.Context, data []*relation } cache = cache.DelGroupMembersInfo(item.GroupID, item.UserID) } - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -489,7 +469,6 @@ func (g *groupDatabase) CreateGroupRequest(ctx context.Context, requests []*rela return err } } - return db.Create(ctx, requests) }) } @@ -525,7 +504,6 @@ func (g *groupDatabase) CreateSuperGroup(ctx context.Context, groupID string, in if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDs); err != nil { return err } - return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(initMemberIDs...).ExecDel(ctx) } @@ -543,12 +521,10 @@ func (g *groupDatabase) DeleteSuperGroup(ctx context.Context, groupID string) er if len(models) > 0 { cache = cache.DelJoinedSuperGroupIDs(models[0].MemberIDs...) } - return nil }); err != nil { return err } - return cache.ExecDel(ctx) } @@ -556,7 +532,6 @@ func (g *groupDatabase) DeleteSuperGroupMember(ctx context.Context, groupID stri if err := g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs); err != nil { return err } - return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx) } @@ -564,7 +539,6 @@ func (g *groupDatabase) CreateSuperGroupMember(ctx context.Context, groupID stri if err := g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs); err != nil { return err } - return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx) } diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index cda660afe..af678f92c 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -135,7 +135,6 @@ func InitCommonMsgDatabase(rdb redis.UniversalClient, database *mongo.Database) cacheModel := cache.NewMsgCacheModel(rdb) msgDocModel := unrelation.NewMsgMongoDriver(database) CommonMsgDatabase := NewCommonMsgDatabase(msgDocModel, cacheModel) - return CommonMsgDatabase } @@ -151,17 +150,14 @@ type commonMsgDatabase struct { func (db *commonMsgDatabase) MsgToMQ(ctx context.Context, key string, msg2mq *sdkws.MsgData) error { _, _, err := db.producer.SendMessage(ctx, key, msg2mq) - return err } func (db *commonMsgDatabase) MsgToModifyMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData) error { if len(messages) > 0 { _, _, err := db.producerToModify.SendMessage(ctx, key, &pbmsg.MsgDataToModifyByMQ{ConversationID: conversationID, Messages: messages}) - return err } - return nil } @@ -169,26 +165,26 @@ func (db *commonMsgDatabase) MsgToPushMQ(ctx context.Context, key, conversationI partition, offset, err := db.producerToPush.SendMessage(ctx, key, &pbmsg.PushMsgDataToMQ{MsgData: msg2mq, ConversationID: conversationID}) if err != nil { log.ZError(ctx, "MsgToPushMQ", err, "key", key, "msg2mq", msg2mq) - return 0, 0, err } - return partition, offset, nil } func (db *commonMsgDatabase) MsgToMongoMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData, lastSeq int64) error { if len(messages) > 0 { _, _, err := db.producerToMongo.SendMessage(ctx, key, &pbmsg.MsgDataToMongoByMQ{LastSeq: lastSeq, ConversationID: conversationID, MsgData: messages}) - return err } - return nil } -func checkTypeForBatchInsertBlock(fields []any, key int8, firstSeq int64) error { +func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, firstSeq int64) error { + if len(fields) == 0 { + return nil + } + num := db.msg.GetSingleGocMsgNum() // num = 100 - for i, field := range fields { // check type + for i, field := range fields { // 检查类型 var ok bool switch key { case updateKeyMsg: @@ -206,106 +202,80 @@ func checkTypeForBatchInsertBlock(fields []any, key int8, firstSeq int64) error return errs.ErrInternalServer.Wrap("field type is invalid") } } - - return nil -} - -func (db *commonMsgDatabase) updateMsgModelForBatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, seq int64, i int) (bool, error) { - var ( - res *mongo.UpdateResult - err error - ) - docID := db.msg.GetDocID(conversationID, seq) - index := db.msg.GetMsgIndex(seq) - field := fields[i] - switch key { - case updateKeyMsg: - res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "msg", field) - case updateKeyRevoke: - res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "revoke", field) - } - if err != nil { - return false, err - } - - return res.MatchedCount > 0, nil -} - -func (db *commonMsgDatabase) newDocForBatchInsertBlock(conversationID string, fields []any, key int8, seq, firstSeq, num int64, i int) (unrelationtb.MsgDocModel, int) { - doc := unrelationtb.MsgDocModel{ - DocID: db.msg.GetDocID(conversationID, seq), - Msg: make([]*unrelationtb.MsgInfoModel, num), - } - var insert int // number of inserted - for j := i; j < len(fields); j++ { - seq = firstSeq + int64(j) - if db.msg.GetDocID(conversationID, seq) != doc.DocID { - break - } - insert++ + // 返回值为true表示数据库存在该文档,false表示数据库不存在该文档 + updateMsgModel := func(seq int64, i int) (bool, error) { + var ( + res *mongo.UpdateResult + err error + ) + docID := db.msg.GetDocID(conversationID, seq) + index := db.msg.GetMsgIndex(seq) + field := fields[i] switch key { case updateKeyMsg: - doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ - Msg: fields[j].(*unrelationtb.MsgDataModel), - } + res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "msg", field) case updateKeyRevoke: - doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ - Revoke: fields[j].(*unrelationtb.RevokeModel), - } - } - } - for i, model := range doc.Msg { - if model == nil { - model = &unrelationtb.MsgInfoModel{} - doc.Msg[i] = model + res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "revoke", field) } - if model.DelList == nil { - doc.Msg[i].DelList = []string{} + if err != nil { + return false, err } - } - - return doc, insert -} - -func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, firstSeq int64) error { - if len(fields) == 0 { - return nil - } - num := db.msg.GetSingleGocMsgNum() - // num = 100 - err := checkTypeForBatchInsertBlock(fields, key, firstSeq) - if err != nil { - return err + return res.MatchedCount > 0, nil } tryUpdate := true for i := 0; i < len(fields); i++ { - seq := firstSeq + int64(i) // current seq - // try update + seq := firstSeq + int64(i) // 当前seq if tryUpdate { - matched, err := db.updateMsgModelForBatchInsertBlock(ctx, conversationID, fields, key, seq, i) + matched, err := updateMsgModel(seq, i) if err != nil { return err } if matched { - continue // if matched,skip + continue // 匹配到了,继续下一个(不一定修改) + } + } + doc := unrelationtb.MsgDocModel{ + DocID: db.msg.GetDocID(conversationID, seq), + Msg: make([]*unrelationtb.MsgInfoModel, num), + } + var insert int // 插入的数量 + for j := i; j < len(fields); j++ { + seq = firstSeq + int64(j) + if db.msg.GetDocID(conversationID, seq) != doc.DocID { + break + } + insert++ + switch key { + case updateKeyMsg: + doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ + Msg: fields[j].(*unrelationtb.MsgDataModel), + } + case updateKeyRevoke: + doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{ + Revoke: fields[j].(*unrelationtb.RevokeModel), + } + } + } + for i, model := range doc.Msg { + if model == nil { + model = &unrelationtb.MsgInfoModel{} + doc.Msg[i] = model + } + if model.DelList == nil { + doc.Msg[i].DelList = []string{} } } - doc, insert := db.newDocForBatchInsertBlock(conversationID, fields, key, seq, firstSeq, num, i) - // insert doc into db if err := db.msgDocDatabase.Create(ctx, &doc); err != nil { if mongo.IsDuplicateKeyError(err) { - i-- // exists concurrent, - tryUpdate = true // try update - + i-- // 存在并发,重试当前数据 + tryUpdate = true // 以修改模式 continue } - return err } - tryUpdate = false // if insert success,change to insert mode - i += insert - 1 // skip inserted data + tryUpdate = false // 当前以插入成功,下一块优先插入模式 + i += insert - 1 // 跳过已插入的数据 } - return nil } @@ -352,7 +322,6 @@ func (db *commonMsgDatabase) BatchInsertChat2DB(ctx context.Context, conversatio Ex: msg.Ex, } } - return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyMsg, msgList[0].Seq) } @@ -369,11 +338,9 @@ func (db *commonMsgDatabase) MarkSingleChatMsgsAsRead(ctx context.Context, userI log.ZDebug(ctx, "MarkSingleChatMsgsAsRead", "userID", userID, "docID", docID, "indexes", indexes) if err := db.msgDocDatabase.MarkSingleChatMsgsAsRead(ctx, userID, docID, indexes); err != nil { log.ZError(ctx, "MarkSingleChatMsgsAsRead", err, "userID", userID, "docID", docID, "indexes", indexes) - return err } } - return nil } @@ -387,9 +354,8 @@ func (db *commonMsgDatabase) DelUserDeleteMsgsList(ctx context.Context, conversa func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (seq int64, isNew bool, err error) { currentMaxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { prome.Inc(prome.SeqGetFailedCounter) - return 0, false, err } prome.Inc(prome.SeqGetSuccessCounter) @@ -400,7 +366,7 @@ func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversa if lenList < 1 { return 0, false, errors.New("too short as 0") } - if errors.Is(err, redis.Nil) { + if errs.Unwrap(err) == redis.Nil { isNew = true } lastMaxSeq := currentMaxSeq @@ -430,7 +396,6 @@ func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversa } else { prome.Inc(prome.SeqSetSuccessCounter) } - return lastMaxSeq, isNew, utils.Wrap(err, "") } @@ -445,7 +410,6 @@ func (db *commonMsgDatabase) getMsgBySeqs(ctx context.Context, userID, conversat totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg)) } } - return totalMsgs, nil } @@ -456,7 +420,6 @@ func (db *commonMsgDatabase) findMsgInfoBySeq(ctx context.Context, userID, docID msg.Msg.IsRead = true } } - return msgs, err } @@ -475,76 +438,16 @@ func (db *commonMsgDatabase) getMsgBySeqsRange(ctx context.Context, userID strin seqMsgs = append(seqMsgs, convert.MsgDB2Pb(msg.Msg)) } } - return seqMsgs, nil } -func (db *commonMsgDatabase) getCacheMsgForGetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin int64, seqs []int64) ([]*sdkws.MsgData, []int64, error) { - newBegin := seqs[0] - newEnd := seqs[len(seqs)-1] - log.ZDebug(ctx, "GetMsgBySeqsRange", "first seqs", seqs, "newBegin", newBegin, "newEnd", newEnd) - cachedMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs) - if err != nil { - if !errors.Is(err, redis.Nil) { - prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs)) - log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", seqs) - } - } - var successMsgs []*sdkws.MsgData - if len(cachedMsgs) == 0 { - return successMsgs, failedSeqs, err - } - // if len(cachedMsgs) > 0 - delSeqs, err2 := db.cache.GetUserDelList(ctx, userID, conversationID) - if err2 != nil && !errors.Is(err2, redis.Nil) { - return nil, nil, err2 - } - var cacheDelNum int - for _, msg := range cachedMsgs { - if !utils.Contain(msg.Seq, delSeqs...) { - successMsgs = append(successMsgs, msg) - } else { - cacheDelNum += 1 - } - } - log.ZDebug(ctx, "get delSeqs from redis", "delSeqs", delSeqs, "userID", userID, "conversationID", conversationID, "cacheDelNum", cacheDelNum) - var reGetSeqsCache []int64 - for i := 1; i <= cacheDelNum; { - newSeq := newBegin - int64(i) - if newSeq >= begin { - if !utils.Contain(newSeq, delSeqs...) { - log.ZDebug(ctx, "seq del in cache, a new seq in range append", "new seq", newSeq) - reGetSeqsCache = append(reGetSeqsCache, newSeq) - i++ - } - } else { - break - } - } - if len(reGetSeqsCache) > 0 { - log.ZDebug(ctx, "reGetSeqsCache", "reGetSeqsCache", reGetSeqsCache) - cachedMsgs, failedSeqs2, err2 := db.cache.GetMessagesBySeq(ctx, conversationID, reGetSeqsCache) - if err2 != nil { - if !errors.Is(err2, redis.Nil) { - prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs2)) - log.ZError(ctx, "get message from redis exception", err2, "conversationID", conversationID, "seqs", reGetSeqsCache) - } - } - failedSeqs = append(failedSeqs, failedSeqs2...) - successMsgs = append(successMsgs, cachedMsgs...) - } - - return successMsgs, failedSeqs, err -} - func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin, end, num, userMaxSeq int64) (int64, int64, []*sdkws.MsgData, error) { - // 从缓存中获取最小和最大序列号,并根据给定的范围值进行调整 userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { return 0, 0, nil, err } minSeq, err := db.cache.GetMinSeq(ctx, conversationID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { return 0, 0, nil, err } if userMinSeq > minSeq { @@ -552,25 +455,18 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin } if minSeq > end { log.ZInfo(ctx, "minSeq > end", "minSeq", minSeq, "end", end) - return 0, 0, nil, nil } maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { return 0, 0, nil, err } - - // log out debug info log.ZDebug(ctx, "GetMsgBySeqsRange", "userMinSeq", userMinSeq, "conMinSeq", minSeq, "conMaxSeq", maxSeq, "userMaxSeq", userMaxSeq) - - // adjust maxSeq according to userMaxSeq if userMaxSeq != 0 { if userMaxSeq < maxSeq { maxSeq = userMaxSeq } } - - // adjust begin and end according to minSeq and maxSeq if begin < minSeq { begin = minSeq } @@ -580,8 +476,6 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin if end < begin { return 0, 0, nil, errs.ErrArgs.Wrap("seq end < begin") } - - // get seqs to search var seqs []int64 for i := end; i > end-num; i-- { if i >= begin { @@ -593,24 +487,67 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin if len(seqs) == 0 { return 0, 0, nil, nil } - - // get info from cache,and filter deleted msg - successMsgs, failedSeqs, err := db.getCacheMsgForGetMsgBySeqsRange(ctx, userID, conversationID, begin, seqs) + newBegin := seqs[0] + newEnd := seqs[len(seqs)-1] + log.ZDebug(ctx, "GetMsgBySeqsRange", "first seqs", seqs, "newBegin", newBegin, "newEnd", newEnd) + cachedMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs) if err != nil { - return 0, 0, nil, err + if err != redis.Nil { + prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs)) + log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", seqs) + } + } + var successMsgs []*sdkws.MsgData + if len(cachedMsgs) > 0 { + delSeqs, err := db.cache.GetUserDelList(ctx, userID, conversationID) + if err != nil && errs.Unwrap(err) != redis.Nil { + return 0, 0, nil, err + } + var cacheDelNum int + for _, msg := range cachedMsgs { + if !utils.Contain(msg.Seq, delSeqs...) { + successMsgs = append(successMsgs, msg) + } else { + cacheDelNum += 1 + } + } + log.ZDebug(ctx, "get delSeqs from redis", "delSeqs", delSeqs, "userID", userID, "conversationID", conversationID, "cacheDelNum", cacheDelNum) + var reGetSeqsCache []int64 + for i := 1; i <= cacheDelNum; { + newSeq := newBegin - int64(i) + if newSeq >= begin { + if !utils.Contain(newSeq, delSeqs...) { + log.ZDebug(ctx, "seq del in cache, a new seq in range append", "new seq", newSeq) + reGetSeqsCache = append(reGetSeqsCache, newSeq) + i++ + } + } else { + break + } + } + if len(reGetSeqsCache) > 0 { + log.ZDebug(ctx, "reGetSeqsCache", "reGetSeqsCache", reGetSeqsCache) + cachedMsgs, failedSeqs2, err := db.cache.GetMessagesBySeq(ctx, conversationID, reGetSeqsCache) + if err != nil { + if err != redis.Nil { + prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs2)) + log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", reGetSeqsCache) + } + } + failedSeqs = append(failedSeqs, failedSeqs2...) + successMsgs = append(successMsgs, cachedMsgs...) + } } - // log out debug info log.ZDebug(ctx, "get msgs from cache", "successMsgs", successMsgs) if len(failedSeqs) != 0 { log.ZDebug(ctx, "msgs not exist in redis", "seqs", failedSeqs) } - // if not found in cache,find in mongo + // get from cache or db prome.Add(prome.MsgPullFromRedisSuccessCounter, len(successMsgs)) if len(failedSeqs) > 0 { mongoMsgs, err := db.getMsgBySeqsRange(ctx, userID, conversationID, failedSeqs, begin, end) if err != nil { prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs)) - return 0, 0, nil, err } prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs)) @@ -622,15 +559,15 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) (int64, int64, []*sdkws.MsgData, error) { userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { return 0, 0, nil, err } minSeq, err := db.cache.GetMinSeq(ctx, conversationID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { return 0, 0, nil, err } maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) - if err != nil && !errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { return 0, 0, nil, err } if userMinSeq < minSeq { @@ -644,7 +581,7 @@ func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, co } successMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, newSeqs) if err != nil { - if !errors.Is(err, redis.Nil) { + if err != redis.Nil { prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs)) log.ZError(ctx, "get message from redis exception", err, "failedSeqs", failedSeqs, "conversationID", conversationID) } @@ -670,13 +607,11 @@ func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, co mongoMsgs, err := db.getMsgBySeqs(ctx, userID, conversationID, failedSeqs) if err != nil { prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs)) - return 0, 0, nil, err } prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs)) successMsgs = append(successMsgs, mongoMsgs...) } - return minSeq, maxSeq, successMsgs, nil } @@ -697,81 +632,61 @@ func (db *commonMsgDatabase) DeleteConversationMsgsAndSetMinSeq(ctx context.Cont log.ZWarn(ctx, "CleanUpOneUserAllMsg", err, "conversationID", conversationID) } } - return db.cache.SetMinSeq(ctx, conversationID, minSeq) } -func processMsgDocModel( - ctx context.Context, - msgDocModel *unrelationtb.MsgDocModel, - userID, conversationID string, - index int64, - destructTime int64, - lastMsgDestructTime time.Time, -) (seqs []int64, over bool) { - if len(msgDocModel.Msg) > 0 { - i := 0 - for _, msg := range msgDocModel.Msg { - i++ - if msg != nil && msg.Msg != nil && msg.Msg.SendTime+destructTime*1000 <= time.Now().UnixMilli() { - if msg.Msg.SendTime+destructTime*1000 > lastMsgDestructTime.UnixMilli() && !utils.Contain(userID, msg.DelList...) { - seqs = append(seqs, msg.Msg.Seq) - } - } else { - log.ZDebug(ctx, "all msg need destruct is found", "conversationID", conversationID, "userID", userID, "index", index, "stop index", i) - over = true - - return seqs, over - } - } - } - - return seqs, over -} - -func (db *commonMsgDatabase) UserMsgsDestruct(ctx context.Context, userID, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) { +func (db *commonMsgDatabase) UserMsgsDestruct(ctx context.Context, userID string, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) { var index int64 - - // refresh msg list for { - // from oldest to newest - msgDocModel, err2 := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1) - if err2 != nil || msgDocModel.DocID == "" { - if err2 != nil { - if errors.Is(err2, unrelation.ErrMsgListNotExist) { + // from oldest 2 newest + msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1) + if err != nil || msgDocModel.DocID == "" { + if err != nil { + if err == unrelation.ErrMsgListNotExist { log.ZDebug(ctx, "not doc find", "conversationID", conversationID, "userID", userID, "index", index) } else { - log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err2, "conversationID", conversationID, "index", index) + log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index) } } - // If there is an error or no message document is found, delete the message physically and return the sequence number, then end the recursion. + // 获取报错,或者获取不到了,物理删除并且返回seq delMongoMsgsPhysical(delStruct.delDocIDList), 结束递归 break } index++ //&& msgDocModel.Msg[0].Msg.SendTime > lastMsgDestructTime.UnixMilli() - curSeqs, over := processMsgDocModel(ctx, msgDocModel, userID, conversationID, index, destructTime, lastMsgDestructTime) - seqs = append(seqs, curSeqs...) - if over { - break + if len(msgDocModel.Msg) > 0 { + i := 0 + var over bool + for _, msg := range msgDocModel.Msg { + i++ + if msg != nil && msg.Msg != nil && msg.Msg.SendTime+destructTime*1000 <= time.Now().UnixMilli() { + if msg.Msg.SendTime+destructTime*1000 > lastMsgDestructTime.UnixMilli() && !utils.Contain(userID, msg.DelList...) { + seqs = append(seqs, msg.Msg.Seq) + } + } else { + log.ZDebug(ctx, "all msg need destruct is found", "conversationID", conversationID, "userID", userID, "index", index, "stop index", i) + over = true + break + } + } + if over { + break + } } } - // Log the result of the function call. + log.ZDebug(ctx, "UserMsgsDestruct", "conversationID", conversationID, "userID", userID, "seqs", seqs) - if len(seqs) == 0 { - return seqs, nil - } - // if len(seqs) > 0 - userMinSeq := seqs[len(seqs)-1] + 1 - currentUserMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) - if err != nil && !errors.Is(err, redis.Nil) { - return nil, err - } - if currentUserMinSeq < userMinSeq { - if err := db.cache.SetConversationUserMinSeq(ctx, conversationID, userID, userMinSeq); err != nil { + if len(seqs) > 0 { + userMinSeq := seqs[len(seqs)-1] + 1 + currentUserMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID) + if err != nil && errs.Unwrap(err) != redis.Nil { return nil, err } + if currentUserMinSeq < userMinSeq { + if err := db.cache.SetConversationUserMinSeq(ctx, conversationID, userID, userMinSeq); err != nil { + return nil, err + } + } } - return seqs, nil } @@ -794,65 +709,45 @@ func (db *commonMsgDatabase) deleteMsgRecursion(ctx context.Context, conversatio msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1) if err != nil || msgDocModel.DocID == "" { if err != nil { - if errors.Is(err, unrelation.ErrMsgListNotExist) { + if err == unrelation.ErrMsgListNotExist { log.ZDebug(ctx, "deleteMsgRecursion ErrMsgListNotExist", "conversationID", conversationID, "index:", index) } else { log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index) } } - // get error or miss content, delete physically and return minSeq,delMongoMsgsPhysical(delStruct.delDocIDList), end recursion + // 获取报错,或者获取不到了,物理删除并且返回seq delMongoMsgsPhysical(delStruct.delDocIDList), 结束递归 err = db.msgDocDatabase.DeleteDocs(ctx, delStruct.delDocIDs) if err != nil { return 0, err } - return delStruct.getSetMinSeq() + 1, nil } - log.ZDebug(ctx, "doc info", "conversationID", conversationID, "index", index, "docID", msgDocModel.DocID, "len", len(msgDocModel.Msg)) if int64(len(msgDocModel.Msg)) > db.msg.GetSingleGocMsgNum() { log.ZWarn(ctx, "msgs too large", nil, "lenth", len(msgDocModel.Msg), "docID:", msgDocModel.DocID) } - fullAndExpired := msgDocModel.IsFull() && msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill() - if fullAndExpired { - handleFullAndExpiredForDeleteMsgRecursion(ctx, msgDocModel, delStruct) + if msgDocModel.IsFull() && msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill() { + log.ZDebug(ctx, "doc is full and all msg is expired", "docID", msgDocModel.DocID) + delStruct.delDocIDs = append(delStruct.delDocIDs, msgDocModel.DocID) + delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq } else { - handleNotFullAndExpiredForDeleteMsgRecursion(ctx, msgDocModel, remainTime, index, conversationID, delStruct, db) - } - seq, err := db.deleteMsgRecursion(ctx, conversationID, index+1, delStruct, remainTime) - - return seq, err -} - -func handleFullAndExpiredForDeleteMsgRecursion(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, delStruct *delMsgRecursionStruct) { - log.ZDebug(ctx, "doc is full and all msg is expired", "docID", msgDocModel.DocID) - delStruct.delDocIDs = append(delStruct.delDocIDs, msgDocModel.DocID) - delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq -} - -func handleNotFullAndExpiredForDeleteMsgRecursion( - ctx context.Context, - msgDocModel *unrelationtb.MsgDocModel, - remainTime, index int64, - conversationID string, - delStruct *delMsgRecursionStruct, - db *commonMsgDatabase, -) { - var delMsgIndexs []int - for i, MsgInfoModel := range msgDocModel.Msg { - if MsgInfoModel != nil && MsgInfoModel.Msg != nil { - if utils.GetCurrentTimestampByMill() > MsgInfoModel.Msg.SendTime+(remainTime*1000) { - delMsgIndexs = append(delMsgIndexs, i) + var delMsgIndexs []int + for i, MsgInfoModel := range msgDocModel.Msg { + if MsgInfoModel != nil && MsgInfoModel.Msg != nil { + if utils.GetCurrentTimestampByMill() > MsgInfoModel.Msg.SendTime+(remainTime*1000) { + delMsgIndexs = append(delMsgIndexs, i) + } } } - } - if len(delMsgIndexs) > 0 { - err2 := db.msgDocDatabase.DeleteMsgsInOneDocByIndex(ctx, msgDocModel.DocID, delMsgIndexs) - if err2 != nil { - log.ZError(ctx, "deleteMsgRecursion DeleteMsgsInOneDocByIndex failed", err2, "conversationID", conversationID, "index", index) + if len(delMsgIndexs) > 0 { + if err := db.msgDocDatabase.DeleteMsgsInOneDocByIndex(ctx, msgDocModel.DocID, delMsgIndexs); err != nil { + log.ZError(ctx, "deleteMsgRecursion DeleteMsgsInOneDocByIndex failed", err, "conversationID", conversationID, "index", index) + } + delStruct.minSeq = int64(msgDocModel.Msg[delMsgIndexs[len(delMsgIndexs)-1]].Msg.Seq) } - delStruct.minSeq = msgDocModel.Msg[delMsgIndexs[len(delMsgIndexs)-1]].Msg.Seq } + seq, err := db.deleteMsgRecursion(ctx, conversationID, index+1, delStruct, remainTime) + return seq, err } func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conversationID string, allSeqs []int64) error { @@ -868,15 +763,13 @@ func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conve return err } } - return nil } func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) error { cachedMsgs, _, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs) - if err != nil && errors.Is(err, redis.Nil) { + if err != nil && errs.Unwrap(err) != redis.Nil { log.ZWarn(ctx, "DeleteUserMsgsBySeqs", err, "conversationID", conversationID, "seqs", seqs) - return err } if len(cachedMsgs) > 0 { @@ -896,7 +789,6 @@ func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID st } } } - return nil } @@ -908,12 +800,11 @@ func (db *commonMsgDatabase) CleanUpUserConversationsMsgs(ctx context.Context, u for _, conversationID := range conversationIDs { maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID) if err != nil { - if errors.Is(err, redis.Nil) { + if err == redis.Nil { log.ZInfo(ctx, "max seq is nil", "conversationID", conversationID) } else { log.ZError(ctx, "get max seq failed", err, "conversationID", conversationID) } - continue } if err := db.cache.SetMinSeq(ctx, conversationID, maxSeq+1); err != nil { @@ -1007,7 +898,6 @@ func (db *commonMsgDatabase) GetConversationMinMaxSeqInMongoAndCache(ctx context if err != nil { return } - return } @@ -1026,7 +916,6 @@ func (db *commonMsgDatabase) GetMinMaxSeqMongo(ctx context.Context, conversation return } maxSeqMongo = newestMsgMongo.Msg.Seq - return } @@ -1054,7 +943,7 @@ func (db *commonMsgDatabase) RangeGroupSendCount( } func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.SearchMessageReq) (total int32, msgData []*sdkws.MsgData, err error) { - totalMsgs := make([]*sdkws.MsgData, 0) + var totalMsgs []*sdkws.MsgData total, msgs, err := db.msgDocDatabase.SearchMessage(ctx, req) if err != nil { return 0, nil, err @@ -1065,7 +954,6 @@ func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.Searc } totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg)) } - return total, totalMsgs, nil } diff --git a/pkg/common/db/controller/msg_test.go b/pkg/common/db/controller/msg_test.go index 15448674b..80e2db122 100644 --- a/pkg/common/db/controller/msg_test.go +++ b/pkg/common/db/controller/msg_test.go @@ -162,7 +162,6 @@ func GetDB() *commonMsgDatabase { if err != nil { panic(err) } - return &commonMsgDatabase{ msgDocDatabase: unrelation.NewMsgMongoDriver(mongo.GetDatabase()), } diff --git a/pkg/common/db/controller/s3.go b/pkg/common/db/controller/s3.go index f848f15a4..6ef3e73b3 100644 --- a/pkg/common/db/controller/s3.go +++ b/pkg/common/db/controller/s3.go @@ -89,6 +89,5 @@ func (s *s3Database) AccessURL(ctx context.Context, name string, expire time.Dur if err != nil { return time.Time{}, "", err } - return expireTime, rawURL, nil } diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index d4a120f1c..9c6fdc5c4 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -90,7 +90,6 @@ func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel if len(miss) > 0 { _ = u.userDB.Create(ctx, miss) } - return nil } @@ -103,35 +102,30 @@ func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (use if len(users) != len(userIDs) { err = errs.ErrRecordNotFound.Wrap("userID not found") } - return } // Find Get the information of the specified user. If the userID is not found, no error will be returned. func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { users, err = u.cache.GetUsersInfo(ctx, userIDs) - return } // Create Insert multiple external guarantees that the userID is not repeated and does not exist in the db. func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) { - err = u.tx.Transaction(func(tx any) error { + if err := u.tx.Transaction(func(tx any) error { err = u.userDB.Create(ctx, users) if err != nil { return err } - return nil - }) - if err != nil { + }); err != nil { return err } - userIDs := make([]string, 0, len(users)) + var userIDs []string for _, user := range users { userIDs = append(userIDs, user.UserID) } - return u.cache.DelUsersInfo(userIDs...).ExecDel(ctx) } @@ -140,7 +134,6 @@ func (u *userDatabase) Update(ctx context.Context, user *relation.UserModel) (er if err := u.userDB.Update(ctx, user); err != nil { return err } - return u.cache.DelUsersInfo(user.UserID).ExecDel(ctx) } @@ -149,7 +142,6 @@ func (u *userDatabase) UpdateByMap(ctx context.Context, userID string, args map[ if err := u.userDB.UpdateByMap(ctx, userID, args); err != nil { return err } - return u.cache.DelUsersInfo(userID).ExecDel(ctx) } @@ -170,7 +162,6 @@ func (u *userDatabase) IsExist(ctx context.Context, userIDs []string) (exist boo if len(users) > 0 { return true, nil } - return false, nil } @@ -192,14 +183,12 @@ func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.T // SubscribeUsersStatus Subscribe or unsubscribe a user's presence status. func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error { err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs) - return err } // UnsubscribeUsersStatus unsubscribe a user's presence status. func (u *userDatabase) UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error { err := u.mongoDB.UnsubscriptionList(ctx, userID, userIDs) - return err } @@ -209,7 +198,6 @@ func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) ( if err != nil { return nil, err } - return list, nil } @@ -219,14 +207,12 @@ func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([] if err != nil { return nil, err } - return list, nil } // GetUserStatus get user status. func (u *userDatabase) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) { onlineStatusList, err := u.cache.GetUserStatus(ctx, userIDs) - return onlineStatusList, err } diff --git a/pkg/common/db/localcache/conversation.go b/pkg/common/db/localcache/conversation.go index b43e58257..c40bcdbce 100644 --- a/pkg/common/db/localcache/conversation.go +++ b/pkg/common/db/localcache/conversation.go @@ -50,7 +50,6 @@ func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, if err != nil { return nil, err } - return resp.UserIDs, nil } diff --git a/pkg/common/db/localcache/group.go b/pkg/common/db/localcache/group.go index 140c3aeaf..4958d91ee 100644 --- a/pkg/common/db/localcache/group.go +++ b/pkg/common/db/localcache/group.go @@ -57,7 +57,6 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) localHashInfo, ok := g.cache[groupID] if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash { g.lock.Unlock() - return localHashInfo.userIDs, nil } g.lock.Unlock() @@ -75,6 +74,5 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash, userIDs: groupMembersResp.UserIDs, } - return g.cache[groupID].userIDs, nil } diff --git a/pkg/common/db/relation/black_model.go b/pkg/common/db/relation/black_model.go index 58dae3745..34123c7a3 100644 --- a/pkg/common/db/relation/black_model.go +++ b/pkg/common/db/relation/black_model.go @@ -63,11 +63,10 @@ func (b *BlackGorm) Find( ctx context.Context, blacks []*relation.BlackModel, ) (blackList []*relation.BlackModel, err error) { - where := make([][]interface{}, 0, len(blacks)) + var where [][]interface{} for _, black := range blacks { where = append(where, []interface{}{black.OwnerUserID, black.BlockUserID}) } - return blackList, utils.Wrap( b.db(ctx).Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error, "", @@ -76,7 +75,6 @@ func (b *BlackGorm) Find( func (b *BlackGorm) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.BlackModel, err error) { black = &relation.BlackModel{} - return black, utils.Wrap( b.db(ctx).Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error, "", @@ -98,7 +96,6 @@ func (b *BlackGorm) FindOwnerBlacks( showNumber, ) total = int64(totalUint32) - return } diff --git a/pkg/common/db/relation/chat_log_model.go b/pkg/common/db/relation/chat_log_model.go index f474a2d34..f183a543f 100644 --- a/pkg/common/db/relation/chat_log_model.go +++ b/pkg/common/db/relation/chat_log_model.go @@ -15,8 +15,6 @@ package relation import ( - - //nolint:staticcheck //tofix: SA1019: "github.com/golang/protobuf/jsonpb" is deprecated: Use the "google.golang.org/protobuf/encoding/protojson" package instead. "github.com/golang/protobuf/jsonpb" "github.com/jinzhu/copier" "google.golang.org/protobuf/proto" @@ -40,10 +38,7 @@ func NewChatLogGorm(db *gorm.DB) relation.ChatLogModelInterface { func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error { chatLog := new(relation.ChatLogModel) - err := copier.Copy(chatLog, msg.MsgData) - if err != nil { - return err - } + copier.Copy(chatLog, msg.MsgData) switch msg.MsgData.SessionType { case constant.GroupChatType, constant.SuperGroupChatType: chatLog.RecvID = msg.MsgData.GroupID @@ -64,6 +59,5 @@ func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error { } chatLog.CreateTime = utils.UnixMillSecondToTime(msg.MsgData.CreateTime) chatLog.SendTime = utils.UnixMillSecondToTime(msg.MsgData.SendTime) - return c.DB.Create(chatLog).Error } diff --git a/pkg/common/db/relation/conversation_model.go b/pkg/common/db/relation/conversation_model.go index 37a4e02be..d5ca92ec2 100644 --- a/pkg/common/db/relation/conversation_model.go +++ b/pkg/common/db/relation/conversation_model.go @@ -54,7 +54,6 @@ func (c *ConversationGorm) UpdateByMap( args map[string]interface{}, ) (rows int64, err error) { result := c.db(ctx).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args) - return result.RowsAffected, utils.Wrap(result.Error, "") } @@ -80,7 +79,6 @@ func (c *ConversationGorm) Find( Error, "", ) - return conversations, err } @@ -89,7 +87,6 @@ func (c *ConversationGorm) Take( userID, conversationID string, ) (conversation *relation.ConversationModel, err error) { cc := &relation.ConversationModel{} - return cc, utils.Wrap( c.db(ctx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error, "", @@ -172,7 +169,6 @@ func (c *ConversationGorm) GetUserRecvMsgOpt( ownerUserID, conversationID string, ) (opt int, err error) { var conversation relation.ConversationModel - return int( conversation.RecvMsgOpt, ), utils.Wrap( @@ -223,7 +219,6 @@ func (c *ConversationGorm) GetConversationIDsNeedDestruct( func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID string, conversationID string) (int32, error) { var recvMsgOpt int32 - return recvMsgOpt, errs.Wrap( c.db(ctx). Model(&relation.ConversationModel{}). @@ -235,7 +230,6 @@ func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID func (c *ConversationGorm) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) { var userIDs []string - return userIDs, errs.Wrap( c.db(ctx). Model(&relation.ConversationModel{}). diff --git a/pkg/common/db/relation/friend_model.go b/pkg/common/db/relation/friend_model.go index 103afd8aa..869254455 100644 --- a/pkg/common/db/relation/friend_model.go +++ b/pkg/common/db/relation/friend_model.go @@ -50,7 +50,6 @@ func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserI Error, "", ) - return err } @@ -85,7 +84,6 @@ func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID } m := make(map[string]interface{}, 1) m["remark"] = "" - return utils.Wrap(f.db(ctx).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "") } @@ -95,7 +93,6 @@ func (f *FriendGorm) Take( ownerUserID, friendUserID string, ) (friend *relation.FriendModel, err error) { friend = &relation.FriendModel{} - return friend, utils.Wrap( f.db(ctx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "", @@ -159,7 +156,6 @@ func (f *FriendGorm) FindOwnerFriends( Error, "", ) - return } @@ -182,7 +178,6 @@ func (f *FriendGorm) FindInWhoseFriends( Error, "", ) - return } diff --git a/pkg/common/db/relation/friend_request_model.go b/pkg/common/db/relation/friend_request_model.go index 4fd372c0c..5678f7b7b 100644 --- a/pkg/common/db/relation/friend_request_model.go +++ b/pkg/common/db/relation/friend_request_model.go @@ -74,7 +74,6 @@ func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation. fr2 := *friendRequest fr2.FromUserID = "" fr2.ToUserID = "" - return utils.Wrap( f.db(ctx). Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID). @@ -94,7 +93,6 @@ func (f *FriendRequestGorm) Find( f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error, "", ) - return friendRequest, err } @@ -107,7 +105,6 @@ func (f *FriendRequestGorm) Take( f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error, "", ) - return friendRequest, err } @@ -130,7 +127,6 @@ func (f *FriendRequestGorm) FindToUserID( Error, "", ) - return } @@ -153,7 +149,6 @@ func (f *FriendRequestGorm) FindFromUserID( Error, "", ) - return } @@ -165,6 +160,5 @@ func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUser Error, "", ) - return } diff --git a/pkg/common/db/relation/group_member_model.go b/pkg/common/db/relation/group_member_model.go index 48baea61c..312e32054 100644 --- a/pkg/common/db/relation/group_member_model.go +++ b/pkg/common/db/relation/group_member_model.go @@ -68,7 +68,6 @@ func (g *GroupMemberGorm) UpdateRoleLevel( db := g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{ "role_level": roleLevel, }) - return db.RowsAffected, utils.Wrap(db.Error, "") } @@ -88,7 +87,6 @@ func (g *GroupMemberGorm) Find( if len(roleLevels) > 0 { db = db.Where("role_level in (?)", roleLevels) } - return groupMembers, utils.Wrap(db.Find(&groupMembers).Error, "") } @@ -98,7 +96,6 @@ func (g *GroupMemberGorm) Take( userID string, ) (groupMember *relation.GroupMemberModel, err error) { groupMember = &relation.GroupMemberModel{} - return groupMember, utils.Wrap( g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "", @@ -110,7 +107,6 @@ func (g *GroupMemberGorm) TakeOwner( groupID string, ) (groupMember *relation.GroupMemberModel, err error) { groupMember = &relation.GroupMemberModel{} - return groupMember, utils.Wrap( g.db(ctx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "", @@ -129,7 +125,6 @@ func (g *GroupMemberGorm) SearchMember( ormutil.GormIn(&db, "group_id", groupIDs) ormutil.GormIn(&db, "user_id", userIDs) ormutil.GormIn(&db, "role_level", roleLevels) - return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber) } @@ -157,7 +152,6 @@ func (g *GroupMemberGorm) FindJoinUserID( groupUsers[item.GroupID] = append(v, item.UserID) } } - return groupUsers, nil } @@ -188,7 +182,6 @@ func (g *GroupMemberGorm) FindUsersJoinedGroupID(ctx context.Context, userIDs [] result[groupMember.UserID] = append(v, groupMember.GroupID) } } - return result, nil } diff --git a/pkg/common/db/relation/group_model.go b/pkg/common/db/relation/group_model.go index 508a86f5f..7a8eee9f0 100644 --- a/pkg/common/db/relation/group_model.go +++ b/pkg/common/db/relation/group_model.go @@ -61,14 +61,12 @@ func (g *GroupGorm) Find(ctx context.Context, groupIDs []string) (groups []*rela func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.GroupModel, err error) { group = &relation.GroupModel{} - return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "") } func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) { db := g.DB db = db.WithContext(ctx).Where("status!=?", constant.GroupStatusDismissed) - return ormutil.GormSearch[relation.GroupModel](db, []string{"name"}, keyword, pageNumber, showNumber) } @@ -84,7 +82,6 @@ func (g *GroupGorm) CountTotal(ctx context.Context, before *time.Time) (count in if err := db.Count(&count).Error; err != nil { return 0, err } - return count, nil } @@ -101,7 +98,6 @@ func (g *GroupGorm) CountRangeEverydayTotal(ctx context.Context, start time.Time for _, r := range res { v[r.Date.Format("2006-01-02")] = r.Count } - return v, nil } diff --git a/pkg/common/db/relation/group_request_model.go b/pkg/common/db/relation/group_request_model.go index 691a83bb0..af3f277e8 100644 --- a/pkg/common/db/relation/group_request_model.go +++ b/pkg/common/db/relation/group_request_model.go @@ -80,7 +80,6 @@ func (g *GroupRequestGorm) Take( userID string, ) (groupRequest *relation.GroupRequestModel, err error) { groupRequest = &relation.GroupRequestModel{} - return groupRequest, utils.Wrap( g.DB.WithContext(ctx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error, utils.GetSelfFuncName(), @@ -115,6 +114,5 @@ func (g *GroupRequestGorm) PageGroup( func (g *GroupRequestGorm) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (total int64, groupRequests []*relation.GroupRequestModel, err error) { err = g.DB.WithContext(ctx).Where("group_id = ? and user_id in ?", groupID, userIDs).Find(&groupRequests).Error - return int64(len(groupRequests)), groupRequests, utils.Wrap(err, utils.GetSelfFuncName()) } diff --git a/pkg/common/db/relation/log_model.go b/pkg/common/db/relation/log_model.go index fc1a82cdb..53365ca5b 100644 --- a/pkg/common/db/relation/log_model.go +++ b/pkg/common/db/relation/log_model.go @@ -25,7 +25,6 @@ func (l *LogGorm) Search(ctx context.Context, keyword string, start time.Time, e db = l.db.WithContext(ctx).Where("create_time <= ?", end) } db = db.Order("create_time desc") - return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber) } @@ -33,7 +32,6 @@ func (l *LogGorm) Delete(ctx context.Context, logIDs []string, userID string) er if userID == "" { return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Delete(&relationtb.Log{}).Error) } - return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Delete(&relationtb.Log{}).Error) } @@ -42,15 +40,10 @@ func (l *LogGorm) Get(ctx context.Context, logIDs []string, userID string) ([]*r if userID == "" { return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Find(&logs).Error) } - return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error) } func NewLogGorm(db *gorm.DB) relationtb.LogInterface { - err := db.AutoMigrate(&relationtb.Log{}) - if err != nil { - panic(err) - } - + db.AutoMigrate(&relationtb.Log{}) return &LogGorm{db: db} } diff --git a/pkg/common/db/relation/meta_db.go b/pkg/common/db/relation/meta_db.go index 00c1b76bc..6ab980120 100644 --- a/pkg/common/db/relation/meta_db.go +++ b/pkg/common/db/relation/meta_db.go @@ -34,6 +34,5 @@ func NewMetaDB(db *gorm.DB, table any) *MetaDB { func (g *MetaDB) db(ctx context.Context) *gorm.DB { db := g.DB.WithContext(ctx).Model(g.table) - return db } diff --git a/pkg/common/db/relation/mysql_init.go b/pkg/common/db/relation/mysql_init.go index 550053ea2..0e5ea5e43 100644 --- a/pkg/common/db/relation/mysql_init.go +++ b/pkg/common/db/relation/mysql_init.go @@ -15,7 +15,6 @@ package relation import ( - "errors" "fmt" "time" @@ -83,7 +82,6 @@ func newMysqlGormDB() (*gorm.DB, error) { sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.MaxLifeTime)) sqlDB.SetMaxOpenConns(config.Config.Mysql.MaxOpenConn) sqlDB.SetMaxIdleConns(config.Config.Mysql.MaxIdleConn) - return db, nil } @@ -96,13 +94,11 @@ func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) { if err == nil { return db, nil } - var mysqlErr *mysqldriver.MySQLError - if errors.As(err, &mysqlErr) && mysqlErr.Number == 1045 { + if mysqlErr, ok := err.(*mysqldriver.MySQLError); ok && mysqlErr.Number == 1045 { return nil, err } time.Sleep(time.Duration(1) * time.Second) } - return nil, err } @@ -110,7 +106,6 @@ func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) { func NewGormDB() (*gorm.DB, error) { specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound) specialerror.AddErrHandler(replaceDuplicateKey) - return newMysqlGormDB() } @@ -118,15 +113,12 @@ func replaceDuplicateKey(err error) errs.CodeError { if IsMysqlDuplicateKey(err) { return errs.ErrDuplicateKey } - return nil } func IsMysqlDuplicateKey(err error) bool { - var mysqlErr *mysqldriver.MySQLError - if errors.As(err, &mysqlErr) { + if mysqlErr, ok := err.(*mysqldriver.MySQLError); ok { return mysqlErr.Number == 1062 } - return false } diff --git a/pkg/common/db/relation/object_model.go b/pkg/common/db/relation/object_model.go index 34b511c6a..c5624a8d4 100644 --- a/pkg/common/db/relation/object_model.go +++ b/pkg/common/db/relation/object_model.go @@ -44,12 +44,10 @@ func (o *ObjectInfoGorm) SetObject(ctx context.Context, obj *relation.ObjectMode if err := o.DB.WithContext(ctx).Where("name = ?", obj.Name).FirstOrCreate(obj).Error; err != nil { return errs.Wrap(err) } - return nil } func (o *ObjectInfoGorm) Take(ctx context.Context, name string) (info *relation.ObjectModel, err error) { info = &relation.ObjectModel{} - return info, errs.Wrap(o.DB.WithContext(ctx).Where("name = ?", name).Take(info).Error) } diff --git a/pkg/common/db/relation/user_model.go b/pkg/common/db/relation/user_model.go index ef605abd9..b04c29816 100644 --- a/pkg/common/db/relation/user_model.go +++ b/pkg/common/db/relation/user_model.go @@ -53,7 +53,6 @@ func (u *UserGorm) Update(ctx context.Context, user *relation.UserModel) (err er // 获取指定用户信息 不存在,也不返回错误. func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "") - return users, err } @@ -61,7 +60,6 @@ func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relatio func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) { user = &relation.UserModel{} err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "") - return user, err } @@ -83,7 +81,6 @@ func (u *UserGorm) Page( Error, "", ) - return } @@ -91,14 +88,13 @@ func (u *UserGorm) Page( func (u *UserGorm) GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) { if pageNumber == 0 || showNumber == 0 { return userIDs, errs.Wrap(u.db(ctx).Pluck("user_id", &userIDs).Error) + } else { + return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error) } - - return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error) } func (u *UserGorm) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) { err = u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Pluck("global_recv_msg_opt", &opt).Error - return opt, err } @@ -110,7 +106,6 @@ func (u *UserGorm) CountTotal(ctx context.Context, before *time.Time) (count int if err := db.Count(&count).Error; err != nil { return 0, err } - return count, nil } @@ -137,6 +132,5 @@ func (u *UserGorm) CountRangeEverydayTotal( for _, r := range res { v[r.Date.Format("2006-01-02")] = r.Count } - return v, nil } diff --git a/pkg/common/db/s3/cont/controller.go b/pkg/common/db/s3/cont/controller.go index 7ff6fa755..6faa997a9 100644 --- a/pkg/common/db/s3/cont/controller.go +++ b/pkg/common/db/s3/cont/controller.go @@ -46,7 +46,6 @@ func (c *Controller) HashPath(md5 string) string { func (c *Controller) NowPath() string { now := time.Now() - return path.Join( fmt.Sprintf("%04d", now.Year()), fmt.Sprintf("%02d", now.Month()), @@ -59,7 +58,6 @@ func (c *Controller) NowPath() string { func (c *Controller) UUID() string { id := uuid.New() - return hex.EncodeToString(id[:]) } @@ -94,24 +92,20 @@ func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64 partNumber++ } if maxParts > 0 && partNumber > 0 && partNumber < maxParts { - return nil, fmt.Errorf("too few parts: %d", partNumber) + return nil, errors.New(fmt.Sprintf("too many parts: %d", partNumber)) } - info, err := c.impl.StatObject(ctx, c.HashPath(hash)) - if err == nil { + if info, err := c.impl.StatObject(ctx, c.HashPath(hash)); err == nil { return nil, &HashAlreadyExistsError{Object: info} - } - if !c.impl.IsNotFound(err) { + } else if !c.impl.IsNotFound(err) { return nil, err } - if size <= partSize { // 预签名上传 key := path.Join(tempPath, c.NowPath(), fmt.Sprintf("%s_%d_%s.presigned", hash, size, c.UUID())) - rawURL, err2 := c.impl.PresignedPutObject(ctx, key, expire) - if err2 != nil { - return nil, err2 + rawURL, err := c.impl.PresignedPutObject(ctx, key, expire) + if err != nil { + return nil, err } - return &InitiateUploadResult{ UploadID: newMultipartUploadID(multipartUploadID{ Type: UploadTypePresigned, @@ -130,39 +124,38 @@ func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64 }, }, }, nil - } - - // 分片上传 - upload, err := c.impl.InitiateMultipartUpload(ctx, c.HashPath(hash)) - if err != nil { - return nil, err - } - if maxParts < 0 { - maxParts = partNumber - } - var authSign *s3.AuthSignResult - if maxParts > 0 { - partNumbers := make([]int, partNumber) - for i := 0; i < maxParts; i++ { - partNumbers[i] = i + 1 - } - authSign, err = c.impl.AuthSign(ctx, upload.UploadID, upload.Key, time.Hour*24, partNumbers) + } else { + // 分片上传 + upload, err := c.impl.InitiateMultipartUpload(ctx, c.HashPath(hash)) if err != nil { return nil, err } + if maxParts < 0 { + maxParts = partNumber + } + var authSign *s3.AuthSignResult + if maxParts > 0 { + partNumbers := make([]int, partNumber) + for i := 0; i < maxParts; i++ { + partNumbers[i] = i + 1 + } + authSign, err = c.impl.AuthSign(ctx, upload.UploadID, upload.Key, time.Hour*24, partNumbers) + if err != nil { + return nil, err + } + } + return &InitiateUploadResult{ + UploadID: newMultipartUploadID(multipartUploadID{ + Type: UploadTypeMultipart, + ID: upload.UploadID, + Key: upload.Key, + Size: size, + Hash: hash, + }), + PartSize: partSize, + Sign: authSign, + }, nil } - - return &InitiateUploadResult{ - UploadID: newMultipartUploadID(multipartUploadID{ - Type: UploadTypeMultipart, - ID: upload.UploadID, - Key: upload.Key, - Size: size, - Hash: hash, - }), - PartSize: partSize, - Sign: authSign, - }, nil } func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHashs []string) (*UploadResult, error) { @@ -171,10 +164,8 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa if err != nil { return nil, err } - //nolint:gosec //tofix G401: Use of weak cryptographic primitive if md5Sum := md5.Sum([]byte(strings.Join(partHashs, partSeparator))); hex.EncodeToString(md5Sum[:]) != upload.Hash { fmt.Println("CompleteUpload sum:", hex.EncodeToString(md5Sum[:]), "upload hash:", upload.Hash) - return nil, errors.New("md5 mismatching") } if info, err := c.impl.StatObject(ctx, c.HashPath(upload.Hash)); err == nil { @@ -202,7 +193,7 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa ETag: part, } } - // todo: verify size + // todo: 验证大小 result, err := c.impl.CompleteMultipartUpload(ctx, upload.ID, upload.Key, parts) if err != nil { return nil, err @@ -217,12 +208,11 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa if uploadInfo.Size != upload.Size { return nil, errors.New("upload size mismatching") } - //nolint:gosec //G401: Use of weak cryptographic primitive md5Sum := md5.Sum([]byte(strings.Join([]string{uploadInfo.ETag}, partSeparator))) if md5val := hex.EncodeToString(md5Sum[:]); md5val != upload.Hash { return nil, errs.ErrArgs.Wrap(fmt.Sprintf("md5 mismatching %s != %s", md5val, upload.Hash)) } - // Prevent concurrent operations at this time to avoid file overwrite + // 防止在这个时候,并发操作,导致文件被覆盖 copyInfo, err := c.impl.CopyObject(ctx, uploadInfo.Key, upload.Key+"."+c.UUID()) if err != nil { return nil, err @@ -240,7 +230,6 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa default: return nil, errors.New("invalid upload id type") } - return &UploadResult{ Key: targetKey, Size: upload.Size, @@ -272,6 +261,5 @@ func (c *Controller) AccessURL(ctx context.Context, name string, expire time.Dur opt.Filename = "" opt.ContentType = "" } - return c.impl.AccessURL(ctx, name, expire, opt) } diff --git a/pkg/common/db/s3/cont/id.go b/pkg/common/db/s3/cont/id.go index a2b723b83..47f37d4aa 100644 --- a/pkg/common/db/s3/cont/id.go +++ b/pkg/common/db/s3/cont/id.go @@ -33,7 +33,6 @@ func newMultipartUploadID(id multipartUploadID) string { if err != nil { panic(err) } - return base64.StdEncoding.EncodeToString(data) } @@ -46,6 +45,5 @@ func parseMultipartUploadID(id string) (*multipartUploadID, error) { if err := json.Unmarshal(data, &upload); err != nil { return nil, fmt.Errorf("invalid multipart upload id: %w", err) } - return &upload, nil } diff --git a/pkg/common/db/s3/cos/cos.go b/pkg/common/db/s3/cos/cos.go index 5484778a5..7add88487 100644 --- a/pkg/common/db/s3/cos/cos.go +++ b/pkg/common/db/s3/cos/cos.go @@ -44,6 +44,11 @@ const ( imageWebp = "webp" ) +const ( + videoSnapshotImagePng = "png" + videoSnapshotImageJpg = "jpg" +) + func NewCos() (s3.Interface, error) { conf := config.Config.Object.Cos u, err := url.Parse(conf.BucketURL) @@ -57,7 +62,6 @@ func NewCos() (s3.Interface, error) { SessionToken: conf.SessionToken, }, }) - return &Cos{ copyURL: u.Host + "/", client: client, @@ -88,7 +92,6 @@ func (c *Cos) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini if err != nil { return nil, err } - return &s3.InitiateMultipartUploadResult{ UploadID: result.UploadID, Bucket: result.Bucket, @@ -110,7 +113,6 @@ func (c *Cos) CompleteMultipartUpload(ctx context.Context, uploadID string, name if err != nil { return nil, err } - return &s3.CompleteMultipartUploadResult{ Location: result.Location, Bucket: result.Bucket, @@ -133,7 +135,6 @@ func (c *Cos) PartSize(ctx context.Context, size int64) (int64, error) { if size%maxNumSize != 0 { partSize++ } - return partSize, nil } @@ -156,7 +157,6 @@ func (c *Cos) AuthSign(ctx context.Context, uploadID string, name string, expire Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}}, } } - return &result, nil } @@ -165,13 +165,11 @@ func (c *Cos) PresignedPutObject(ctx context.Context, name string, expire time.D if err != nil { return "", err } - return rawURL.String(), nil } func (c *Cos) DeleteObject(ctx context.Context, name string) error { _, err := c.client.Object.Delete(ctx, name) - return err } @@ -187,26 +185,25 @@ func (c *Cos) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, erro if res.ETag = strings.ToLower(strings.ReplaceAll(info.Header.Get("ETag"), `"`, "")); res.ETag == "" { return nil, errors.New("StatObject etag not found") } - contentLengthStr := info.Header.Get("Content-Length") - if contentLengthStr == "" { + if contentLengthStr := info.Header.Get("Content-Length"); contentLengthStr == "" { return nil, errors.New("StatObject content-length not found") + } else { + res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) + if err != nil { + return nil, fmt.Errorf("StatObject content-length parse error: %w", err) + } + if res.Size < 0 { + return nil, errors.New("StatObject content-length must be greater than 0") + } } - res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) - if err != nil { - return nil, fmt.Errorf("StatObject content-length parse error: %w", err) - } - if res.Size < 0 { - return nil, errors.New("StatObject content-length must be greater than 0") - } - lastModified := info.Header.Get("Last-Modified") - if lastModified == "" { + if lastModified := info.Header.Get("Last-Modified"); lastModified == "" { return nil, errors.New("StatObject last-modified not found") + } else { + res.LastModified, err = time.Parse(http.TimeFormat, lastModified) + if err != nil { + return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) + } } - res.LastModified, err = time.Parse(http.TimeFormat, lastModified) - if err != nil { - return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) - } - return res, nil } @@ -216,7 +213,6 @@ func (c *Cos) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO if err != nil { return nil, err } - return &s3.CopyObjectInfo{ Key: dst, ETag: strings.ReplaceAll(result.ETag, `"`, ``), @@ -224,17 +220,16 @@ func (c *Cos) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO } func (c *Cos) IsNotFound(err error) bool { - var cosErr *cos.ErrorResponse - if errors.As(err, &cosErr) { - return cosErr.Response.StatusCode == http.StatusNotFound || cosErr.Code == "NoSuchKey" + switch e := err.(type) { + case *cos.ErrorResponse: + return e.Response.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" + default: + return false } - - return false } func (c *Cos) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error { _, err := c.client.Object.AbortMultipartUpload(ctx, name, uploadID) - return err } @@ -262,59 +257,46 @@ func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name strin Size: part.Size, } } - return res, nil } func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) { var imageMogr string var option cos.PresignedURLOptions - getImageMogr := func(opt *s3.AccessURLOption) (imageMogr string) { - if opt.Image == nil { - return imageMogr - } - // https://cloud.tencent.com/document/product/436/44880 - style := make([]string, 0, 2) - wh := make([]string, 2) - if opt.Image.Width > 0 { - wh[0] = strconv.Itoa(opt.Image.Width) - } - if opt.Image.Height > 0 { - wh[1] = strconv.Itoa(opt.Image.Height) - } - if opt.Image.Width > 0 || opt.Image.Height > 0 { - style = append(style, strings.Join(wh, "x")) - } - switch opt.Image.Format { - case - imagePng, - imageJpg, - imageJpeg, - imageGif, - imageWebp: - style = append(style, "format/"+opt.Image.Format) - } - if len(style) > 0 { - imageMogr = "imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1" + if opt != nil { + query := make(url.Values) + if opt.Image != nil { + // https://cloud.tencent.com/document/product/436/44880 + style := make([]string, 0, 2) + wh := make([]string, 2) + if opt.Image.Width > 0 { + wh[0] = strconv.Itoa(opt.Image.Width) + } + if opt.Image.Height > 0 { + wh[1] = strconv.Itoa(opt.Image.Height) + } + if opt.Image.Width > 0 || opt.Image.Height > 0 { + style = append(style, strings.Join(wh, "x")) + } + switch opt.Image.Format { + case + imagePng, + imageJpg, + imageJpeg, + imageGif, + imageWebp: + style = append(style, "format/"+opt.Image.Format) + } + if len(style) > 0 { + imageMogr = "imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1" + } } - - return imageMogr - } - getQuery := func(opt *s3.AccessURLOption) (query url.Values) { - query = make(url.Values) if opt.ContentType != "" { query.Set("response-content-type", opt.ContentType) } if opt.Filename != "" { query.Set("response-content-disposition", `attachment; filename=`+strconv.Quote(opt.Filename)) } - - return query - } - - if opt != nil { - imageMogr = getImageMogr(opt) - query := getQuery(opt) if len(query) > 0 { option.Query = &query } @@ -335,7 +317,6 @@ func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, rawURL.RawQuery = rawURL.RawQuery + "&" + imageMogr } } - return rawURL.String(), nil } @@ -343,6 +324,5 @@ func (c *Cos) getPresignedURL(ctx context.Context, name string, expire time.Dura if !config.Config.Object.Cos.PublicRead { return c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, opt) } - return c.client.Object.GetObjectURL(name), nil } diff --git a/pkg/common/db/s3/minio/image.go b/pkg/common/db/s3/minio/image.go index 4812f47ca..71db1ea51 100644 --- a/pkg/common/db/s3/minio/image.go +++ b/pkg/common/db/s3/minio/image.go @@ -39,7 +39,6 @@ func ImageStat(reader io.Reader) (image.Image, string, error) { func ImageWidthHeight(img image.Image) (int, int) { bounds := img.Bounds().Max - return bounds.X, bounds.Y } @@ -48,27 +47,27 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { imgWidth := bounds.Max.X imgHeight := bounds.Max.Y - // Calculate scaling ratio + // 计算缩放比例 scaleWidth := float64(maxWidth) / float64(imgWidth) scaleHeight := float64(maxHeight) / float64(imgHeight) - // If both maxWidth and maxHeight are 0, return the original image + // 如果都为0,则不缩放,返回原始图片 if maxWidth == 0 && maxHeight == 0 { return img } - // If both maxWidth and maxHeight are greater than 0, choose the smaller scaling ratio to maintain aspect ratio + // 如果宽度和高度都大于0,则选择较小的缩放比例,以保持宽高比 if maxWidth > 0 && maxHeight > 0 { scale := scaleWidth if scaleHeight < scaleWidth { scale = scaleHeight } - // Calculate thumbnail size + // 计算缩略图尺寸 thumbnailWidth := int(float64(imgWidth) * scale) thumbnailHeight := int(float64(imgHeight) * scale) - // Generate thumbnail using the Resample method of the "image" library + // 使用"image"库的Resample方法生成缩略图 thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight)) for y := 0; y < thumbnailHeight; y++ { for x := 0; x < thumbnailWidth; x++ { @@ -81,12 +80,12 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { return thumbnail } - // If only maxWidth or maxHeight is specified, generate thumbnail according to the "max not exceed" rule + // 如果只指定了宽度或高度,则根据最大不超过的规则生成缩略图 if maxWidth > 0 { thumbnailWidth := maxWidth thumbnailHeight := int(float64(imgHeight) * scaleWidth) - // Generate thumbnail using the Resample method of the "image" library + // 使用"image"库的Resample方法生成缩略图 thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight)) for y := 0; y < thumbnailHeight; y++ { for x := 0; x < thumbnailWidth; x++ { @@ -103,7 +102,7 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { thumbnailWidth := int(float64(imgWidth) * scaleHeight) thumbnailHeight := maxHeight - // Generate thumbnail using the Resample method of the "image" library + // 使用"image"库的Resample方法生成缩略图 thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight)) for y := 0; y < thumbnailHeight; y++ { for x := 0; x < thumbnailWidth; x++ { @@ -116,6 +115,6 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image { return thumbnail } - // By default, return the original image + // 默认情况下,返回原始图片 return img } diff --git a/pkg/common/db/s3/minio/minio.go b/pkg/common/db/s3/minio/minio.go index 78ed381d5..7984df5a0 100644 --- a/pkg/common/db/s3/minio/minio.go +++ b/pkg/common/db/s3/minio/minio.go @@ -111,7 +111,6 @@ func NewMinio() (s3.Interface, error) { if err := m.initMinio(ctx); err != nil { fmt.Println("init minio error:", err) } - return m, nil } @@ -142,9 +141,8 @@ func (m *Minio) initMinio(ctx context.Context) error { return fmt.Errorf("check bucket exists error: %w", err) } if !exists { - err2 := m.core.Client.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{}) - if err2 != nil { - return fmt.Errorf("make bucket error: %w", err2) + if err := m.core.Client.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{}); err != nil { + return fmt.Errorf("make bucket error: %w", err) } } if conf.PublicRead { @@ -152,9 +150,8 @@ func (m *Minio) initMinio(ctx context.Context) error { `{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject","s3:PutObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::%s/*"],"Sid": ""}]}`, conf.Bucket, ) - err2 := m.core.Client.SetBucketPolicy(ctx, conf.Bucket, policy) - if err2 != nil { - return err2 + if err := m.core.Client.SetBucketPolicy(ctx, conf.Bucket, policy); err != nil { + return err } } m.location, err = m.core.Client.GetBucketLocation(ctx, conf.Bucket) @@ -185,7 +182,6 @@ func (m *Minio) initMinio(ctx context.Context) error { vblc.Elem().Elem().Interface().(interface{ Set(string, string) }).Set(conf.Bucket, m.location) }() m.init = true - return nil } @@ -209,7 +205,6 @@ func (m *Minio) InitiateMultipartUpload(ctx context.Context, name string) (*s3.I if err != nil { return nil, err } - return &s3.InitiateMultipartUploadResult{ Bucket: m.bucket, Key: name, @@ -232,7 +227,6 @@ func (m *Minio) CompleteMultipartUpload(ctx context.Context, uploadID string, na if err != nil { return nil, err } - return &s3.CompleteMultipartUploadResult{ Location: upload.Location, Bucket: upload.Bucket, @@ -255,7 +249,6 @@ func (m *Minio) PartSize(ctx context.Context, size int64) (int64, error) { if size%maxNumSize != 0 { partSize++ } - return partSize, nil } @@ -289,7 +282,6 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi if m.prefix != "" { result.URL = m.signEndpoint + m.prefix + "/" + m.bucket + "/" + name } - return &result, nil } @@ -304,7 +296,6 @@ func (m *Minio) PresignedPutObject(ctx context.Context, name string, expire time if m.prefix != "" { rawURL.Path = path.Join(m.prefix, rawURL.Path) } - return rawURL.String(), nil } @@ -312,7 +303,6 @@ func (m *Minio) DeleteObject(ctx context.Context, name string) error { if err := m.initMinio(ctx); err != nil { return err } - return m.core.Client.RemoveObject(ctx, m.bucket, name, minio.RemoveObjectOptions{}) } @@ -324,7 +314,6 @@ func (m *Minio) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, er if err != nil { return nil, err } - return &s3.ObjectInfo{ ETag: strings.ToLower(info.ETag), Key: info.Key, @@ -347,7 +336,6 @@ func (m *Minio) CopyObject(ctx context.Context, src string, dst string) (*s3.Cop if err != nil { return nil, err } - return &s3.CopyObjectInfo{ Key: dst, ETag: strings.ToLower(result.ETag), @@ -358,23 +346,20 @@ func (m *Minio) IsNotFound(err error) bool { if err == nil { return false } - var minioErr minio.ErrorResponse - if errors.As(err, &minio.ErrorResponse{}) { - return minioErr.StatusCode == http.StatusNotFound || minioErr.Code == "NoSuchKey" - } - var minioErr2 *minio.ErrorResponse - if errors.As(err, &minioErr2) { - return minioErr2.StatusCode == http.StatusNotFound || minioErr2.Code == "NoSuchKey" + switch e := err.(type) { + case minio.ErrorResponse: + return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" + case *minio.ErrorResponse: + return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" + default: + return false } - - return false } func (m *Minio) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error { if err := m.initMinio(ctx); err != nil { return err } - return m.core.AbortMultipartUpload(ctx, m.bucket, name, uploadID) } @@ -401,7 +386,6 @@ func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name str Size: part.Size, } } - return res, nil } @@ -426,17 +410,14 @@ func (m *Minio) presignedGetObject(ctx context.Context, name string, expire time if m.prefix != "" { rawURL.Path = path.Join(m.prefix, rawURL.Path) } - return rawURL.String(), nil } -func (m *Minio) getImageInfoForAccessURL( - ctx context.Context, - name string, - expire time.Duration, - opt *s3.AccessURLOption, - reqParams url.Values, -) (fileInfo *s3.ObjectInfo, objectInfoPath, msg string, err error) { +func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) { + if err := m.initMinio(ctx); err != nil { + return "", err + } + reqParams := make(url.Values) if opt != nil { if opt.ContentType != "" { reqParams.Set("response-content-type", opt.ContentType) @@ -446,47 +427,35 @@ func (m *Minio) getImageInfoForAccessURL( } } if opt.Image == nil || (opt.Image.Width < 0 && opt.Image.Height < 0 && opt.Image.Format == "") || (opt.Image.Width > maxImageWidth || opt.Image.Height > maxImageHeight) { - msg, err = m.presignedGetObject(ctx, name, expire, reqParams) - - return nil, "", msg, err + return m.presignedGetObject(ctx, name, expire, reqParams) } - fileInfo, err = m.StatObject(ctx, name) - objectInfoPath = path.Join(pathInfo, fileInfo.ETag, "image.json") + fileInfo, err := m.StatObject(ctx, name) if err != nil { - return nil, "", msg, err + return "", err } if fileInfo.Size > maxImageSize { - return nil, "", "", errors.New("file size too large") + return "", errors.New("file size too large") } - - return fileInfo, objectInfoPath, "", nil -} - -func (m *Minio) loadImgDataForAccessURL(objectInfoPath string, ctx context.Context, name string, info *minioImageInfo) (img image.Image, msg string, err error) { - var data []byte - data, err = m.getObjectData(ctx, objectInfoPath, 1024) - - //nolint:nestif //easy enough to understand + objectInfoPath := path.Join(pathInfo, fileInfo.ETag, "image.json") + var ( + img image.Image + info minioImageInfo + ) + data, err := m.getObjectData(ctx, objectInfoPath, 1024) if err == nil { - err = json.Unmarshal(data, &info) - if err != nil { - return nil, "", fmt.Errorf("unmarshal minio image info.json error: %w", err) + if err := json.Unmarshal(data, &info); err != nil { + return "", fmt.Errorf("unmarshal minio image info.json error: %w", err) } if info.NotImage { - return nil, "", errors.New("not image") + return "", errors.New("not image") } } else if m.IsNotFound(err) { - var reader *minio.Object - reader, err = m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) + reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) if err != nil { - return img, msg, err + return "", err } defer reader.Close() - var ( - imageInfo image.Image - format string - ) - imageInfo, format, err = ImageStat(reader) + imageInfo, format, err := ImageStat(reader) if err == nil { info.NotImage = false info.Format = format @@ -495,22 +464,16 @@ func (m *Minio) loadImgDataForAccessURL(objectInfoPath string, ctx context.Conte } else { info.NotImage = true } - - data, err = json.Marshal(&info) + data, err := json.Marshal(&info) if err != nil { - return img, msg, err + return "", err } - - _, err = m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}) - if err != nil { - return img, msg, err + if _, err := m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil { + return "", err } + } else { + return "", err } - - return img, msg, err -} - -func (m *Minio) formatImgInfoForAccessURL(opt *s3.AccessURLOption, info *minioImageInfo, reqParams url.Values) { if opt.Image.Width > info.Width || opt.Image.Width <= 0 { opt.Image.Width = info.Width } @@ -533,24 +496,24 @@ func (m *Minio) formatImgInfoForAccessURL(opt *s3.AccessURLOption, info *minioIm } } reqParams.Set("response-content-type", "image/"+opt.Image.Format) -} - -func (m *Minio) cacheImgInfoForAccessURL(ctx context.Context, name, cacheKey string, img image.Image, expire time.Duration, opt *s3.AccessURLOption, reqParams url.Values) (string, error) { - _, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{}) - if err == nil { + if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format { + return m.presignedGetObject(ctx, name, expire, reqParams) + } + cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format)) + if _, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{}); err == nil { return m.presignedGetObject(ctx, cacheKey, expire, reqParams) } else if !m.IsNotFound(err) { return "", err } if img == nil { - reader, err2 := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) - if err2 != nil { - return "", err2 + reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{}) + if err != nil { + return "", err } defer reader.Close() - img, _, err2 = ImageStat(reader) - if err2 != nil { - return "", err2 + img, _, err = ImageStat(reader) + if err != nil { + return "", err } } thumbnail := resizeImage(img, opt.Image.Width, opt.Image.Height) @@ -563,48 +526,9 @@ func (m *Minio) cacheImgInfoForAccessURL(ctx context.Context, name, cacheKey str case formatGif: err = gif.Encode(buf, thumbnail, nil) } - if err != nil { - return "", err - } if _, err := m.core.Client.PutObject(ctx, m.bucket, cacheKey, buf, int64(buf.Len()), minio.PutObjectOptions{}); err != nil { return "", err } - - return "", nil -} - -func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) { - errInit := m.initMinio(ctx) - if errInit != nil { - return "", errInit - } - reqParams := make(url.Values) - fileInfo, objectInfoPath, msg, err := m.getImageInfoForAccessURL(ctx, name, expire, opt, reqParams) - if err != nil { - return msg, err - } - // load-cache img data - var ( - img image.Image - info minioImageInfo - ) - img, msg, err = m.loadImgDataForAccessURL(objectInfoPath, ctx, name, &info) - if err != nil { - return msg, err - } - // format img info - m.formatImgInfoForAccessURL(opt, &info, reqParams) - // no need resize - if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format { - return m.presignedGetObject(ctx, name, expire, reqParams) - } - // cache img - cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format)) - msg, err = m.cacheImgInfoForAccessURL(ctx, name, cacheKey, img, expire, opt, reqParams) - if err != nil { - return msg, err - } - // return cache img return m.presignedGetObject(ctx, cacheKey, expire, reqParams) } @@ -617,6 +541,5 @@ func (m *Minio) getObjectData(ctx context.Context, name string, limit int64) ([] if limit < 0 { return io.ReadAll(object) } - return io.ReadAll(io.LimitReader(object, 1024)) } diff --git a/pkg/common/db/s3/oss/oss.go b/pkg/common/db/s3/oss/oss.go index 4f7f37497..6a728127b 100755 --- a/pkg/common/db/s3/oss/oss.go +++ b/pkg/common/db/s3/oss/oss.go @@ -45,6 +45,11 @@ const ( imageWebp = "webp" ) +const ( + videoSnapshotImagePng = "png" + videoSnapshotImageJpg = "jpg" +) + func NewOSS() (s3.Interface, error) { conf := config.Config.Object.Oss if conf.BucketURL == "" { @@ -61,7 +66,6 @@ func NewOSS() (s3.Interface, error) { if conf.BucketURL[len(conf.BucketURL)-1] != '/' { conf.BucketURL += "/" } - return &OSS{ bucketURL: conf.BucketURL, bucket: bucket, @@ -94,7 +98,6 @@ func (o *OSS) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini if err != nil { return nil, err } - return &s3.InitiateMultipartUploadResult{ UploadID: result.UploadID, Bucket: result.Bucket, @@ -118,7 +121,6 @@ func (o *OSS) CompleteMultipartUpload(ctx context.Context, uploadID string, name if err != nil { return nil, err } - return &s3.CompleteMultipartUploadResult{ Location: result.Location, Bucket: result.Bucket, @@ -141,7 +143,6 @@ func (o *OSS) PartSize(ctx context.Context, size int64) (int64, error) { if size%maxNumSize != 0 { partSize++ } - return partSize, nil } @@ -154,7 +155,7 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire } for i, partNumber := range partNumbers { rawURL := fmt.Sprintf(`%s%s?partNumber=%d&uploadId=%s`, o.bucketURL, name, partNumber, uploadID) - request, err := http.NewRequestWithContext(context.Background(), http.MethodPut, rawURL, nil) + request, err := http.NewRequest(http.MethodPut, rawURL, nil) if err != nil { return nil, err } @@ -174,7 +175,6 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire Header: request.Header, } } - return &result, nil } @@ -191,26 +191,25 @@ func (o *OSS) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, erro if res.ETag = strings.ToLower(strings.ReplaceAll(header.Get("ETag"), `"`, ``)); res.ETag == "" { return nil, errors.New("StatObject etag not found") } - contentLengthStr := header.Get("Content-Length") - if contentLengthStr == "" { + if contentLengthStr := header.Get("Content-Length"); contentLengthStr == "" { return nil, errors.New("StatObject content-length not found") + } else { + res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) + if err != nil { + return nil, fmt.Errorf("StatObject content-length parse error: %w", err) + } + if res.Size < 0 { + return nil, errors.New("StatObject content-length must be greater than 0") + } } - res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64) - if err != nil { - return nil, fmt.Errorf("StatObject content-length parse error: %w", err) - } - if res.Size < 0 { - return nil, errors.New("StatObject content-length must be greater than 0") - } - lastModified := header.Get("Last-Modified") - if lastModified == "" { + if lastModified := header.Get("Last-Modified"); lastModified == "" { return nil, errors.New("StatObject last-modified not found") + } else { + res.LastModified, err = time.Parse(http.TimeFormat, lastModified) + if err != nil { + return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) + } } - res.LastModified, err = time.Parse(http.TimeFormat, lastModified) - if err != nil { - return nil, fmt.Errorf("StatObject last-modified parse error: %w", err) - } - return res, nil } @@ -223,7 +222,6 @@ func (o *OSS) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO if err != nil { return nil, err } - return &s3.CopyObjectInfo{ Key: dst, ETag: strings.ToLower(strings.ReplaceAll(result.ETag, `"`, ``)), @@ -231,7 +229,6 @@ func (o *OSS) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO } func (o *OSS) IsNotFound(err error) bool { - //nolint:errorlint //this is exactly what we want,there is no risk for no wrapped errors switch e := err.(type) { case oss.ServiceError: return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey" @@ -274,7 +271,6 @@ func (o *OSS) ListUploadedParts(ctx context.Context, uploadID string, name strin Size: int64(part.Size), } } - return res, nil } @@ -282,7 +278,39 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration, publicRead := config.Config.Object.Oss.PublicRead var opts []oss.Option if opt != nil { - opts = optsForAccessURL(opt, opts, publicRead) + if opt.Image != nil { + // 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji + var format string + switch opt.Image.Format { + case + imagePng, + imageJpg, + imageJpeg, + imageGif, + imageWebp: + format = opt.Image.Format + default: + opt.Image.Format = imageJpg + } + // https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit + process := "image/resize,m_lfit" + if opt.Image.Width > 0 { + process += ",w_" + strconv.Itoa(opt.Image.Width) + } + if opt.Image.Height > 0 { + process += ",h_" + strconv.Itoa(opt.Image.Height) + } + process += ",format," + format + opts = append(opts, oss.Process(process)) + } + if !publicRead { + if opt.ContentType != "" { + opts = append(opts, oss.ResponseContentType(opt.ContentType)) + } + if opt.Filename != "" { + opts = append(opts, oss.ResponseContentDisposition(`attachment; filename=`+strconv.Quote(opt.Filename))) + } + } } if expire <= 0 { expire = time.Hour * 24 * 365 * 99 // 99 years @@ -297,44 +325,5 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration, return "", err } params := getURLParams(*o.bucket.Client.Conn, rawParams) - return getURL(o.um, o.bucket.BucketName, name, params).String(), nil } - -func optsForAccessURL(opt *s3.AccessURLOption, opts []oss.Option, publicRead bool) []oss.Option { - if opt.Image != nil { - // 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji - var format string - switch opt.Image.Format { - case - imagePng, - imageJpg, - imageJpeg, - imageGif, - imageWebp: - format = opt.Image.Format - default: - opt.Image.Format = imageJpg - } - // https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit - process := "image/resize,m_lfit" - if opt.Image.Width > 0 { - process += ",w_" + strconv.Itoa(opt.Image.Width) - } - if opt.Image.Height > 0 { - process += ",h_" + strconv.Itoa(opt.Image.Height) - } - process += ",format," + format - opts = append(opts, oss.Process(process)) - } - if !publicRead { - if opt.ContentType != "" { - opts = append(opts, oss.ResponseContentType(opt.ContentType)) - } - if opt.Filename != "" { - opts = append(opts, oss.ResponseContentDisposition(`attachment; filename=`+strconv.Quote(opt.Filename))) - } - } - - return opts -} diff --git a/pkg/common/db/table/relation/group.go b/pkg/common/db/table/relation/group.go index 309114ee2..6759e0d35 100644 --- a/pkg/common/db/table/relation/group.go +++ b/pkg/common/db/table/relation/group.go @@ -30,7 +30,7 @@ type GroupModel struct { Introduction string `gorm:"column:introduction;size:255" json:"introduction"` FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"` CreateTime time.Time `gorm:"column:create_time;index:create_time;autoCreateTime"` - Ex string `gorm:"column:ex;size:1024" json:"ex"` + Ex string `gorm:"column:ex" json:"ex;size:1024"` Status int32 `gorm:"column:status"` CreatorUserID string `gorm:"column:creator_user_id;size:64"` GroupType int32 `gorm:"column:group_type"` diff --git a/pkg/common/db/table/relation/utils.go b/pkg/common/db/table/relation/utils.go index bc2639e1a..c944eae8b 100644 --- a/pkg/common/db/table/relation/utils.go +++ b/pkg/common/db/table/relation/utils.go @@ -15,8 +15,6 @@ package relation import ( - "errors" - "gorm.io/gorm" "github.com/OpenIMSDK/tools/utils" @@ -34,5 +32,5 @@ type GroupSimpleUserID struct { } func IsNotFound(err error) bool { - return errors.Is(utils.Unwrap(err), gorm.ErrRecordNotFound) + return utils.Unwrap(err) == gorm.ErrRecordNotFound } diff --git a/pkg/common/db/table/unrelation/msg.go b/pkg/common/db/table/unrelation/msg.go index 542f318ad..c95b211a8 100644 --- a/pkg/common/db/table/unrelation/msg.go +++ b/pkg/common/db/table/unrelation/msg.go @@ -150,7 +150,6 @@ func (m *MsgDocModel) IsFull() bool { func (m MsgDocModel) GetDocID(conversationID string, seq int64) string { seqSuffix := (seq - 1) / singleGocMsgNum - return m.indexGen(conversationID, seqSuffix) } @@ -165,7 +164,6 @@ func (m MsgDocModel) GetDocIDSeqsMap(conversationID string, seqs []int64) map[st t[docID] = append(value, seqs[i]) } } - return t } @@ -183,6 +181,5 @@ func (MsgDocModel) GenExceptionMessageBySeqs(seqs []int64) (exceptionMsg []*sdkw msgModel.Seq = v exceptionMsg = append(exceptionMsg, msgModel) } - return exceptionMsg } diff --git a/pkg/common/db/unrelation/mongo.go b/pkg/common/db/unrelation/mongo.go index 8a90a2a2c..09e3e904e 100755 --- a/pkg/common/db/unrelation/mongo.go +++ b/pkg/common/db/unrelation/mongo.go @@ -16,7 +16,6 @@ package unrelation import ( "context" - "errors" "fmt" "strings" "time" @@ -45,12 +44,27 @@ type Mongo struct { // NewMongo Initialize MongoDB connection. func NewMongo() (*Mongo, error) { specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound) - // uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" - var uri string + uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" if config.Config.Mongo.Uri != "" { uri = config.Config.Mongo.Uri } else { - uri = defaultMongoUriForNewMongo() + mongodbHosts := "" + for i, v := range config.Config.Mongo.Address { + if i == len(config.Config.Mongo.Address)-1 { + mongodbHosts += v + } else { + mongodbHosts += v + "," + } + } + if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" { + uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", + config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts, + config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize) + } else { + uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", + mongodbHosts, config.Config.Mongo.Database, + config.Config.Mongo.MaxPoolSize) + } } fmt.Println("mongo:", uri) var mongoClient *mongo.Client @@ -62,41 +76,17 @@ func NewMongo() (*Mongo, error) { if err == nil { return &Mongo{db: mongoClient}, nil } - var cmdErr mongo.CommandError - if errors.As(err, &cmdErr) { + if cmdErr, ok := err.(mongo.CommandError); ok { if cmdErr.Code == 13 || cmdErr.Code == 18 { return nil, err + } else { + fmt.Printf("Failed to connect to MongoDB: %s\n", err) } - fmt.Printf("Failed to connect to MongoDB: %s\n", err) } } - return nil, err } -func defaultMongoUriForNewMongo() string { - var uri string - mongodbHosts := "" - for i, v := range config.Config.Mongo.Address { - if i == len(config.Config.Mongo.Address)-1 { - mongodbHosts += v - } else { - mongodbHosts += v + "," - } - } - if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" { - uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", - config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts, - config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize) - } else { - uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", - mongodbHosts, config.Config.Mongo.Database, - config.Config.Mongo.MaxPoolSize) - } - - return uri -} - func (m *Mongo) GetClient() *mongo.Client { return m.db } @@ -116,7 +106,6 @@ func (m *Mongo) CreateSuperGroupIndex() error { if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil { return err } - return nil } @@ -150,6 +139,5 @@ func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...strin if err != nil { return utils.Wrap(err, result) } - return nil } diff --git a/pkg/common/db/unrelation/msg.go b/pkg/common/db/unrelation/msg.go index afa2f81e4..9b461dd1f 100755 --- a/pkg/common/db/unrelation/msg.go +++ b/pkg/common/db/unrelation/msg.go @@ -49,7 +49,6 @@ type MsgMongoDriver struct { func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface { collection := database.Collection(table.MsgDocModel{}.TableName()) - return &MsgMongoDriver{MsgCollection: collection} } @@ -60,7 +59,6 @@ func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsTo func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error { _, err := m.MsgCollection.InsertOne(ctx, model) - return err } @@ -83,7 +81,6 @@ func (m *MsgMongoDriver) UpdateMsg( if err != nil { return nil, utils.Wrap(err, "") } - return res, nil } @@ -111,7 +108,6 @@ func (m *MsgMongoDriver) PushUnique( if err != nil { return nil, utils.Wrap(err, "") } - return res, nil } @@ -124,7 +120,6 @@ func (m *MsgMongoDriver) UpdateMsgContent(ctx context.Context, docID string, ind if err != nil { return utils.Wrap(err, "") } - return nil } @@ -148,14 +143,12 @@ func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc( if err != nil { return utils.Wrap(err, "") } - return nil } func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) { doc := &table.MsgDocModel{} err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc) - return doc, err } @@ -184,7 +177,6 @@ func (m *MsgMongoDriver) GetMsgDocModelByIndex( if len(msgs) > 0 { return &msgs[0], nil } - return nil, ErrMsgListNotExist } @@ -233,7 +225,6 @@ func (m *MsgMongoDriver) DeleteMsgsInOneDocByIndex(ctx context.Context, docID st if err != nil { return utils.Wrap(err, "") } - return nil } @@ -242,7 +233,6 @@ func (m *MsgMongoDriver) DeleteDocs(ctx context.Context, docIDs []string) error return nil } _, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}}) - return err } @@ -256,7 +246,6 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc( for _, seq := range seqs { indexs = append(indexs, m.model.GetMsgIndex(seq)) } - //nolint:govet //This is already the officially recommended standard practice. pipeline := mongo.Pipeline{ { {"$match", bson.D{ @@ -347,7 +336,6 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc( } msgs = append(msgs, msg) } - return msgs, nil } @@ -356,7 +344,6 @@ func (m *MsgMongoDriver) IsExistDocID(ctx context.Context, docID string) (bool, if err != nil { return false, errs.Wrap(err) } - return count > 0, nil } @@ -385,7 +372,6 @@ func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead( updates = append(updates, updateModel) } _, err := m.MsgCollection.BulkWrite(ctx, updates) - return err } @@ -625,39 +611,7 @@ func (m *MsgMongoDriver) RangeUserSendCount( }, ) } - pipeline := buildPiplineForRangeUserSendCount(or, start, end, sort, pageNumber, showNumber) - cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) - if err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - defer cur.Close(ctx) - var result []Result - if err = cur.All(ctx, &result); err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - if len(result) == 0 { - return 0, 0, nil, nil, errs.Wrap(err) - } - users = make([]*table.UserCount, len(result[0].Users)) - for i, r := range result[0].Users { - users[i] = &table.UserCount{ - UserID: r.UserID, - Count: r.Count, - } - } - dateCount = make(map[string]int64) - for _, r := range result[0].Dates { - dateCount[r.Date] = r.Count - } - - return result[0].MsgCount, result[0].UserCount, users, dateCount, nil -} - -//nolint:funlen // it need to be such long -func buildPiplineForRangeUserSendCount(or bson.A, start time.Time, - end time.Time, sort int, pageNumber, showNumber int32, -) bson.A { - return bson.A{ + pipeline := bson.A{ bson.M{ "$match": bson.M{ "$and": bson.A{ @@ -841,6 +795,30 @@ func buildPiplineForRangeUserSendCount(or bson.A, start time.Time, }, }, } + cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) + if err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + defer cur.Close(ctx) + var result []Result + if err := cur.All(ctx, &result); err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + if len(result) == 0 { + return 0, 0, nil, nil, errs.Wrap(err) + } + users = make([]*table.UserCount, len(result[0].Users)) + for i, r := range result[0].Users { + users[i] = &table.UserCount{ + UserID: r.UserID, + Count: r.Count, + } + } + dateCount = make(map[string]int64) + for _, r := range result[0].Dates { + dateCount[r.Date] = r.Count + } + return result[0].MsgCount, result[0].UserCount, users, dateCount, nil } func (m *MsgMongoDriver) RangeGroupSendCount( @@ -869,39 +847,7 @@ func (m *MsgMongoDriver) RangeGroupSendCount( Count int64 `bson:"count"` } `bson:"dates"` } - pipeline := buildPiplineForRangeGroupSendCount(start, end, sort, pageNumber, showNumber) - cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) - if err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - defer cur.Close(ctx) - var result []Result - if err = cur.All(ctx, &result); err != nil { - return 0, 0, nil, nil, errs.Wrap(err) - } - if len(result) == 0 { - return 0, 0, nil, nil, errs.Wrap(err) - } - groups = make([]*table.GroupCount, len(result[0].Groups)) - for i, r := range result[0].Groups { - groups[i] = &table.GroupCount{ - GroupID: r.GroupID, - Count: r.Count, - } - } - dateCount = make(map[string]int64) - for _, r := range result[0].Dates { - dateCount[r.Date] = r.Count - } - - return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil -} - -//nolint:funlen //it need to has such length -func buildPiplineForRangeGroupSendCount(start time.Time, - end time.Time, sort int, pageNumber, showNumber int32, -) bson.A { - return bson.A{ + pipeline := bson.A{ bson.M{ "$match": bson.M{ "$and": bson.A{ @@ -1098,6 +1044,30 @@ func buildPiplineForRangeGroupSendCount(start time.Time, }, }, } + cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true)) + if err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + defer cur.Close(ctx) + var result []Result + if err := cur.All(ctx, &result); err != nil { + return 0, 0, nil, nil, errs.Wrap(err) + } + if len(result) == 0 { + return 0, 0, nil, nil, errs.Wrap(err) + } + groups = make([]*table.GroupCount, len(result[0].Groups)) + for i, r := range result[0].Groups { + groups[i] = &table.GroupCount{ + GroupID: r.GroupID, + Count: r.Count, + } + } + dateCount = make(map[string]int64) + for _, r := range result[0].Dates { + dateCount[r.Date] = r.Count + } + return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil } func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) { @@ -1105,7 +1075,6 @@ func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessa if err != nil { return 0, nil, err } - return total, msgs, nil } @@ -1150,7 +1119,7 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa }, }, ) - //nolint:govet //this is already standard + pipe = mongo.Pipeline{ { {"$match", bson.D{ @@ -1245,6 +1214,5 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa } else { msgs = msgs[start:] } - return n, msgs, nil } diff --git a/pkg/common/db/unrelation/msg_convert.go b/pkg/common/db/unrelation/msg_convert.go index a5b28a5c7..810b4f419 100644 --- a/pkg/common/db/unrelation/msg_convert.go +++ b/pkg/common/db/unrelation/msg_convert.go @@ -31,14 +31,12 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": regex}) if err != nil { log.ZError(ctx, "convertAll find msg doc failed", err, "conversationID", conversationID) - continue } var msgDocs []table.MsgDocModel err = cursor.All(ctx, &msgDocs) if err != nil { log.ZError(ctx, "convertAll cursor all failed", err, "conversationID", conversationID) - continue } if len(msgDocs) < 1 { @@ -46,45 +44,39 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs } log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(msgDocs)", len(msgDocs)) if len(msgDocs[0].Msg) == int(m.model.GetSingleGocMsgNum5000()) { - convertMsgDocs(m, ctx, msgDocs, conversationID, regex) - } - } -} - -func convertMsgDocs(m *MsgMongoDriver, ctx context.Context, msgDocs []table.MsgDocModel, conversationID string, regex primitive.Regex) { - var err error - if _, err = m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": regex}); err != nil { - log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID) - - return - } - var newMsgDocs []interface{} - for _, msgDoc := range msgDocs { - if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() { - continue - } - var index int64 - for index < int64(len(msgDoc.Msg)) { - msg := msgDoc.Msg[index] - if msg != nil && msg.Msg != nil { - msgDocModel := table.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)} - end := index + m.model.GetSingleGocMsgNum() - if int(end) >= len(msgDoc.Msg) { - msgDocModel.Msg = msgDoc.Msg[index:] - } else { - msgDocModel.Msg = msgDoc.Msg[index:end] + if _, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": regex}); err != nil { + log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID) + continue + } + var newMsgDocs []interface{} + for _, msgDoc := range msgDocs { + if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() { + continue } - newMsgDocs = append(newMsgDocs, msgDocModel) - index = end + var index int64 + for index < int64(len(msgDoc.Msg)) { + msg := msgDoc.Msg[index] + if msg != nil && msg.Msg != nil { + msgDocModel := table.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)} + end := index + m.model.GetSingleGocMsgNum() + if int(end) >= len(msgDoc.Msg) { + msgDocModel.Msg = msgDoc.Msg[index:] + } else { + msgDocModel.Msg = msgDoc.Msg[index:end] + } + newMsgDocs = append(newMsgDocs, msgDocModel) + index = end + } else { + break + } + } + } + _, err = m.MsgCollection.InsertMany(ctx, newMsgDocs) + if err != nil { + log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) } else { - break + log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) } } } - _, err = m.MsgCollection.InsertMany(ctx, newMsgDocs) - if err != nil { - log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) - } else { - log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs)) - } } diff --git a/pkg/common/db/unrelation/super_group.go b/pkg/common/db/unrelation/super_group.go index 7f9aecfd6..c762140a2 100644 --- a/pkg/common/db/unrelation/super_group.go +++ b/pkg/common/db/unrelation/super_group.go @@ -59,7 +59,6 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st return err } } - return nil } @@ -70,7 +69,6 @@ func (s *SuperGroupMongoDriver) TakeSuperGroup( if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil { return nil, utils.Wrap(err, "") } - return group, nil } @@ -88,7 +86,6 @@ func (s *SuperGroupMongoDriver) FindSuperGroup( if err := cursor.All(ctx, &groups); err != nil { return nil, utils.Wrap(err, "") } - return groups, nil } @@ -116,7 +113,6 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID return utils.Wrap(err, "transaction failed") } } - return nil } @@ -133,7 +129,6 @@ func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, g if err != nil { return err } - return nil } @@ -143,7 +138,6 @@ func (s *SuperGroupMongoDriver) GetSuperGroupByUserID( ) (*unrelation.UserToSuperGroupModel, error) { var user unrelation.UserToSuperGroupModel err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user) - return &user, utils.Wrap(err, "") } @@ -155,7 +149,6 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil { return utils.Wrap(err, "") } - return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs) } @@ -165,6 +158,5 @@ func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}}, ) - return utils.Wrap(err, "") } diff --git a/pkg/common/db/unrelation/user.go b/pkg/common/db/unrelation/user.go index ad02968bd..4b4a78c79 100755 --- a/pkg/common/db/unrelation/user.go +++ b/pkg/common/db/unrelation/user.go @@ -16,7 +16,6 @@ package unrelation import ( "context" - "errors" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/utils" @@ -51,7 +50,6 @@ type UserMongoDriver struct { // AddSubscriptionList Subscriber's handling of thresholds. func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error { // Check the number of lists in the key. - //nolint:govet //this has already been the standard format for mongo.Pipeline pipeline := mongo.Pipeline{ {{"$match", bson.D{{"user_id", SubscriptionPrefix + userID}}}}, {{"$project", bson.D{{"count", bson.D{{"$size", "$user_id_list"}}}}}}, @@ -67,7 +65,7 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string } // iterate over aggregated results for cursor.Next(ctx) { - err = cursor.Decode(&cnt) + err := cursor.Decode(&cnt) if err != nil { return errs.Wrap(err) } @@ -124,7 +122,6 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string return utils.Wrap(err, "transaction failed") } } - return nil } @@ -142,7 +139,6 @@ func (u *UserMongoDriver) UnsubscriptionList(ctx context.Context, userID string, if err != nil { return errs.Wrap(err) } - return nil } @@ -156,7 +152,6 @@ func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, user bson.M{"$pull": bson.M{"user_id_list": userID}}, ) } - return errs.Wrap(err) } @@ -168,13 +163,12 @@ func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string bson.M{"user_id": SubscriptionPrefix + userID}) err = cursor.Decode(&user) if err != nil { - if errors.Is(err, mongo.ErrNoDocuments) { + if err == mongo.ErrNoDocuments { return []string{}, nil + } else { + return nil, errs.Wrap(err) } - - return nil, errs.Wrap(err) } - return user.UserIDList, nil } @@ -186,12 +180,11 @@ func (u *UserMongoDriver) GetSubscribedList(ctx context.Context, userID string) bson.M{"user_id": SubscribedPrefix + userID}) err = cursor.Decode(&user) if err != nil { - if errors.Is(err, mongo.ErrNoDocuments) { + if err == mongo.ErrNoDocuments { return []string{}, nil + } else { + return nil, errs.Wrap(err) } - - return nil, errs.Wrap(err) } - return user.UserIDList, nil } diff --git a/pkg/common/discovery_register/k8s_discovery_register.go b/pkg/common/discovery_register/k8s_discovery_register.go index 72179fdbd..70f9f39f3 100644 --- a/pkg/common/discovery_register/k8s_discovery_register.go +++ b/pkg/common/discovery_register/k8s_discovery_register.go @@ -4,14 +4,12 @@ import ( "context" "errors" "fmt" - "time" - "github.com/OpenIMSDK/tools/discoveryregistry" openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" - "google.golang.org/grpc" - "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "google.golang.org/grpc" + "time" ) func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) { @@ -30,7 +28,6 @@ func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistr client = nil err = errors.New("envType not correct") } - return client, err } @@ -45,51 +42,47 @@ func NewK8sDiscoveryRegister() (discoveryregistry.SvcDiscoveryRegistry, error) { func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error { cli.rpcRegisterAddr = serviceName - return nil } - func (cli *K8sDR) UnRegister() error { + return nil } - func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error { + return nil } - func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error { + return nil } func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) { + return nil, nil } - func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) { - conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) + conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) return []*grpc.ClientConn{conn}, err } - func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { + return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...) } - func (cli *K8sDR) GetSelfConnTarget() string { + return cli.rpcRegisterAddr } - func (cli *K8sDR) AddOption(opts ...grpc.DialOption) { cli.options = append(cli.options, opts...) } - func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) { conn.Close() } -// do not use this method for call rpc. +// do not use this method for call rpc func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn { fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!") - return nil } diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index 579643964..2d7c24c77 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -34,21 +34,16 @@ import ( var client http.Client func Get(url string) (response []byte, err error) { - clientGet := http.Client{Timeout: 5 * time.Second} - req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) + client := http.Client{Timeout: 5 * time.Second} + resp, err := client.Get(url) if err != nil { return nil, err } - resp, err2 := clientGet.Do(req) - if err2 != nil { - return nil, err - } defer resp.Body.Close() - body, err3 := io.ReadAll(resp.Body) - if err3 != nil { + body, err := io.ReadAll(resp.Body) + if err != nil { return nil, err } - return body, nil } @@ -88,7 +83,6 @@ func Post( if err != nil { return nil, err } - return result, nil } @@ -104,7 +98,6 @@ func PostReturn( return err } err = json.Unmarshal(b, output) - return err } @@ -123,22 +116,17 @@ func callBackPostReturn( if err != nil { if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue { log.ZWarn(ctx, "callback failed but continue", err, "url", url) - return errs.ErrCallbackContinue } - return errs.ErrNetwork.Wrap(err.Error()) } if err = json.Unmarshal(b, output); err != nil { if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue { log.ZWarn(ctx, "callback failed but continue", err, "url", url) - return errs.ErrCallbackContinue } - return errs.ErrData.Wrap(err.Error()) } - return output.Parse() } diff --git a/pkg/common/kafka/consumer_group.go b/pkg/common/kafka/consumer_group.go index c5ec69533..1eb7b522a 100644 --- a/pkg/common/kafka/consumer_group.go +++ b/pkg/common/kafka/consumer_group.go @@ -51,7 +51,6 @@ func NewMConsumerGroup(consumerConfig *MConsumerGroupConfig, topics, addrs []str if err != nil { panic(err.Error()) } - return &MConsumerGroup{ consumerGroup, groupID, diff --git a/pkg/common/kafka/producer.go b/pkg/common/kafka/producer.go index b4545af9b..4a52d2bef 100644 --- a/pkg/common/kafka/producer.go +++ b/pkg/common/kafka/producer.go @@ -67,7 +67,6 @@ func NewKafkaProducer(addr []string, topic string) *Producer { producer, err = sarama.NewSyncProducer(p.addr, p.config) // Initialize the client if err == nil { p.producer = producer - return &p } //TODO If the password is wrong, exit directly @@ -84,7 +83,6 @@ func NewKafkaProducer(addr []string, topic string) *Producer { panic(err.Error()) } p.producer = producer - return &p } @@ -93,7 +91,6 @@ func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error) if err != nil { return nil, err } - return []sarama.RecordHeader{ {Key: []byte(constant.OperationID), Value: []byte(operationID)}, {Key: []byte(constant.OpUserID), Value: []byte(opUserID)}, @@ -103,11 +100,10 @@ func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error) } func GetContextWithMQHeader(header []*sarama.RecordHeader) context.Context { - values := make([]string, 0, len(header)) + var values []string for _, recordHeader := range header { values = append(values, string(recordHeader.Value)) } - return mcontext.WithMustInfoCtx(values) // TODO } @@ -138,6 +134,5 @@ func (p *Producer) SendMessage(ctx context.Context, key string, msg proto.Messag if err == nil { prome.Inc(prome.SendMsgCounter) } - return partition, offset, utils.Wrap(err, "") } diff --git a/pkg/common/locker/message_locker.go b/pkg/common/locker/message_locker.go index 108dbbd48..55241eb5f 100644 --- a/pkg/common/locker/message_locker.go +++ b/pkg/common/locker/message_locker.go @@ -42,13 +42,11 @@ func (l *LockerMessage) LockMessageTypeKey(ctx context.Context, clientMsgID, typ err = l.cache.LockMessageTypeKey(ctx, clientMsgID, typeKey) if err != nil { time.Sleep(time.Millisecond * 100) - continue } else { break } } - return err } @@ -57,13 +55,11 @@ func (l *LockerMessage) LockGlobalMessage(ctx context.Context, clientMsgID strin err = l.cache.LockMessageTypeKey(ctx, clientMsgID, GlOBALLOCK) if err != nil { time.Sleep(time.Millisecond * 100) - continue } else { break } } - return err } diff --git a/pkg/common/prome/gather.go b/pkg/common/prome/gather.go index 184034e25..eb4bc6c3b 100644 --- a/pkg/common/prome/gather.go +++ b/pkg/common/prome/gather.go @@ -79,7 +79,6 @@ var ( ConversationCreateFailedCounter prometheus.Counter ) -//nolint:promlinter //no idea to fix it func NewUserLoginCounter() { if UserLoginCounter != nil { return @@ -90,7 +89,6 @@ func NewUserLoginCounter() { }) } -//nolint:promlinter //no idea to fix it func NewUserRegisterCounter() { if UserRegisterCounter != nil { return @@ -101,7 +99,6 @@ func NewUserRegisterCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSeqGetSuccessCounter() { if SeqGetSuccessCounter != nil { return @@ -112,7 +109,6 @@ func NewSeqGetSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSeqGetFailedCounter() { if SeqGetFailedCounter != nil { return @@ -123,7 +119,6 @@ func NewSeqGetFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSeqSetSuccessCounter() { if SeqSetSuccessCounter != nil { return @@ -134,7 +129,6 @@ func NewSeqSetSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSeqSetFailedCounter() { if SeqSetFailedCounter != nil { return @@ -145,7 +139,6 @@ func NewSeqSetFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewApiRequestCounter() { if ApiRequestCounter != nil { return @@ -156,7 +149,6 @@ func NewApiRequestCounter() { }) } -//nolint:promlinter //no idea to fix it func NewApiRequestSuccessCounter() { if ApiRequestSuccessCounter != nil { return @@ -167,7 +159,6 @@ func NewApiRequestSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewApiRequestFailedCounter() { if ApiRequestFailedCounter != nil { return @@ -178,7 +169,6 @@ func NewApiRequestFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewGrpcRequestCounter() { if GrpcRequestCounter != nil { return @@ -189,7 +179,6 @@ func NewGrpcRequestCounter() { }) } -//nolint:promlinter //no idea to fix it func NewGrpcRequestSuccessCounter() { if GrpcRequestSuccessCounter != nil { return @@ -200,7 +189,6 @@ func NewGrpcRequestSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewGrpcRequestFailedCounter() { if GrpcRequestFailedCounter != nil { return @@ -211,7 +199,6 @@ func NewGrpcRequestFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSendMsgCount() { if SendMsgCounter != nil { return @@ -222,7 +209,6 @@ func NewSendMsgCount() { }) } -//nolint:promlinter //no idea to fix it func NewMsgInsertRedisSuccessCounter() { if MsgInsertRedisSuccessCounter != nil { return @@ -233,7 +219,6 @@ func NewMsgInsertRedisSuccessCounter() { }) } -//nolint:promlinter //no idea to fix its func NewMsgInsertRedisFailedCounter() { if MsgInsertRedisFailedCounter != nil { return @@ -244,7 +229,6 @@ func NewMsgInsertRedisFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgInsertMongoSuccessCounter() { if MsgInsertMongoSuccessCounter != nil { return @@ -255,7 +239,6 @@ func NewMsgInsertMongoSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgInsertMongoFailedCounter() { if MsgInsertMongoFailedCounter != nil { return @@ -266,7 +249,6 @@ func NewMsgInsertMongoFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgPullFromRedisSuccessCounter() { if MsgPullFromRedisSuccessCounter != nil { return @@ -277,7 +259,6 @@ func NewMsgPullFromRedisSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgPullFromRedisFailedCounter() { if MsgPullFromRedisFailedCounter != nil { return @@ -288,7 +269,6 @@ func NewMsgPullFromRedisFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgPullFromMongoSuccessCounter() { if MsgPullFromMongoSuccessCounter != nil { return @@ -299,7 +279,6 @@ func NewMsgPullFromMongoSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgPullFromMongoFailedCounter() { if MsgPullFromMongoFailedCounter != nil { return @@ -340,7 +319,6 @@ func NewPullMsgBySeqListTotalCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSingleChatMsgRecvSuccessCounter() { if SingleChatMsgRecvSuccessCounter != nil { return @@ -351,7 +329,6 @@ func NewSingleChatMsgRecvSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewGroupChatMsgRecvSuccessCounter() { if GroupChatMsgRecvSuccessCounter != nil { return @@ -362,7 +339,6 @@ func NewGroupChatMsgRecvSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewWorkSuperGroupChatMsgRecvSuccessCounter() { if WorkSuperGroupChatMsgRecvSuccessCounter != nil { return @@ -383,7 +359,6 @@ func NewOnlineUserGauges() { }) } -//nolint:promlinter //no idea to fix it func NewSingleChatMsgProcessSuccessCounter() { if SingleChatMsgProcessSuccessCounter != nil { return @@ -394,7 +369,6 @@ func NewSingleChatMsgProcessSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewSingleChatMsgProcessFailedCounter() { if SingleChatMsgProcessFailedCounter != nil { return @@ -405,7 +379,6 @@ func NewSingleChatMsgProcessFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewGroupChatMsgProcessSuccessCounter() { if GroupChatMsgProcessSuccessCounter != nil { return @@ -416,7 +389,6 @@ func NewGroupChatMsgProcessSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewGroupChatMsgProcessFailedCounter() { if GroupChatMsgProcessFailedCounter != nil { return @@ -427,7 +399,6 @@ func NewGroupChatMsgProcessFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewWorkSuperGroupChatMsgProcessSuccessCounter() { if WorkSuperGroupChatMsgProcessSuccessCounter != nil { return @@ -438,7 +409,6 @@ func NewWorkSuperGroupChatMsgProcessSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewWorkSuperGroupChatMsgProcessFailedCounter() { if WorkSuperGroupChatMsgProcessFailedCounter != nil { return @@ -449,7 +419,6 @@ func NewWorkSuperGroupChatMsgProcessFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgOnlinePushSuccessCounter() { if MsgOnlinePushSuccessCounter != nil { return @@ -460,7 +429,6 @@ func NewMsgOnlinePushSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgOfflinePushSuccessCounter() { if MsgOfflinePushSuccessCounter != nil { return @@ -471,7 +439,6 @@ func NewMsgOfflinePushSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewMsgOfflinePushFailedCounter() { if MsgOfflinePushFailedCounter != nil { return @@ -482,7 +449,6 @@ func NewMsgOfflinePushFailedCounter() { }) } -//nolint:promlinter //no idea to fix it func NewConversationCreateSuccessCounter() { if ConversationCreateSuccessCounter != nil { return @@ -493,7 +459,6 @@ func NewConversationCreateSuccessCounter() { }) } -//nolint:promlinter //no idea to fix it func NewConversationCreateFailedCounter() { if ConversationCreateFailedCounter != nil { return diff --git a/pkg/common/prome/prometheus.go b/pkg/common/prome/prometheus.go index 60df5b0af..254a6c9ea 100644 --- a/pkg/common/prome/prometheus.go +++ b/pkg/common/prome/prometheus.go @@ -30,16 +30,13 @@ func StartPrometheusSrv(prometheusPort int) error { if config.Config.Prometheus.Enable { http.Handle("/metrics", promhttp.Handler()) err := http.ListenAndServe(":"+strconv.Itoa(prometheusPort), nil) - return err } - return nil } func PrometheusHandler() gin.HandlerFunc { h := promhttp.Handler() - return func(c *gin.Context) { h.ServeHTTP(c.Writer, c.Request) } @@ -52,7 +49,6 @@ type responseBodyWriter struct { func (r responseBodyWriter) Write(b []byte) (int, error) { r.body.Write(b) - return r.ResponseWriter.Write(b) } diff --git a/pkg/common/tls/tls.go b/pkg/common/tls/tls.go index 7b3e9033e..3bf91beb9 100755 --- a/pkg/common/tls/tls.go +++ b/pkg/common/tls/tls.go @@ -24,7 +24,6 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) -//nolint:staticcheck //we have not time looking for a replacement for x509 to fix the security valnerability func decryptPEM(data []byte, passphrase []byte) ([]byte, error) { if len(passphrase) == 0 { return data, nil @@ -34,7 +33,6 @@ func decryptPEM(data []byte, passphrase []byte) ([]byte, error) { if err != nil { return nil, err } - return pem.EncodeToMemory(&pem.Block{ Type: b.Type, Bytes: d, @@ -46,7 +44,6 @@ func readEncryptablePEMBlock(path string, pwd []byte) ([]byte, error) { if err != nil { return nil, err } - return decryptPEM(data, pwd) } diff --git a/pkg/msgprocessor/conversation.go b/pkg/msgprocessor/conversation.go index 559994eaf..ca77438ea 100644 --- a/pkg/msgprocessor/conversation.go +++ b/pkg/msgprocessor/conversation.go @@ -28,7 +28,6 @@ func GetNotificationConversationIDByMsg(msg *sdkws.MsgData) string { case constant.SingleChatType: l := []string{msg.SendID, msg.RecvID} sort.Strings(l) - return "n_" + strings.Join(l, "_") case constant.GroupChatType: return "n_" + msg.GroupID @@ -37,7 +36,6 @@ func GetNotificationConversationIDByMsg(msg *sdkws.MsgData) string { case constant.NotificationChatType: return "n_" + msg.SendID + "_" + msg.RecvID } - return "" } @@ -46,7 +44,6 @@ func GetChatConversationIDByMsg(msg *sdkws.MsgData) string { case constant.SingleChatType: l := []string{msg.SendID, msg.RecvID} sort.Strings(l) - return "si_" + strings.Join(l, "_") case constant.GroupChatType: return "g_" + msg.GroupID @@ -55,7 +52,6 @@ func GetChatConversationIDByMsg(msg *sdkws.MsgData) string { case constant.NotificationChatType: return "sn_" + msg.SendID + "_" + msg.RecvID } - return "" } @@ -64,12 +60,10 @@ func GenConversationUniqueKey(msg *sdkws.MsgData) string { case constant.SingleChatType, constant.NotificationChatType: l := []string{msg.SendID, msg.RecvID} sort.Strings(l) - return strings.Join(l, "_") case constant.SuperGroupChatType: return msg.GroupID } - return "" } @@ -82,28 +76,23 @@ func GetConversationIDByMsg(msg *sdkws.MsgData) string { if !options.IsNotNotification() { return "n_" + strings.Join(l, "_") } - return "si_" + strings.Join(l, "_") // single chat case constant.GroupChatType: if !options.IsNotNotification() { return "n_" + msg.GroupID // group chat } - return "g_" + msg.GroupID // group chat case constant.SuperGroupChatType: if !options.IsNotNotification() { return "n_" + msg.GroupID // super group chat } - return "sg_" + msg.GroupID // super group chat case constant.NotificationChatType: if !options.IsNotNotification() { return "n_" + msg.SendID + "_" + msg.RecvID // super group chat } - return "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat } - return "" } @@ -122,7 +111,6 @@ func GetConversationIDBySessionType(sessionType int, ids ...string) string { case constant.NotificationChatType: return "sn_" + ids[0] // server notification chat } - return "" } @@ -130,11 +118,10 @@ func GetNotificationConversationIDByConversationID(conversationID string) string l := strings.Split(conversationID, "_") if len(l) > 1 { l[0] = "n" - return strings.Join(l, "_") + } else { + return "" } - - return "" } func GetNotificationConversationID(sessionType int, ids ...string) string { @@ -148,7 +135,6 @@ func GetNotificationConversationID(sessionType int, ids ...string) string { case constant.SuperGroupChatType: return "n_" + ids[0] // super group chat } - return "" } @@ -169,22 +155,18 @@ func ParseConversationID(msg *sdkws.MsgData) (isNotification bool, conversationI if !options.IsNotNotification() { return true, "n_" + strings.Join(l, "_") } - return false, "si_" + strings.Join(l, "_") // single chat case constant.SuperGroupChatType: if !options.IsNotNotification() { return true, "n_" + msg.GroupID // super group chat } - return false, "sg_" + msg.GroupID // super group chat case constant.NotificationChatType: if !options.IsNotNotification() { return true, "n_" + msg.SendID + "_" + msg.RecvID // super group chat } - return false, "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat } - return false, "" } @@ -207,7 +189,6 @@ func Pb2String(pb proto.Message) (string, error) { if err != nil { return "", err } - return string(s), nil } diff --git a/pkg/msgprocessor/options.go b/pkg/msgprocessor/options.go index 27bbb839a..c17c7cb05 100644 --- a/pkg/msgprocessor/options.go +++ b/pkg/msgprocessor/options.go @@ -38,14 +38,12 @@ func NewOptions(opts ...OptionsOpt) Options { for _, opt := range opts { opt(options) } - return options } func NewMsgOptions() Options { options := make(map[string]bool, 11) options[constant.IsOfflinePush] = false - return make(map[string]bool) } @@ -53,7 +51,6 @@ func WithOptions(options Options, opts ...OptionsOpt) Options { for _, opt := range opts { opt(options) } - return options } @@ -134,7 +131,6 @@ func (o Options) Is(notification string) bool { if !ok || v { return true } - return false } diff --git a/pkg/rpcclient/auth.go b/pkg/rpcclient/auth.go index 4859e541d..0ee021de1 100644 --- a/pkg/rpcclient/auth.go +++ b/pkg/rpcclient/auth.go @@ -31,7 +31,6 @@ func NewAuth(discov discoveryregistry.SvcDiscoveryRegistry) *Auth { panic(err) } client := auth.NewAuthClient(conn) - return &Auth{discov: discov, conn: conn, Client: client} } diff --git a/pkg/rpcclient/conversation.go b/pkg/rpcclient/conversation.go index df01bcb8f..3ba8dd8c0 100644 --- a/pkg/rpcclient/conversation.go +++ b/pkg/rpcclient/conversation.go @@ -60,8 +60,7 @@ func (c *ConversationRpcClient) GetSingleConversationRecvMsgOpt(ctx context.Cont } func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Context, recvID, sendID, - conversationID string, conversationType int32, -) error { + conversationID string, conversationType int32) error { _, err := c.Client.CreateSingleChatConversations(ctx, &pbconversation.CreateSingleChatConversationsReq{ RecvID: recvID, SendID: sendID, ConversationID: conversationID, @@ -72,19 +71,16 @@ func (c *ConversationRpcClient) SingleChatFirstCreateConversation(ctx context.Co func (c *ConversationRpcClient) GroupChatFirstCreateConversation(ctx context.Context, groupID string, userIDs []string) error { _, err := c.Client.CreateGroupChatConversations(ctx, &pbconversation.CreateGroupChatConversationsReq{UserIDs: userIDs, GroupID: groupID}) - return err } func (c *ConversationRpcClient) SetConversationMaxSeq(ctx context.Context, ownerUserIDs []string, conversationID string, maxSeq int64) error { _, err := c.Client.SetConversationMaxSeq(ctx, &pbconversation.SetConversationMaxSeqReq{OwnerUserID: ownerUserIDs, ConversationID: conversationID, MaxSeq: maxSeq}) - return err } func (c *ConversationRpcClient) SetConversations(ctx context.Context, userIDs []string, conversation *pbconversation.ConversationReq) error { _, err := c.Client.SetConversations(ctx, &pbconversation.SetConversationsReq{UserIDs: userIDs, Conversation: conversation}) - return err } @@ -93,7 +89,6 @@ func (c *ConversationRpcClient) GetConversationIDs(ctx context.Context, ownerUse if err != nil { return nil, err } - return resp.ConversationIDs, nil } @@ -102,7 +97,6 @@ func (c *ConversationRpcClient) GetConversation(ctx context.Context, ownerUserID if err != nil { return nil, err } - return resp.Conversation, nil } @@ -117,7 +111,6 @@ func (c *ConversationRpcClient) GetConversationsByConversationID(ctx context.Con if len(resp.Conversations) == 0 { return nil, errs.ErrRecordNotFound.Wrap(fmt.Sprintf("conversationIDs: %v not found", conversationIDs)) } - return resp.Conversations, nil } @@ -136,6 +129,5 @@ func (c *ConversationRpcClient) GetConversations( if err != nil { return nil, err } - return resp.Conversations, nil } diff --git a/pkg/rpcclient/friend.go b/pkg/rpcclient/friend.go index 6b214aaf2..b84db40d4 100644 --- a/pkg/rpcclient/friend.go +++ b/pkg/rpcclient/friend.go @@ -38,7 +38,6 @@ func NewFriend(discov discoveryregistry.SvcDiscoveryRegistry) *Friend { panic(err) } client := friend.NewFriendClient(conn) - return &Friend{discov: discov, conn: conn, Client: client} } @@ -60,7 +59,6 @@ func (f *FriendRpcClient) GetFriendsInfo( return nil, err } resp = r.FriendsInfo[0] - return } @@ -70,7 +68,6 @@ func (f *FriendRpcClient) IsFriend(ctx context.Context, possibleFriendUserID, us if err != nil { return false, err } - return resp.InUser1Friends, nil } @@ -80,7 +77,6 @@ func (f *FriendRpcClient) GetFriendIDs(ctx context.Context, ownerUserID string) if err != nil { return nil, err } - return resp.FriendIDs, nil } @@ -89,6 +85,5 @@ func (b *FriendRpcClient) IsBlocked(ctx context.Context, possibleBlackUserID, us if err != nil { return false, err } - return r.InUser2Blacks, nil } diff --git a/pkg/rpcclient/group.go b/pkg/rpcclient/group.go index 5a340875b..bf0efe60c 100644 --- a/pkg/rpcclient/group.go +++ b/pkg/rpcclient/group.go @@ -42,7 +42,6 @@ func NewGroup(discov discoveryregistry.SvcDiscoveryRegistry) *Group { panic(err) } client := group.NewGroupClient(conn) - return &Group{discov: discov, conn: conn, Client: client} } @@ -70,7 +69,6 @@ func (g *GroupRpcClient) GetGroupInfos( return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ",")) } } - return resp.GroupInfos, nil } @@ -79,7 +77,6 @@ func (g *GroupRpcClient) GetGroupInfo(ctx context.Context, groupID string) (*sdk if err != nil { return nil, err } - return groups[0], nil } @@ -92,7 +89,6 @@ func (g *GroupRpcClient) GetGroupInfoMap( if err != nil { return nil, err } - return utils.SliceToMap(groups, func(e *sdkws.GroupInfo) string { return e.GroupID }), nil @@ -118,7 +114,6 @@ func (g *GroupRpcClient) GetGroupMemberInfos( return nil, errs.ErrNotInGroupYet.Wrap(strings.Join(ids, ",")) } } - return resp.Members, nil } @@ -131,7 +126,6 @@ func (g *GroupRpcClient) GetGroupMemberInfo( if err != nil { return nil, err } - return members[0], nil } @@ -145,7 +139,6 @@ func (g *GroupRpcClient) GetGroupMemberInfoMap( if err != nil { return nil, err } - return utils.SliceToMap(members, func(e *sdkws.GroupMemberFullInfo) string { return e.UserID }), nil @@ -162,7 +155,6 @@ func (g *GroupRpcClient) GetOwnerAndAdminInfos( if err != nil { return nil, err } - return resp.Members, nil } @@ -171,7 +163,6 @@ func (g *GroupRpcClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdk GroupID: groupID, RoleLevels: []int32{constant.GroupOwner}, }) - return resp.Members[0], err } @@ -182,7 +173,6 @@ func (g *GroupRpcClient) GetGroupMemberIDs(ctx context.Context, groupID string) if err != nil { return nil, err } - return resp.UserIDs, nil } @@ -193,7 +183,6 @@ func (g *GroupRpcClient) GetGroupInfoCache(ctx context.Context, groupID string) if err != nil { return nil, err } - return resp.GroupInfo, nil } @@ -209,7 +198,6 @@ func (g *GroupRpcClient) GetGroupMemberCache( if err != nil { return nil, err } - return resp.Member, nil } @@ -218,7 +206,6 @@ func (g *GroupRpcClient) DismissGroup(ctx context.Context, groupID string) error GroupID: groupID, DeleteMember: true, }) - return err } @@ -226,6 +213,5 @@ func (g *GroupRpcClient) NotificationUserInfoUpdate(ctx context.Context, userID _, err := g.Client.NotificationUserInfoUpdate(ctx, &group.NotificationUserInfoUpdateReq{ UserID: userID, }) - return err } diff --git a/pkg/rpcclient/msg.go b/pkg/rpcclient/msg.go index 00b0fa3f1..3b09b5062 100644 --- a/pkg/rpcclient/msg.go +++ b/pkg/rpcclient/msg.go @@ -136,7 +136,6 @@ func NewMessage(discov discoveryregistry.SvcDiscoveryRegistry) *Message { panic(err) } client := msg.NewMsgClient(conn) - return &Message{discov: discov, conn: conn, Client: client} } @@ -148,19 +147,16 @@ func NewMessageRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) MessageR func (m *MessageRpcClient) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) { resp, err := m.Client.SendMsg(ctx, req) - return resp, err } func (m *MessageRpcClient) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) { resp, err := m.Client.GetMaxSeq(ctx, req) - return resp, err } func (m *MessageRpcClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMessageBySeqsReq) (*sdkws.PullMessageBySeqsResp, error) { resp, err := m.Client.PullMessageBySeqs(ctx, req) - return resp, err } @@ -169,7 +165,6 @@ func (m *MessageRpcClient) GetConversationMaxSeq(ctx context.Context, conversati if err != nil { return 0, err } - return resp.MaxSeq, nil } @@ -205,7 +200,6 @@ func NewNotificationSender(opts ...NotificationSenderOptions) *NotificationSende for _, opt := range opts { opt(notificationSender) } - return notificationSender } @@ -221,13 +215,11 @@ func WithRpcGetUserName() NotificationOptions { } } -func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, sendID, recvID string, - contentType, sesstionType int32, m proto.Message, opts ...NotificationOptions) (err error) { +func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, sendID, recvID string, contentType, sesstionType int32, m proto.Message, opts ...NotificationOptions) (err error) { n := sdkws.NotificationElem{Detail: utils.StructToJsonString(m)} content, err := json.Marshal(&n) if err != nil { - log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", - sendID, "recvID", recvID, "contentType", contentType, "msg", m) + log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m) return err } notificationOpt := ¬ificationOpt{} @@ -276,7 +268,6 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s } else { log.ZError(ctx, "MsgClient Notification SendMsg failed", err, "req", &req) } - return err } diff --git a/pkg/rpcclient/notification/friend.go b/pkg/rpcclient/notification/friend.go index 9dae27c6e..b061a24ae 100644 --- a/pkg/rpcclient/notification/friend.go +++ b/pkg/rpcclient/notification/friend.go @@ -57,7 +57,6 @@ func WithDBFunc( for _, user := range users { result = append(result, user) } - return result, nil } s.getUsersInfo = f @@ -76,7 +75,6 @@ func WithRpcFunc( for _, user := range users { result = append(result, user) } - return result, err } s.getUsersInfo = f @@ -93,7 +91,6 @@ func NewFriendNotificationSender( for _, opt := range opts { opt(f) } - return f } @@ -109,13 +106,22 @@ func (f *FriendNotificationSender) getUsersInfoMap( for _, user := range users { result[user.GetUserID()] = user.(*sdkws.UserInfo) } - return result, nil } +func (f *FriendNotificationSender) getFromToUserNickname( + ctx context.Context, + fromUserID, toUserID string, +) (string, string, error) { + users, err := f.getUsersInfoMap(ctx, []string{fromUserID, toUserID}) + if err != nil { + return "", "", nil + } + return users[fromUserID].Nickname, users[toUserID].Nickname, nil +} + func (f *FriendNotificationSender) UserInfoUpdatedNotification(ctx context.Context, changedUserID string) error { tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} - return f.Notification(ctx, mcontext.GetOpUserID(ctx), changedUserID, constant.UserInfoUpdatedNotification, &tips) } @@ -127,7 +133,6 @@ func (f *FriendNotificationSender) FriendApplicationAddNotification( FromUserID: req.FromUserID, ToUserID: req.ToUserID, }} - return f.Notification(ctx, req.FromUserID, req.ToUserID, constant.FriendApplicationNotification, &tips) } @@ -139,7 +144,6 @@ func (f *FriendNotificationSender) FriendApplicationAgreedNotification( FromUserID: req.FromUserID, ToUserID: req.ToUserID, }, HandleMsg: req.HandleMsg} - return f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationApprovedNotification, &tips) } @@ -151,7 +155,6 @@ func (f *FriendNotificationSender) FriendApplicationRefusedNotification( FromUserID: req.FromUserID, ToUserID: req.ToUserID, }, HandleMsg: req.HandleMsg} - return f.Notification(ctx, req.ToUserID, req.FromUserID, constant.FriendApplicationRejectedNotification, &tips) } @@ -176,7 +179,6 @@ func (f *FriendNotificationSender) FriendAddedNotification( if err != nil { return err } - return f.Notification(ctx, fromUserID, toUserID, constant.FriendAddedNotification, &tips) } @@ -185,7 +187,6 @@ func (f *FriendNotificationSender) FriendDeletedNotification(ctx context.Context FromUserID: req.OwnerUserID, ToUserID: req.FriendUserID, }} - return f.Notification(ctx, req.OwnerUserID, req.FriendUserID, constant.FriendDeletedNotification, &tips) } @@ -193,7 +194,6 @@ func (f *FriendNotificationSender) FriendRemarkSetNotification(ctx context.Conte tips := sdkws.FriendInfoChangedTips{FromToUserID: &sdkws.FromToUserID{}} tips.FromToUserID.FromUserID = fromUserID tips.FromToUserID.ToUserID = toUserID - return f.Notification(ctx, fromUserID, toUserID, constant.FriendRemarkSetNotification, &tips) } @@ -201,7 +201,6 @@ func (f *FriendNotificationSender) BlackAddedNotification(ctx context.Context, r tips := sdkws.BlackAddedTips{FromToUserID: &sdkws.FromToUserID{}} tips.FromToUserID.FromUserID = req.OwnerUserID tips.FromToUserID.ToUserID = req.BlackUserID - return f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackAddedNotification, &tips) } @@ -210,10 +209,7 @@ func (f *FriendNotificationSender) BlackDeletedNotification(ctx context.Context, FromUserID: req.OwnerUserID, ToUserID: req.BlackUserID, }} - err := f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips) - if err != nil { - panic(err) - } + f.Notification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips) } func (f *FriendNotificationSender) FriendInfoUpdatedNotification( @@ -222,8 +218,5 @@ func (f *FriendNotificationSender) FriendInfoUpdatedNotification( needNotifiedUserID string, ) { tips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} - err := f.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips) - if err != nil { - panic(err) - } + f.Notification(ctx, mcontext.GetOpUserID(ctx), needNotifiedUserID, constant.FriendInfoUpdatedNotification, &tips) } diff --git a/pkg/rpcclient/notification/group.go b/pkg/rpcclient/notification/group.go index 23341af70..8e71f61c3 100755 --- a/pkg/rpcclient/notification/group.go +++ b/pkg/rpcclient/notification/group.go @@ -60,7 +60,6 @@ func (g *GroupNotificationSender) getUser(ctx context.Context, userID string) (* if len(users) == 0 { return nil, errs.ErrUserIDNotFound.Wrap(fmt.Sprintf("user %s not found", userID)) } - return &sdkws.PublicUserInfo{ UserID: users[0].GetUserID(), Nickname: users[0].GetNickname(), @@ -69,23 +68,6 @@ func (g *GroupNotificationSender) getUser(ctx context.Context, userID string) (* }, nil } -func (g *GroupNotificationSender) groupMemberDB2PB(member *relation.GroupMemberModel, appMangerLevel int32) *sdkws.GroupMemberFullInfo { - return &sdkws.GroupMemberFullInfo{ - GroupID: member.GroupID, - UserID: member.UserID, - RoleLevel: member.RoleLevel, - JoinTime: member.JoinTime.UnixMilli(), - Nickname: member.Nickname, - FaceURL: member.FaceURL, - AppMangerLevel: appMangerLevel, - JoinSource: member.JoinSource, - OperatorUserID: member.OperatorUserID, - Ex: member.Ex, - MuteEndTime: member.MuteEndTime.UnixMilli(), - InviterUserID: member.InviterUserID, - } -} - func (g *GroupNotificationSender) getGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { gm, err := g.db.TakeGroup(ctx, groupID) if err != nil { @@ -99,7 +81,6 @@ func (g *GroupNotificationSender) getGroupInfo(ctx context.Context, groupID stri if err != nil { return nil, err } - return &sdkws.GroupInfo{ GroupID: gm.GroupID, GroupName: gm.GroupName, @@ -167,7 +148,6 @@ func (g *GroupNotificationSender) getGroupMemberMap(ctx context.Context, groupID for i, member := range members { m[member.UserID] = members[i] } - return m, nil } @@ -179,7 +159,6 @@ func (g *GroupNotificationSender) getGroupMember(ctx context.Context, groupID st if len(members) == 0 { return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("group %s member %s not found", groupID, userID)) } - return members[0], nil } @@ -189,10 +168,48 @@ func (g *GroupNotificationSender) getGroupOwnerAndAdminUserID(ctx context.Contex return nil, err } fn := func(e *relation.GroupMemberModel) string { return e.UserID } - return utils.Slice(members, fn), nil } +func (g *GroupNotificationSender) groupDB2PB(group *relation.GroupModel, ownerUserID string, memberCount uint32) *sdkws.GroupInfo { + return &sdkws.GroupInfo{ + GroupID: group.GroupID, + GroupName: group.GroupName, + Notification: group.Notification, + Introduction: group.Introduction, + FaceURL: group.FaceURL, + OwnerUserID: ownerUserID, + CreateTime: group.CreateTime.UnixMilli(), + MemberCount: memberCount, + Ex: group.Ex, + Status: group.Status, + CreatorUserID: group.CreatorUserID, + GroupType: group.GroupType, + NeedVerification: group.NeedVerification, + LookMemberInfo: group.LookMemberInfo, + ApplyMemberFriend: group.ApplyMemberFriend, + NotificationUpdateTime: group.NotificationUpdateTime.UnixMilli(), + NotificationUserID: group.NotificationUserID, + } +} + +func (g *GroupNotificationSender) groupMemberDB2PB(member *relation.GroupMemberModel, appMangerLevel int32) *sdkws.GroupMemberFullInfo { + return &sdkws.GroupMemberFullInfo{ + GroupID: member.GroupID, + UserID: member.UserID, + RoleLevel: member.RoleLevel, + JoinTime: member.JoinTime.UnixMilli(), + Nickname: member.Nickname, + FaceURL: member.FaceURL, + AppMangerLevel: appMangerLevel, + JoinSource: member.JoinSource, + OperatorUserID: member.OperatorUserID, + Ex: member.Ex, + MuteEndTime: member.MuteEndTime.UnixMilli(), + InviterUserID: member.InviterUserID, + } +} + func (g *GroupNotificationSender) getUsersInfoMap(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error) { users, err := g.getUsersInfo(ctx, userIDs) if err != nil { @@ -202,7 +219,6 @@ func (g *GroupNotificationSender) getUsersInfoMap(ctx context.Context, userIDs [ for _, user := range users { result[user.GetUserID()] = user.(*sdkws.UserInfo) } - return result, nil } @@ -220,31 +236,21 @@ func (g *GroupNotificationSender) fillOpUser(ctx context.Context, opUser **sdkws return nil } userID := mcontext.GetOpUserID(ctx) - getOpUser := func(g *GroupNotificationSender, groupID, userID string) (opUser *sdkws.GroupMemberFullInfo, err error) { + if groupID != "" { if authverify.IsManagerUserID(userID) { - opUser = &sdkws.GroupMemberFullInfo{ + *opUser = &sdkws.GroupMemberFullInfo{ GroupID: groupID, UserID: userID, RoleLevel: constant.GroupAdmin, AppMangerLevel: constant.AppAdmin, } - - return opUser, nil - } - var member *relation.GroupMemberModel - member, err = g.db.TakeGroupMember(ctx, groupID, userID) - if err == nil { - opUser = g.groupMemberDB2PB(member, 0) - } else if !errs.ErrRecordNotFound.Is(err) { - return nil, err - } - - return opUser, nil - } - if groupID != "" { - *opUser, err = getOpUser(g, groupID, userID) - if err != nil { - return err + } else { + member, err := g.db.TakeGroupMember(ctx, groupID, userID) + if err == nil { + *opUser = g.groupMemberDB2PB(member, 0) + } else if !errs.ErrRecordNotFound.Is(err) { + return err + } } } user, err := g.getUser(ctx, userID) @@ -267,7 +273,6 @@ func (g *GroupNotificationSender) fillOpUser(ctx context.Context, opUser **sdkws (*opUser).FaceURL = user.FaceURL } } - return nil } @@ -281,7 +286,6 @@ func (g *GroupNotificationSender) GroupCreatedNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupCreatedNotification, tips) } @@ -295,7 +299,6 @@ func (g *GroupNotificationSender) GroupInfoSetNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetNotification, tips, rpcclient.WithRpcGetUserName()) } @@ -309,7 +312,6 @@ func (g *GroupNotificationSender) GroupInfoSetNameNotification(ctx context.Conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetNameNotification, tips) } @@ -323,7 +325,6 @@ func (g *GroupNotificationSender) GroupInfoSetAnnouncementNotification(ctx conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetAnnouncementNotification, tips, rpcclient.WithRpcGetUserName()) } @@ -354,7 +355,6 @@ func (g *GroupNotificationSender) JoinGroupApplicationNotification(ctx context.C log.ZError(ctx, "JoinGroupApplicationNotification failed", err, "group", req.GroupID, "userID", userID) } } - return nil } @@ -370,7 +370,6 @@ func (g *GroupNotificationSender) MemberQuitNotification(ctx context.Context, me return err } tips := &sdkws.MemberQuitTips{Group: group, QuitUser: member} - return g.Notification(ctx, mcontext.GetOpUserID(ctx), member.GroupID, constant.MemberQuitNotification, tips) } @@ -390,8 +389,7 @@ func (g *GroupNotificationSender) GroupApplicationAcceptedNotification(ctx conte return err } tips := &sdkws.GroupApplicationAcceptedTips{Group: group, HandleMsg: req.HandledMsg, ReceiverAs: 1} - err = g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID) - if err != nil { + if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } for _, userID := range append(userIDs, mcontext.GetOpUserID(ctx)) { @@ -400,7 +398,6 @@ func (g *GroupNotificationSender) GroupApplicationAcceptedNotification(ctx conte log.ZError(ctx, "failed", err) } } - return nil } @@ -420,8 +417,7 @@ func (g *GroupNotificationSender) GroupApplicationRejectedNotification(ctx conte return err } tips := &sdkws.GroupApplicationRejectedTips{Group: group, HandleMsg: req.HandledMsg} - err = g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID) - if err != nil { + if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } for _, userID := range append(userIDs, mcontext.GetOpUserID(ctx)) { @@ -430,7 +426,6 @@ func (g *GroupNotificationSender) GroupApplicationRejectedNotification(ctx conte log.ZError(ctx, "failed", err) } } - return nil } @@ -454,7 +449,6 @@ func (g *GroupNotificationSender) GroupOwnerTransferredNotification(ctx context. if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupOwnerTransferredNotification, tips) } @@ -468,7 +462,6 @@ func (g *GroupNotificationSender) MemberKickedNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.MemberKickedNotification, tips) } @@ -494,7 +487,6 @@ func (g *GroupNotificationSender) MemberInvitedNotification(ctx context.Context, if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.MemberInvitedNotification, tips) } @@ -514,7 +506,6 @@ func (g *GroupNotificationSender) MemberEnterNotification(ctx context.Context, g return err } tips := &sdkws.MemberEnterTips{Group: group, EntrantUser: user} - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.MemberEnterNotification, tips) } @@ -528,7 +519,6 @@ func (g *GroupNotificationSender) GroupDismissedNotification(ctx context.Context if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupDismissedNotification, tips) } @@ -554,7 +544,6 @@ func (g *GroupNotificationSender) GroupMemberMutedNotification(ctx context.Conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberMutedNotification, tips) } @@ -577,7 +566,6 @@ func (g *GroupNotificationSender) GroupMemberCancelMutedNotification(ctx context if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberCancelMutedNotification, tips) } @@ -603,7 +591,6 @@ func (g *GroupNotificationSender) GroupMutedNotification(ctx context.Context, gr if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMutedNotification, tips) } @@ -629,7 +616,6 @@ func (g *GroupNotificationSender) GroupCancelMutedNotification(ctx context.Conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupCancelMutedNotification, tips) } @@ -652,7 +638,6 @@ func (g *GroupNotificationSender) GroupMemberInfoSetNotification(ctx context.Con if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberInfoSetNotification, tips) } @@ -675,7 +660,6 @@ func (g *GroupNotificationSender) GroupMemberSetToAdminNotification(ctx context. if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberSetToAdminNotification, tips) } @@ -698,7 +682,6 @@ func (g *GroupNotificationSender) GroupMemberSetToOrdinaryUserNotification(ctx c if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), group.GroupID, constant.GroupMemberSetToOrdinaryUserNotification, tips) } @@ -710,6 +693,5 @@ func (g *GroupNotificationSender) SuperGroupNotification(ctx context.Context, se } }() err = g.Notification(ctx, sendID, recvID, constant.SuperGroupUpdateNotification, nil) - return err } diff --git a/pkg/rpcclient/notification/msg.go b/pkg/rpcclient/notification/msg.go index 6e367ac04..60fa64f40 100644 --- a/pkg/rpcclient/notification/msg.go +++ b/pkg/rpcclient/notification/msg.go @@ -37,7 +37,6 @@ func (m *MsgNotificationSender) UserDeleteMsgsNotification(ctx context.Context, ConversationID: conversationID, Seqs: seqs, } - return m.Notification(ctx, userID, userID, constant.DeleteMsgsNotification, &tips) } @@ -48,6 +47,5 @@ func (m *MsgNotificationSender) MarkAsReadNotification(ctx context.Context, conv Seqs: seqs, HasReadSeq: hasReadSeq, } - return m.NotificationWithSesstionType(ctx, sendID, recvID, constant.HasReadReceipt, sesstionType, tips) } diff --git a/pkg/rpcclient/notification/user.go b/pkg/rpcclient/notification/user.go index f6e592d18..4feebf7b9 100644 --- a/pkg/rpcclient/notification/user.go +++ b/pkg/rpcclient/notification/user.go @@ -52,7 +52,6 @@ func WithUserFunc( for _, user := range users { result = append(result, user) } - return result, nil } u.getUsersInfo = f @@ -69,37 +68,34 @@ func NewUserNotificationSender( for _, opt := range opts { opt(f) } - return f } -// func (u *UserNotificationSender) getUsersInfoMap( -// ctx context.Context, -// userIDs []string, -// ) (map[string]*sdkws.UserInfo, error) { -// users, err := u.getUsersInfo(ctx, userIDs) -// if err != nil { -// return nil, err -// } -// result := make(map[string]*sdkws.UserInfo) -// for _, user := range users { -// result[user.GetUserID()] = user.(*sdkws.UserInfo) -// } - -// return result, nil -// } - -// func (u *UserNotificationSender) getFromToUserNickname( -// ctx context.Context, -// fromUserID, toUserID string, -// ) (string, string, error) { -// users, err := u.getUsersInfoMap(ctx, []string{fromUserID, toUserID}) -// if err != nil { -// return "", "", err -// } +func (u *UserNotificationSender) getUsersInfoMap( + ctx context.Context, + userIDs []string, +) (map[string]*sdkws.UserInfo, error) { + users, err := u.getUsersInfo(ctx, userIDs) + if err != nil { + return nil, err + } + result := make(map[string]*sdkws.UserInfo) + for _, user := range users { + result[user.GetUserID()] = user.(*sdkws.UserInfo) + } + return result, nil +} -// return users[fromUserID].Nickname, users[toUserID].Nickname, nil -// } +func (u *UserNotificationSender) getFromToUserNickname( + ctx context.Context, + fromUserID, toUserID string, +) (string, string, error) { + users, err := u.getUsersInfoMap(ctx, []string{fromUserID, toUserID}) + if err != nil { + return "", "", nil + } + return users[fromUserID].Nickname, users[toUserID].Nickname, nil +} func (u *UserNotificationSender) UserStatusChangeNotification( ctx context.Context, diff --git a/pkg/rpcclient/push.go b/pkg/rpcclient/push.go index 7733572bf..6d0876972 100644 --- a/pkg/rpcclient/push.go +++ b/pkg/rpcclient/push.go @@ -36,7 +36,6 @@ func NewPush(discov discoveryregistry.SvcDiscoveryRegistry) *Push { if err != nil { panic(err) } - return &Push{ discov: discov, conn: conn, diff --git a/pkg/rpcclient/third.go b/pkg/rpcclient/third.go index 2bb761450..48a537112 100755 --- a/pkg/rpcclient/third.go +++ b/pkg/rpcclient/third.go @@ -42,13 +42,14 @@ func NewThird(discov discoveryregistry.SvcDiscoveryRegistry) *Third { panic(err) } client := third.NewThirdClient(conn) - minioClient, _ := minioInit() - + minioClient, err := minioInit() return &Third{discov: discov, Client: client, conn: conn, MinioClient: minioClient} } func minioInit() (*minio.Client, error) { - initUrl := config.Config.Object.Minio.Endpoint + minioClient := &minio.Client{} + var initUrl string + initUrl = config.Config.Object.Minio.Endpoint minioUrl, err := url.Parse(initUrl) if err != nil { return nil, err @@ -62,11 +63,9 @@ func minioInit() (*minio.Client, error) { } else if minioUrl.Scheme == "https" { opts.Secure = true } - var minioClient *minio.Client minioClient, err = minio.New(minioUrl.Host, opts) if err != nil { return nil, err } - return minioClient, nil } diff --git a/pkg/rpcclient/user.go b/pkg/rpcclient/user.go index dfd93fb0b..c40d95727 100644 --- a/pkg/rpcclient/user.go +++ b/pkg/rpcclient/user.go @@ -45,7 +45,6 @@ func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User { panic(err) } client := user.NewUserClient(conn) - return &User{Discov: discov, Client: client, conn: conn} } @@ -55,7 +54,6 @@ type UserRpcClient User // NewUserRpcClientByUser initializes a UserRpcClient based on the provided User instance. func NewUserRpcClientByUser(user *User) *UserRpcClient { rpc := UserRpcClient(*user) - return &rpc } @@ -77,7 +75,6 @@ func (u *UserRpcClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]* })); len(ids) > 0 { return nil, errs.ErrUserIDNotFound.Wrap(strings.Join(ids, ",")) } - return resp.UsersInfo, nil } @@ -87,7 +84,6 @@ func (u *UserRpcClient) GetUserInfo(ctx context.Context, userID string) (*sdkws. if err != nil { return nil, err } - return users[0], nil } @@ -97,7 +93,6 @@ func (u *UserRpcClient) GetUsersInfoMap(ctx context.Context, userIDs []string) ( if err != nil { return nil, err } - return utils.SliceToMap(users, func(e *sdkws.UserInfo) string { return e.UserID }), nil @@ -113,7 +108,6 @@ func (u *UserRpcClient) GetPublicUserInfos( if err != nil { return nil, err } - return utils.Slice(users, func(e *sdkws.UserInfo) *sdkws.PublicUserInfo { return &sdkws.PublicUserInfo{ UserID: e.UserID, @@ -130,7 +124,6 @@ func (u *UserRpcClient) GetPublicUserInfo(ctx context.Context, userID string) (* if err != nil { return nil, err } - return users[0], nil } @@ -144,7 +137,6 @@ func (u *UserRpcClient) GetPublicUserInfoMap( if err != nil { return nil, err } - return utils.SliceToMap(users, func(e *sdkws.PublicUserInfo) string { return e.UserID }), nil @@ -158,7 +150,6 @@ func (u *UserRpcClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID stri if err != nil { return 0, err } - return resp.GlobalRecvMsgOpt, nil } @@ -168,7 +159,6 @@ func (u *UserRpcClient) Access(ctx context.Context, ownerUserID string) error { if err != nil { return err } - return authverify.CheckAccessV3(ctx, ownerUserID) } @@ -178,7 +168,6 @@ func (u *UserRpcClient) GetAllUserIDs(ctx context.Context, pageNumber, showNumbe if err != nil { return nil, err } - return resp.UserIDs, nil } @@ -188,6 +177,5 @@ func (u *UserRpcClient) SetUserStatus(ctx context.Context, userID string, status UserID: userID, Status: status, PlatformID: int32(platformID), }) - return err } diff --git a/pkg/statistics/statistics.go b/pkg/statistics/statistics.go index 080933c73..de6d04fec 100644 --- a/pkg/statistics/statistics.go +++ b/pkg/statistics/statistics.go @@ -36,8 +36,9 @@ func (s *Statistics) output() { var timeIntervalNum uint64 for { sum = *s.AllCount - <-t.C - + select { + case <-t.C: + } if *s.AllCount-sum <= 0 { intervalCount = 0 } else { @@ -65,6 +66,5 @@ func (s *Statistics) output() { func NewStatistics(allCount *uint64, moduleName, printArgs string, sleepTime int) *Statistics { p := &Statistics{AllCount: allCount, ModuleName: moduleName, SleepTime: uint64(sleepTime), PrintArgs: printArgs} go p.output() - return p } diff --git a/tools/url2im/pkg/buffer.go b/tools/url2im/pkg/buffer.go index 008400926..8ccc5c52f 100644 --- a/tools/url2im/pkg/buffer.go +++ b/tools/url2im/pkg/buffer.go @@ -15,7 +15,7 @@ func NewReader(r io.Reader, max int64, path string) (ReadSeekSizeCloser, error) buf := make([]byte, max+1) n, err := io.ReadFull(r, buf) if err == nil { - f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666) + f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o666) if err != nil { return nil, err } diff --git a/tools/yamlfmt/yamlfmt.go b/tools/yamlfmt/yamlfmt.go index 38004bfde..a8d3a76f6 100644 --- a/tools/yamlfmt/yamlfmt.go +++ b/tools/yamlfmt/yamlfmt.go @@ -39,7 +39,7 @@ func main() { fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) continue } - writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666) if err != nil { fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) continue From 38ab3e0ed791a9d981494ce6b5726f616b0043f4 Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:22:43 +0800 Subject: [PATCH 21/21] fix: zk add close to avoid zk block. (#1284) * fix: zk add close to avoid zk block. * fix: zk add close to avoid zk block. * fix: zk add close to avoid zk block. * fix: zk add close to avoid zk block. --- go.mod | 2 +- internal/rpc/msg/sync_msg.go | 11 +++++++++++ .../k8s_discovery_register.go | 3 +++ pkg/common/startrpc/start.go | 17 ++++++----------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 4bd7999b7..4d4ed879b 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require github.com/google/uuid v1.3.1 require ( github.com/IBM/sarama v1.41.3 github.com/OpenIMSDK/protocol v0.0.30 - github.com/OpenIMSDK/tools v0.0.14 + github.com/OpenIMSDK/tools v0.0.15 github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible github.com/go-redis/redis v6.15.9+incompatible github.com/go-sql-driver/mysql v1.7.1 diff --git a/internal/rpc/msg/sync_msg.go b/internal/rpc/msg/sync_msg.go index aaafb45bb..7c67ff05f 100644 --- a/internal/rpc/msg/sync_msg.go +++ b/internal/rpc/msg/sync_msg.go @@ -62,6 +62,11 @@ func (m *msgServer) PullMessageBySeqs( case sdkws.PullOrder_PullOrderDesc: isEnd = seq.Begin <= minSeq } + if len(msgs) == 0 { + log.ZWarn(ctx, "not have msgs", nil, "conversationID", seq.ConversationID, "seq", seq) + + continue + } resp.Msgs[seq.ConversationID] = &sdkws.PullMsgs{Msgs: msgs, IsEnd: isEnd} } else { var seqs []int64 @@ -71,6 +76,7 @@ func (m *msgServer) PullMessageBySeqs( minSeq, maxSeq, notificationMsgs, err := m.MsgDatabase.GetMsgBySeqs(ctx, req.UserID, seq.ConversationID, seqs) if err != nil { log.ZWarn(ctx, "GetMsgBySeqs error", err, "conversationID", seq.ConversationID, "seq", seq) + continue } var isEnd bool @@ -80,6 +86,11 @@ func (m *msgServer) PullMessageBySeqs( case sdkws.PullOrder_PullOrderDesc: isEnd = seq.Begin <= minSeq } + if len(notificationMsgs) == 0 { + log.ZWarn(ctx, "not have notificationMsgs", nil, "conversationID", seq.ConversationID, "seq", seq) + + continue + } resp.NotificationMsgs[seq.ConversationID] = &sdkws.PullMsgs{Msgs: notificationMsgs, IsEnd: isEnd} } } diff --git a/pkg/common/discovery_register/k8s_discovery_register.go b/pkg/common/discovery_register/k8s_discovery_register.go index 70f9f39f3..811d35b96 100644 --- a/pkg/common/discovery_register/k8s_discovery_register.go +++ b/pkg/common/discovery_register/k8s_discovery_register.go @@ -86,3 +86,6 @@ func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn { fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!") return nil } +func (cli *K8sDR) Close() { + return +} diff --git a/pkg/common/startrpc/start.go b/pkg/common/startrpc/start.go index 2aeff3cf0..f04ab2508 100644 --- a/pkg/common/startrpc/start.go +++ b/pkg/common/startrpc/start.go @@ -51,17 +51,12 @@ func Start( return err } defer listener.Close() - zkClient, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) - /* - zkClient, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, - zookeeper.WithFreq(time.Hour), zookeeper.WithUserNameAndPassword( - config.Config.Zookeeper.Username, - config.Config.Zookeeper.Password, - ), zookeeper.WithRoundRobin(), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger()))*/if err != nil { + client, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery) + if err != nil { return utils.Wrap1(err) } - // defer zkClient.CloseZK() - zkClient.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) + defer client.Close() + client.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) registerIP, err := network.GetRpcRegisterIP(config.Config.Rpc.RegisterIP) if err != nil { return err @@ -81,11 +76,11 @@ func Start( } srv := grpc.NewServer(options...) defer srv.GracefulStop() - err = rpcFn(zkClient, srv) + err = rpcFn(client, srv) if err != nil { return utils.Wrap1(err) } - err = zkClient.Register( + err = client.Register( rpcRegisterName, registerIP, rpcPort,