Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How about add MemorySafeLinkedBlockingQueue ? #3303

Closed
loongs-zhang opened this issue Apr 23, 2022 · 0 comments
Closed

[Question] How about add MemorySafeLinkedBlockingQueue ? #3303

loongs-zhang opened this issue Apr 23, 2022 · 0 comments
Labels
type: question Further information is requested

Comments

@loongs-zhang
Copy link
Member

loongs-zhang commented Apr 23, 2022

Question

Since #3245 get merged, I think we can add a MemorySafeLinkedBlockingQueue which use MemoryLimitCalculator#maxAvailable:

/**
 * 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,
                                         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);
    }
}
@loongs-zhang loongs-zhang added the type: question Further information is requested label Apr 23, 2022
loongs-zhang added a commit to loongs-zhang/shenyu that referenced this issue Apr 23, 2022
loongs-zhang added a commit to loongs-zhang/shenyu that referenced this issue Apr 24, 2022
yu199195 pushed a commit that referenced this issue Apr 25, 2022
* [ISSUE #3303] [type: refactor] refactor shared thread pool

* fix comment
loongs-zhang added a commit to loongs-zhang/shenyu that referenced this issue Apr 25, 2022
yu199195 pushed a commit that referenced this issue Apr 25, 2022
* [ISSUE #3303] [type:refactor] add MemorySafeTaskQueue for ShenyuThreadPoolExecutor

* add test and fix bug

* fix code style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant