This article details how to leverage HTTP Keep-Alive to enhance network performance when using the Solana transaction acceleration service on 0slot.trade, with practical code examples provided.
HTTP Keep-Alive is a network optimization mechanism that allows multiple HTTP requests and responses to be sent and received over a single TCP connection. Traditional HTTP requests establish a new TCP connection for each request and close it afterward. This approach is inefficient for frequent requests due to the overhead of repeatedly establishing and closing connections. By keeping the connection open, HTTP Keep-Alive avoids redundant connection setup and teardown processes, significantly improving network request efficiency.
Below is the basic workflow of the HTTP Keep-Alive mechanism:
Without Keep-Alive:
With Keep-Alive:
Using HTTP Keep-Alive offers the following advantages:
The maximum duration for a 0slot.trade Keep-Alive is 65 seconds. It is recommended to perform an access approximately every 60 seconds.
To demonstrate how to leverage HTTP Keep-Alive in practice, we provide a Python script showing how the Solana transaction acceleration service on 0slot.trade optimizes network requests using HTTP Keep-Alive.
import time
import base58
import asyncio
import argparse
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.transaction import Transaction
from solders.message import Message
from solders.system_program import TransferParams, transfer
from solana.rpc.async_api import AsyncClient
"""
This access is unrestricted in content—any request and any response will not affect the Keep-Alive.
For SDKs: You can call the RPC method getHealth, which will return a correct "OK."
For manual HTTP access: You can execute a GET request or omit the apk-key. The advantage is that this access does not count toward TPS calculations.
getHealth refers to the official HTTP RPC method provided by Solana.
https://solana.com/zh/docs/rpc/http/gethealth
python solana sdk is
AsyncClient.is_connected()
golang solana sdk is
rpc.GetHealth()
node.js solana sdk is
rpc.getHealth()
java solana sdk is
client.getApi().getHealth()
rust solana sdk is
rpcClient.get_health()
"""
async def send_solana_transaction(api_key, private_key, tip_key, to_public_key, keep):
client_for_blockhash = AsyncClient(f"https://api.mainnet-beta.solana.com")
client_for_send = AsyncClient(f"https://de.0slot.trade?api-key=" + api_key)
latest_blockhash = await client_for_blockhash.get_latest_blockhash()
await client_for_blockhash.close()
sender = Keypair.from_bytes(base58.b58decode(private_key))
receiver = Pubkey.from_string(to_public_key)
tip_receiver = Pubkey.from_string(tip_key)
main_transfer_instruction = transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=receiver, lamports=1))
tip_transfer_instruction = transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=tip_receiver, lamports=1000000))
message = Message.new_with_blockhash([main_transfer_instruction, tip_transfer_instruction], payer=sender.pubkey(), blockhash=latest_blockhash.value.blockhash)
transaction = Transaction.new_unsigned(message)
transaction.sign([sender], latest_blockhash.value.blockhash)
try:
# If Keep Alive is enabled, regularly check the connection status
if keep:
await client_for_send.is_connected()
start_time = time.perf_counter()
result = await client_for_send.send_transaction(transaction)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Transaction sent successfully in {elapsed_time:.4f} seconds")
print("Transaction signature:", result.value)
except Exception as e:
print("Error:", str(e))
finally:
await client_for_send.close()
async def main():
parser = argparse.ArgumentParser(description="Send a Solana transaction with speed test")
parser.add_argument("--api_key", required=True, help="Solana API key for accessing the network.")
parser.add_argument("--private_key", required=True, help="Sender's private key for signing the transaction.")
parser.add_argument("--tip_key", required=True, help="Public key of the tip receiver.")
parser.add_argument("--to_public_key", required=True, help="Public key of the main receiver.")
args = parser.parse_args()
# 分别以 keep=False 和 keep=True 的方式发送交易
await send_solana_transaction(args.api_key, args.private_key, args.tip_key, args.to_public_key, False)
await send_solana_transaction(args.api_key, args.private_key, args.tip_key, args.to_public_key, True)
if __name__ == "__main__":
asyncio.run(main())
Assuming you have installed the required dependencies (e.g., solders and solana), run the code using the following command:
python test_keepalive.py --api_key <your_api_key> --private_key <your_private_key> --tip_key <tip_key> --to_public_key <to_public_key>
The results will display the transaction send times with and without Keep-Alive, demonstrating the performance improvement from the Keep-Alive mechanism.
We hope this article helps! If you have questions or need further clarification, feel free to reach out.
Our support team is available through these channels: