-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_http_post_file.go
158 lines (129 loc) · 2.86 KB
/
worker_http_post_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/buger/jsonparser"
)
var inputParamsUrl string
const (
ErrCreateFormFile = ErrCategoryHttpRequest + 1
ErrOpenFile = ErrCategoryHttpRequest + 2
ErrCopyFile = ErrCategoryHttpRequest + 3
ErrCloseFile = ErrCategoryHttpRequest + 4
ErrCloseWriter = ErrCategoryHttpRequest + 5
ErrCreateHttpRequest = ErrCategoryHttpRequest + 6
ErrJsonParse = ErrCategoryJson + 1
)
func postFile(b *bytes.Buffer, fid int) Result {
r := Result{Fid: fid}
b.Reset()
w := multipart.NewWriter(b)
fileName := fmt.Sprintf("%d", fid)
formFile, err := w.CreateFormFile("file", fileName)
if err != nil {
r.Ret = ErrCreateFormFile
r.Err = err
return r
}
fp := filepath.Join(PathFiles, fileName)
f, err := os.Open(fp)
if err != nil {
r.Ret = ErrOpenFile
r.Err = err
return r
}
_, err = io.Copy(formFile, f)
if err != nil {
r.Ret = ErrCopyFile
r.Err = err
return r
}
err = f.Close()
if err != nil {
r.Ret = ErrCloseFile
r.Err = err
return r
}
err = w.Close()
if err != nil {
r.Ret = ErrCloseWriter
r.Err = err
return r
}
req, err := http.NewRequest(http.MethodPost, baseUrl()+inputParamsUrl, b)
if err != nil {
r.Ret = ErrCreateHttpRequest
r.Err = err
return r
}
req.Header.Add("Content-Type", w.FormDataContentType())
r = doHttpRequest(req, false)
if r.Ret == 0 {
//r.Cid, r.Err = jsonparser.GetString([]byte(r.Resp), "cid")
//if r.Err != nil {
// r.Ret = ErrJsonParse
//}
_, r.Err = jsonparser.ArrayEach([]byte(r.Resp), func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
r.Cid, r.Err = jsonparser.GetString(value, "cid")
})
if r.Err != nil {
r.Ret = ErrJsonParse
}
}
r.Fid = fid
return r
}
func postFileWithRetry(b *bytes.Buffer, fid int) Result {
retry := 0
var r Result
for retry < p.MaxRetry {
retry++
r = postFile(b, fid)
if r.Ret == 0 {
return r
}
logger.Debug(fmt.Sprintf("fid:%d, ret:%d, resp:%s, retry:%d, err:%s", r.Fid, r.Ret, r.Resp, retry, r.Err.Error()))
time.Sleep(time.Second * 2 * time.Duration(retry))
}
return r
}
func postFiles(input ClusterAddInput) error {
var countResultsWg sync.WaitGroup
countResultsWg.Add(1)
go countResults(&countResultsWg)
inputParamsUrl = input.paramsUrl()
chFids := make(chan int, 10000)
go func() {
for i := input.From; i < input.To; i++ {
chFids <- i
}
close(chFids)
}()
var wg sync.WaitGroup
wg.Add(p.Goroutines)
for i := 0; i < p.Goroutines; i++ {
go func() {
defer wg.Done()
fileBuffer := make([]byte, 0, 1024*1024*fileBufferSize)
buffer := bytes.NewBuffer(fileBuffer)
for {
fid, ok := <-chFids
if !ok {
return
}
chResults <- postFileWithRetry(buffer, fid)
}
}()
}
wg.Wait()
close(chResults)
countResultsWg.Wait()
return nil
}