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] add MemorySafeTaskQueue for Sheny…
…uThreadPoolExecutor
- Loading branch information
1 parent
2eef279
commit fb43590
Showing
7 changed files
with
203 additions
and
4 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
89 changes: 89 additions & 0 deletions
89
...mmon/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueue.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,89 @@ | ||
/* | ||
* 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.Collection; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Can completely solve the OOM problem caused by {@link java.util.concurrent.LinkedBlockingQueue}, | ||
* does not depend on {@link java.lang.instrument.Instrumentation} and is easier to use than | ||
* {@link org.apache.shenyu.common.concurrent.MemoryLimitedLinkedBlockingQueue}. | ||
*/ | ||
public class MemorySafeLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> { | ||
|
||
private static final long serialVersionUID = 8032578371749960142L; | ||
|
||
private int maxFreeMemory; | ||
|
||
public MemorySafeLinkedBlockingQueue(final int maxFreeMemory) { | ||
super(Integer.MAX_VALUE); | ||
this.maxFreeMemory = maxFreeMemory; | ||
} | ||
|
||
public MemorySafeLinkedBlockingQueue(final Collection<? extends E> c, | ||
final int maxFreeMemory) { | ||
super(c); | ||
this.maxFreeMemory = maxFreeMemory; | ||
} | ||
|
||
/** | ||
* set the max free memory. | ||
* | ||
* @param maxFreeMemory the max free memory | ||
*/ | ||
public void setMaxFreeMemory(final int maxFreeMemory) { | ||
this.maxFreeMemory = maxFreeMemory; | ||
} | ||
|
||
/** | ||
* get the max free memory. | ||
* | ||
* @return the max free memory limit | ||
*/ | ||
public int getMaxFreeMemory() { | ||
return maxFreeMemory; | ||
} | ||
|
||
/** | ||
* determine if there is any remaining free memory. | ||
* | ||
* @return true if has free memory | ||
*/ | ||
public boolean hasRemainedMemory() { | ||
return MemoryLimitCalculator.maxAvailable() < maxFreeMemory; | ||
} | ||
|
||
@Override | ||
public void put(final E e) throws InterruptedException { | ||
if (hasRemainedMemory()) { | ||
super.put(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException { | ||
return hasRemainedMemory() && super.offer(e, timeout, unit); | ||
} | ||
|
||
@Override | ||
public boolean offer(final E e) { | ||
return hasRemainedMemory() && super.offer(e); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeTaskQueue.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,58 @@ | ||
/* | ||
* 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.Collection; | ||
|
||
/** | ||
* MemorySafeTaskQueue in the {@link org.apache.shenyu.common.concurrent.ShenyuThreadPoolExecutor}. | ||
* It offer a task if the executor's submittedTaskCount less than currentPoolThreadSize | ||
* or the currentPoolThreadSize more than executor's maximumPoolSize. | ||
* That can make the executor create new worker | ||
* when the task num is bigger than corePoolSize but less than maximumPoolSize. | ||
*/ | ||
public class MemorySafeTaskQueue<R extends Runnable> extends MemorySafeLinkedBlockingQueue<Runnable> implements TaskQueue<Runnable> { | ||
|
||
private static final long serialVersionUID = -1998413481091670338L; | ||
|
||
private EagerExecutorService executor; | ||
|
||
public MemorySafeTaskQueue(final int maxFreeMemory) { | ||
super(maxFreeMemory); | ||
} | ||
|
||
public MemorySafeTaskQueue(final Collection<? extends Runnable> c, final int maxFreeMemory) { | ||
super(c, maxFreeMemory); | ||
} | ||
|
||
@Override | ||
public EagerExecutorService getExecutor() { | ||
return this.executor; | ||
} | ||
|
||
@Override | ||
public void setExecutor(final EagerExecutorService executor) { | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public boolean doOffer(final Runnable runnable) { | ||
return super.offer(runnable); | ||
} | ||
|
||
} |
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