LoginSignup
3
3

More than 5 years have passed since last update.

aws-sdk-rubyでバックエンドに非同期リクエストする

Posted at

勉強中ですがメモ。そのうち試す。

v2なaws-sdk-core-rubyは未確認。
v1のaws-sdk-rubyの調査結果です。


HttpHandlerの実装

非同期リクエストするには、まずデフォルトのHttpHandlerを差し替える必要がある。
デフォルトのHttpHandlerであるAWS::Core::Http::NetHttpHandlerはnet/httpベースのクライアントであり、非同期実装は無い。

定義するべきメソッドは3つ。
雛形を書くと、以下のようになる(initializeは略)。

hoge_http_handler.rb
module AWS
  module Core
    module Http
      class HogeHttpHandler

        def handle(request, http_response, &read_block)
        end

        def handle_async(request, http_response, handle)
        end

        def sleep_with_callback(seconds, &block)
        end

      end
    end
  end
end

リクエストオプションに{async: true}が含まれると、handle_async()がcallされる。
sleep_with_callback()は、リトライ時にcallされる。

HttpHandlerの登録

configurationとして登録する。

AWS.config(http_handler: handler)

各AWSリソースのinitializeオプションとして渡してやってもOK。

EventMachine(EM)を使ったクライアント

調べてみるとEmAws が既にあった。
触ってみよう ←今ココ

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3