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 19 #include <climits> 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. 50 // (retry_count, retransmission_time_interval, dot_connect_timeout_ms) 51 static constexpr const char* const kExperimentFlagKeyList[] = { 52 "keep_listening_udp", "parallel_lookup", "parallel_lookup_sleep_time", 53 "sort_nameservers"}; 54 // This value is used in updateInternal as the default value if any flags can't be found. 55 static constexpr int kFlagIntDefault = INT_MIN; 56 // For testing. 57 friend class ExperimentsTest; 58 const GetExperimentFlagIntFunction mGetExperimentFlagIntFunction; 59 }; 60 61 } // namespace android::net 62