refactor(api): 优化抖音API请求重试机制和客户端配置
构建上传工具 / build (push) Successful in 1m12s

- 实现了抖音API请求失败时的自动重试逻辑,间隔100毫秒
- 添加了上下文取消支持,避免无限重试
- 将HTTP客户端改为全局变量,提升性能和连接复用
- 前端上传线程数限制从100调整为500
- 更新类型声明以符合最新Go语言规范
This commit is contained in:
2026-07-27 00:41:14 +08:00
parent 8889e9ab55
commit 2c0207c45f
3 changed files with 18 additions and 5 deletions
+1 -1
View File
@@ -200,7 +200,7 @@ try {
<div class="form-item"> <div class="form-item">
<label>单文件上传线程</label> <label>单文件上传线程</label>
<el-input-number v-model="uploadThreads" :min="1" :max="100" :disabled="isRunning" <el-input-number v-model="uploadThreads" :min="1" :max="500" :disabled="isRunning"
@change="writeUploadThreads()"/> @change="writeUploadThreads()"/>
</div> </div>
+15 -2
View File
@@ -64,7 +64,19 @@ func UploadDataToServer(ctx context.Context, data string) error {
secUserId := strings.Split(data, "----")[1] secUserId := strings.Split(data, "----")[1]
hasVideo, err := CheckHasVideo(secUserId) hasVideo, err := CheckHasVideo(secUserId)
if err != nil { if err != nil {
for err != nil {
fmt.Println("抖音请求api重试")
//定时器定时100毫秒
ticker := time.NewTicker(100 * time.Millisecond)
select {
case <-ctx.Done():
return err return err
case <-ticker.C:
ticker.Stop()
hasVideo, err = CheckHasVideo(secUserId)
}
}
} }
if hasVideo { if hasVideo {
params.Set("token", config.APPConfig.HasVideoToken) params.Set("token", config.APPConfig.HasVideoToken)
@@ -97,8 +109,9 @@ func UploadDataToServer(ctx context.Context, data string) error {
return err return err
} }
var douYinClient = &http.Client{}
func CheckHasVideo(secUserId string) (bool, error) { func CheckHasVideo(secUserId string) (bool, error) {
client := &http.Client{}
req, err := http.NewRequest( req, err := http.NewRequest(
"GET", "GET",
"https://imdesktop.douyin.com/aweme/v1/web/user/profile/other/?sec_user_id="+secUserId, "https://imdesktop.douyin.com/aweme/v1/web/user/profile/other/?sec_user_id="+secUserId,
@@ -114,7 +127,7 @@ func CheckHasVideo(secUserId string) (bool, error) {
req.Header.Add("Host", "imdesktop.douyin.com") req.Header.Add("Host", "imdesktop.douyin.com")
req.Header.Add("Connection", "keep-alive") req.Header.Add("Connection", "keep-alive")
res, err := client.Do(req) res, err := douYinClient.Do(req)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return false, err return false, err
+1 -1
View File
@@ -30,7 +30,7 @@ func main() {
}, },
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1}, BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: app.startup, OnStartup: app.startup,
Bind: []interface{}{ Bind: []any{
app, app,
}, },
}) })