Skip to content

Commit

Permalink
Making GCS Client Singleton (#280)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #280

Making the GCS Client a singleton

Differential Revision: D37853909

fbshipit-source-id: 49293778ec8ef6da9ede5dff5f8561fcda31fbdc
  • Loading branch information
achyutFB authored and facebook-github-bot committed Jul 15, 2022
1 parent b19781b commit 3f468d7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
13 changes: 10 additions & 3 deletions fbpcf/io/cloud_util/CloudFileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

#include "fbpcf/io/cloud_util/CloudFileUtil.h"
#include <aws/s3/S3Client.h>
#include <google/cloud/storage/client.h>
#include <re2/re2.h>

#include "fbpcf/aws/S3Util.h"
#include "fbpcf/exception/PcfException.h"
#include "fbpcf/gcp/GCSUtil.h"
#include "fbpcf/io/cloud_util/CloudFileUtil.h"
#include "fbpcf/io/cloud_util/GCSClient.h"
#include "fbpcf/io/cloud_util/GCSFileReader.h"
#include "fbpcf/io/cloud_util/GCSFileUploader.h"
#include "fbpcf/io/cloud_util/S3Client.h"
Expand Down Expand Up @@ -65,7 +68,9 @@ std::unique_ptr<IFileReader> getCloudFileReader(const std::string& filePath) {
fbpcf::aws::S3ClientOption{.region = ref.region})
.getS3Client());
} else if (fileType == CloudFileType::GCS) {
return std::make_unique<GCSFileReader>(fbpcf::gcp::createGCSClient());
return std::make_unique<GCSFileReader>(
fbpcf::cloudio::GCSClient::getInstance(fbpcf::gcp::GCSClientOption{})
.getGCSClient());
} else {
throw fbpcf::PcfException("Not supported yet.");
}
Expand All @@ -83,7 +88,9 @@ std::unique_ptr<IFileUploader> getCloudFileUploader(
filePath);
} else if (fileType == CloudFileType::GCS) {
return std::make_unique<GCSFileUploader>(
fbpcf::gcp::createGCSClient(), filePath);
fbpcf::cloudio::GCSClient::getInstance(fbpcf::gcp::GCSClientOption{})
.getGCSClient(),
filePath);
} else {
throw fbpcf::PcfException("Not supported yet.");
}
Expand Down
17 changes: 17 additions & 0 deletions fbpcf/io/cloud_util/GCSClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <google/cloud/storage/client.h>

#include "fbpcf/io/cloud_util/GCSClient.h"

namespace fbpcf::cloudio {
GCSClient& GCSClient::getInstance(const fbpcf::gcp::GCSClientOption& option) {
static GCSClient GCSClient(option);
return GCSClient;
}
} // namespace fbpcf::cloudio
34 changes: 34 additions & 0 deletions fbpcf/io/cloud_util/GCSClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <memory>

#include <google/cloud/storage/client.h>
#include "fbpcf/gcp/GCSUtil.h"

namespace fbpcf::cloudio {

class GCSClient {
private:
explicit GCSClient(const fbpcf::gcp::GCSClientOption& option) {
GCSClient_ = fbpcf::gcp::createGCSClient();
}

public:
static GCSClient& getInstance(const fbpcf::gcp::GCSClientOption& option);

std::shared_ptr<google::cloud::storage::Client> getGCSClient() {
return GCSClient_;
}

private:
std::shared_ptr<google::cloud::storage::Client> GCSClient_;
};

} // namespace fbpcf::cloudio

0 comments on commit 3f468d7

Please sign in to comment.