Skip to content

Commit

Permalink
Folly SocketOptionMap with strings - SocketOptionValue class
Browse files Browse the repository at this point in the history
Summary:
# Context
Currently SocketOptionMap can only hold integer values. All users of async sockets (proxygen, wangle, etc) can only set int socket options and when needing to set non-int option (for example TCP_CONGESTION with "bbr" value), have to obtain socket fd and set those options manually.  This diff stack updates SocketOptionMap to be able to hold both int and string values, while attempting to maintain compile time compatibility with code using int values.

# This Diff
In this diff introducing container class, simple wrapper around `std::variant`. The reason for wrapper class instead of just `using` alias, to allow custom functions for printing/conversion. Only ostream/folly::to implemented, I'd like to include fmt/folly format too, but folly/io is referenced on various platforms and don't want to introduce new dependencies with this change. For same reason int to string conversion is implemented via sprint instead of `folly::to<string>`.
In this diff, new class is added, but not yet used.

Reviewed By: dmm-fb

Differential Revision: D49557452

fbshipit-source-id: 54d309a602dddd7309109339bfd1168fb807b283
  • Loading branch information
avasylev authored and facebook-github-bot committed Sep 29, 2023
1 parent 75ba8fe commit f7dc2e6
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ if (BUILD_TESTS OR BUILD_BENCHMARKS)
TEST record_io_test WINDOWS_DISABLED SOURCES RecordIOTest.cpp
TEST ShutdownSocketSetTest HANGING
SOURCES ShutdownSocketSetTest.cpp
TEST SocketOptionValueTest HANGING
SOURCES SocketOptionValueTest.cpp

DIRECTORY io/async/test/
# A number of tests in the async_test binary are unfortunately flaky.
Expand Down
100 changes: 100 additions & 0 deletions folly/io/SocketOptionValue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed 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.
*/

#include <folly/io/SocketOptionValue.h>

#include <ostream>

namespace folly {

int SocketOptionValue::asInt() const {
return std::get<int>(val_);
}

const std::string& SocketOptionValue::asString() const {
return std::get<std::string>(val_);
}

bool SocketOptionValue::hasInt() const {
return std::holds_alternative<int>(val_);
}

bool SocketOptionValue::hasString() const {
return std::holds_alternative<std::string>(val_);
}

std::string SocketOptionValue::toString() const {
if (hasInt()) {
char sval[20];
int written = snprintf(sval, sizeof(sval), "%d", asInt());
if (written > 0 && written < static_cast<int>(sizeof(sval))) {
return std::string(sval, written);
} else {
return std::string();
}
} else {
return asString();
}
}

bool operator==(const SocketOptionValue& lhs, const SocketOptionValue& rhs) {
if (lhs.hasInt() && !rhs.hasInt()) {
return false;
} else if (lhs.hasInt() && rhs.hasInt()) {
return lhs.asInt() == rhs.asInt();
} else if (lhs.hasString() && rhs.hasString()) {
return lhs.asString() == rhs.asString();
} else {
return false;
}
}

bool operator==(const SocketOptionValue& lhs, int rhs) {
if (!lhs.hasInt()) {
return false;
}
return (lhs.asInt() == rhs);
}

bool operator==(const SocketOptionValue& lhs, const std::string& rhs) {
if (!lhs.hasString()) {
return false;
}
return (lhs.asString() == rhs);
}

bool operator!=(const SocketOptionValue& lhs, const SocketOptionValue& rhs) {
return !(lhs == rhs);
}

bool operator!=(const SocketOptionValue& lhs, int rhs) {
return !(lhs == rhs);
}

bool operator!=(const SocketOptionValue& lhs, const std::string& rhs) {
return !(lhs == rhs);
}

void toAppend(const SocketOptionValue& val, std::string* result) {
result->append(val.toString());
}

std::ostream& operator<<(std::ostream& os, const SocketOptionValue& val) {
os << val.toString();
return os;
}

} // namespace folly
68 changes: 68 additions & 0 deletions folly/io/SocketOptionValue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed 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.
*/

#pragma once

#include <string>
#include <variant>

