bakaid commented on a change in pull request #558: MINIFICPP-542 - Add PutSFTP processor URL: https://github.com/apache/nifi-minifi-cpp/pull/558#discussion_r285566274
########## File path: extensions/sftp/client/SFTPClient.h ########## @@ -0,0 +1,183 @@ +/** + * + * 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 __SFTP_CLIENT_H__ +#define __SFTP_CLIENT_H__ + +#include <curl/curl.h> +#include <libssh2.h> +#include <libssh2_sftp.h> +#include <vector> +#include <iostream> +#include <string> +#include <uuid/uuid.h> +#include <vector> + +#include "utils/HTTPClient.h" +#include "core/logging/Logger.h" +#include "core/logging/LoggerConfiguration.h" +#include "properties/Configure.h" +#include "io/validation.h" +#include "io/BaseStream.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +/** + * Initializes and cleans up curl and libssh2 once. + * Cleanup will only occur at the end of our execution since we are relying on a static variable. + */ +class SFTPClientInitializer { + public: + static SFTPClientInitializer *getInstance() { + static SFTPClientInitializer initializer; + return &initializer; + } + void initialize() { + + } + private: + SFTPClientInitializer() { + libssh2_init(0); + curl_global_init(CURL_GLOBAL_DEFAULT); Review comment: Neither is libssh2_init thread-safe. You're right, I have modeled the SFTPClientInitializer on HTTPClientInitializer from http-curl, and I don't see how it would be more or less thread-safe in itself than that (a block-scope static object provides synchronization). It is even initialized in the same way (lazily, both by the respective client and in the constructor of the Processor that uses that client). However, now I do realize, that globally it became less thread-safe, because if both initializers would be constructed at the same time on different threads, there would be no synchronization between them, and curl_global_init could indeed be called in an unsafe manner. If this could happen, we need to create global initializers for these third-party libraries. Creating a separate CurlInitializer and LibSSH2Initializer won't solve the problem, however, because even calling curl_global_init and libssh2_init at the same time is unsafe, because they both potentially call OpenSSL's initialization, which is not thread-safe in itself. To solve this we need to find a way to synchronize all thirdparty global initializations and deinitializations. What do you think, @phrocker ? ---------------------------------------------------------------- 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