How to Prevent HTTP 429 Errors with a Simple Go Request Queue

This article explains how to avoid HTTP 429 rate‑limit errors when calling third‑party APIs by wrapping each request in a struct and processing them through a channel‑based request queue written in Go, complete with usage examples and optional throttling.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Prevent HTTP 429 Errors with a Simple Go Request Queue

When calling third‑party APIs, rate limits often cause HTTP 429 “Too Many Requests”. By routing each request through a simple request queue, the author eliminated these errors.

Implementation Idea

Each request is wrapped in a RequestParam struct containing the URL, method, parameters and a response channel. The struct is placed into a channel‑based queue; a worker reads from the queue, performs the request using a custom apiclient, and sends the response back through the channel.

package util
import (
    "fmt"

    apiclient "gitee.com/wangyubin/gutils/api_client"
    "gitee.com/wangyubin/gutils/logger"
)
// request 包含的内容
type RequestParam struct {
    Api      string
    Method   string
    JsonReq  interface{}
    Resp     chan []byte
}
// 请求队列, 本质是一个channel
type RequestQueue struct {
    Queue chan RequestParam
}
var queue *RequestQueue
// 获取队列
func GetQueue() *RequestQueue {
    return queue
}
// 初始化队列
func InitRequestQueue(size int) {
    queue = &RequestQueue{
        Queue: make(chan RequestParam, size),
    }
}

// 将请求放入队列
func (rq *RequestQueue) Enqueue(p RequestParam) {
    rq.Queue <- p
}

// 请求队列服务, 一直等待接受和处理请求
func (rq *RequestQueue) Run() {
    lg := logger.GetLogger()
    for p := range rq.Queue {
        var resp []byte
        var err error
        switch p.Method {
        case "GET":
            resp, err = apiclient.GetJson(p.Api, p.JsonReq)
        case "POST":
            resp, err = apiclient.PostJson(p.Api, p.JsonReq)
        default:
            err = fmt.Errorf("Wrong type of METHOD(%s)
", p.Method)
        }
        if err != nil {
            lg.Err(err).Msg("access api error: " + p.Api)
            continue
        }
        if p.Resp != nil {
            p.Resp <- resp
            close(p.Resp)
        }

    }
    lg.Info().Msg("request queue finished!")
}

The queue can be initialized with a desired size, started in a separate goroutine, and optionally throttled by adding a time.Sleep inside the processing loop.

func (rq *RequestQueue) Run() {
    lg := logger.GetLogger()
    for p := range rq.Queue {
        time.Sleep(1 * time.Second)
        // ... omitted code ...
    }

    lg.Info().Msg("request queue finished!")
}

Usage

Initialize the queue (e.g., util.InitRequestQueue(100)), obtain it via util.GetQueue(), and start the service with go queue.Run(). To make a request, create a RequestParam, enqueue it, and read the response from its channel.

func main() {
    // init request queue and start queue service
    util.InitRequestQueue(100)
    queue := util.GetQueue()
    defer close(queue.Queue)
    go queue.Run()

    // other startup code
}
func Request(param1 string, param2 int) error {
    api := "http://xxxx.com"
    api = fmt.Sprintf("%s?period=%s&size=%d", api, param1, param2)

    queue := util.GetQueue()
    param := util.RequestParam{
        Api:    api,
        Method: "GET",
        Resp:   make(chan []byte, 1),
    }
    queue.Enqueue(param)

    var respData struct {
        Status string `json:"status"`
        Data   []model.Data `json:"data"`
    }
    var err error
    for resp := range param.Resp {
        err = json.Unmarshal(resp, &respData)
        if err != nil {
            lg.Err(err).Msg("unmarshal json error")
            return err
        }
    }

    fmt.Println(respData)
    return err
}

If the processing speed is still too high, a delay can be added inside Run() to further respect API rate limits.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

concurrencyGoAPIRequest Queue
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.