1818package org .apache .shenyu .common .timer ;
1919
2020import java .util .Objects ;
21+ import org .apache .shenyu .common .concurrent .MemorySafeTaskQueue ;
2122import org .apache .shenyu .common .concurrent .ShenyuThreadFactory ;
23+ import org .apache .shenyu .common .concurrent .ShenyuThreadPoolExecutor ;
24+ import org .apache .shenyu .common .constant .Constants ;
25+ import org .slf4j .Logger ;
26+ import org .slf4j .LoggerFactory ;
2227
2328import java .util .concurrent .DelayQueue ;
2429import java .util .concurrent .ExecutorService ;
25- import java .util .concurrent .LinkedBlockingQueue ;
30+ import java .util .concurrent .RejectedExecutionException ;
2631import java .util .concurrent .ThreadFactory ;
2732import java .util .concurrent .ThreadPoolExecutor ;
2833import java .util .concurrent .TimeUnit ;
@@ -41,6 +46,8 @@ public class HierarchicalWheelTimer implements Timer {
4146 private static final AtomicIntegerFieldUpdater <HierarchicalWheelTimer > WORKER_STATE_UPDATER =
4247 AtomicIntegerFieldUpdater .newUpdater (HierarchicalWheelTimer .class , "workerState" );
4348
49+ private static final Logger LOG = LoggerFactory .getLogger (HierarchicalWheelTimer .class );
50+
4451 private final ExecutorService taskExecutor ;
4552
4653 private final DelayQueue <TimerTaskList > delayQueue = new DelayQueue <>();
@@ -80,9 +87,9 @@ public HierarchicalWheelTimer(final String executorName,
8087 final Long tickMs ,
8188 final Integer wheelSize ,
8289 final Long startMs ) {
83- ThreadFactory threadFactory = ShenyuThreadFactory .create (executorName , false );
84- taskExecutor = new ThreadPoolExecutor (1 , 1 , 0L , TimeUnit .MILLISECONDS ,
85- new LinkedBlockingQueue <>( ), threadFactory );
90+ ThreadFactory threadFactory = ShenyuThreadFactory .create (executorName , true );
91+ taskExecutor = new ShenyuThreadPoolExecutor (1 , 1 , 0L , TimeUnit .MILLISECONDS ,
92+ new MemorySafeTaskQueue < Runnable >( Constants . THE_256_MB ), threadFactory , new ThreadPoolExecutor . AbortPolicy () );
8693 workerThread = threadFactory .newThread (new Worker (this ));
8794 timingWheel = new TimingWheel (tickMs , wheelSize , startMs , taskCounter , delayQueue );
8895 }
@@ -106,7 +113,11 @@ public void add(final TimerTask timerTask) {
106113 private void addTimerTaskEntry (final TimerTaskList .TimerTaskEntry timerTaskEntry ) {
107114 if (!timingWheel .add (timerTaskEntry )) {
108115 if (!timerTaskEntry .cancelled ()) {
109- taskExecutor .submit (() -> timerTaskEntry .getTimerTask ().run (timerTaskEntry ));
116+ try {
117+ taskExecutor .submit (() -> timerTaskEntry .getTimerTask ().run (timerTaskEntry ));
118+ } catch (RejectedExecutionException e ) {
119+ LOG .warn ("Dropping the expired timer task because the timer task queue has no remaining memory" , e );
120+ }
110121 }
111122 }
112123 }
0 commit comments