From 85cab0b4b3c95904cb5843e6cae6735b1bf01067 Mon Sep 17 00:00:00 2001 From: cncsmonster Date: Mon, 23 Oct 2023 00:13:45 +0800 Subject: [PATCH] fix: fix rest lint errors in pkg --- .../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 +++ 10 files changed, 111 insertions(+), 17 deletions(-) mode change 100644 => 100755 pkg/common/tls/tls.go 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 }