private void recordTick(Tick tick, RingBufferMath timingAverage) { timingAverage.add(tick.endTime - tick.startTime); requestTimingPenalty = (int)((1450.0f / ((1450.0f - System.Math.Min(timingAverage.mean(), 1440)) / 30.0f)) - 30.0f); lock (tickHistory) { if (tickHistory.Count == NODE_REQUEST_HISTORY) { tickHistory.RemoveFirst(); } tickHistory.AddLast(tick); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private boolean processOneTick(HttpInterface httpInterface, RingBufferMath timingAverage) throws Exception private bool processOneTick(HttpInterface httpInterface, RingBufferMath timingAverage) { TickBuilder tickBuilder = new TickBuilder(DateTimeHelperClass.CurrentUnixTimeMillis()); try { if (!dispatchOneTick(httpInterface, tickBuilder)) { return(false); } } finally { tickBuilder.endTime = DateTimeHelperClass.CurrentUnixTimeMillis(); recordTick(tickBuilder.build(), timingAverage); } long sleepDuration = System.Math.Max((tickBuilder.startTime + 500) - tickBuilder.endTime, 10); System.Threading.Thread.Sleep((int)sleepDuration); return(true); }
public void run() { if (closed || !threadRunning.compareAndSet(false, true)) { log.LogDebug("Not running node processor for {}, thread already active.", nodeAddress); return; } log.LogDebug("Trying to connect to node {}.", nodeAddress); connectionState.set(ConnectionState.PENDING.id()); try { using (HttpInterface httpInterface = httpInterfaceManager.Interface) { RingBufferMath timingAverage = new RingBufferMath(10, @in => Math.Pow(@in, 5.0), @out => Math.Pow(@out, 0.2)); while (processOneTick(httpInterface, timingAverage)) { aliveTickCounter = System.Math.Max(1, aliveTickCounter + 1); lastAliveTime = DateTimeHelperClass.CurrentUnixTimeMillis(); } } } catch (InterruptedException) { log.LogInformation("Node {} processing was stopped.", nodeAddress); System.Threading.Thread.CurrentThread.Interrupt(); } catch (IOException e) { if (aliveTickCounter > 0) { log.LogError("Node {} went offline with exception.", nodeAddress, e); } else { log.LogDebug("Retry, node {} is still offline.", nodeAddress); } } catch (System.Exception e) { log.LogError("Node {} appears offline due to unexpected exception.", nodeAddress, e); ExceptionTools.rethrowErrors(e); } finally { processHealthCheck(true); connectionState.set(ConnectionState.OFFLINE.id()); aliveTickCounter = System.Math.Min(-1, aliveTickCounter - 1); threadRunning.set(false); if (!closed) { long delay = ScheduleDelay; if (aliveTickCounter == -1) { log.LogInformation("Node {} loop ended, retry scheduled in {}.", nodeAddress, delay); } scheduledExecutor.schedule(this, delay, TimeUnit.MILLISECONDS); } else { log.LogInformation("Node {} loop ended, node was removed.", nodeAddress); } } }