Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: net/ssl/ssl_client_session_cache_openssl.cc

Issue 1079943006: Make SSLClientSessionCacheOpenSSL thread-safe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/ssl/ssl_client_session_cache_openssl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/ssl/ssl_client_session_cache_openssl.h" 5 #include "net/ssl/ssl_client_session_cache_openssl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h"
10 #include "base/time/clock.h" 9 #include "base/time/clock.h"
11 #include "base/time/default_clock.h" 10 #include "base/time/default_clock.h"
12 11
13 namespace net { 12 namespace net {
14 13
15 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) 14 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config)
16 : clock_(new base::DefaultClock), 15 : clock_(new base::DefaultClock),
17 config_(config), 16 config_(config),
18 cache_(config.max_entries), 17 cache_(config.max_entries),
19 lookups_since_flush_(0) { 18 lookups_since_flush_(0) {
20 } 19 }
21 20
22 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { 21 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() {
23 // TODO(davidben): The session cache is currently a singleton, so it is
24 // destroyed on a different thread than the one it's created on. When
25 // https://crbug.com/458365 is fixed, this will no longer be an issue.
26 thread_checker_.DetachFromThread();
27
28 Flush(); 22 Flush();
29 } 23 }
30 24
31 size_t SSLClientSessionCacheOpenSSL::size() const { 25 size_t SSLClientSessionCacheOpenSSL::size() const {
32 return cache_.size(); 26 return cache_.size();
33 } 27 }
34 28
35 SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( 29 SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup(
36 const std::string& cache_key) { 30 const std::string& cache_key) {
37 DCHECK(thread_checker_.CalledOnValidThread()); 31 base::AutoLock lock(lock_);
38 32
39 // Expire stale sessions. 33 // Expire stale sessions.
40 lookups_since_flush_++; 34 lookups_since_flush_++;
41 if (lookups_since_flush_ >= config_.expiration_check_count) { 35 if (lookups_since_flush_ >= config_.expiration_check_count) {
42 lookups_since_flush_ = 0; 36 lookups_since_flush_ = 0;
43 FlushExpiredSessions(); 37 FlushExpiredSessions();
44 } 38 }
45 39
46 CacheEntryMap::iterator iter = cache_.Get(cache_key); 40 CacheEntryMap::iterator iter = cache_.Get(cache_key);
47 if (iter == cache_.end()) 41 if (iter == cache_.end())
48 return nullptr; 42 return nullptr;
49 if (IsExpired(iter->second, clock_->Now())) { 43 if (IsExpired(iter->second, clock_->Now())) {
50 cache_.Erase(iter); 44 cache_.Erase(iter);
51 return nullptr; 45 return nullptr;
52 } 46 }
53 return iter->second->session.get(); 47 return iter->second->session.get();
54 } 48 }
55 49
56 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, 50 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key,
57 SSL_SESSION* session) { 51 SSL_SESSION* session) {
58 DCHECK(thread_checker_.CalledOnValidThread()); 52 base::AutoLock lock(lock_);
59 53
60 // Make a new entry. 54 // Make a new entry.
61 CacheEntry* entry = new CacheEntry; 55 CacheEntry* entry = new CacheEntry;
62 entry->session.reset(SSL_SESSION_up_ref(session)); 56 entry->session.reset(SSL_SESSION_up_ref(session));
63 entry->creation_time = clock_->Now(); 57 entry->creation_time = clock_->Now();
64 58
65 // Takes ownership. 59 // Takes ownership.
66 cache_.Put(cache_key, entry); 60 cache_.Put(cache_key, entry);
67 } 61 }
68 62
69 void SSLClientSessionCacheOpenSSL::Flush() { 63 void SSLClientSessionCacheOpenSSL::Flush() {
70 DCHECK(thread_checker_.CalledOnValidThread()); 64 base::AutoLock lock(lock_);
71 65
72 cache_.Clear(); 66 cache_.Clear();
73 } 67 }
74 68
75 void SSLClientSessionCacheOpenSSL::SetClockForTesting( 69 void SSLClientSessionCacheOpenSSL::SetClockForTesting(
76 scoped_ptr<base::Clock> clock) { 70 scoped_ptr<base::Clock> clock) {
77 DCHECK(thread_checker_.CalledOnValidThread());
78
79 clock_ = clock.Pass(); 71 clock_ = clock.Pass();
80 } 72 }
81 73
82 SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() { 74 SSLClientSessionCacheOpenSSL::CacheEntry::CacheEntry() {
83 } 75 }
84 76
85 SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() { 77 SSLClientSessionCacheOpenSSL::CacheEntry::~CacheEntry() {
86 } 78 }
87 79
88 bool SSLClientSessionCacheOpenSSL::IsExpired( 80 bool SSLClientSessionCacheOpenSSL::IsExpired(
89 SSLClientSessionCacheOpenSSL::CacheEntry* entry, 81 SSLClientSessionCacheOpenSSL::CacheEntry* entry,
90 const base::Time& now) { 82 const base::Time& now) {
91 return now < entry->creation_time || 83 return now < entry->creation_time ||
92 entry->creation_time + config_.timeout < now; 84 entry->creation_time + config_.timeout < now;
93 } 85 }
94 86
95 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { 87 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() {
96 base::Time now = clock_->Now(); 88 base::Time now = clock_->Now();
97 CacheEntryMap::iterator iter = cache_.begin(); 89 CacheEntryMap::iterator iter = cache_.begin();
98 while (iter != cache_.end()) { 90 while (iter != cache_.end()) {
99 if (IsExpired(iter->second, now)) { 91 if (IsExpired(iter->second, now)) {
100 iter = cache_.Erase(iter); 92 iter = cache_.Erase(iter);
101 } else { 93 } else {
102 ++iter; 94 ++iter;
103 } 95 }
104 } 96 }
105 } 97 }
106 98
107 } // namespace net 99 } // namespace net
OLDNEW
« no previous file with comments | « net/ssl/ssl_client_session_cache_openssl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698