| Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2020, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| Luke Huang | a893556 | 2020-04-30 11:26:41 +0800 | [diff] [blame] | 19 | #include <climits> |
| Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame] | 20 | #include <mutex> |
| 21 | #include <string> |
| 22 | #include <string_view> |
| 23 | #include <unordered_map> |
| 24 | |
| 25 | #include <android-base/thread_annotations.h> |
| 26 | #include <netdutils/DumpWriter.h> |
| 27 | |
| 28 | namespace android::net { |
| 29 | |
| 30 | // TODO: Add some way to update the stored experiment flags periodically. |
| 31 | // TODO: Refactor this class and make things easier. (e.g. remove string map.) |
| 32 | class Experiments { |
| 33 | public: |
| 34 | using GetExperimentFlagIntFunction = std::function<int(const std::string&, int)>; |
| 35 | static Experiments* getInstance(); |
| 36 | int getFlag(std::string_view key, int defaultValue) const EXCLUDES(mMutex); |
| 37 | void update(); |
| 38 | void dump(netdutils::DumpWriter& dw) const EXCLUDES(mMutex); |
| 39 | |
| 40 | Experiments(Experiments const&) = delete; |
| 41 | void operator=(Experiments const&) = delete; |
| 42 | |
| 43 | private: |
| 44 | explicit Experiments(GetExperimentFlagIntFunction getExperimentFlagIntFunction); |
| 45 | Experiments() = delete; |
| 46 | void updateInternal() EXCLUDES(mMutex); |
| 47 | mutable std::mutex mMutex; |
| 48 | std::unordered_map<std::string_view, int> mFlagsMapInt GUARDED_BY(mMutex); |
| 49 | // TODO: Migrate other experiment flags to here. |
| Mike Yu | 1919271 | 2020-08-28 11:56:31 +0800 | [diff] [blame] | 50 | // (retry_count, retransmission_time_interval) |
| Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame] | 51 | static constexpr const char* const kExperimentFlagKeyList[] = { |
| Mike Yu | bb22c7c | 2021-06-16 11:48:13 +0800 | [diff] [blame] | 52 | "keep_listening_udp", |
| 53 | "parallel_lookup_release", |
| 54 | "parallel_lookup_sleep_time", |
| 55 | "sort_nameservers", |
| 56 | "dot_async_handshake", |
| 57 | "dot_connect_timeout_ms", |
| 58 | "dot_maxtries", |
| 59 | "dot_revalidation_threshold", |
| 60 | "dot_xport_unusable_threshold", |
| Mike Yu | 08b2f2b | 2020-12-16 11:45:36 +0800 | [diff] [blame] | 61 | "dot_query_timeout_ms", |
| Mike Yu | bb22c7c | 2021-06-16 11:48:13 +0800 | [diff] [blame] | 62 | "dot_validation_latency_factor", |
| 63 | "dot_validation_latency_offset_ms", |
| Luke Huang | 3682c07 | 2021-06-24 02:43:54 +0800 | [diff] [blame] | 64 | "doh", |
| chenbruce | 03e5920 | 2021-07-09 20:14:28 +0800 | [diff] [blame] | 65 | "mdns_resolution", |
| Mike Yu | 5daa40d | 2021-06-10 21:34:32 +0800 | [diff] [blame] | 66 | }; |
| Luke Huang | a893556 | 2020-04-30 11:26:41 +0800 | [diff] [blame] | 67 | // This value is used in updateInternal as the default value if any flags can't be found. |
| 68 | static constexpr int kFlagIntDefault = INT_MIN; |
| Luke Huang | f40df9c | 2020-04-21 08:51:48 +0800 | [diff] [blame] | 69 | // For testing. |
| 70 | friend class ExperimentsTest; |
| 71 | const GetExperimentFlagIntFunction mGetExperimentFlagIntFunction; |
| 72 | }; |
| 73 | |
| 74 | } // namespace android::net |