forked from apache/shenyu
-
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.
[ISSUE apache#3303] [type: refactor] refactor shared thread pool
- Loading branch information
1 parent
dfd5cb3
commit c0b8eb3
Showing
5 changed files
with
178 additions
and
53 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/EagerExecutorService.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,48 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 org.apache.shenyu.common.concurrent; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* EagerExecutorService. | ||
*/ | ||
public interface EagerExecutorService extends ExecutorService { | ||
|
||
/** | ||
* Returns the current number of threads in the pool. | ||
* | ||
* @return the number of threads | ||
*/ | ||
int getPoolSize(); | ||
|
||
/** | ||
* Returns the approximate number of threads that are actively | ||
* executing tasks. | ||
* | ||
* @return the number of threads | ||
*/ | ||
int getActiveCount(); | ||
|
||
/** | ||
* Returns the maximum allowed number of threads. | ||
* | ||
* @return the maximum allowed number of threads | ||
*/ | ||
int getMaximumPoolSize(); | ||
} |
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
88 changes: 88 additions & 0 deletions
88
shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/TaskQueue.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,88 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 org.apache.shenyu.common.concurrent; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* TaskQueue. | ||
*/ | ||
public interface TaskQueue<E> extends BlockingQueue<E> { | ||
|
||
/** | ||
* get executor. | ||
* | ||
* @return the executor | ||
*/ | ||
EagerExecutorService getExecutor(); | ||
|
||
/** | ||
* set the executor. | ||
* | ||
* @param executor executor | ||
*/ | ||
void setExecutor(EagerExecutorService executor); | ||
|
||
@Override | ||
default boolean offer(final E e) { | ||
if (getExecutor() == null) { | ||
throw new RejectedExecutionException("The task queue does not have executor!"); | ||
} | ||
|
||
int currentPoolThreadSize = getExecutor().getPoolSize(); | ||
// have free worker. put task into queue to let the worker deal with task. | ||
if (getExecutor().getActiveCount() < currentPoolThreadSize) { | ||
return doOffer(e); | ||
} | ||
|
||
// return false to let executor create new worker. | ||
if (currentPoolThreadSize < getExecutor().getMaximumPoolSize()) { | ||
return false; | ||
} | ||
|
||
// currentPoolThreadSize >= max | ||
return doOffer(e); | ||
} | ||
|
||
/** | ||
* offer element to the queue. | ||
* | ||
* @param e the element to add | ||
* @return {@code true} if the element was added to this queue, else {@code false} | ||
*/ | ||
boolean doOffer(E e); | ||
|
||
/** | ||
* retry offer task. | ||
* | ||
* @param o task | ||
* @param timeout timeout | ||
* @param unit timeout unit | ||
* @return offer success or not | ||
* @throws java.util.concurrent.RejectedExecutionException if executor is terminated. | ||
* @throws java.lang.InterruptedException if the current thread is interrupted. | ||
*/ | ||
default boolean retryOffer(final E o, final long timeout, final TimeUnit unit) throws InterruptedException { | ||
if (getExecutor().isShutdown()) { | ||
throw new RejectedExecutionException("Executor is shutdown!"); | ||
} | ||
return offer(o, timeout, unit); | ||
} | ||
} |
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