wolfstudy commented on a change in pull request #3854: [issue #3767] support go 
function for pulsar
URL: https://github.com/apache/pulsar/pull/3854#discussion_r273301121
 
 

 ##########
 File path: pulsar-function-go/pf/instance.go
 ##########
 @@ -0,0 +1,272 @@
+//
+// 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 pf
+
+import (
+       "context"
+       "math"
+       "time"
+
+       "github.com/apache/pulsar/pulsar-client-go/pulsar"
+       "github.com/apache/pulsar/pulsar-function-go/log"
+       "github.com/apache/pulsar/pulsar-function-go/pb"
+)
+
+type goInstance struct {
+       function  function
+       context   *FunctionContext
+       producer  pulsar.Producer
+       consumers map[string]pulsar.Consumer
+       client    pulsar.Client
+}
+
+// newGoInstance init goInstance and init function context
+func newGoInstance() *goInstance {
+       goInstance := &goInstance{
+               context:   NewFuncContext(),
+               consumers: make(map[string]pulsar.Consumer),
+       }
+       return goInstance
+}
+
+func (gi *goInstance) startFunction(function function) error {
+       gi.function = function
+       err := gi.setupClient()
+       if err != nil {
+               log.Errorf("setup client failed, error is:%v", err)
+               return err
+       }
+       err = gi.setupProducer()
+       if err != nil {
+               log.Errorf("setup producer failed, error is:%v", err)
+               return err
+       }
+       channel, err := gi.setupConsumer()
+       if err != nil {
+               log.Errorf("setup consumer failed, error is:%v", err)
+               return err
+       }
+
+CLOSE:
+       for {
+               select {
+               case cm := <-channel:
+                       msgInput := cm.Message
+                       atMostOnce := 
gi.context.instanceConf.funcDetails.ProcessingGuarantees == 
pb.ProcessingGuarantees_ATMOST_ONCE
+                       autoAck := gi.context.instanceConf.funcDetails.AutoAck
+                       if autoAck && atMostOnce {
+                               gi.ackInputMessage(msgInput)
+                       }
+                       output, err := gi.handlerMsg(msgInput)
+                       if err != nil {
+                               log.Errorf("handler message error:%v", err)
+                               gi.nackInputMessage(msgInput)
 
 Review comment:
   @srkukarni yes, my mistake, already fix it.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to