Merge pull request #434 from Freika/fix/photon-rate-limiting

Fix photon rate limiting
This commit is contained in:
Evgenii Burmakin 2024-11-24 15:32:06 +01:00 committed by GitHub
commit 0067786792
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 32 additions and 16 deletions

View file

@ -1 +1 @@
0.16.8
0.16.9

View file

@ -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/)
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
### Changed

View file

@ -27,6 +27,7 @@ gem 'rswag-ui'
gem 'shrine', '~> 3.6'
gem 'sidekiq'
gem 'sidekiq-cron'
gem 'sidekiq-limit_fetch'
gem 'sprockets-rails'
gem 'stimulus-rails'
gem 'tailwindcss-rails'

View file

@ -370,6 +370,8 @@ GEM
fugit (~> 1.8, >= 1.11.1)
globalid (>= 1.0.1)
sidekiq (>= 6.5.0)
sidekiq-limit_fetch (4.4.1)
sidekiq (>= 6)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
@ -465,6 +467,7 @@ DEPENDENCIES
shrine (~> 3.6)
sidekiq
sidekiq-cron
sidekiq-limit_fetch
simplecov
sprockets-rails
stimulus-rails

View file

@ -69,10 +69,6 @@ module ApplicationHelper
CheckAppVersion.new.call
end
def app_version
File.read('.app_version').strip
end
def app_theme
current_user&.theme == 'light' ? 'light' : 'dark'
end

View file

@ -6,6 +6,8 @@ class ReverseGeocodingJob < ApplicationJob
def perform(klass, id)
return unless REVERSE_GEOCODING_ENABLED
rate_limit_for_photon_api
data_fetcher(klass, id).call
end
@ -14,4 +16,10 @@ class ReverseGeocodingJob < ApplicationJob
def data_fetcher(klass, id)
"ReverseGeocoding::#{klass.pluralize.camelize}::FetchData".constantize.new(id)
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

View file

@ -5,11 +5,10 @@ class CheckAppVersion
def initialize
@repo_url = 'https://api.github.com/repos/Freika/dawarich/tags'
@app_version = File.read('.app_version').strip
end
def call
latest_version != @app_version
latest_version != APP_VERSION
rescue StandardError
false
end

View file

@ -19,10 +19,10 @@
<a href="https://github.com/Freika/dawarich/releases/latest" target="_blank" class="inline-flex items-center">
<% if new_version_available? %>
<span class="tooltip tooltip-bottom" data-tip="New version available! Check out Github releases!">
<span class="hidden sm:inline"><%= app_version %>&nbsp;!</span>
<span class="hidden sm:inline"><%= APP_VERSION %>&nbsp;!</span>
</span>
<% else %>
<span class="hidden sm:inline"><%= app_version %></span>
<span class="hidden sm:inline"><%= APP_VERSION %></span>
<% end %>
</a>
</div>

View file

@ -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_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true'
DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym
APP_VERSION = File.read('.app_version').strip

View file

@ -6,6 +6,7 @@ settings = {
cache: Redis.new,
always_raise: :all,
use_https: PHOTON_API_USE_HTTPS,
http_headers: { 'User-Agent' => "Dawarich #{APP_VERSION} (https://dawarich.app)" },
cache_options: {
expiration: 1.day
}

View file

@ -24,3 +24,5 @@ end
Sidekiq.configure_client do |config|
config.redis = { url: ENV['REDIS_URL'] }
end
Sidekiq::Queue['reverse_geocoding'].limit = 1 if PHOTON_API_HOST == 'photon.komoot.io'

View file

@ -6,27 +6,25 @@ RSpec.describe CheckAppVersion do
describe '#call' do
subject(:check_app_version) { described_class.new.call }
let(:app_version) { File.read('.app_version').strip }
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
stub_const('APP_VERSION', '1.0.0')
end
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 }
end
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 }
end
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 }
end
@ -34,7 +32,7 @@ RSpec.describe CheckAppVersion do
context 'when request fails' do
before do
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
it { is_expected.to be false }