1 /*
2  * Copyright 2016 The Netty Project
3  *
4  * The Netty Project licenses this file to you under the Apache License,
5  * version 2.0 (the "License"); you may not use this file except in compliance
6  * with the License. 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package io.netty.util.concurrent;
17
18 import io.netty.util.internal.ObjectUtil;
19
20 import java.util.concurrent.RejectedExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.locks.LockSupport;
23
24 /**
25  * Expose helper methods which create different {@link RejectedExecutionHandler}s.
26  */

27 public final class RejectedExecutionHandlers {
28     private static final RejectedExecutionHandler REJECT = new RejectedExecutionHandler() {
29         @Override
30         public void rejected(Runnable task, SingleThreadEventExecutor executor) {
31             throw new RejectedExecutionException();
32         }
33     };
34
35     private RejectedExecutionHandlers() { }
36
37     /**
38      * Returns a {@link RejectedExecutionHandler} that will always just throw a {@link RejectedExecutionException}.
39      */

40     public static RejectedExecutionHandler reject() {
41         return REJECT;
42     }
43
44     /**
45      * Tries to backoff when the task can not be added due restrictions for an configured amount of time. This
46      * is only done if the task was added from outside of the event loop which means
47      * {@link EventExecutor#inEventLoop()} returns {@code false}.
48      */

49     public static RejectedExecutionHandler backoff(final int retries, long backoffAmount, TimeUnit unit) {
50         ObjectUtil.checkPositive(retries, "retries");
51         final long backOffNanos = unit.toNanos(backoffAmount);
52         return new RejectedExecutionHandler() {
53             @Override
54             public void rejected(Runnable task, SingleThreadEventExecutor executor) {
55                 if (!executor.inEventLoop()) {
56                     for (int i = 0; i < retries; i++) {
57                         // Try to wake up the executor so it will empty its task queue.
58                         executor.wakeup(false);
59
60                         LockSupport.parkNanos(backOffNanos);
61                         if (executor.offerTask(task)) {
62                             return;
63                         }
64                     }
65                 }
66                 // Either we tried to add the task from within the EventLoop or we was not able to add it even with
67                 // backoff.
68                 throw new RejectedExecutionException();
69             }
70         };
71     }
72 }
73