blob: dbe21fceeb9972f8acd4a4c33e08336ead9ebfc6 [file] [log] [blame]
Luke Huangf40df9c2020-04-21 08:51:48 +08001/**
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 Huanga8935562020-04-30 11:26:41 +080019#include <climits>
Luke Huangf40df9c2020-04-21 08:51:48 +080020#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
28namespace 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.)
32class 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 Yu19192712020-08-28 11:56:31 +080050 // (retry_count, retransmission_time_interval)
Luke Huangf40df9c2020-04-21 08:51:48 +080051 static constexpr const char* const kExperimentFlagKeyList[] = {
Mike Yubb22c7c2021-06-16 11:48:13 +080052 "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 Yu08b2f2b2020-12-16 11:45:36 +080061 "dot_query_timeout_ms",
Mike Yubb22c7c2021-06-16 11:48:13 +080062 "dot_validation_latency_factor",
63 "dot_validation_latency_offset_ms",
Luke Huang3682c072021-06-24 02:43:54 +080064 "doh",
chenbruce03e59202021-07-09 20:14:28 +080065 "mdns_resolution",
Mike Yu5daa40d2021-06-10 21:34:32 +080066 };
Luke Huanga8935562020-04-30 11:26:41 +080067 // 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 Huangf40df9c2020-04-21 08:51:48 +080069 // For testing.
70 friend class ExperimentsTest;
71 const GetExperimentFlagIntFunction mGetExperimentFlagIntFunction;
72};
73
74} // namespace android::net