forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to inject RejectedExecutionHandler for different EventLoops and…
… EventExecutors Motiviation: Sometimes it is useful to allow to specify a custom strategy to handle rejected tasks. For example if someone tries to add tasks from outside the eventloop it may make sense to try to backoff and retries and so give the executor time to recover. Modification: Add RejectedEventExecutor interface and implementations and allow to inject it. Result: More flexible handling of executor overload.
- Loading branch information
1 parent
a0d4fb1
commit 731f52f
Showing
10 changed files
with
182 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
common/src/main/java/io/netty/util/concurrent/RejectedExecutionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2016 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.util.concurrent; | ||
|
||
/** | ||
* Similar to {@link java.util.concurrent.RejectedExecutionHandler} but specific to {@link SingleThreadEventExecutor}. | ||
*/ | ||
public interface RejectedExecutionHandler { | ||
|
||
/** | ||
* Called when someone tried to add a task to {@link SingleThreadEventExecutor} but this failed due capacity | ||
* restrictions. | ||
*/ | ||
void rejected(Runnable task, SingleThreadEventExecutor executor); | ||
} |
72 changes: 72 additions & 0 deletions
72
common/src/main/java/io/netty/util/concurrent/RejectedExecutionHandlers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2016 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.util.concurrent; | ||
|
||
import io.netty.util.internal.ObjectUtil; | ||
|
||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
/** | ||
* Expose helper methods which create different {@link RejectedExecutionHandler}s. | ||
*/ | ||
public final class RejectedExecutionHandlers { | ||
private static final RejectedExecutionHandler REJECT = new RejectedExecutionHandler() { | ||
@Override | ||
public void rejected(Runnable task, SingleThreadEventExecutor executor) { | ||
throw new RejectedExecutionException(); | ||
} | ||
}; | ||
|
||
private RejectedExecutionHandlers() { } | ||
|
||
/** | ||
* Returns a {@link RejectedExecutionHandler} that will always just throw a {@link RejectedExecutionException}. | ||
*/ | ||
public static RejectedExecutionHandler reject() { | ||
return REJECT; | ||
} | ||
|
||
/** | ||
* Tries to backoff when the task can not be added due restrictions for an configured amount of time. This | ||
* is only done if the task was added from outside of the event loop which means | ||
* {@link EventExecutor#inEventLoop()} returns {@code false}. | ||
*/ | ||
public static RejectedExecutionHandler backoff(final int retries, long backoffAmount, TimeUnit unit) { | ||
ObjectUtil.checkPositive(retries, "retries"); | ||
final long backOffNanos = unit.toNanos(backoffAmount); | ||
return new RejectedExecutionHandler() { | ||
@Override | ||
public void rejected(Runnable task, SingleThreadEventExecutor executor) { | ||
if (!executor.inEventLoop()) { | ||
for (int i = 0; i < retries; i++) { | ||
// Try to wakup the executor so it will empty its task queue. | ||
executor.wakeup(false); | ||
|
||
LockSupport.parkNanos(backOffNanos); | ||
if (executor.offerTask(task)) { | ||
return; | ||
} | ||
} | ||
} | ||
// Either we tried to add the task from within the EventLoop or we was not able to add it even with | ||
// backoff. | ||
throw new RejectedExecutionException(); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.