/
githubmirror
/
the-algorithm
Обзор
Документация
Войти
/
githubmirror
/
the-algorithm
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/java/com/twitter/search/earlybird/partition/InstrumentedQueue.java
51 строка
1 KB
twitter-team
Twitter Recommendation Algorithm
01 апр 2023, 01:36
01 апр 2023, 01:36
ef4c5eb
Код
Авторство
О чём код?
package com.twitter.search.earlybird.partition; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.atomic.AtomicLong; import com.twitter.search.common.metrics.SearchLongGauge; import com.twitter.search.common.metrics.SearchRateCounter; /** * A queue with metrics on size, enqueue rate and dequeue rate. */ public class InstrumentedQueue<T> { private final SearchRateCounter enqueueRate; private final SearchRateCounter dequeueRate; private final AtomicLong queueSize = new AtomicLong(); private final ConcurrentLinkedDeque<T> queue; public InstrumentedQueue(String statsPrefix) { SearchLongGauge.export(statsPrefix + "_size", queueSize); enqueueRate = SearchRateCounter.export(statsPrefix + "_enqueue"); dequeueRate = SearchRateCounter.export(statsPrefix + "_dequeue"); queue = new ConcurrentLinkedDeque<>(); } /** * Adds a new element to the queue. */ public void add(T tve) { queue.add(tve); enqueueRate.increment(); queueSize.incrementAndGet(); } /** * Returns the first element in the queue. If the queue is empty, {@code null} is returned. */ public T poll() { T tve = queue.poll(); if (tve != null) { dequeueRate.increment(); queueSize.decrementAndGet(); } return tve; } public long getQueueSize() { return queueSize.get(); } }