在我尝试使用 Cognitect 的 aws-api 在 Lambda 函数中从 S3 获取文件时,我遇到了意外的缓慢行为。下面的示例代码被用来获取每个相关操作所需的一般时间感。获取客户端 (aws/client {:api :s3})
持续需要超过 4 秒钟。作为一个比较点,执行同一代码从我的本地开发环境访问同一个 S3 桶/键,执行时没有注意到同样的延迟,就像它在 Lambda 中执行时那样。
我应该指出,这个函数所在的命名空间利用了 gen-class 功能。
(defn read-s3-file
[bucket obj-key]
(let [_ (println (str "-----> Before declaring s3 client: " (java.time.LocalDateTime/now)))
s3 (aws/client {:api :s3})
_ (println (str "-----> After declaring s3 client: " (java.time.LocalDateTime/now)))
_ (println (str "---> bucket: " bucket ", key " obj-key))
body (:Body (aws/invoke s3 {:op :GetObject :request {:Bucket bucket :Key obj-key}}))
_ (println (str "-----> After GetObject: " (java.time.LocalDateTime/now)))
]
(println (str "-----> Before calling slurp: " (java.time.LocalDateTime/now)))
(slurp body)
)
)
这是否是由于 Lambda 所需的 java8 运行时引起的?
本地输出
clj-lead-updater.core=> (clj-lead-updater.s3/read-s3-file "automattix-incoming-emails" "tdf10001AnsSvc/0dqsmtvh5mquq3tdhpq550lnqcp6162f0k29da01")
-----> Before declaring s3 client: 2019-11-18T18:52:27.611925
-----> After declaring s3 client: 2019-11-18T18:52:27.638097
---> bucket: automattix-incoming-emails, key tdf10001AnsSvc/0dqsmtvh5mquq3tdhpq550lnqcp6162f0k29da01
-----> After GetObject: 2019-11-18T18:52:28.096044
-----> Before calling slurp: 2019-11-18T18:52:28.096431
两个相关的指标是
-----> Before declaring s3 client: 2019-11-18T18:52:27.611925
-----> After declaring s3 client: 2019-11-18T18:52:27.638097
Lambda 输出
-----> Before turning input into a map: 2019-11-17T02:27:08.705
-----> Before declaring s3 client: 2019-11-17T02:27:08.781
2019-11-17 02:27:10.641:INFO::main: Logging initialized @8431ms to org.eclipse.jetty.util.log.StdErrLog
-----> After declaring s3 client: 2019-11-17T02:27:12.923
-----> After GetObject: 2019-11-17T02:27:14.446
-----> Before calling slurp: 2019-11-17T02:27:14.446
再次,两个相关的指标是
-----> Before declaring s3 client: 2019-11-17T02:27:08.781
-----> After declaring s3 client: 2019-11-17T02:27:12.923
项目中的相关依赖项
[org.clojure/clojure "1.10.1"]
[com.cognitect.aws/api "0.8.391"]
[com.cognitect.aws/endpoints "1.1.11.670"]
[com.cognitect.aws/s3 "770.2.568.0"]