1+
2+ #include " util/base_op_util.h"
3+ #include " cos_sys_config.h"
4+ #include < thread>
5+ #include " util/simple_dns_cache.h"
6+ #include " util/codec_util.h"
7+
8+ namespace qcloud_cos {
9+ SimpleDnsCache& GetGlobalDnsCacheInstance () {
10+ static SimpleDnsCache dns_cache (CosSysConfig::GetDnsCacheSize (),
11+ CosSysConfig::GetDnsCacheExpireSeconds ());
12+ return dns_cache;
13+ }
14+
15+ bool BaseOpUtil::ShouldChangeBackupDomain (const CosResult &result, const uint32_t &request_num, const bool is_ci_req) const
16+ {
17+ if (is_ci_req) {
18+ // 请求到万象的, 不切域名
19+ return false ;
20+ }
21+ if (!CosSysConfig::GetRetryChangeDomain ()) {
22+ // 没开启开关, 不切域名
23+ return false ;
24+ }
25+ if (!UseDefaultDomain ()) {
26+ // 未使用默认域名, 不切域名
27+ return false ;
28+ }
29+ if (!result.GetXCosRequestId ().empty ()) {
30+ // 响应中有x-cos-request-id, 说明请求到了 COS, 不切域名
31+ return false ;
32+ }
33+ const int statusCode = result.GetHttpStatus ();
34+ if (statusCode == 301 || statusCode == 302 || statusCode == 307 ) {
35+ // 3xx 的响应码只要满足切换条件就切换域名, 不需要等最后一次重试
36+ return true ;
37+ }
38+ if (request_num + 1 < m_config->GetMaxRetryTimes ()) {
39+ // 还没到最大重试次数, 不切域名
40+ return false ;
41+ }
42+ return statusCode >= 500 ;
43+ }
44+
45+ bool BaseOpUtil::UseDefaultDomain () const {
46+ if (m_config && m_config->GetSetIntranetOnce () && m_config->IsUseIntranet () && !m_config->GetIntranetAddr ().empty ()
47+ || CosSysConfig::IsUseIntranet () && !CosSysConfig::GetIntranetAddr ().empty ()
48+ || (!m_config->GetDestDomain ().empty ())
49+ || !CosSysConfig::GetDestDomain ().empty ()
50+ || m_config->GetRegion () == " accelerate" ) {
51+ return false ;
52+ }
53+ return true ;
54+ }
55+
56+ std::string BaseOpUtil::GetRealUrl (const std::string& host, const std::string& path,
57+ bool is_https, bool is_generate_presigned_url) const
58+ {
59+ // 1. host优先级,私有ip > 自定义域名 > DNS cache > 默认域名
60+ std::string dest_uri;
61+ std::string dest_host = host;
62+ std::string dest_path = path;
63+ std::string dest_protocal = " http://" ; // NOCA:HttpHardcoded(ignore)
64+ if (is_https) {
65+ dest_protocal = " https://" ;
66+ }
67+
68+ if (dest_path.empty () || ' /' != dest_path[0 ]) {
69+ dest_path = " /" + dest_path;
70+ }
71+
72+ if (m_config &&
73+ m_config->GetSetIntranetOnce () &&
74+ m_config->IsUseIntranet () &&
75+ !m_config->GetIntranetAddr ().empty () && !is_generate_presigned_url) {
76+ dest_host = m_config->GetIntranetAddr ();
77+ } else if (CosSysConfig::IsUseIntranet () &&
78+ !CosSysConfig::GetIntranetAddr ().empty () && !is_generate_presigned_url) {
79+ dest_host = CosSysConfig::GetIntranetAddr ();
80+ } else if (m_config &&
81+ (!m_config->GetDestDomain ().empty ())) {
82+ dest_host = m_config->GetDestDomain ();
83+ } else if (!CosSysConfig::GetDestDomain ().empty ()) {
84+ dest_host = CosSysConfig::GetDestDomain ();
85+ } else if (CosSysConfig::GetUseDnsCache () && !is_generate_presigned_url) {
86+ dest_host = GetGlobalDnsCacheInstance ().Resolve (host);
87+ }
88+
89+ dest_uri = dest_protocal + dest_host + CodecUtil::EncodeKey (dest_path);
90+ SDK_LOG_DBG (" dest_uri: %s" , dest_uri.c_str ());
91+ return dest_uri;
92+ }
93+
94+ uint64_t BaseOpUtil::GetMaxRetryTimes () const
95+ {
96+ return m_config->GetMaxRetryTimes ();
97+ }
98+
99+ void BaseOpUtil::SleepBeforeRetry (const uint32_t & request_num) const
100+ {
101+ const uint32_t interval_ms = m_config->GetRetryIntervalMs () * (request_num + 1 );
102+ std::this_thread::sleep_for (std::chrono::milliseconds (interval_ms));
103+ }
104+
105+ std::string BaseOpUtil::ChangeHostSuffix (const std::string& host) {
106+ const std::string old_suffix = " .myqcloud.com" ;
107+ const std::string new_suffix = " .tencentcos.cn" ;
108+
109+ const size_t suffix_pos = host.rfind (old_suffix);
110+ if (suffix_pos != std::string::npos) {
111+ std::string new_host = host.substr (0 , suffix_pos) + new_suffix;
112+ return new_host;
113+ }
114+ return host;
115+ }
116+ } // namespace qcloud_cos
0 commit comments