Any way to turn off RateLimiter temporarily for bulk creation by admin?

So here is my issue. I have a requirement to essentially seed a Discourse production instance with around 200 posts at a time from a plugin admin action. These posts will be ‘created by’ 1 of 10 different regular users. The reason it’s a plugin action is because the users of this particular instance have a team of moderators they want to train up, and wanted some test posts to train them on, and the ability to seed more when they need them.

I got this working fine by passing skip_validations: true to PostCreator.new, however now there’s a requirement that some of the created posts are also flagged.

I’m using PostActionCreator.create to flag some of these posts, but now I’m getting hit by the rate limiter here: https://github.com/discourse/discourse/blob/ad7a13921f2af8c792530c84386b64911c8e7ea2/lib/post_action_creator.rb#L69

I first attempted to disable the RateLimiter but that was causing my action to crash the server process eventually, possibly when I was trying to turn it back on, and then I realised it probably wasn’t a good idea anyway.

So my question is, is there a better way to bypass the rate limiter when running some arbitrary code i.e. something like:

RateLimiter.bypass do
# run some code not affected by the rate limiter
end

Or do I need to basically just need to copy most of what the PostActionCreator is doing but leave out the troublesome line?

Any help would be greatly appreciated! I’m still absorbing a lot of the Discourse code so I’m aware I’ve probably missed something that makes this a lot easier!

「いいね!」 1

What I’d likely do is run a script in the Rails console. Have you checked out: Global rate limits and throttling in Discourse? It looks like you can change those rate limits.

「いいね!」 2

This isn’t really a path I want to go down as I want it to initiated by users rather than them relying on me to run a script in the console.

「いいね!」 3

Did you look at the link about changing the rate limits?

これを解決しましたか? YAMLファイルを変更するのは理想的ではありません。再構築が必要になるためです。Rails/Discourseに詳しくないので、コンソールで一時的に無効にする方法がわかりません。

ユースケースは何ですか?レート制限が妨げとなっている問題は何ですか?

API経由で再構築せずに、できるだけ速く60,000人のユーザーをインポートしようとしています。テストインストールでYAML経由でADMIN API制限を引き上げようとしましたが、うまくいかなかったようです。あるいは、何か間違ったことをしたのかもしれません。(コンテナでインポートを実行しているので、HTTPスロットリングは適用されないはずです)。

これはあまり役に立ちませんが、API経由ではなくRailsでユーザーを作成します。discourse/script/import_scripts/csv_importer.rb at main · discourse/discourse · GitHub が参考になるでしょう。

しかし、コンテナに入り、/var/www/discourse/config/discourse.conf を編集してそこにそれらの変数を設定し、その後 sv restart unicorn (または sv reload unicorn) を実行することもできます。エディタを用意するために、おそらく apt-get update; apt-get install -y vim のようなことを行う必要があるでしょう。

「いいね!」 2

奇妙ですね。conf ファイルを見ると、新しい制限はすでに設定されています。YAML の更新は機能したようです。

max_admin_api_reqs_per_key_per_minute = '6000'

しかし、機能していないようです。依然として 1 分あたり 60 件の制限があります。1 分あたり 60 件のリクエストを制限すると、ほとんどのリクエストは通過しますが、ジッターにより一部はレート リミッターによってトリガーされます。

{'success': True, 'active': True, 'message': 'Your account is activated and ready to use.', 'user_id': 3596}
{'success': False, 'message': ' New registrations are not allowed from your IP address (maximum limit reached). Contact a staff member.', 'errors': {'ip_address': ['New registrations are not allowed from your IP address (maximum limit reached). Contact a staff member.']}, 'values': {'name': None, 'username': 'asdfd', 'email': 'user@domain.com'}, 'is_developer': False}
{'success': True, 'active': True, 'message': 'Your account is activated and ready to use.', 'user_id': 3597}

引用符の問題でしょうか?

SiteSettings.max_admin_api_reqs_per_key_per_minute を確認し、整数かどうか見てみてください。

「いいね!」 1

default_current_user_provider.rb で(この値に言及しているように見えた唯一のファイルですが、次のような内容が含まれています):

limit = GlobalSetting.max_admin_api_reqs_per_minute.to_i

そのため、実際には変換されているようです。コード内で数値に置き換えてみました。

「いいね!」 1

しまった。やはりそうだったか。

「いいね!」 1

別の制限に関連していると思います。IPアドレスあたりの最大登録数制限に達していると思いますが、なぜそれに達した後、永続的にブロックされないのか理解できません。このスパム制限にバグがあるのでしょうか?

もしそれらのユーザーがすべて同じIPアドレスを持っているなら、あなたの言う通りだと思います。解決策は、その設定を変更するか、127.0.0.x のようなダミーIPアドレスをユーザーに割り当てること(あるいは、もっと簡単にするためにIPv6を使用する?)だと思います。

「いいね!」 1

リクエストの発信元(ホストコンピューター)を検出しているようですが、同じIPアドレスにTL2ユーザーを追加するか、IP制限を調整することで回避できます。

ユーザー追加APIは非常に遅いですが。

だから、Railsでやることをお勧めします。おそらく1分あたり500件処理できるでしょう。

「いいね!」 1

はい、試してみます。しかし、スクリプトをどのように実行するのか、どこにコピーすればよいのか、そして(CSVを適切な場所に配置したら)どのように実行するのかがよくわかりません。

他については、他の「使い方」トピックを参照してください。ほとんどすべて同じように機能します。

「いいね!」 1

スクリプトやRubyなどを理解しようとした労力と、予想されるパフォーマンスを考慮すると、別の方法を取り、SQLを使用してデータベースに直接書き込むことで、すべての制限を回避しました。これにより、1秒あたり数千件の挿入が可能になりました。

必要なテーブルについてのトピックがありましたが、現在は閉じられているため、ここにユーザーを挿入するために変更したテーブルを記載します。

user_avatars
user_emails
user_profiles
user_search_data
user_stats
users
「いいね!」 1