Home >Common Problem >How to limit the number of requests in go language

How to limit the number of requests in go language

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-06-09 13:13:101232browse

The method for limiting the number of requests in the Go language is: 1. Create a Go sample file; 2. Import the required packages and set the maximum number of simultaneous requests "concurrency"; 3. Limit the maximum number of concurrency through the sem channel. Block other requests, the syntax is "sem := make(chan struct{}, concurrency)" until an idle channel is available.

How to limit the number of requests in go language

Operating system for this tutorial: Windows 10 system, Go1.20.1 version, Dell G3 computer.

Go language provides a data type called channel channel. You can use this channel to control the number of requests.

The following is some sample code, using a channel with cache, setting the maximum number of requests as capacity, and blocking each time a request is sent until there is free channel capacity before continuing to request:

package main
import (
    "fmt"
    "net/http"
)
func main() {
    urls := []string{
        "https://www.google.com",
        "https://www.facebook.com",
        "https://www.airbnb.com",
        "https://www.github.com",
        "https://www.twitter.com",
        "https://www.linkedin.com",
        "https://www.youtube.com",
        "https://www.microsoft.com",
        "https://www.reddit.com",
        "https://www.stackoverflow.com",
    }
    concurrency := 3 // 最大同时请求数量
    sem := make(chan struct{}, concurrency)
    for _, url := range urls {
        sem <- struct{}{}
        go func(url string) {
            request(url)
            <-sem
        }(url)
    }
    for i := 0; i < concurrency; i++ {
        sem <- struct{}{}
    }
}
func request(url string) {
    res, err := http.Get(url)
    if err != nil {
        fmt.Printf("%s is down\n", url)
        return
    }
    defer res.Body.Close()
    fmt.Printf("%s -> status code: %d \n", url, res.StatusCode)
}

In the above example, we started 10 HTTP requests, but only 3 requests occurred at the same time because we set the maximum number of concurrencies in the sem channel. This way, when we reach the concurrency limit, all other requests will be blocked (sem <- struct {}{}) until a free channel is available.

The above is the detailed content of How to limit the number of requests in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn