Skip to content

Commit a223aa9

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

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@
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- clear-directory
16+
"Recursively delete all contents of a directory"
17+
[^java.io.File dir]
18+
(when (.exists dir)
19+
(doseq [file (.listFiles dir)]
20+
(if (.isDirectory file)
21+
(do
22+
(clear-directory file)
23+
(.delete file))
24+
(.delete file)))
25+
(log/info "Cleared disk cache directory:" (.getPath dir))))
26+
1427
(defn -init []
15-
; NOTE: This ensure xtdb is warmed up before starting the server
28+
; Clear disk cache from previous Lambda container executions
29+
(clear-directory (io/file "/tmp/xtdb-cache"))
30+
31+
; NOTE: This ensures xtdb is warmed up before starting the server
1632
; Otherwise, the first few requests will time out
1733
(with-open [node (xtn/start-node config/node-config)]
1834
(xt/status node))

src/clj/xt_play/xtdb.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
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- clear-cache-directory
11+
"Clear disk cache before starting a new node to prevent /tmp exhaustion"
12+
[]
13+
(let [cache-dir (io/file "/tmp/xtdb-cache")]
14+
(when (.exists cache-dir)
15+
(doseq [file (.listFiles cache-dir)]
16+
(if (.isDirectory file)
17+
(do
18+
(doseq [f (.listFiles file)]
19+
(.delete f))
20+
(.delete file))
21+
(.delete file)))
22+
(log/debug "Cleared disk cache directory before node start"))))
23+
924
(defn with-xtdb [f]
25+
;; Clear cache before each request to prevent /tmp exhaustion
26+
;; This is critical for Lambda with limited ephemeral storage
27+
(clear-cache-directory)
28+
1029
(with-open [node (xtn/start-node config/node-config)]
1130
(f node)))
1231

0 commit comments

Comments
 (0)