Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
外部APIとうまく付き合う
Search
makinoshi
February 17, 2018
Programming
0
350
外部APIとうまく付き合う
Clojureを使って外部APIを使うコードを設計する、書く方法について
makinoshi
February 17, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
240
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
650
NPOでのDevinの活用
codeforeveryone
0
420
iOSアプリ開発で 関数型プログラミングを実現する The Composable Architectureの紹介
yimajo
2
220
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
230
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
260
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
5
1.4k
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
20
3.7k
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
140
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
560
Go1.25からのGOMAXPROCS
kuro_kurorrr
1
810
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
390
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
800
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
How STYLIGHT went responsive
nonsquared
100
5.6k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
KATA
mclloyd
29
14k
Faster Mobile Websites
deanohume
307
31k
RailsConf 2023
tenderlove
30
1.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
A Tale of Four Properties
chriscoyier
160
23k
The Invisible Side of Design
smashingmag
300
51k
Transcript
֎෦"1*ͱ ͏·͖͘߹͏ 2018-02-11 clj-ebisu #2 @makinoshi
Me • αΠϘζελʔτΞοϓεגࣜձࣾ • Like: C++(11~), PostgreSQL(SQL), Clojure, Emacs(Spacemacs), MS
Sculpt Ergonomic Keyboard • Hobby: ϚϯΨ,͓ञ • ࠷ۙͷΠνΦγ: cljr-create-fn-from-example
ΞδΣϯμ • (֎෦)APIʹΞΫηε͢Δ෦ΛͲ͏࡞Δ͔ • ೖྗ͢ΔσʔλߏΛͲ͏ͭ͘Δ͔ • ΧϓηϧԽ͞Εͨ෦ॲཧΛͭ͘Δ • ݁ՌΛද͢σʔλߏ
ೖྗ͢ΔσʔλߏΛ࡞Δ
ྫ) SendGridͷAPI {:headers {"Authorization" "Bearer ...."} :content-type :json :form-params {:subject
"໊݅" :content [{:type "text/plain" :value "ຊจ"}] :from {:name "MyService" :email "
[email protected]
"} :personalizations [{:to [{:email "
[email protected]
" :name "a͞Μ"} {:email "
[email protected]
" :name "a͞Μ"}] :custom_args {:id "1"}} {:to [{:email "
[email protected]
" :name “b͞Μ"}] :custom_args {:id “2"}}]}} • ͜ͷߏΛҙࣝͨ͠ίʔυॻ͖ͨ͘ͳ͍
Builderύλʔϯ • ֤ϝιουͰҾΛνΣοΫͭͭ͠σʔλཷ ΊࠐΉ • buildϝιουͰ͑ΔɺνΣοΫ͢Δ • ೖग़ྗܗࣜͷมߋΛ෦ͰٵऩͰ͖ΔˠAPIͷ มߋ͏APIࣗମͷมߋ༰қʹͳΔ
࣮ྫ (-> (mail/builder something-component) (mail/with-subject "໊݅") (mail/with-content "ຊจ") (mail/with-recipients [{:email
"
[email protected]
" :to-name "a͞Μ" :id 1 :callback “/app/path/1”} {:email "
[email protected]
" :to-name "b͞Μ" :id 2 :callback “/app/path/2”}]) mail/build) • AWS SDK for Javaͷ࣮͕BuilderύλʔϯΛ ͱ্ͯखʹ͍ͬͯΔͱࢥ͍·͢
෦ॲཧΛΧϓηϧԽ͢Δ
͏ଆʹ࿙Βͨ͘͠ͳ͍ • ͏ϥΠϒϥϦͷڍಈ • ྫ֎͕ൃੜ͢Δঢ়گͷࠩҟɺΓͷࠩҟ(map, promise, etc..) • ࣮ߦ •
rate limit • ಉ࣌ଓ • ϦτϥΠ • ෳͷϦΫΤετ୯ҰϦΫΤετͷγʔέϯεͱͯ͠ѻ͍͍ͨ
diehardͱڞʹ • https://github.com/sunng87/diehard • ઃఆϕʔεͰɺretry/circuit breaker/rate limitterΛ؆୯ʹ࣮Ͱ͖Δ • JavaͷϥΠϒϥϦɺnet.jodah/failsafeͷϥο ύ
࣮ྫ (diehard.core/defratelimiter rl {:rate 100}) ; per sec (clj-http.client/with-connection-pool {:timeout
10 :insecure? false :default-per-route 100} (->> coll (map #(future (diehard.core/with-retry {:retry-if (fn [resp exp] (or (>= (:status resp) 500) (instance? IOException exp))) :max-retries 3 :delay-ms 1000} (diehard.core/with-rate-limiter rl (api/call endpoint %))))) (map deref) doll)) • ΊͪΌͪ͘Ό؆୯ • ͬͱهड़ྔগͳ͘ఆٛ͢Δํ๏υΩϡϝϯτʹ͋Γ·͢
Γͷදݱ
ओͳΤϥʔॲཧ • ѻ͍͍͢Γ • ୯ͳΔσʔλ • Τϥʔ͔Ͳ͏͔ఆ͍͢͠ • Τϥʔʹͳͬͨͱ͖ͷҾΛͦͷ··औΓग़ͤΔ •
ѻ͍ʹ͍͘Γ • ྫ֎͕͛ΒΕͨΓɺ͛ΒΕͳ͔ͬͨΓ • ֎෦ͷϥΠϒϥϦͰఆٛ͞Εͨܕ • promise(ಛʹ֎෦ϥΠϒϥϦͷ) • ඇಉظ͕͍͍͔Ͳ͏͔ݺͼग़͠ଆͷࣄ • => GolangελΠϧ or ಠࣗఆٛͷEitherͷͲͪΒ͔͕Αͦ͞͏
࣮ࡍʹ྆ํͬͯΈͨ݁Ռ • GolangελΠϧͷํ͕ॻ͍ͯͯϛε͕গͳͦ͞͏ʹײ͍ͯ͡· ͢ • ѻ͍͘͢ɺγϯϓϧʹॻ͚Δ • (group-by #(-> %
second some?) results) (try (let [resp (clj-http.client/post endpoint params)] [{:resp resp resp :params params} nil]) (catch clojure.lang.ExceptionInfo e [{:params params} (ex-data e)]) (catch java.io.IOException e [{:params params} e]))
·ͱΊ • ClojureͰσʔλ࡞ͷநԽʹBuilder ཱͭ • ϥΠϒϥϦͷڍಈAPIͰ͋Δ͜ͱࣗମ֎͔ ΒӅ͢ˠޙʑ؆୯ʹมߋͰ͖Δ • Γྫ֎Ͱͳ͘σʔλͱͯ͠ѻ͍͘͢
͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ ͥͻ͝ҙݟΛԼ͍͞ɻ