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,6 +8,7 @@ require "searchkick/query" | ||
8 | require "searchkick/reindex_job" | 8 | require "searchkick/reindex_job" |
9 | require "searchkick/model" | 9 | require "searchkick/model" |
10 | require "searchkick/tasks" | 10 | require "searchkick/tasks" |
11 | +require "searchkick/middleware" | ||
11 | require "searchkick/logging" if defined?(Rails) | 12 | require "searchkick/logging" if defined?(Rails) |
12 | 13 | ||
13 | # background jobs | 14 | # background jobs |
@@ -30,6 +31,7 @@ module Searchkick | @@ -30,6 +31,7 @@ module Searchkick | ||
30 | attr_accessor :search_method_name | 31 | attr_accessor :search_method_name |
31 | attr_accessor :wordnet_path | 32 | attr_accessor :wordnet_path |
32 | attr_accessor :timeout | 33 | attr_accessor :timeout |
34 | + attr_writer :search_timeout | ||
33 | attr_accessor :models | 35 | attr_accessor :models |
34 | attr_writer :env | 36 | attr_writer :env |
35 | end | 37 | end |
@@ -43,13 +45,19 @@ module Searchkick | @@ -43,13 +45,19 @@ module Searchkick | ||
43 | Elasticsearch::Client.new( | 45 | Elasticsearch::Client.new( |
44 | url: ENV["ELASTICSEARCH_URL"], | 46 | url: ENV["ELASTICSEARCH_URL"], |
45 | transport_options: {request: {timeout: timeout}} | 47 | transport_options: {request: {timeout: timeout}} |
46 | - ) | 48 | + ) do |f| |
49 | + f.use Searchkick::Middleware | ||
50 | + end | ||
47 | end | 51 | end |
48 | 52 | ||
49 | class << self | 53 | class << self |
50 | attr_writer :client | 54 | attr_writer :client |
51 | end | 55 | end |
52 | 56 | ||
57 | + def self.search_timeout | ||
58 | + @search_timeout || timeout | ||
59 | + end | ||
60 | + | ||
53 | def self.server_version | 61 | def self.server_version |
54 | @server_version ||= client.info["version"]["number"] | 62 | @server_version ||= client.info["version"]["number"] |
55 | end | 63 | end |
@@ -0,0 +1,12 @@ | @@ -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,6 +11,7 @@ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) | ||
11 | 11 | ||
12 | File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") | 12 | File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") |
13 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") | 13 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
14 | +Searchkick.search_timeout = 5 | ||
14 | 15 | ||
15 | puts "Running against Elasticsearch #{Searchkick.server_version}" | 16 | puts "Running against Elasticsearch #{Searchkick.server_version}" |
16 | 17 |