hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject httpcomponents-client git commit: BasicCookieStore no longer synchronizes on reads
Date Tue, 08 Aug 2017 21:09:02 GMT
Repository: httpcomponents-client
Updated Branches:
  refs/heads/4.6.x 6913bd31b -> 644b23beb


BasicCookieStore no longer synchronizes on reads

BasicCookieStore uses a ReentrantReadWriteLock to avoid
synchronization on getCookies/toString while maintaining
thread safety.


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/644b23be
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/644b23be
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/644b23be

Branch: refs/heads/4.6.x
Commit: 644b23beba1ce8c37383d46de5c67bf1d4118870
Parents: 6913bd3
Author: Carter Kozak <c4kofony@gmail.com>
Authored: Mon Aug 7 09:47:04 2017 -0400
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Tue Aug 8 14:45:45 2017 +0200

----------------------------------------------------------------------
 .../http/impl/client/BasicCookieStore.java      | 73 ++++++++++++++------
 1 file changed, 51 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/644b23be/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java
----------------------------------------------------------------------
diff --git a/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java b/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java
index d2468f6..c536c6e 100644
--- a/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java
+++ b/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java
@@ -32,6 +32,8 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.TreeSet;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.http.annotation.Contract;
 import org.apache.http.annotation.ThreadingBehavior;
@@ -51,10 +53,12 @@ public class BasicCookieStore implements CookieStore, Serializable {
     private static final long serialVersionUID = -7581093305228232025L;
 
     private final TreeSet<Cookie> cookies;
+    private final ReadWriteLock lock;
 
     public BasicCookieStore() {
         super();
         this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
+        this.lock = new ReentrantReadWriteLock();
     }
 
     /**
@@ -68,12 +72,17 @@ public class BasicCookieStore implements CookieStore, Serializable {
      *
      */
     @Override
-    public synchronized void addCookie(final Cookie cookie) {
+    public void addCookie(final Cookie cookie) {
         if (cookie != null) {
-            // first remove any old cookie that is equivalent
-            cookies.remove(cookie);
-            if (!cookie.isExpired(new Date())) {
-                cookies.add(cookie);
+            lock.writeLock().lock();
+            try {
+                // first remove any old cookie that is equivalent
+                cookies.remove(cookie);
+                if (!cookie.isExpired(new Date())) {
+                    cookies.add(cookie);
+                }
+            } finally {
+                lock.writeLock().unlock();
             }
         }
     }
@@ -88,10 +97,10 @@ public class BasicCookieStore implements CookieStore, Serializable {
      * @see #addCookie(Cookie)
      *
      */
-    public synchronized void addCookies(final Cookie[] cookies) {
+    public void addCookies(final Cookie[] cookies) {
         if (cookies != null) {
-            for (final Cookie cooky : cookies) {
-                this.addCookie(cooky);
+            for (final Cookie cookie : cookies) {
+                this.addCookie(cookie);
             }
         }
     }
@@ -103,9 +112,14 @@ public class BasicCookieStore implements CookieStore, Serializable {
      * @return an array of {@link Cookie cookies}.
      */
     @Override
-    public synchronized List<Cookie> getCookies() {
-        //create defensive copy so it won't be concurrently modified
-        return new ArrayList<Cookie>(cookies);
+    public List<Cookie> getCookies() {
+        lock.readLock().lock();
+        try {
+            //create defensive copy so it won't be concurrently modified
+            return new ArrayList<Cookie>(cookies);
+        } finally {
+            lock.readLock().unlock();
+        }
     }
 
     /**
@@ -117,31 +131,46 @@ public class BasicCookieStore implements CookieStore, Serializable {
      * @see Cookie#isExpired(Date)
      */
     @Override
-    public synchronized boolean clearExpired(final Date date) {
+    public boolean clearExpired(final Date date) {
         if (date == null) {
             return false;
         }
-        boolean removed = false;
-        for (final Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
-            if (it.next().isExpired(date)) {
-                it.remove();
-                removed = true;
+        lock.writeLock().lock();
+        try {
+            boolean removed = false;
+            for (final Iterator<Cookie> it = cookies.iterator(); it.hasNext(); ) {
+                if (it.next().isExpired(date)) {
+                    it.remove();
+                    removed = true;
+                }
             }
+            return removed;
+        } finally {
+            lock.writeLock().unlock();
         }
-        return removed;
     }
 
     /**
      * Clears all cookies.
      */
     @Override
-    public synchronized void clear() {
-        cookies.clear();
+    public void clear() {
+        lock.writeLock().lock();
+        try {
+            cookies.clear();
+        } finally {
+            lock.writeLock().unlock();
+        }
     }
 
     @Override
-    public synchronized String toString() {
-        return cookies.toString();
+    public String toString() {
+        lock.readLock().lock();
+        try {
+            return cookies.toString();
+        } finally {
+            lock.readLock().unlock();
+        }
     }
 
 }


Mime
View raw message