kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5095909642853f9525e940cd938fde82db613d237a29b4ee510fd95575293ac3 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-04-10 00:00 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-04-17 20:19 +02:00 |
| Parents | 0e7ff2256dc1 |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +15 −5
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.lang.ref.WeakReference; | |
| 18 | 19 | import java.net.InetSocketAddress; |
| 19 | 20 | import java.util.HashSet; |
| 20 | 21 | import java.util.Iterator; |
| @@ -26,6 +27,7 @@ | ||
| 26 | 27 | import java.util.concurrent.atomic.AtomicReference; |
| 27 | 28 | |
| 28 | 29 | import com.google.common.collect.Lists; |
| 30 | +import com.google.common.collect.MapMaker; | |
| 29 | 31 | import com.google.common.util.concurrent.*; |
| 30 | 32 | import io.netty.bootstrap.Bootstrap; |
| 31 | 33 | import io.netty.channel.*; |
| @@ -780,7 +782,7 @@ | ||
| 780 | 782 | } |
| 781 | 783 | |
| 782 | 784 | private static final class Flusher implements Runnable { |
| 783 | - final EventLoop eventLoop; | |
| 785 | + final WeakReference<EventLoop> eventLoopRef; | |
| 784 | 786 | final Queue<FlushItem> queued = new ConcurrentLinkedQueue<FlushItem>(); |
| 785 | 787 | final AtomicBoolean running = new AtomicBoolean(false); |
| 786 | 788 | final HashSet<Channel> channels = new HashSet<Channel>(); |
| @@ -789,12 +791,14 @@ | ||
| 789 | 791 | int runsWithNoWork = 0; |
| 790 | 792 | |
| 791 | 793 | private Flusher(EventLoop eventLoop) { |
| 792 | - this.eventLoop = eventLoop; | |
| 794 | + this.eventLoopRef = new WeakReference<EventLoop>(eventLoop); | |
| 793 | 795 | } |
| 794 | 796 | |
| 795 | 797 | void start() { |
| 796 | 798 | if (!running.get() && running.compareAndSet(false, true)) { |
| 797 | - this.eventLoop.execute(this); | |
| 799 | + EventLoop eventLoop = eventLoopRef.get(); | |
| 800 | + if (eventLoop != null) | |
| 801 | + eventLoop.execute(this); | |
| 798 | 802 | } |
| 799 | 803 | } |
| 800 | 804 | |
| @@ -832,11 +836,17 @@ | ||
| 832 | 836 | } |
| 833 | 837 | } |
| 834 | 838 | |
| 835 | - eventLoop.schedule(this, 10000, TimeUnit.NANOSECONDS); | |
| 839 | + EventLoop eventLoop = eventLoopRef.get(); | |
| 840 | + if(eventLoop != null) { | |
| 841 | + eventLoop.schedule(this, 10000, TimeUnit.NANOSECONDS); | |
| 842 | + } | |
| 836 | 843 | } |
| 837 | 844 | } |
| 838 | 845 | |
| 839 | - private static final ConcurrentMap<EventLoop, Flusher> flusherLookup = new ConcurrentHashMap<EventLoop, Flusher>(); | |
| 846 | + private static final ConcurrentMap<EventLoop, Flusher> flusherLookup = new MapMaker() | |
| 847 | + .concurrencyLevel(16) | |
| 848 | + .weakKeys() | |
| 849 | + .makeMap(); | |
| 840 | 850 | |
| 841 | 851 | private static class FlushItem { |
| 842 | 852 | final Channel channel; |