mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Merge pull request #434 from Freika/fix/photon-rate-limiting
Fix photon rate limiting
This commit is contained in:
commit
0067786792
12 changed files with 32 additions and 16 deletions
|
|
@ -1 +1 @@
|
||||||
0.16.8
|
0.16.9
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
||||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
# 0.16.9 - 2024-11-24
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Rate limit for the Photon API is now 1 request per second. If you host your own Photon API instance, reverse geocoding requests will not be limited.
|
||||||
|
- Requests to the Photon API are now have User-Agent header set to "Dawarich #{APP_VERSION} (https://dawarich.app)"
|
||||||
|
|
||||||
# 0.16.8 - 2024-11-20
|
# 0.16.8 - 2024-11-20
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
||||||
1
Gemfile
1
Gemfile
|
|
@ -27,6 +27,7 @@ gem 'rswag-ui'
|
||||||
gem 'shrine', '~> 3.6'
|
gem 'shrine', '~> 3.6'
|
||||||
gem 'sidekiq'
|
gem 'sidekiq'
|
||||||
gem 'sidekiq-cron'
|
gem 'sidekiq-cron'
|
||||||
|
gem 'sidekiq-limit_fetch'
|
||||||
gem 'sprockets-rails'
|
gem 'sprockets-rails'
|
||||||
gem 'stimulus-rails'
|
gem 'stimulus-rails'
|
||||||
gem 'tailwindcss-rails'
|
gem 'tailwindcss-rails'
|
||||||
|
|
|
||||||
|
|
@ -370,6 +370,8 @@ GEM
|
||||||
fugit (~> 1.8, >= 1.11.1)
|
fugit (~> 1.8, >= 1.11.1)
|
||||||
globalid (>= 1.0.1)
|
globalid (>= 1.0.1)
|
||||||
sidekiq (>= 6.5.0)
|
sidekiq (>= 6.5.0)
|
||||||
|
sidekiq-limit_fetch (4.4.1)
|
||||||
|
sidekiq (>= 6)
|
||||||
simplecov (0.22.0)
|
simplecov (0.22.0)
|
||||||
docile (~> 1.1)
|
docile (~> 1.1)
|
||||||
simplecov-html (~> 0.11)
|
simplecov-html (~> 0.11)
|
||||||
|
|
@ -465,6 +467,7 @@ DEPENDENCIES
|
||||||
shrine (~> 3.6)
|
shrine (~> 3.6)
|
||||||
sidekiq
|
sidekiq
|
||||||
sidekiq-cron
|
sidekiq-cron
|
||||||
|
sidekiq-limit_fetch
|
||||||
simplecov
|
simplecov
|
||||||
sprockets-rails
|
sprockets-rails
|
||||||
stimulus-rails
|
stimulus-rails
|
||||||
|
|
|
||||||
|
|
@ -69,10 +69,6 @@ module ApplicationHelper
|
||||||
CheckAppVersion.new.call
|
CheckAppVersion.new.call
|
||||||
end
|
end
|
||||||
|
|
||||||
def app_version
|
|
||||||
File.read('.app_version').strip
|
|
||||||
end
|
|
||||||
|
|
||||||
def app_theme
|
def app_theme
|
||||||
current_user&.theme == 'light' ? 'light' : 'dark'
|
current_user&.theme == 'light' ? 'light' : 'dark'
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ class ReverseGeocodingJob < ApplicationJob
|
||||||
def perform(klass, id)
|
def perform(klass, id)
|
||||||
return unless REVERSE_GEOCODING_ENABLED
|
return unless REVERSE_GEOCODING_ENABLED
|
||||||
|
|
||||||
|
rate_limit_for_photon_api
|
||||||
|
|
||||||
data_fetcher(klass, id).call
|
data_fetcher(klass, id).call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -14,4 +16,10 @@ class ReverseGeocodingJob < ApplicationJob
|
||||||
def data_fetcher(klass, id)
|
def data_fetcher(klass, id)
|
||||||
"ReverseGeocoding::#{klass.pluralize.camelize}::FetchData".constantize.new(id)
|
"ReverseGeocoding::#{klass.pluralize.camelize}::FetchData".constantize.new(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rate_limit_for_photon_api
|
||||||
|
return unless PHOTON_API_HOST == 'photon.komoot.io'
|
||||||
|
|
||||||
|
sleep 1 if PHOTON_API_HOST == 'photon.komoot.io'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,10 @@ class CheckAppVersion
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@repo_url = 'https://api.github.com/repos/Freika/dawarich/tags'
|
@repo_url = 'https://api.github.com/repos/Freika/dawarich/tags'
|
||||||
@app_version = File.read('.app_version').strip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
latest_version != @app_version
|
latest_version != APP_VERSION
|
||||||
rescue StandardError
|
rescue StandardError
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,10 @@
|
||||||
<a href="https://github.com/Freika/dawarich/releases/latest" target="_blank" class="inline-flex items-center">
|
<a href="https://github.com/Freika/dawarich/releases/latest" target="_blank" class="inline-flex items-center">
|
||||||
<% if new_version_available? %>
|
<% if new_version_available? %>
|
||||||
<span class="tooltip tooltip-bottom" data-tip="New version available! Check out Github releases!">
|
<span class="tooltip tooltip-bottom" data-tip="New version available! Check out Github releases!">
|
||||||
<span class="hidden sm:inline"><%= app_version %> !</span>
|
<span class="hidden sm:inline"><%= APP_VERSION %> !</span>
|
||||||
</span>
|
</span>
|
||||||
<% else %>
|
<% else %>
|
||||||
<span class="hidden sm:inline"><%= app_version %></span>
|
<span class="hidden sm:inline"><%= APP_VERSION %></span>
|
||||||
<% end %>
|
<% end %>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,4 @@ REVERSE_GEOCODING_ENABLED = ENV.fetch('REVERSE_GEOCODING_ENABLED', 'true') == 't
|
||||||
PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil)
|
PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil)
|
||||||
PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true'
|
PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true'
|
||||||
DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym
|
DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym
|
||||||
|
APP_VERSION = File.read('.app_version').strip
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ settings = {
|
||||||
cache: Redis.new,
|
cache: Redis.new,
|
||||||
always_raise: :all,
|
always_raise: :all,
|
||||||
use_https: PHOTON_API_USE_HTTPS,
|
use_https: PHOTON_API_USE_HTTPS,
|
||||||
|
http_headers: { 'User-Agent' => "Dawarich #{APP_VERSION} (https://dawarich.app)" },
|
||||||
cache_options: {
|
cache_options: {
|
||||||
expiration: 1.day
|
expiration: 1.day
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,3 +24,5 @@ end
|
||||||
Sidekiq.configure_client do |config|
|
Sidekiq.configure_client do |config|
|
||||||
config.redis = { url: ENV['REDIS_URL'] }
|
config.redis = { url: ENV['REDIS_URL'] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Sidekiq::Queue['reverse_geocoding'].limit = 1 if PHOTON_API_HOST == 'photon.komoot.io'
|
||||||
|
|
|
||||||
|
|
@ -6,27 +6,25 @@ RSpec.describe CheckAppVersion do
|
||||||
describe '#call' do
|
describe '#call' do
|
||||||
subject(:check_app_version) { described_class.new.call }
|
subject(:check_app_version) { described_class.new.call }
|
||||||
|
|
||||||
let(:app_version) { File.read('.app_version').strip }
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
|
||||||
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
|
||||||
|
|
||||||
|
stub_const('APP_VERSION', '1.0.0')
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when latest version is newer' do
|
context 'when latest version is newer' do
|
||||||
before { allow(File).to receive(:read).with('.app_version').and_return('0.9.0') }
|
before { stub_const('APP_VERSION', '0.9.0') }
|
||||||
|
|
||||||
it { is_expected.to be true }
|
it { is_expected.to be true }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when latest version is the same' do
|
context 'when latest version is the same' do
|
||||||
before { allow(File).to receive(:read).with('.app_version').and_return('1.0.0') }
|
|
||||||
|
|
||||||
it { is_expected.to be false }
|
it { is_expected.to be false }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when latest version is older' do
|
context 'when latest version is older' do
|
||||||
before { allow(File).to receive(:read).with('.app_version').and_return('1.1.0') }
|
before { stub_const('APP_VERSION', '1.1.0') }
|
||||||
|
|
||||||
it { is_expected.to be true }
|
it { is_expected.to be true }
|
||||||
end
|
end
|
||||||
|
|
@ -34,7 +32,7 @@ RSpec.describe CheckAppVersion do
|
||||||
context 'when request fails' do
|
context 'when request fails' do
|
||||||
before do
|
before do
|
||||||
allow(Net::HTTP).to receive(:get).and_raise(StandardError)
|
allow(Net::HTTP).to receive(:get).and_raise(StandardError)
|
||||||
allow(File).to receive(:read).with('.app_version').and_return(app_version)
|
allow(File).to receive(:read).with('.app_version').and_return(APP_VERSION)
|
||||||
end
|
end
|
||||||
|
|
||||||
it { is_expected.to be false }
|
it { is_expected.to be false }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue