/*
* @Author: xiangcai
* @Date: 2021-11-02 19:52:51
* @LastEditors: xiangcai
* @LastEditTime: 2021-11-03 15:40:23
* @Description: file content
*/
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"path"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/sgs921107/gcommon"
"github.com/sgs921107/glogging"
)
var (
zapLogging = glogging.NewZapLogging(glogging.Options{})
zapLogger = zapLogging.GetSugaredLogger()
barInterval = time.Millisecond * 100
)
type Headers map[string]string
type ConcurrentDownloader struct {
// 请求头
headers Headers
// 并发数
Concurrent int
client http.Client
// 下载链接
URI string
filePath string
logger *glogging.ZapSugaredLogger
wg *sync.WaitGroup
downloadedSize int64
size int64
exit bool
}
func (c ConcurrentDownloader) SendReq(
method string,
body io.Reader,
headers *Headers,
) (*http.Response, error) {
request, err := http.NewRequest(method, c.URI, body)
for key, value := range c.headers {
request.Header.Add(key, value)
}
if headers != nil {
for key, value := range *headers {
request.Header.Add(key, value)
}
}
if err != nil {
c.logger.Errorw("Create Request Failed",
"errMsg", err.Error(),
)
return nil, err
}
return c.client.Do(request)
}
func (c *ConcurrentDownloader) SubDownload(num, start, end int) error {
defer c.wg.Done()
c.log
基于go实现并发下载工具
最新推荐文章于 2022-02-24 22:57:57 发布