You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2022/09/15 13:08:44 UTC

[GitHub] [incubator-eventmesh] github-code-scanning[bot] commented on a diff in pull request #1289: [ISSUE #1288] add go server RocketMQ consumer

github-code-scanning[bot] commented on code in PR #1289:
URL: https://github.com/apache/incubator-eventmesh/pull/1289#discussion_r971965488


##########
eventmesh-server-go/plugin/connector/rocketmq/client/rocketmq_consumer.go:
##########
@@ -0,0 +1,159 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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.
+
+package client
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"github.com/apache/incubator-eventmesh/eventmesh-server-go/plugin/connector/rocketmq/constants"
+	"github.com/apache/rocketmq-client-go/v2"
+	"github.com/apache/rocketmq-client-go/v2/consumer"
+	"github.com/apache/rocketmq-client-go/v2/primitive"
+	"strconv"
+	"strings"
+)
+
+type SubscribeFunc func(context.Context, ...*primitive.MessageExt) (consumer.ConsumeResult, error)
+
+// RocketMQConsumer RocketMQ consumer interface
+type RocketMQConsumer interface {
+	Start() error
+	Shutdown() error
+	Subscribe(topic string, selector consumer.MessageSelector, f SubscribeFunc) error
+	Unsubscribe(topic string) error
+	Suspend()
+	Resume()
+	IsBroadCasting() bool
+}
+
+// RocketMQConsumerWrapper consumer wrapper of RocketMQ client
+type RocketMQConsumerWrapper struct {
+	options      []consumer.Option
+	consumer     rocketmq.PushConsumer
+	messageModel consumer.MessageModel
+}
+
+// NewRocketMQConsumerWrapper get a consumer wrapper of RocketMQ client
+func NewRocketMQConsumerWrapper(properties map[string]string) (RocketMQConsumer, error) {
+	consumerWrapper := &RocketMQConsumerWrapper{messageModel: consumer.Clustering}
+
+	options, err := consumerWrapper.getConsumerOptionsFromProperties(properties)
+	if err != nil {
+		return nil, err
+	}
+
+	rocketMQConsumer, err := rocketmq.NewPushConsumer(options...)
+	if err != nil {
+		return nil, err
+	}
+	consumerWrapper.consumer = rocketMQConsumer
+
+	return consumerWrapper, nil
+}
+
+// Start wrapper start function
+func (r *RocketMQConsumerWrapper) Start() error {
+	return r.consumer.Start()
+}
+
+// Shutdown wrapper shutdown function
+func (r *RocketMQConsumerWrapper) Shutdown() error {
+	return r.consumer.Shutdown()
+}
+
+// Subscribe wrapper subscribe function
+func (r *RocketMQConsumerWrapper) Subscribe(topic string, selector consumer.MessageSelector, f SubscribeFunc) error {
+	return r.consumer.Subscribe(topic, selector, f)
+}
+
+// Unsubscribe wrapper unsubscribe function
+func (r *RocketMQConsumerWrapper) Unsubscribe(topic string) error {
+	return r.consumer.Unsubscribe(topic)
+}
+
+// Suspend wrapper suspend function
+func (r *RocketMQConsumerWrapper) Suspend() {
+	r.consumer.Suspend()
+}
+
+// Resume wrapper resume function
+func (r *RocketMQConsumerWrapper) Resume() {
+	r.consumer.Resume()
+}
+
+// IsBroadCasting check if consumer mode is broadcasting
+func (r *RocketMQConsumerWrapper) IsBroadCasting() bool {
+	return r.messageModel == consumer.BroadCasting
+}
+
+// getConsumerOptionsFromProperties convert properties map to client options
+func (r *RocketMQConsumerWrapper) getConsumerOptionsFromProperties(properties map[string]string) ([]consumer.Option, error) {
+	clientConfig, err := getClientConfigFromProperties(properties)
+	if clientConfig == nil {
+		return nil, err
+	}
+	options := make([]consumer.Option, 0)
+
+	accessPoints := clientConfig.AccessPoints
+	if len(accessPoints) == 0 {
+		return nil, errors.New("fail to parse rocketmq consumer config, invalid access points")
+	}
+
+	// name server address
+	options = append(options, consumer.WithNameServer(strings.Split(accessPoints, ",")))
+
+	// max reconsume times
+	if len(clientConfig.MaxReconsumeTimes) != 0 {
+		maxReconsumeTimes, err := strconv.Atoi(clientConfig.MaxReconsumeTimes)
+		if err == nil {
+			options = append(options, consumer.WithMaxReconsumeTimes(int32(maxReconsumeTimes)))

Review Comment:
   ## Incorrect conversion between integer types
   
   Incorrect conversion of an integer with architecture-dependent bit size from [strconv.Atoi](1) to a lower bit size type int32 without an upper bound check.
   
   [Show more details](https://github.com/apache/incubator-eventmesh/security/code-scanning/6)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org