|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.schedulers; |
| 17 | + |
| 18 | +import rx.Scheduler; |
| 19 | +import rx.Subscription; |
| 20 | +import rx.functions.Action0; |
| 21 | +import rx.subscriptions.CompositeSubscription; |
| 22 | +import rx.subscriptions.Subscriptions; |
| 23 | + |
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.concurrent.*; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | + |
| 29 | +/* package */class CachedThreadScheduler extends Scheduler { |
| 30 | + private static final class CachedWorkerPool { |
| 31 | + final ThreadFactory factory = new ThreadFactory() { |
| 32 | + final AtomicInteger counter = new AtomicInteger(); |
| 33 | + |
| 34 | + @Override |
| 35 | + public Thread newThread(Runnable r) { |
| 36 | + Thread t = new Thread(r, "RxCachedThreadScheduler-" + counter.incrementAndGet()); |
| 37 | + t.setDaemon(true); |
| 38 | + return t; |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + private final long keepAliveTime; |
| 43 | + private final ConcurrentLinkedQueue<PoolWorker> expiringQueue; |
| 44 | + private final ScheduledExecutorService evictExpiredWorkerExecutor; |
| 45 | + |
| 46 | + CachedWorkerPool(long keepAliveTime, TimeUnit unit) { |
| 47 | + this.keepAliveTime = unit.toNanos(keepAliveTime); |
| 48 | + this.expiringQueue = new ConcurrentLinkedQueue<PoolWorker>(); |
| 49 | + |
| 50 | + evictExpiredWorkerExecutor = Executors.newScheduledThreadPool(1, new ThreadFactory() { |
| 51 | + final AtomicInteger counter = new AtomicInteger(); |
| 52 | + |
| 53 | + @Override |
| 54 | + public Thread newThread(Runnable r) { |
| 55 | + Thread t = new Thread(r, "RxCachedWorkerPoolEvictor-" + counter.incrementAndGet()); |
| 56 | + t.setDaemon(true); |
| 57 | + return t; |
| 58 | + } |
| 59 | + }); |
| 60 | + evictExpiredWorkerExecutor.scheduleWithFixedDelay( |
| 61 | + new Runnable() { |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + evictExpiredWorkers(); |
| 65 | + } |
| 66 | + }, this.keepAliveTime, this.keepAliveTime, TimeUnit.NANOSECONDS |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + private static CachedWorkerPool INSTANCE = new CachedWorkerPool( |
| 71 | + 60L, TimeUnit.SECONDS |
| 72 | + ); |
| 73 | + |
| 74 | + PoolWorker get() { |
| 75 | + while (!expiringQueue.isEmpty()) { |
| 76 | + PoolWorker poolWorker = expiringQueue.poll(); |
| 77 | + if (poolWorker != null) { |
| 78 | + return poolWorker; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // No cached worker found, so create a new one. |
| 83 | + return new PoolWorker(factory); |
| 84 | + } |
| 85 | + |
| 86 | + void release(PoolWorker poolWorker) { |
| 87 | + // Refresh expire time before putting worker back in pool |
| 88 | + poolWorker.setExpirationTime(now() + keepAliveTime); |
| 89 | + |
| 90 | + expiringQueue.add(poolWorker); |
| 91 | + } |
| 92 | + |
| 93 | + void evictExpiredWorkers() { |
| 94 | + if (!expiringQueue.isEmpty()) { |
| 95 | + long currentTimestamp = now(); |
| 96 | + |
| 97 | + Iterator<PoolWorker> poolWorkerIterator = expiringQueue.iterator(); |
| 98 | + while (poolWorkerIterator.hasNext()) { |
| 99 | + PoolWorker poolWorker = poolWorkerIterator.next(); |
| 100 | + if (poolWorker.getExpirationTime() <= currentTimestamp) { |
| 101 | + poolWorkerIterator.remove(); |
| 102 | + poolWorker.unsubscribe(); |
| 103 | + } else { |
| 104 | + // Queue is ordered with the worker that will expire first in the beginning, so when we |
| 105 | + // find a non-expired worker we can stop evicting. |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + long now() { |
| 113 | + return System.nanoTime(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Worker createWorker() { |
| 119 | + return new EventLoopWorker(CachedWorkerPool.INSTANCE.get()); |
| 120 | + } |
| 121 | + |
| 122 | + private static class EventLoopWorker extends Scheduler.Worker { |
| 123 | + private final CompositeSubscription innerSubscription = new CompositeSubscription(); |
| 124 | + private final PoolWorker poolWorker; |
| 125 | + private final AtomicBoolean releasePoolWorkerOnce = new AtomicBoolean(false); |
| 126 | + |
| 127 | + EventLoopWorker(PoolWorker poolWorker) { |
| 128 | + this.poolWorker = poolWorker; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void unsubscribe() { |
| 133 | + if (releasePoolWorkerOnce.compareAndSet(false, true)) { |
| 134 | + // unsubscribe should be idempotent, so only do this once |
| 135 | + CachedWorkerPool.INSTANCE.release(poolWorker); |
| 136 | + } |
| 137 | + innerSubscription.unsubscribe(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public boolean isUnsubscribed() { |
| 142 | + return innerSubscription.isUnsubscribed(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Subscription schedule(Action0 action) { |
| 147 | + return schedule(action, 0, null); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) { |
| 152 | + if (innerSubscription.isUnsubscribed()) { |
| 153 | + // don't schedule, we are unsubscribed |
| 154 | + return Subscriptions.empty(); |
| 155 | + } |
| 156 | + |
| 157 | + NewThreadScheduler.NewThreadWorker.ScheduledAction s = poolWorker.scheduleActual(action, delayTime, unit); |
| 158 | + innerSubscription.add(s); |
| 159 | + s.addParent(innerSubscription); |
| 160 | + return s; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static final class PoolWorker extends NewThreadScheduler.NewThreadWorker { |
| 165 | + private long expirationTime; |
| 166 | + |
| 167 | + PoolWorker(ThreadFactory threadFactory) { |
| 168 | + super(threadFactory); |
| 169 | + this.expirationTime = 0L; |
| 170 | + } |
| 171 | + |
| 172 | + public long getExpirationTime() { |
| 173 | + return expirationTime; |
| 174 | + } |
| 175 | + |
| 176 | + public void setExpirationTime(long expirationTime) { |
| 177 | + this.expirationTime = expirationTime; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments