登录
首页 >  Golang >  Go教程

Golang并发HTTP请求实战教程

时间:2025-11-14 12:14:31 178浏览 收藏

从现在开始,努力学习吧!本文《Golang并发HTTP请求处理实战项目》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

使用Goroutine和sync.WaitGroup实现并发HTTP请求,通过带缓冲channel控制最大并发数,结合context.WithTimeout管理超时,自定义http.Transport复用连接以提升性能,构建高效稳定的并发处理程序。

Golang并发HTTP请求处理项目

在Go语言中处理并发HTTP请求是其核心优势之一,得益于Goroutine和高效的网络库。构建一个能高效处理并发HTTP请求的项目,关键在于合理利用标准库、控制并发数量、避免资源竞争,并做好错误处理与超时管理。

使用Goroutine发起并发请求

Go的net/http包配合Goroutine可以轻松实现高并发请求。每个请求在一个独立的Goroutine中执行,主线程不阻塞。

示例:并发获取多个URL内容

package main
<p>import (
"fmt"
"io/ioutil"
"net/http"
"sync"
)</p><p>func fetch(url string, wg *sync.WaitGroup) {
defer wg.Done()
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error fetching %s: %v\n", url, err)
return
}
defer resp.Body.Close()</p><pre class="brush:php;toolbar:false"><code>body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Fetched %d bytes from %s\n", len(body), url)</code>

}

func main() { urls := []string{ "https://httpbin.org/delay/1", "https://httpbin.org/json", "https://httpbin.org/uuid", }

var wg sync.WaitGroup
for _, url := range urls {
    wg.Add(1)
    go fetch(url, &wg)
}
wg.Wait()

}

限制并发数避免系统过载

无限制地启动Goroutine可能导致内存溢出或被目标服务器封禁。使用带缓冲的channel可有效控制最大并发数。

func limitedFetch(urls []string) {
    const maxConcurrency = 5
    sem := make(chan struct{}, maxConcurrency)
    var wg sync.WaitGroup
<pre class="brush:php;toolbar:false"><code>for _, url := range urls {
    wg.Add(1)
    go func(u string) {
        defer wg.Done()
        sem <- struct{}{} // 获取令牌
        defer func() { <-sem }() // 释放令牌

        resp, err := http.Get(u)
        if err != nil {
            fmt.Printf("Failed: %s - %v\n", u, err)
            return
        }
        defer resp.Body.Close()
        fmt.Printf("Success: %s (Status: %d)\n", u, resp.StatusCode)
    }(url)
}
wg.Wait()</code>

}

使用Context控制超时和取消

长时间挂起的请求会浪费资源。通过context.WithTimeout设置请求级超时,提升系统响应性。

func fetchWithTimeout(url string, timeoutSec int) error {
    ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSec)*time.Second)
    defer cancel()
<pre class="brush:php;toolbar:false"><code>req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
    return err
}
defer resp.Body.Close()
return nil</code>

}

复用连接提升性能

频繁创建TCP连接开销大。通过自定义Transport启用长连接并限制空闲连接数,显著提高吞吐量。

client := &http.Client{
    Transport: &http.Transport{
        MaxIdleConns:        100,
        MaxIdleConnsPerHost: 10,
        IdleConnTimeout:     30 * time.Second,
    },
    Timeout: 10 * time.Second,
}

基本上就这些。结合Goroutine、channel、context和优化的HTTP客户端配置,就能构建稳定高效的并发HTTP处理程序。实际项目中还可加入重试机制、日志记录和结果收集等功能。不复杂但容易忽略细节。

以上就是《Golang并发HTTP请求实战教程》的详细内容,更多关于的资料请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>