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

 ##########
 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:
   @bruth this is the only item I've left out so far. I want to understand a 
bit more the context around Context (ehem). 
   
    * Is that a universally accepted pattern? 
    * Won't make the method a bit more complex? (eg: now I need to think at 
what's that parameter) 
    * Since there are more blocking calls, (eg `Producer.Send()`, 
`Client.CreateProducer()`, etc..) should all of them take a context as arg?

----------------------------------------------------------------
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