// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;
import com.google.inject.Inject;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.TimeoutManager;
import com.yahoo.jdisc.Timer;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Simon Thoresen
*/
public class TimeoutManagerImpl {
private static final ContentChannel IGNORED_CONTENT = new IgnoredContent();
private static final Logger log = Logger.getLogger(TimeoutManagerImpl.class.getName());
private final ScheduledQueue schedules[] = new ScheduledQueue[Runtime.getRuntime().availableProcessors()];
private final Thread thread;
private final Timer timer;
private volatile int nextScheduler = 0;
private volatile int queueSize = 0;
private volatile boolean done = false;
@Inject
public TimeoutManagerImpl(ThreadFactory factory, Timer timer) {
this.thread = factory.newThread(new ManagerTask());
this.thread.setName(getClass().getName());
this.timer = timer;
long now = timer.currentTimeMillis();
for (int i = 0; i < schedules.length; ++i) {
schedules[i] = new ScheduledQueue(now);
}
}
public void start() {
thread.start();
}
public void shutdown() {
done = true;
}
public RequestHandler manageHandler(RequestHandler handler) {
return new ManagedRequestHandler(handler);
}
int queueSize() {
return queueSize; // unstable snapshot, only for test purposes
}
Timer timer() {
return timer;
}
void checkTasks(long currentTimeMillis) {
Queue