Skip to content

Commit a88e28f

Browse files
committed
clear disk-cache between invocations and limit it to 128MB
1 parent b49b4f8 commit a88e28f

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/clj/xt_play/config.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
(def node-config
66
{:server {:port pgwire-port}
7-
:disk-cache {:path "/tmp/xtdb-cache"}
7+
:disk-cache {:path "/tmp/xtdb-cache"
8+
:max-size-bytes (* 128 1024 1024)} ; 128MB - limit for Lambda
89
:compactor {:threads 0}})

src/clj/xt_play/lambda.clj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,37 @@
55
(:require [muuntaja.core :as m]
66
[clojure.java.io :as io]
77
[clojure.string :as str]
8+
[clojure.tools.logging :as log]
89
[xtdb.api :as xt]
910
[xtdb.node :as xtn]
1011
[xt-play.config :as config]
1112
[xt-play.base64 :as b64]
1213
[xt-play.handler :as h]))
1314

15+
(defn- delete-recursively
16+
"Recursively delete a file or directory and all its contents"
17+
[^java.io.File file]
18+
(when (.exists file)
19+
(if (.isDirectory file)
20+
(do
21+
(doseq [child (.listFiles file)]
22+
(delete-recursively child))
23+
(.delete file))
24+
(.delete file))))
25+
26+
(defn- clear-directory
27+
"Clear all contents of a directory"
28+
[^java.io.File dir]
29+
(when (.exists dir)
30+
(doseq [file (.listFiles dir)]
31+
(delete-recursively file))
32+
(log/info "Cleared disk cache directory:" (.getPath dir))))
33+
1434
(defn -init []
15-
; NOTE: This ensure xtdb is warmed up before starting the server
35+
; Clear disk cache from previous Lambda container executions
36+
(clear-directory (io/file "/tmp/xtdb-cache"))
37+
38+
; NOTE: This ensures xtdb is warmed up before starting the server
1639
; Otherwise, the first few requests will time out
1740
(with-open [node (xtn/start-node config/node-config)]
1841
(xt/status node))

src/clj/xt_play/xtdb.clj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
(ns xt-play.xtdb
22
(:require [clojure.tools.logging :as log]
3+
[clojure.java.io :as io]
34
[next.jdbc :as jdbc]
45
[xt-play.config :as config]
56
[xtdb.api :as xt]
67
[xtdb.node :as xtn]
78
[xtdb.next.jdbc :as xjdbc]))
89

10+
(defn- delete-recursively
11+
"Recursively delete a directory and all its contents"
12+
[^java.io.File file]
13+
(when (.exists file)
14+
(if (.isDirectory file)
15+
(do
16+
(doseq [child (.listFiles file)]
17+
(delete-recursively child))
18+
(.delete file))
19+
(.delete file))))
20+
21+
(defn- clear-cache-directory
22+
"Clear disk cache before starting a new node to prevent /tmp exhaustion"
23+
[]
24+
(let [cache-dir (io/file "/tmp/xtdb-cache")]
25+
(when (.exists cache-dir)
26+
(doseq [file (.listFiles cache-dir)]
27+
(delete-recursively file))
28+
(log/debug "Cleared disk cache directory before node start"))))
29+
930
(defn with-xtdb [f]
31+
;; Clear cache before each request to prevent /tmp exhaustion
32+
;; This is critical for Lambda with limited ephemeral storage
33+
(clear-cache-directory)
34+
1035
(with-open [node (xtn/start-node config/node-config)]
1136
(f node)))
1237

0 commit comments

Comments
 (0)