|
28 | 28 | import java.util.concurrent.TimeUnit; |
29 | 29 | import java.util.concurrent.atomic.AtomicBoolean; |
30 | 30 | import java.util.concurrent.atomic.AtomicInteger; |
| 31 | +import java.util.concurrent.locks.ReentrantLock; |
| 32 | + |
31 | 33 | import org.slf4j.Logger; |
32 | 34 |
|
33 | 35 | /** |
@@ -74,7 +76,7 @@ public class Connection { |
74 | 76 | /** |
75 | 77 | * 申请锁 |
76 | 78 | */ |
77 | | - private final Object lock = new Object(); |
| 79 | + private final ReentrantLock lock = new ReentrantLock(); |
78 | 80 |
|
79 | 81 | /** |
80 | 82 | * 连接是否已经关闭 |
@@ -108,53 +110,90 @@ public static boolean isAvailableConnection(Connection connection) { |
108 | 110 | */ |
109 | 111 | public boolean acquire(String opKey) { |
110 | 112 | if (lazyDestroy.get()) { |
| 113 | + LOG.warn("connection {}: acquired for op {} need destroy, curRef is {}", connID, opKey, ref.get()); |
111 | 114 | return false; |
112 | 115 | } |
113 | | - synchronized (lock) { |
114 | | - if (lazyDestroy.get()) { |
| 116 | + boolean locked = false; |
| 117 | + try { |
| 118 | + // retry outside. |
| 119 | + locked = lock.tryLock(1, TimeUnit.SECONDS); |
| 120 | + if (locked) { |
| 121 | + if (lazyDestroy.get()) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + int curRef = ref.incrementAndGet(); |
| 125 | + LOG.debug("connection {}: acquired for op {}, curRef is {}", connID, opKey, curRef); |
| 126 | + return true; |
| 127 | + } else { |
| 128 | + LOG.warn("connection {}: acquired for op {} timeout, curRef is {}", connID, opKey, ref.get()); |
115 | 129 | return false; |
116 | 130 | } |
117 | | - int curRef = ref.incrementAndGet(); |
118 | | - LOG.debug("connection {}: acquired for op {}, curRef is {}", connID, opKey, curRef); |
119 | | - return true; |
| 131 | + } catch (Exception e) { |
| 132 | + LOG.warn("connection {}: acquired for op {} occur exception, curRef is {}, msg:{}", connID, opKey, ref.get(), e.getMessage()); |
| 133 | + return false; |
| 134 | + } finally { |
| 135 | + if (locked) { |
| 136 | + lock.unlock(); |
| 137 | + } |
120 | 138 | } |
121 | 139 | } |
122 | 140 |
|
123 | 141 | /** |
124 | 142 | * 关闭连接 |
125 | 143 | */ |
126 | 144 | public void closeConnection() { |
127 | | - synchronized (lock) { |
128 | | - if (ref.get() <= 0 && !closed) { |
129 | | - LOG.info("connection {}: closed", connID); |
130 | | - closed = true; |
131 | | - // Gracefully shutdown the gRPC managed-channel. |
132 | | - if (channel != null && !channel.isShutdown()) { |
133 | | - try { |
134 | | - channel.shutdown(); |
135 | | - if (!channel.awaitTermination(1, TimeUnit.SECONDS)) { |
136 | | - LOG.warn("Timed out gracefully shutting down connection: {}. ", connID); |
137 | | - } |
138 | | - } catch (Exception e) { |
139 | | - LOG.error("Unexpected exception while waiting for channel {} gracefully termination", connID, e); |
| 145 | + while (true) { |
| 146 | + boolean locked = false; |
| 147 | + try { |
| 148 | + locked = lock.tryLock(1, TimeUnit.SECONDS); |
| 149 | + if (locked) { |
| 150 | + doCloseConnection(); |
| 151 | + break; |
| 152 | + } else { |
| 153 | + LOG.warn("connection {}: get lock timeout, retry, curRef is {}", connID, ref.get()); |
| 154 | + } |
| 155 | + } catch (Exception e) { |
| 156 | + LOG.warn("connection {}: get lock occur exception, retry, curRef is {}, msg:{}", connID, ref.get(), e.getMessage()); |
| 157 | + } finally { |
| 158 | + if (locked) { |
| 159 | + lock.unlock(); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private void doCloseConnection() { |
| 166 | + if (ref.get() <= 0 && !closed) { |
| 167 | + LOG.info("[doCloseConnection] connection {}: closed", connID); |
| 168 | + closed = true; |
| 169 | + // Gracefully shutdown the gRPC managed-channel. |
| 170 | + if (channel != null && !channel.isShutdown()) { |
| 171 | + try { |
| 172 | + channel.shutdown(); |
| 173 | + if (!channel.awaitTermination(1, TimeUnit.SECONDS)) { |
| 174 | + LOG.warn("Timed out gracefully shutting down connection: {}. ", connID); |
140 | 175 | } |
| 176 | + } catch (Exception e) { |
| 177 | + LOG.error("Unexpected exception while waiting for channel {} gracefully termination", connID, e); |
141 | 178 | } |
| 179 | + } |
142 | 180 |
|
143 | | - // Forcefully shutdown if still not terminated. |
144 | | - if (channel != null && !channel.isTerminated()) { |
145 | | - try { |
146 | | - channel.shutdownNow(); |
147 | | - if (!channel.awaitTermination(100, TimeUnit.MILLISECONDS)) { |
148 | | - LOG.warn("Timed out forcefully shutting down connection: {}. ", connID); |
149 | | - } |
150 | | - LOG.debug("Success to forcefully shutdown connection: {}. ", connID); |
151 | | - } catch (Exception e) { |
152 | | - LOG.error("Unexpected exception while waiting for channel {} forcefully termination", connID, e); |
| 181 | + // Forcefully shutdown if still not terminated. |
| 182 | + if (channel != null && !channel.isTerminated()) { |
| 183 | + try { |
| 184 | + channel.shutdownNow(); |
| 185 | + if (!channel.awaitTermination(100, TimeUnit.MILLISECONDS)) { |
| 186 | + LOG.warn("Timed out forcefully shutting down connection: {}. ", connID); |
153 | 187 | } |
154 | | - } else { |
155 | | - LOG.debug("Success to gracefully shutdown connection: {}. ", connID); |
| 188 | + LOG.debug("Success to forcefully shutdown connection: {}. ", connID); |
| 189 | + } catch (Exception e) { |
| 190 | + LOG.error("Unexpected exception while waiting for channel {} forcefully termination", connID, e); |
156 | 191 | } |
| 192 | + } else { |
| 193 | + LOG.debug("Success to gracefully shutdown connection: {}. ", connID); |
157 | 194 | } |
| 195 | + } else { |
| 196 | + LOG.info("[doCloseConnection] connection {}: ref is {}, closed is {}, skip close", connID, ref.get(), closed); |
158 | 197 | } |
159 | 198 | } |
160 | 199 |
|
|
0 commit comments