forked from sogou/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDnsCache.cc
More file actions
118 lines (93 loc) · 2.77 KB
/
Copy pathDnsCache.cc
File metadata and controls
118 lines (93 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Copyright (c) 2019 Sogou, Inc.
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.
Authors: Wu Jiaxu ([email protected])
Xie Han ([email protected])
*/
#include <stdint.h>
#include <chrono>
#include "DnsCache.h"
#define GET_CURRENT_SECOND std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count()
#define CONFIDENT_INC 10
#define TTL_INC 10
const DnsCache::DnsHandle *DnsCache::get_inner(const HostPort& host_port, int type)
{
int64_t cur_time = GET_CURRENT_SECOND;
std::lock_guard<std::mutex> lock(mutex_);
const DnsHandle *handle = cache_pool_.get(host_port);
if (handle)
{
switch (type)
{
case GET_TYPE_TTL:
if (cur_time > handle->value.expire_time)
{
const_cast<DnsHandle *>(handle)->value.expire_time += TTL_INC;
cache_pool_.release(handle);
return NULL;
}
break;
case GET_TYPE_CONFIDENT:
if (cur_time > handle->value.confident_time)
{
const_cast<DnsHandle *>(handle)->value.confident_time += CONFIDENT_INC;
cache_pool_.release(handle);
return NULL;
}
break;
default:
break;
}
}
return handle;
}
const DnsCache::DnsHandle *DnsCache::put(const HostPort& host_port,
struct addrinfo *addrinfo,
unsigned int dns_ttl_default,
unsigned int dns_ttl_min)
{
int64_t expire_time;
int64_t confident_time;
int64_t cur_time = GET_CURRENT_SECOND;
if (dns_ttl_min > dns_ttl_default)
dns_ttl_min = dns_ttl_default;
if (dns_ttl_min == (unsigned int)-1)
confident_time = INT64_MAX;
else
confident_time = cur_time + dns_ttl_min;
if (dns_ttl_default == (unsigned int)-1)
expire_time = INT64_MAX;
else
expire_time = cur_time + dns_ttl_default;
std::lock_guard<std::mutex> lock(mutex_);
return cache_pool_.put(host_port, {addrinfo, confident_time, expire_time});
}
const DnsCache::DnsHandle *DnsCache::get(const DnsCache::HostPort& host_port)
{
std::lock_guard<std::mutex> lock(mutex_);
return cache_pool_.get(host_port);
}
void DnsCache::release(const DnsCache::DnsHandle *handle)
{
std::lock_guard<std::mutex> lock(mutex_);
cache_pool_.release(handle);
}
void DnsCache::del(const DnsCache::HostPort& key)
{
std::lock_guard<std::mutex> lock(mutex_);
cache_pool_.del(key);
}
DnsCache::DnsCache()
{
}
DnsCache::~DnsCache()
{
}