All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.reactivemessaging.http.runtime.StrictQueueSizeGuard Maven / Gradle / Ivy

Go to download

Connect to HTTP or WebSocket and expose HTTP or WebSocket endpoints for Reactive Messaging

There is a newer version: 2.4.1
Show newest version
package io.quarkus.reactivemessaging.http.runtime;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * A non-blocking utility class to keep the amount of enqueued emissions under a certain number ({@code queueSize})
 */
class StrictQueueSizeGuard {
    private final int queueSize;
    private final AtomicInteger enqueued = new AtomicInteger();

    /**
     * @param queueSize size of the queue
     */
    StrictQueueSizeGuard(int queueSize) {
        this.queueSize = queueSize;
    }

    /**
     * mark an emission as dequeued. In other words, remove it from the count
     */
    void dequeue() {
        enqueued.decrementAndGet();
    }

    /**
     *
     * @return if the message can be emitted or not
     */
    boolean prepareToEmit() {
        while (true) {
            int oldVal = enqueued.get();
            int newVal = oldVal + 1;
            if (newVal <= queueSize) {
                if (enqueued.compareAndSet(oldVal, newVal)) {
                    return true;
                } // else try again
            } else {
                return false; // too many messages to enqueue
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy