Skip to content

Commit 1d3a6f7

Browse files
committed
Added ram and cpus configuration
1 parent a8104c7 commit 1d3a6f7

4 files changed

Lines changed: 12 additions & 2 deletions

File tree

JShellAPI/src/main/java/org/togetherjava/jshellapi/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ public record Config(
88
long oneTimeSessionTimeoutSeconds,
99
long evalTimeoutSeconds,
1010
long maxAliveSessions,
11+
int dockerMaxRamMegaBytes,
12+
double dockerCPUsUsage,
1113
int schedulerThreadCount,
1214
long schedulerSessionKillScanRate) {
1315
public Config {
1416
if(regularSessionTimeoutSeconds <= 0) throw new RuntimeException("Invalid value " + regularSessionTimeoutSeconds);
1517
if(oneTimeSessionTimeoutSeconds <= 0) throw new RuntimeException("Invalid value " + oneTimeSessionTimeoutSeconds);
1618
if(evalTimeoutSeconds <= 0) throw new RuntimeException("Invalid value " + evalTimeoutSeconds);
1719
if(maxAliveSessions <= 0) throw new RuntimeException("Invalid value " + maxAliveSessions);
20+
if(dockerMaxRamMegaBytes <= 0) throw new RuntimeException("Invalid value " + dockerMaxRamMegaBytes);
21+
if(dockerCPUsUsage <= 0) throw new RuntimeException("Invalid value " + dockerCPUsUsage);
1822
if(schedulerThreadCount <= 0) throw new RuntimeException("Invalid value " + schedulerThreadCount);
1923
if(schedulerSessionKillScanRate <= 0) throw new RuntimeException("Invalid value " + schedulerSessionKillScanRate);
2024
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class JShellService implements Closeable {
2929
private final boolean renewable;
3030
private boolean doingOperation;
3131

32-
public JShellService(JShellSessionService sessionService, String id, long timeout, boolean renewable, long evalTimeout) throws DockerException {
32+
public JShellService(JShellSessionService sessionService, String id, long timeout, boolean renewable, long evalTimeout, int maxMemory, double cpus) throws DockerException {
3333
this.sessionService = sessionService;
3434
this.id = id;
3535
this.timeout = timeout;
@@ -52,6 +52,8 @@ public JShellService(JShellSessionService sessionService, String id, long timeou
5252
"--pids-limit=2000",
5353
"--memory=500M",
5454
"--read-only",
55+
"--memory=" + maxMemory + "m",
56+
"--cpus=" + cpus,
5557
"--name", containerName(),
5658
"jshellwrapper",
5759
"java", "-DevalTimeoutSeconds=%d".formatted(evalTimeout), "-jar", "JShellWrapper.jar")

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private synchronized JShellService createSession(String id, long sessionTimeout,
6868
if(jshellSessions.size() >= config.maxAliveSessions()) {
6969
throw new ResponseStatusException(HttpStatus.TOO_MANY_REQUESTS, "Too many sessions, try again later :(.");
7070
}
71-
JShellService service = new JShellService(this, id, sessionTimeout, renewable, evalTimeout);
71+
JShellService service = new JShellService(this, id, sessionTimeout, renewable, evalTimeout, config.dockerMaxRamMegaBytes(), config.dockerCPUsUsage());
7272
jshellSessions.put(id, service);
7373
return service;
7474
}

JShellAPI/src/main/resources/application.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ jshellapi.oneTimeSessionTimeoutSeconds=30
55
jshellapi.evalTimeoutSeconds=15
66
jshellapi.maxAliveSessions=10
77

8+
# Docker limits config
9+
jshellapi.dockerMaxRamMegaBytes=100
10+
jshellapi.dockerCPUsUsage=0.5
11+
812
# Internal config
913
jshellapi.schedulerThreadCount=1
1014
jshellapi.schedulerSessionKillScanRate=60

0 commit comments

Comments
 (0)