bruth commented on a change in pull request #1764: WIP - Pulsar Go client 
library
URL: https://github.com/apache/incubator-pulsar/pull/1764#discussion_r187783773
 
 

 ##########
 File path: pulsar-client-go/pulsar/c_consumer.go
 ##########
 @@ -0,0 +1,227 @@
+//
+// 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 pulsar
+
+/*
+#cgo CFLAGS: -I../../pulsar-client-cpp/include
+#cgo LDFLAGS: -lpulsar -L../../pulsar-client-cpp/lib
+#include "c_go_pulsar.h"
+*/
+import "C"
+
+import (
+       "github.com/mattn/go-pointer"
+       "runtime"
+       "time"
+       "unsafe"
+)
+
+type consumer struct {
+       ptr            *C.pulsar_consumer_t
+       defaultChannel chan ConsumerMessage
+}
+
+func consumerFinalizer(c *consumer) {
+       if c.ptr != nil {
+               C.pulsar_consumer_free(c.ptr)
+       }
+}
+
+//export pulsarSubscribeCallbackProxy
+func pulsarSubscribeCallbackProxy(res C.pulsar_result, ptr 
*C.pulsar_consumer_t, ctx unsafe.Pointer) {
+       cc := pointer.Restore(ctx).(*subscribeContext)
+
+       C.pulsar_consumer_configuration_free(cc.conf)
+
+       if res != C.pulsar_result_Ok {
+               cc.callback(nil, newError(res, "Failed to subscribe to topic"))
+       } else {
+               cc.consumer.ptr = ptr
+               runtime.SetFinalizer(cc.consumer, consumerFinalizer)
+               cc.callback(cc.consumer, nil)
+       }
+}
+
+type subscribeContext struct {
+       conf     *C.pulsar_consumer_configuration_t
+       consumer *consumer
+       callback func(Consumer, error)
+}
+
+func subscribeAsync(client *client, options ConsumerOptions, callback 
func(Consumer, error)) {
+       if options.Topic == "" {
+               callback(nil, newError(C.pulsar_result_InvalidConfiguration, 
"topic is required"))
+               return
+       }
+
+       if options.SubscriptionName == "" {
+               callback(nil, newError(C.pulsar_result_InvalidConfiguration, 
"subscription name is required"))
+               return
+       }
+
+       conf := C.pulsar_consumer_configuration_create()
+
+       consumer := &consumer{}
+
+       if options.MessageChannel == nil {
+               // If there is no message listener, set a default channel so 
that we can have receive to
+               // use that
+               consumer.defaultChannel = make(chan ConsumerMessage)
+               options.MessageChannel = consumer.defaultChannel
+       }
+
+       C._pulsar_consumer_configuration_set_message_listener(conf, 
pointer.Save(&consumerCallback{
+               consumer: consumer,
+               channel:  options.MessageChannel,
+       }))
+
+       if options.AckTimeout != 0 {
+               timeoutMillis := options.AckTimeout.Nanoseconds() / 
int64(time.Millisecond)
+               C.pulsar_consumer_set_unacked_messages_timeout_ms(conf, 
C.ulonglong(timeoutMillis))
+       }
+
+       if options.Type != Exclusive {
+               C.pulsar_consumer_configuration_set_consumer_type(conf, 
C.pulsar_consumer_type(options.Type))
+       }
+
+       // ReceiverQueueSize==0 means to use the default queue size
+       // -1 means to disable the consumer prefetching
+       if options.ReceiverQueueSize > 0 {
+               C.pulsar_consumer_configuration_set_receiver_queue_size(conf, 
C.int(options.ReceiverQueueSize))
+       } else if options.ReceiverQueueSize < 0 {
+               // In C++ client lib, 0 means disable prefetching
+               C.pulsar_consumer_configuration_set_receiver_queue_size(conf, 
C.int(0))
+       }
+
+       if options.MaxTotalReceiverQueueSizeAcrossPartitions != 0 {
+               
C.pulsar_consumer_set_max_total_receiver_queue_size_across_partitions(conf,
+                       
C.int(options.MaxTotalReceiverQueueSizeAcrossPartitions))
+       }
+
+       if options.Name != "" {
+               name := C.CString(options.Name)
+               defer C.free(unsafe.Pointer(name))
+
+               C.pulsar_consumer_set_consumer_name(conf, name)
+       }
+
+       topic := C.CString(options.Topic)
+       subName := C.CString(options.SubscriptionName)
+       defer C.free(unsafe.Pointer(topic))
+       defer C.free(unsafe.Pointer(subName))
+       C._pulsar_client_subscribe_async(client.ptr, topic, subName,
+               conf, pointer.Save(&subscribeContext{conf: conf, consumer: 
consumer, callback: callback}))
+}
+
+type consumerCallback struct {
+       consumer Consumer
+       channel  chan ConsumerMessage
+}
+
+//export pulsarMessageListenerProxy
+func pulsarMessageListenerProxy(cConsumer *C.pulsar_consumer_t, message 
*C.pulsar_message_t, ctx unsafe.Pointer) {
+       cc := pointer.Restore(ctx).(*consumerCallback)
+       cc.channel <- ConsumerMessage{cc.consumer, newMessageWrapper(message)}
+}
+
+//// Consumer
+
+func (c *consumer) Topic() string {
+       return C.GoString(C.pulsar_consumer_get_topic(c.ptr))
+}
+
+func (c *consumer) Subscription() string {
+       return C.GoString(C.pulsar_consumer_get_subscription_name(c.ptr))
+}
+
+func (c *consumer) Unsubscribe() error {
+       channel := make(chan error)
+       c.UnsubscribeAsync(func(err error) { channel <- err; close(channel) })
+       return <-channel
+}
+
+func (c *consumer) UnsubscribeAsync(callback Callback) {
+       C._pulsar_consumer_unsubscribe_async(c.ptr, pointer.Save(callback))
+}
+
+//export pulsarConsumerUnsubscribeCallbackProxy
+func pulsarConsumerUnsubscribeCallbackProxy(res C.pulsar_result, ctx 
unsafe.Pointer) {
+       callback := pointer.Restore(ctx).(func(err error))
+
+       if res != C.pulsar_result_Ok {
+               callback(newError(res, "Failed to unsubscribe consumer"))
+       } else {
+               callback(nil)
+       }
+}
+
+func (c *consumer) Receive() (Message, error) {
 
 Review comment:
   It is a fairly accepted pattern. A [blog 
post](https://blog.golang.org/context) from 2014 describes the intent. The wiki 
has a [section on 
it](https://github.com/golang/go/wiki/CodeReviewComments#contexts). It is 
primarily to provide control over long-running tasks. Specifically it enables 
setting a deadline (via a timeout + now or an explicit future time) or as a 
cancelable context.
   
   In this case, since channels are being used internally in the library, this 
degree of control would be useful in case a client only wants to wait so long.. 
or more importantly a graceful shutdown can occur on an interrupt/terminate 
signal.
   
   > Won't make the method a bit more complex?
   
   Yes has been the biggest complaint early on, but haven't heard that in a 
while. The past few versions of the standard library have added more methods 
that take context natively (such as for database queries and HTTP requests, 
etc.).
   
   > Since there are more blocking calls, should all of them take a context as 
arg?
   
   Yes.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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