Commit 49a39f34ae81f573b4f2358b7c825bc2bebb5d98
1 parent
c24d3ac7
Exists in
master
and in
21 other branches
Added search_timeout option - closes #583
Showing
4 changed files
with
26 additions
and
1 deletions
Show diff stats
CHANGELOG.md
lib/searchkick.rb
... | ... | @@ -8,6 +8,7 @@ require "searchkick/query" |
8 | 8 | require "searchkick/reindex_job" |
9 | 9 | require "searchkick/model" |
10 | 10 | require "searchkick/tasks" |
11 | +require "searchkick/middleware" | |
11 | 12 | require "searchkick/logging" if defined?(Rails) |
12 | 13 | |
13 | 14 | # background jobs |
... | ... | @@ -30,6 +31,7 @@ module Searchkick |
30 | 31 | attr_accessor :search_method_name |
31 | 32 | attr_accessor :wordnet_path |
32 | 33 | attr_accessor :timeout |
34 | + attr_writer :search_timeout | |
33 | 35 | attr_accessor :models |
34 | 36 | attr_writer :env |
35 | 37 | end |
... | ... | @@ -43,13 +45,19 @@ module Searchkick |
43 | 45 | Elasticsearch::Client.new( |
44 | 46 | url: ENV["ELASTICSEARCH_URL"], |
45 | 47 | transport_options: {request: {timeout: timeout}} |
46 | - ) | |
48 | + ) do |f| | |
49 | + f.use Searchkick::Middleware | |
50 | + end | |
47 | 51 | end |
48 | 52 | |
49 | 53 | class << self |
50 | 54 | attr_writer :client |
51 | 55 | end |
52 | 56 | |
57 | + def self.search_timeout | |
58 | + @search_timeout || timeout | |
59 | + end | |
60 | + | |
53 | 61 | def self.server_version |
54 | 62 | @server_version ||= client.info["version"]["number"] |
55 | 63 | end | ... | ... |
... | ... | @@ -0,0 +1,12 @@ |
1 | +require "faraday/middleware" | |
2 | + | |
3 | +module Searchkick | |
4 | + class Middleware < Faraday::Middleware | |
5 | + def call(env) | |
6 | + if env.method == :get && env.url.path.to_s.end_with?("/_search") | |
7 | + env.request.timeout = Searchkick.search_timeout | |
8 | + end | |
9 | + @app.call(env) | |
10 | + end | |
11 | + end | |
12 | +end | ... | ... |
test/test_helper.rb
... | ... | @@ -11,6 +11,7 @@ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) |
11 | 11 | |
12 | 12 | File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") |
13 | 13 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
14 | +Searchkick.search_timeout = 5 | |
14 | 15 | |
15 | 16 | puts "Running against Elasticsearch #{Searchkick.server_version}" |
16 | 17 | ... | ... |