# 迁移主题和帖子时达到访问频率限制

**URL:** <https://meta.discourse.org/t/ratelimit-exceeded-when-migrating-threads-and-posts/350588>\
**Category:** Migration\
**Created:** [2025年二月5日 07:30 UTC](https://meta.discourse.org/t/ratelimit-exceeded-when-migrating-threads-and-posts/350588 "2025-02-05T07:30:09Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Klaas\_Tammling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/klaas_tammling/32/486599_2.png) [@Klaas\_Tammling](https://meta.discourse.org/u/Klaas_Tammling)\
**Post date:** [2025年二月5日 07:30 UTC](https://meta.discourse.org/t/ratelimit-exceeded-when-migrating-threads-and-posts/350588/1 "2025-02-05T07:30:09Z")

</div>

大家好，

我目前正在编写一个将 Woltlab (Wbb) 论坛迁移到 Discourse 的工具，一旦完成，我很乐意分享。

目前，我正在比较顺利地迁移分类、论坛、用户及其群组，没有太多问题。然而，在处理帖子和主题时，偶尔会遇到“Ratelimit Exceeded”（超出速率限制）的错误，而且情况越来越糟。

我的问题是，我相对较新，主要从其他迁移工具中收集零散的代码，所以我目前不清楚这个消息来自哪里。

它来自 Discourse 的 API 吗？如果是，我能否在导入期间完全禁用速率限制？

```plaintext
def migrate_threads_and_posts
  puts "Migrating threads and posts..."
  @client.query("SELECT * FROM wbb1_thread ORDER BY time ASC").each do |thread_row|
    begin
      next if thread_row[:isDeleted] == 1 || thread_row[:movedThreadID]

      # Ensure lastPosterID maps to a valid user
      last_post_user_id = @user_map[thread_row[:lastPosterID]] || @user_map[thread_row[:userID]]

      # If lastPosterID and userID are null, create a placeholder user
      if last_post_user_id.nil? && thread_row[:lastPoster]
        last_post_user_id = find_or_create_placeholder_user(thread_row[:lastPoster])
      end

      # Ensure a valid user ID exists
      last_post_user_id ||= User.first.id

      # Get topic creator user ID or create placeholder
      creator_user_id = @user_map[thread_row[:userID]] || find_or_create_placeholder_user(thread_row[:username]) || User.first.id

      # Create the topic
      topic = Topic.new(
        title: thread_row[:topic],
        user_id: creator_user_id,
        category_id: @forum_map[thread_row[:boardID]],
        created_at: Time.at(thread_row[:time]),
        updated_at: Time.at(thread_row[:lastPostTime]),
        last_post_user_id: last_post_user_id
      )

      if topic.save(validate: false)
        puts "Imported thread: #{topic.title} (ID: #{topic.id})"

        # Migrate posts
        migrate_posts(thread_row[:threadID], topic.id)
      else
        puts "Error importing thread #{thread_row[:topic]}: #{topic.errors.full_messages.join(', ')}"
      end

      sleep(7) # Increased sleep to avoid rate limiting
    rescue => e
      puts "Exception importing thread #{thread_row[:topic]}: #{e.message}"
    end
  end
end

```

我试图清理代码并尽可能添加注释。希望有人能帮助解决速率限制问题。

我也尝试在这里的论坛搜索过，但只看到很多人有类似的问题，但从未真正解决。所以如果我错过了什么，抱歉重复发帖。

谢谢大家。

---

_[View the full topic](https://meta.discourse.org/t/ratelimit-exceeded-when-migrating-threads-and-posts/350588)._
