Skip to content

Commit

Permalink
feat: optimize server code (#1931)
Browse files Browse the repository at this point in the history
* fix: GroupApplicationAcceptedNotification

* fix: GroupApplicationAcceptedNotification

* fix: NotificationUserInfoUpdate

* cicd: robot automated Change

* utils.Wrap -> errs.Wrap

* utils.Wrap -> errs.Wrap

---------

Co-authored-by: withchao <[email protected]>
  • Loading branch information
withchao and withchao authored Feb 20, 2024
1 parent 01886ee commit d5d2803
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 98 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
firebase.google.com/go v3.13.0+incompatible
github.com/OpenIMSDK/protocol v0.0.55
github.com/OpenIMSDK/tools v0.0.35
github.com/OpenIMSDK/tools v0.0.36
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/dtm-labs/rockscache v0.1.1
github.com/gin-gonic/gin v1.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/IBM/sarama v1.42.2 h1:VoY4hVIZ+WQJ8G9KNY/SQlWguBQXQ9uvFPOnrcu8hEw=
github.com/IBM/sarama v1.42.2/go.mod h1:FLPGUGwYqEs62hq2bVG6Io2+5n+pS6s/WOXVKWSLFtE=
github.com/OpenIMSDK/protocol v0.0.55 h1:eBjg8DyuhxGmuCUjpoZjg6MJJJXU/xJ3xJwFhrn34yA=
github.com/OpenIMSDK/protocol v0.0.55/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y=
github.com/OpenIMSDK/tools v0.0.35 h1:YH8UYoaErXqfNrwpUvQxe8nhL++gFH6qCisQPyzk0w8=
github.com/OpenIMSDK/tools v0.0.35/go.mod h1:wBfR5CYmEyvxl03QJbTkhz1CluK6J4/lX0lviu8JAjE=
github.com/OpenIMSDK/tools v0.0.36 h1:BT0q64l4f3QJDW16Rc0uJYt1gQFkiPoUQYQ33vo0EcE=
github.com/OpenIMSDK/tools v0.0.36/go.mod h1:wBfR5CYmEyvxl03QJbTkhz1CluK6J4/lX0lviu8JAjE=
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
Expand Down
15 changes: 8 additions & 7 deletions internal/msggateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"runtime/debug"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -173,7 +174,7 @@ func (c *Client) handleMessage(message []byte) error {
var err error
message, err = c.longConnServer.DecompressWithPool(message)
if err != nil {
return utils.Wrap(err, "")
return errs.Wrap(err)
}
}

Expand All @@ -182,15 +183,15 @@ func (c *Client) handleMessage(message []byte) error {

err := c.longConnServer.Decode(message, binaryReq)
if err != nil {
return utils.Wrap(err, "")
return errs.Wrap(err)
}

if err := c.longConnServer.Validate(binaryReq); err != nil {
return utils.Wrap(err, "")
return errs.Wrap(err)
}

if binaryReq.SendID != c.UserID {
return utils.Wrap(errors.New("exception conn userID not same to req userID"), binaryReq.String())
return errs.Wrap(errors.New("exception conn userID not same to req userID"), binaryReq.String())
}

ctx := mcontext.WithMustInfoCtx(
Expand Down Expand Up @@ -313,7 +314,7 @@ func (c *Client) writeBinaryMsg(resp Resp) error {

encodedBuf, err := c.longConnServer.Encode(resp)
if err != nil {
return utils.Wrap(err, "")
return errs.Wrap(err)
}

c.w.Lock()
Expand All @@ -323,7 +324,7 @@ func (c *Client) writeBinaryMsg(resp Resp) error {
if c.IsCompress {
resultBuf, compressErr := c.longConnServer.CompressWithPool(encodedBuf)
if compressErr != nil {
return utils.Wrap(compressErr, "")
return errs.Wrap(compressErr)
}
return c.conn.WriteMessage(MessageBinary, resultBuf)
}
Expand All @@ -341,7 +342,7 @@ func (c *Client) writePongMsg() error {

err := c.conn.SetWriteDeadline(writeWait)
if err != nil {
return utils.Wrap(err, "")
return errs.Wrap(err)
}

return c.conn.WriteMessage(PongMessage, nil)
Expand Down
21 changes: 10 additions & 11 deletions internal/msggateway/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
"bytes"
"compress/gzip"
"errors"
"github.com/OpenIMSDK/tools/errs"
"io"
"sync"

"github.com/OpenIMSDK/tools/utils"
)

var (
Expand All @@ -47,10 +46,10 @@ func (g *GzipCompressor) Compress(rawData []byte) ([]byte, error) {
gzipBuffer := bytes.Buffer{}
gz := gzip.NewWriter(&gzipBuffer)
if _, err := gz.Write(rawData); err != nil {
return nil, utils.Wrap(err, "")
return nil, errs.Wrap(err)
}
if err := gz.Close(); err != nil {
return nil, utils.Wrap(err, "")
return nil, errs.Wrap(err)
}
return gzipBuffer.Bytes(), nil
}
Expand All @@ -63,10 +62,10 @@ func (g *GzipCompressor) CompressWithPool(rawData []byte) ([]byte, error) {
gz.Reset(&gzipBuffer)

if _, err := gz.Write(rawData); err != nil {
return nil, utils.Wrap(err, "")
return nil, errs.Wrap(err)
}
if err := gz.Close(); err != nil {
return nil, utils.Wrap(err, "")
return nil, errs.Wrap(err)
}
return gzipBuffer.Bytes(), nil
}
Expand All @@ -75,11 +74,11 @@ func (g *GzipCompressor) DeCompress(compressedData []byte) ([]byte, error) {
buff := bytes.NewBuffer(compressedData)
reader, err := gzip.NewReader(buff)
if err != nil {
return nil, utils.Wrap(err, "NewReader failed")
return nil, errs.Wrap(err, "NewReader failed")
}
compressedData, err = io.ReadAll(reader)
if err != nil {
return nil, utils.Wrap(err, "ReadAll failed")
return nil, errs.Wrap(err, "ReadAll failed")
}
_ = reader.Close()
return compressedData, nil
Expand All @@ -88,18 +87,18 @@ func (g *GzipCompressor) DeCompress(compressedData []byte) ([]byte, error) {
func (g *GzipCompressor) DecompressWithPool(compressedData []byte) ([]byte, error) {
reader := gzipReaderPool.Get().(*gzip.Reader)
if reader == nil {
return nil, errors.New("NewReader failed")
return nil, errs.Wrap(errors.New("NewReader failed"))
}
defer gzipReaderPool.Put(reader)

err := reader.Reset(bytes.NewReader(compressedData))
if err != nil {
return nil, utils.Wrap(err, "NewReader failed")
return nil, errs.Wrap(err, "NewReader failed")
}

compressedData, err = io.ReadAll(reader)
if err != nil {
return nil, utils.Wrap(err, "ReadAll failed")
return nil, errs.Wrap(err, "ReadAll failed")
}
_ = reader.Close()
return compressedData, nil
Expand Down
5 changes: 2 additions & 3 deletions internal/msggateway/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package msggateway
import (
"bytes"
"encoding/gob"

"github.com/OpenIMSDK/tools/utils"
"github.com/OpenIMSDK/tools/errs"
)

type Encoder interface {
Expand Down Expand Up @@ -47,7 +46,7 @@ func (g *GobEncoder) Decode(encodeData []byte, decodeData any) error {
dec := gob.NewDecoder(buff)
err := dec.Decode(decodeData)
if err != nil {
return utils.Wrap(err, "")
return errs.Wrap(err)
}
return nil
}
4 changes: 1 addition & 3 deletions internal/push/offlinepush/getui/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import (
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
http2 "github.com/openimsdk/open-im-server/v3/pkg/common/http"

"github.com/OpenIMSDK/tools/utils"
)

var (
Expand Down Expand Up @@ -137,7 +135,7 @@ func (g *Client) GetTaskID(ctx context.Context, token string, pushReq PushReq) (
pushReq.Settings = &Settings{TTL: &ttl}
err := g.request(ctx, taskURL, pushReq, token, &respTask)
if err != nil {
return "", utils.Wrap(err, "")
return "", errs.Wrap(err)
}
return respTask.TaskID, nil
}
Expand Down
6 changes: 2 additions & 4 deletions internal/rpc/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"github.com/OpenIMSDK/tools/log"
"github.com/OpenIMSDK/tools/mcontext"
"github.com/OpenIMSDK/tools/tokenverify"
"github.com/OpenIMSDK/tools/utils"

"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/controller"
Expand Down Expand Up @@ -105,7 +103,7 @@ func (s *authServer) GetUserToken(ctx context.Context, req *pbauth.GetUserTokenR
func (s *authServer) parseToken(ctx context.Context, tokensString string) (claims *tokenverify.Claims, err error) {
claims, err = tokenverify.GetClaimFromToken(tokensString, authverify.Secret())
if err != nil {
return nil, utils.Wrap(err, "")
return nil, errs.Wrap(err)
}
m, err := s.authDatabase.GetTokensWithoutError(ctx, claims.UserID, claims.PlatformID)
if err != nil {
Expand All @@ -121,7 +119,7 @@ func (s *authServer) parseToken(ctx context.Context, tokensString string) (claim
case constant.KickedToken:
return nil, errs.ErrTokenKicked.Wrap()
default:
return nil, utils.Wrap(errs.ErrTokenUnknown, "")
return nil, errs.Wrap(errs.ErrTokenUnknown)
}
}
return nil, errs.ErrTokenNotExist.Wrap()
Expand Down
2 changes: 1 addition & 1 deletion internal/rpc/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ func (s *groupServer) SetGroupInfo(ctx context.Context, req *pbgroup.SetGroupInf
return nil, err
}
if group.Status == constant.GroupStatusDismissed {
return nil, utils.Wrap(errs.ErrDismissedAlready, "")
return nil, errs.Wrap(errs.ErrDismissedAlready)
}
resp := &pbgroup.SetGroupInfoResp{}
count, err := s.db.FindGroupMemberNum(ctx, group.GroupID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/db/cache/meta_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin
}
bs, err := json.Marshal(t)
if err != nil {
return "", utils.Wrap(err, "")
return "", errs.Wrap(err)
}
write = true

Expand All @@ -153,7 +153,7 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin
if err != nil {
log.ZError(ctx, "cache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire)

return t, utils.Wrap(err, "")
return t, errs.Wrap(err)
}

return t, nil
Expand Down
Loading

0 comments on commit d5d2803

Please sign in to comment.