namespace folly {

/**
* Variant container for socket option values: integer or string.
* Implicit ctor/compares with int for backward compatibility.
*/
class SocketOptionValue {
public:
/* implicit */ SocketOptionValue(int val) : val_(val) {}
/* implicit */ SocketOptionValue(const std::string& val) : val_(val) {}
SocketOptionValue() : val_(0) {}

// Return true if container holds int
bool hasInt() const;
// Returns int value, prior to calling must verify container holds integer via
// hasInt()
int asInt() const;

// Return true if container holds string
bool hasString() const;
// Returns string value, prior to calling must verify container holds string
// via hasString()
const std::string& asString() const;

// If holding string value, returns string value is.
// If integer, converts to string.
std::string toString() const;

friend bool operator==(
const SocketOptionValue& lhs, const SocketOptionValue& rhs);
friend bool operator==(const SocketOptionValue& lhs, int rhs);
friend bool operator==(const SocketOptionValue& lhs, const std::string& rhs);

friend bool operator!=(
const SocketOptionValue& lhs, const SocketOptionValue& rhs);
friend bool operator!=(const SocketOptionValue& lhs, int rhs);
friend bool operator!=(const SocketOptionValue& lhs, const std::string& rhs);

private:
std::variant<int, std::string> val_;
};

void toAppend(const SocketOptionValue& val, std::string* result);

std::ostream& operator<<(std::ostream&, const SocketOptionValue&);

} // namespace folly
109 changes: 109 additions & 0 deletions folly/io/test/SocketOptionValueTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed 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.
*/

#include <folly/io/SocketOptionValue.h>

#include <limits>

#include <glog/logging.h>

#include <folly/Conv.h>
#include <folly/portability/GTest.h>

namespace folly {
namespace test {

TEST(SocketOptionValueTest, IntValue) {
SocketOptionValue i1(13);
int i = 13;
SocketOptionValue i2(i);
// from uint32_t, int64_t for backward compatibility
uint32_t u = 15;
SocketOptionValue i3(u);
int64_t b = 15;
SocketOptionValue i4(b);

EXPECT_TRUE(i1.hasInt());
EXPECT_FALSE(i1.hasString());
EXPECT_EQ(i1.asInt(), 13);

EXPECT_TRUE(i1 == i2);
EXPECT_FALSE(i1 == i3);
EXPECT_TRUE(i1 != i3);
EXPECT_TRUE(i3 == i4);

EXPECT_TRUE(i1 == 13);
EXPECT_FALSE(i1 == 15);
EXPECT_FALSE(i1 == "15");
EXPECT_TRUE(i1 != 0);
EXPECT_FALSE(i1 != 13);
}

TEST(ShutdownSocketSetTest, StringValue) {
SocketOptionValue s1("folly");
SocketOptionValue s2("folly");
SocketOptionValue s3("yllof");

EXPECT_TRUE(s1.hasString());
EXPECT_FALSE(s1.hasInt());
EXPECT_EQ(s1.asString(), "folly");

EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 == s3);

EXPECT_TRUE(s1 == "folly");
EXPECT_FALSE(s1 == "yllof");
EXPECT_FALSE(s1 == 15);
}

TEST(ShutdownSocketSetTest, StringVsInt) {
SocketOptionValue i1(13);
SocketOptionValue s1("folly");

EXPECT_FALSE(i1 == s1);
}

TEST(ShutdownSocketSetTest, ToString) {
SocketOptionValue i1(13);
SocketOptionValue iMin(std::numeric_limits<int>::min());
SocketOptionValue iMax(std::numeric_limits<int>::max());
SocketOptionValue s1("folly");

EXPECT_EQ(folly::to<std::string>(i1), "13");
EXPECT_EQ(
folly::to<std::string>(iMin),
folly::to<std::string>(std::numeric_limits<int>::min()));
EXPECT_EQ(
folly::to<std::string>(iMax),
folly::to<std::string>(std::numeric_limits<int>::max()));
EXPECT_EQ(folly::to<std::string>(s1), "folly");

LOG(INFO) << "SocketOptionValue test: " << i1;
}

TEST(ShutdownSocketSetTest, Maps) {
SocketOptionValue i1(13);
SocketOptionValue i2(13);

std::map<std::string, SocketOptionValue> m;
m["key1"] = i1;
m["key2"] = i2;
i1 = i2;
m["key3"] = std::move(i1);
}

} // namespace test
} // namespace folly

0 comments on commit f7dc2e6

Please sign in to comment.