bakaid commented on a change in pull request #621: MINIFICPP-959: Review 
librdkafka thread safety
URL: https://github.com/apache/nifi-minifi-cpp/pull/621#discussion_r314649513
 
 

 ##########
 File path: extensions/librdkafka/KafkaPool.h
 ##########
 @@ -0,0 +1,67 @@
+/**
+ * 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.
+ */
+
+#ifndef NIFI_MINIFI_CPP_KAFKAPOOL_H
+#define NIFI_MINIFI_CPP_KAFKAPOOL_H
+
+#include "KafkaConnection.h"
+
+class KafkaPool {
+ public:
+
+  explicit KafkaPool(int max)
+      : max_(max) {
+  }
+
+  bool removeConnection(const KafkaConnectionKey &key) {
+    std::lock_guard<std::mutex> lock(mutex_);
+    return map_.erase(key) == 1;
+  }
+
+  std::unique_ptr<KafkaLease> getOrCreateConnection(const KafkaConnectionKey 
&key) {
+    std::lock_guard<std::mutex> lock(mutex_);
 
 Review comment:
   This logic has some duplication in it. I think it could be more elegant - 
and less error-prone - if you would unify the connection creation logic and the 
lease acquiring logic.
   So first check whether a connection exists with this key, if not, create 
one, then you can have a common logic that uses `tryUse` on this connection. 
This way you could also avoid having to set the `lease_` to `true` in the 
KafkaConnection's constructor, which is hard to understand at first, I think.

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