Search in sources :

Example 1 with RingBufferMath

use of com.sedmelluq.discord.lavaplayer.tools.RingBufferMath in project lavaplayer by sedmelluq.

the class RemoteNodeProcessor method run.

@Override
public void run() {
    if (closed || !threadRunning.compareAndSet(false, true)) {
        log.debug("Not running node processor for {}, thread already active.", nodeAddress);
        return;
    }
    log.debug("Trying to connect to node {}.", nodeAddress);
    connectionState.set(ConnectionState.PENDING.id());
    try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) {
        RingBufferMath timingAverage = new RingBufferMath(10, in -> Math.pow(in, 5.0), out -> Math.pow(out, 0.2));
        while (processOneTick(httpInterface, timingAverage)) {
            aliveTickCounter = Math.max(1, aliveTickCounter + 1);
            lastAliveTime = System.currentTimeMillis();
        }
    } catch (InterruptedException e) {
        log.info("Node {} processing was stopped.", nodeAddress);
        Thread.currentThread().interrupt();
    } catch (IOException e) {
        if (aliveTickCounter > 0) {
            log.error("Node {} went offline with exception.", nodeAddress, e);
        } else {
            log.debug("Retry, node {} is still offline.", nodeAddress);
        }
    } catch (Throwable e) {
        log.error("Node {} appears offline due to unexpected exception.", nodeAddress, e);
        ExceptionTools.rethrowErrors(e);
    } finally {
        processHealthCheck(true);
        connectionState.set(ConnectionState.OFFLINE.id());
        aliveTickCounter = Math.min(-1, aliveTickCounter - 1);
        threadRunning.set(false);
        if (!closed) {
            long delay = getScheduleDelay();
            if (aliveTickCounter == -1) {
                log.info("Node {} loop ended, retry scheduled in {}.", nodeAddress, delay);
            }
            scheduledExecutor.schedule(this, delay, TimeUnit.MILLISECONDS);
        } else {
            log.info("Node {} loop ended, node was removed.", nodeAddress);
        }
    }
}
Also used : HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) RingBufferMath(com.sedmelluq.discord.lavaplayer.tools.RingBufferMath) IOException(java.io.IOException)

Aggregations

RingBufferMath (com.sedmelluq.discord.lavaplayer.tools.RingBufferMath)1 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)1 IOException (java.io.IOException)1