com.swirlds.common.test.merkle.util.LaggingAsyncOutputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swirlds-common-testing Show documentation
Show all versions of swirlds-common-testing Show documentation
Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.
/*
* Copyright (C) 2016-2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swirlds.common.test.merkle.util;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.synchronization.config.ReconnectConfig;
import com.swirlds.common.merkle.synchronization.streams.AsyncOutputStream;
import com.swirlds.common.merkle.synchronization.utility.MerkleSynchronizationException;
import com.swirlds.common.threading.pool.StandardWorkGroup;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* This variant of the async output stream introduces extra latency.
*/
public class LaggingAsyncOutputStream extends AsyncOutputStream {
private final BlockingQueue messageTimes;
private final long latencyMilliseconds;
public LaggingAsyncOutputStream(
final SerializableDataOutputStream out,
final StandardWorkGroup workGroup,
final long latencyMilliseconds,
final ReconnectConfig reconnectConfig) {
super(out, workGroup, reconnectConfig);
this.messageTimes = new LinkedBlockingQueue<>();
this.latencyMilliseconds = latencyMilliseconds;
}
/**
* {@inheritDoc}
*/
@Override
public void sendAsync(final T message) throws InterruptedException {
if (!isAlive()) {
throw new MerkleSynchronizationException("Messages can not be sent after close has been called.");
}
messageTimes.put(System.currentTimeMillis());
getOutgoingMessages().put(message);
}
/**
* {@inheritDoc}
*/
@Override
protected void serializeMessage(final T message) throws IOException {
long messageTime = messageTimes.remove();
long now = System.currentTimeMillis();
long waitTime = (messageTime + latencyMilliseconds) - now;
if (waitTime > 0) {
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
message.serialize(getOutputStream());
}
}