A high-performance real-time data service for meme token trading platform built in Rust, providing K-line (candlestick) chart data and real-time transaction streaming with a modern web interface.
- Real-time K-line Data: Provides candlestick chart data for meme tokens (DOGE, SHIB, PEPE)
- Multiple Time Intervals: Supports 1s, 1m, 5m, 15m, 1h intervals with proper time alignment
- Real-time Transaction Streaming: WebSocket-based live transaction feed
- Interactive Web Interface: Modern HTML5 interface with real-time data visualization
- Mock Data Generation: Built-in configurable data generator for testing and demonstration
- Configuration Management: TOML-based configuration with environment support
Access the interactive web interface at http://localhost:8080/:
- Real-time Transaction Stream: Filter by token (ALL, DOGE, SHIB, PEPE)
- Multi-timeframe K-line Display: Switch between different intervals
- System Status Monitoring: Connection status and subscription management
- Responsive Design: Works on desktop and mobile devices
GET /api/v1/klines- Get historical K-line data with filteringGET /api/v1/klines/latest- Get the latest completed K-lineGET /api/v1/klines/current- Get current open K-lineGET /api/v1/tokens- Get list of available tokensGET /api/v1/stats- Get service statisticsGET /api/v1/health- Health check endpoint
WS /ws- Real-time data streaming endpoint
The WebSocket API supports three types of subscriptions:
-
All Transactions: Receive all transaction updates
{"action":"subscribe","subscription":{"type":"all_transactions"}} -
Token-specific Transactions: Receive transactions for specific tokens
{"action":"subscribe","subscription":{"type":"transactions","tokens":["DOGE","SHIB"]}} -
K-line Updates: Receive real-time K-line updates for specific token/interval
{"action":"subscribe","subscription":{"type":"klines","token":"DOGE","interval":"1m"}}
src/
βββ main.rs # Application entry point with dependency injection
βββ lib.rs # Library exports
βββ config.rs # Configuration management
βββ models/ # Data models
β βββ mod.rs # Module exports
β βββ kline.rs # K-line data structure with time alignment
β βββ transaction.rs # Transaction data structure
β βββ time_interval.rs # Time interval enum with proper parsing
βββ services/ # Business logic
β βββ mod.rs # Module exports
β βββ kline.rs # K-line data management with DashMap
β βββ mock_data.rs # Configurable mock data generation
βββ api/ # API layer
βββ mod.rs # Module exports
βββ rest.rs # REST API endpoints with proper error handling
βββ websocket.rs # WebSocket implementation with session management
config/ # Configuration files
βββ default.toml # Default configuration
βββ development.toml # Development environment
βββ production.toml # Production environment
tests/ # Test suites
βββ api_tests.rs # API endpoint tests
βββ kline_tests.rs # K-line service tests
βββ time_interval_tests.rs # Time alignment tests
benches/ # Performance benchmarks
βββ performance.rs # Benchmark suite
websocket_test.html # Interactive web interface
docker-compose.yml # Docker deployment configuration
Dockerfile # Multi-stage Docker build
- Rust 1.82+
- Cargo
git clone <repository-url>
cd k-line
cargo build --releaseThe service uses a hierarchical TOML configuration system for easy management across different environments.
The service loads configuration in the following order:
- Base Configuration:
config/default.toml- Default settings - Environment Configuration:
config/{environment}.toml- Environment-specific overrides - Environment Variables: Optional runtime overrides (see below)
Base configuration (config/default.toml):
[server]
host = "0.0.0.0"
port = 8080
[tokens]
[[tokens.supported_tokens]]
symbol = "DOGE"
base_price = 0.15
volatility = 5.0
[[tokens.supported_tokens]]
symbol = "SHIB"
base_price = 0.00005
volatility = 8.0
[data_generation]
enabled = true
interval_ms = 100
volatility = 0.02
volume_range = [100.0, 1000.0]
[performance]
worker_threads = 4
websocket_heartbeat_interval = 5
client_timeout = 10
kline_retention_hours = 24
max_websocket_connections = 1000Development configuration (config/development.toml):
[server]
host = "127.0.0.1" # Override for local development
workers = 2
[logging]
level = "debug"
file_output = false
[performance]
worker_threads = 2
max_websocket_connections = 100# Use development configuration (default)
cargo run
# Use production configuration
RUST_ENV=production cargo run
# Use custom environment
RUST_ENV=staging cargo run# Development mode (default)
cargo run
# Production mode
RUST_ENV=production cargo run# Build and run with Docker Compose
docker-compose up --build
# Run in background
docker-compose up -d --build
# View logs
docker-compose logs -f k-line
# Stop the service
docker-compose downThe service will start on http://localhost:8080
- Open your browser and navigate to
http://localhost:8080/websocket_test.html - Click "Connect" to establish WebSocket connection
- Monitor real-time data:
- Transaction stream with token filtering
- K-line data with interval switching
- System status and connection info
curl http://localhost:8080/api/v1/tokens
# Response: {"tokens":["DOGE","SHIB","PEPE"],"count":3}curl "http://localhost:8080/api/v1/klines?token=DOGE&interval=1m&limit=10"
# Response: {"token":"DOGE","interval":"1m","data":[...]}curl "http://localhost:8080/api/v1/klines/current?token=DOGE&interval=1m"
# Response: {"token":"DOGE","interval":"1m","data":{...},"is_open":true}curl http://localhost:8080/api/v1/health
# Response: {"status":"healthy","service":"k-line-data-service","timestamp":"..."}Connect to ws://localhost:8080/ws and send subscription messages:
const ws = new WebSocket('ws://localhost:8080/ws');
ws.onopen = function() {
// Subscribe to all transactions
ws.send(JSON.stringify({
action: 'subscribe',
subscription: { type: 'all_transactions' }
}));
// Subscribe to DOGE 1-minute K-lines
ws.send(JSON.stringify({
action: 'subscribe',
subscription: { type: 'klines', token: 'DOGE', interval: '1m' }
}));
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log('Received:', data);
};{
"token": "DOGE",
"timestamp": "2025-05-28T04:00:00Z",
"interval": "1m",
"open": 0.15,
"high": 0.16,
"low": 0.14,
"close": 0.155,
"volume": 1000.0,
"is_closed": false
}{
"token": "DOGE",
"price": 0.15,
"volume": 100.0,
"timestamp": "2025-05-28T04:00:00Z",
"is_buy": true
}- Mock Data Generator creates random transactions every 100ms (configurable)
- K-line Service processes transactions and updates K-lines for all intervals simultaneously
- Time Alignment ensures K-lines align to natural time boundaries
- WebSocket Manager broadcasts updates to subscribed clients with session management
- REST API provides historical data access with proper error handling
- Storage: Direct
DashMapusage for high-performance concurrent access - Memory Management: In-memory storage with configurable retention policies
- Concurrency: Lock-free data structures for optimal performance
- Time Handling: Precise interval alignment using UTC timestamps
- Error Handling: Comprehensive error propagation and logging
Based on benchmark results:
- Single Transaction Processing: ~11.8 Β΅s
- Concurrent Transaction Processing: ~167 Β΅s
- K-line Query: ~4.2 Β΅s
- High-frequency Updates: ~1.17 ms
- WebSocket Broadcasting: Sub-millisecond latency
Run the comprehensive test suite:
# Run all tests
cargo test
# Run specific test suite
cargo test --test api_tests
cargo test --test kline_tests
cargo test --test time_interval_tests
# Run with output
cargo test -- --nocapture
# Run benchmarks
cargo benchTest Coverage:
- β 7 Unit tests (models, config)
- β 5 API tests (REST endpoints)
- β 9 K-line service tests
- β 6 Time alignment tests
- β Performance benchmarks
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support:
- Create an issue in the repository
- Check the documentation in the
docs/directory - Review the test cases for usage examples