3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Tips: RubyでCloud Functionsを書くときに、ライブラリを使っている場合のレスポンスを返すのを高速化したい時のテクニック

Posted at

この記事は何

でレスポンスだけ先に返すことで、slackのスラッシュコマンドのタイムアウトを逃れる方法を紹介しました。
また、Cloud Formationの立ちたがりを早くする方法として

で記事にしました。

しかし、処理の実装にライブラリを使っていると、上で紹介している方法でもスラッシュコマンドのタイムアウトを逃れられない場合があります。
この記事では、そのような場合に追加で行える高速化についてご紹介します。

方法

方法は、https://qiita.com/getty104/items/1d43da1cf830cc5b99ea で紹介したThreadによる非同期化に加え、Thread内でライブラリのロードを行うようにすることです。これにより、よりCloud Functionの立ち上がりのスピードが早くなり、レスポンスを返すのが早くなります。

↑の記事で書いている例を使うと以下のようなイメージです。

require 'functions_framework'

FunctionsFramework.http 'function' do |request|
  Thread.new do
    require 'some_library'
    require 'some_library2'
    ...

    very_long_transaction
    'OK'
  end
  
  'Running...'
end
3
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?