Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ windows = { version = "0.48", features = ["Win32_Graphics_Dxgi"] }
default = ["metal"]
metal = ["dep:core-graphics", "dep:metal"]
windows = ["dep:dxgi", "winapi"]
dashboard = []

[[bin]]
name = "gpu-share-vm-manager"
Expand All @@ -56,3 +57,8 @@ path = "src/main.rs"
[dev-dependencies]
tokio = { version = "1.0", features = ["full"] }
rand = "0.8"
futures = "0.3"

[[bin]]
name = "dashboard"
path = "src/bin/dashboard.rs"
36 changes: 4 additions & 32 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build stage
FROM rust:1.75-slim-bookworm as builder
FROM rust:1.75 as builder

WORKDIR /usr/src/app
RUN apt-get update && apt-get install -y \
Expand All @@ -22,34 +22,6 @@ COPY . ./
RUN cargo build --release --locked

# Runtime stage
FROM debian:bookworm-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
libvirt0 \
libnvidia-ml1 \
libvirt-clients \
&& rm -rf /var/lib/apt/lists/*

# Create non-root user
RUN useradd -ms /bin/bash appuser
WORKDIR /app
RUN chown appuser:appuser /app
USER appuser

# Copy the built executable
COPY --from=builder /usr/src/app/target/release/gpu-share-vm-manager .
# Copy configuration
COPY config /app/config

# Create necessary directories
RUN mkdir -p /var/lib/gpu-share/images

# Set environment variables
ENV CONFIG_PATH=/app/config
ENV RUST_LOG=info

EXPOSE 3000

ENTRYPOINT ["./gpu-share-vm-manager"]
CMD ["serve"]
FROM gcr.io/distroless/cc-debian12
COPY --from=builder /app/target/release/gpu-share-vm-manager /app/
CMD ["/app/gpu-share-vm-manager"]
7 changes: 5 additions & 2 deletions src/api/middleware/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use headers::{Authorization, HeaderMapExt};
use jsonwebtoken::{decode, DecodingKey, Validation};

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
pub struct Claims {
sub: String,
role: String,
exp: usize,
}

pub async fn auth_middleware<B>(mut req: Request<B>, next: Next<B>) -> Result<Response, StatusCode> {
pub async fn auth_middleware<B>(
mut req: Request<B>,
next: Next<B>,
) -> Result<Response, StatusCode> {
let token = req.headers()
.typed_get::<Authorization<Bearer>>()
.ok_or(StatusCode::UNAUTHORIZED)?;
Expand Down
31 changes: 28 additions & 3 deletions src/api/middleware/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use std::{num::NonZeroU32, sync::Arc, time::Duration};
use tower::limit::RateLimitLayer;
use std::error::Error as StdError;
use std::fmt;
use crate::monitoring::MetricsCollector;

/// Rate limiting configuration for API endpoints
#[derive(Debug, Clone)]
pub struct RateLimitConfig {
pub requests: NonZeroU32,
pub per_seconds: u64,
pub requests: NonZeroU32,
pub per_seconds: u64,
}

impl RateLimitConfig {
Expand Down Expand Up @@ -114,7 +115,14 @@ impl fmt::Display for RateLimitExceeded {
}
#[derive(Clone)]
pub struct RateLimit<T> {
inner: T,
pub inner: T,
pub limiter: Arc<RateLimiter<String, DashMapStore<String>, QuantaClock, NoOpMiddleware>>,
}

impl<T> RateLimit<T> {
pub fn new(inner: T, limiter: Arc<RateLimiter<String, DashMapStore<String>, QuantaClock, NoOpMiddleware>>) -> Self {
Self { inner, limiter }
}
}

// wrapper for RateLimitLayer
Expand Down Expand Up @@ -209,4 +217,21 @@ mod tests {
.unwrap();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
}
}


pub struct AdaptiveRateLimiter {
base_limits: RateLimitConfig,
metrics: Arc<MetricsCollector>,
}

impl AdaptiveRateLimiter {
pub fn adjust_limits_based_on_load(&mut self) {
let current_load = self.metrics.current_load();
if current_load > 80.0 {
self.base_limits.requests = NonZeroU32::new(
self.base_limits.requests.get() * 2
).unwrap();
}
}
}
19 changes: 9 additions & 10 deletions src/bin/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ use crossterm::event::{Event, KeyCode};
use crossterm::{execute, terminal::*};
use ratatui::{prelude::*, widgets::*};
use std::sync::{Arc, Mutex};
use gpu_share_vm_manager::dashboard;
use tokio::runtime::Runtime;
use tokio::sync::Mutex as TokioMutex;

#[tokio::main]
async fn main() -> Result<()> {
let gpupool = Arc::new(Mutex::new(GPUPool::new()));
let user_manager = Arc::new(Mutex::new(UserManager::new()));
let billing_system = Arc::new(Mutex::new(BillingSystem::new()));
fn main() -> anyhow::Result<()> {
let gpupool = Arc::new(TokioMutex::new(GPUPool::new()));
let users = Arc::new(TokioMutex::new(UserManager::new()));
let billing = Arc::new(TokioMutex::new(BillingSystem::new()));

start_dashboard(
gpupool,
user_manager,
billing_system
).await
let rt = Runtime::new()?;
rt.block_on(dashboard::start_dashboard(gpupool, users, billing))
}

pub async fn start_dashboard(
Expand Down
Loading