-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
137 lines (115 loc) · 2.82 KB
/
main.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
package main
import (
"config"
"flag"
"fmt"
"localstg"
"log"
"qiniustg"
"runtime"
"sync"
"utils"
)
type procFunc func(recordsCh, retCh chan string, cfg config.Config)
var procFuncs = map[string]procFunc{
"req": qiniustg.HttpReq,
"download": qiniustg.Download,
"bucket_list": localstg.BucketList,
"adl": qiniustg.Adl,
"chstatus": batchFunc(qiniustg.ChangeFileStatus),
"chtype": batchFunc(qiniustg.ChangeFileType),
"async_fetch": qiniustg.AsyncFetch,
"md5": qiniustg.Md5,
"getHttpCode": qiniustg.GetHttpCode,
"getPrivateUrl": qiniustg.GetPrivateUrl,
}
func usage() {
fmt.Println(
`
./qlist -cfg_path cfg.json req
./qlist -cfg_path cfg.json download
./qlist -cfg_path cfg.json bucket_list
./qlist -cfg_path cfg.json chstatus
./qlist -cfg_path cfg.json chtype
./qlist -cfg_path cfg.json async_fetch
./qlist -cfg_path cfg.json md5
./qlist -cfg_path cfg.json getHttpCode
./qlist -cfg_path cfg.json getPrivateUrl
`)
}
func main() {
cfgPath := flag.String("cfg_path", "", "")
flag.Parse()
if *cfgPath == "" {
usage()
return
}
funcName := flag.Arg(0)
procFunc, ok := procFuncs[funcName]
if !ok {
fmt.Printf("No %s Function", funcName)
return
}
cfg, err := config.LoadConfig(*cfgPath)
if err != nil {
panic(err)
}
cli := QNClient{cfg}
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
//channels to cache records and proc result
recordsCh := make(chan string, 1000*100)
procResultCh := make(chan string, 1000*10)
if cfg.IsQiniuSrc() {
//list records qiniu storage
go qiniustg.NewQNClient(cfg).List2(recordsCh)
} else {
//list records local file
go localstg.NewQNClient(cfg).List(recordsCh)
}
//proc records
go cli.work(recordsCh, procResultCh, procFunc)
//log proc result
procResultLogWait := make(chan bool)
go utils.Log(procResultCh, cli.ProcResultsPath, procResultLogWait)
log.Println("Proc Result in file: ", cli.ProcResultsPath)
<-procResultLogWait
log.Println("List and Proc done!")
}
type QNClient struct {
config.Config
}
func (c *QNClient) work(recordsCh, retCh chan string, proc procFunc) {
wg := sync.WaitGroup{}
if c.WorkerCount == 0 {
c.WorkerCount = 10
}
wg.Add(c.WorkerCount)
for i := 0; i < c.WorkerCount; i++ {
go func(wg *sync.WaitGroup) {
defer wg.Done()
proc(recordsCh, retCh, c.Config)
}(&wg)
}
wg.Wait()
close(retCh)
return
}
func batchFunc(batch func(retCh chan string, records []string, cfg config.Config)) func(recordsCh, retCh chan string, cfg config.Config) {
return func(recordsCh, retCh chan string, cfg config.Config) {
records := []string{}
for {
record, ok := <-recordsCh
if ok {
records = append(records, record)
if len(records) < 1000 {
continue
}
}
if len(records) == 0 {
break
}
batch(retCh, records, cfg)
records = []string{}
}
}
}