forked from F4n4t/GoPostStuff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spawner.go
276 lines (237 loc) · 7.01 KB
/
spawner.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"fmt"
"github.com/ERR1R/gopoststuff/simplenntp"
"os"
"path/filepath"
"sync"
"time"
)
type FileData struct {
path string
size int64
}
type Totals struct {
start time.Time
end time.Time
bytes int64
}
func Spawner(filenames []string) {
var wg sync.WaitGroup
files := make([]FileData, 0)
var altnzbpath string
nzbinfo := make(map[string]NzbFile, 0)
segs := make(map[string][]NzbSegment, 0)
log.Debug("Spawner started")
// Walk any directories and collect files
for _, filename := range filenames {
err := filepath.Walk(filename, func(path string, fi os.FileInfo, err error) error {
if !fi.IsDir() && fi.Size() > 0 {
files = append(files, FileData{path: path, size: fi.Size()})
}
return err
})
if err != nil {
log.Fatalf("Spawner walk error: %s", err)
}
}
// Log a message about what we're posting
var totalBytes int64
for _, fd := range files {
totalBytes += int64(fd.size)
}
totalMB := float64(totalBytes) / 1024 / 1024
log.Info("Found %d file(s) totalling %.1fMiB", len(files), totalMB)
// Make a channel to stuff TimeDatas into
tdchan := make(chan *simplenntp.TimeData, 100000)
// Use specified server
serverList := make(map[string]*ConfigServer, len(Config.Server))
if len(*serverFlag) > 0 {
serverList[*serverFlag] = Config.Server[*serverFlag]
} else if len(Config.Global.DefaultServer) > 0 {
serverList[Config.Global.DefaultServer] = Config.Server[Config.Global.DefaultServer]
} else {
serverList = Config.Server
}
// Iterate over configured servers
for name, server := range serverList {
log.Info("[%s] Starting %d connections", name, server.Connections)
// Make a channel to stuff Articles into
achan := make(chan *Article, server.Connections)
// Make a channel to stuff Totals into
tchan := make(chan *Totals, server.Connections)
// Start a goroutine to generate articles
wg.Add(1)
go func(c chan *Article, files []FileData) {
defer wg.Done()
log.Debug("[%s] Article generator started", name)
mc := NewMmapCache()
for filenum, fd := range files {
// Open and mmap the file
md, err := mc.MapFile(fd.path, len(serverList))
if err != nil {
log.Fatalf("MapFile error: %s", err)
}
// Work out how many parts we need
parts := fd.size / Config.Global.ArticleSize
rem := fd.size % Config.Global.ArticleSize
if rem > 0 {
parts++
}
// Build some articles
for partnum := int64(0); partnum < parts; partnum++ {
start := partnum * Config.Global.ArticleSize
end := min((partnum+1)*Config.Global.ArticleSize, fd.size)
ad := &ArticleData{
PartNum: partnum + 1,
PartTotal: parts,
PartSize: end - start,
PartBegin: start,
PartEnd: end,
FileNum: filenum + 1,
FileTotal: len(files),
FileSize: fd.size,
FileName: filepath.Base(fd.path),
}
var subject string
if *dirSubjectFlag {
subject = filepath.Base(filepath.Dir(fd.path))
} else {
subject = *subjectFlag
}
if len(altnzbpath) == 0 {
altnzbpath = SafeFileName(subject)
}
a := NewArticle(md.data[start:end], ad, subject)
c <- a
}
if md.Decrement() {
err = mc.CloseFile(fd.path)
if err != nil {
log.Fatalf("CloseFile error: %s", err)
}
log.Debug("[%s] Closed file %s", name, fd.path)
}
}
close(c)
}(achan, files)
// Start a goroutine for each individual connection
for i := 0; i < server.Connections; i++ {
connID := i + 1
// Increment the WaitGroup counters
wg.Add(1)
go func(achan chan *Article, tchan chan *Totals) {
// Decrement the counter when the goroutine completes
defer wg.Done()
// Connect
log.Debug("[%s:%02d] Connecting...", name, connID)
conn, err := simplenntp.Dial(server.Address, server.Port, server.TLS, server.InsecureSSL, tdchan)
if err != nil {
log.Fatalf("[%s] Error while connecting: %s", name, err)
}
log.Debug("[%s:%02d] Connected", name, connID)
// Authenticate if required
if len(server.Username) > 0 {
log.Debug("[%s:%02d] Authenticating...", name, connID)
err := conn.Authenticate(server.Username, server.Password)
if err != nil {
log.Fatalf("[%s:%02d] Error while authenticating: %s", name, connID, err)
}
log.Debug("[%s:%02d] Authenticated", name, connID)
}
t := &Totals{start: time.Now()}
// Begin consuming
for article := range achan {
err := conn.Post(article.Body, Config.Global.ChunkSize)
if err != nil {
log.Warning("[%s:%02d] Post error: %s", name, connID, err)
} else {
nzbinfo[article.FileName] = article.NzbData
segs[article.FileName] = append(segs[article.FileName], article.Segment)
}
t.bytes += int64(len(article.Body))
}
// Stick our totals struct into the channel
t.end = time.Now()
tchan <- t
// Close the connection
log.Debug("[%s:%02d] Closing connection", name, connID)
err = conn.Quit()
if err != nil {
log.Warning("[%s:%02d] Error while closing connection: %s", name, connID, err)
}
}(achan, tchan)
}
// Start a goroutine to print some stats when done, sigh
wg.Add(1)
go func(tchan chan *Totals) {
defer wg.Done()
minStart := time.Now()
var maxEnd time.Time
var totalBytes int64
for i := 0; i < server.Connections; i++ {
t := <-tchan
if t.start.Before(minStart) {
minStart = t.start
}
if t.end.After(maxEnd) {
maxEnd = t.end
}
totalBytes += t.bytes
}
// Calculate and log the result
dur := maxEnd.Sub(minStart)
speed, speedUnit := prettySize(float64(totalBytes) / dur.Seconds())
totalMB := float64(totalBytes) / 1024 / 1024
log.Info("[%s] Posted %.1fMiB in %s at %.1f%s/s", name, totalMB, dur.String(), speed, speedUnit)
}(tchan)
}
// Start our weird status goroutine
statusTicker := time.NewTicker(time.Second * 1)
go StatusLogger(statusTicker, tdchan)
// Wait for all connections to complete
wg.Wait()
// Generate Nzb
var nzbpath string
if len(*nzbFlag) > 0 {
nzbpath = *nzbFlag
} else if len(Config.Global.DefaultNzb) > 0 {
nzbpath = Config.Global.DefaultNzb
} else {
nzbpath = fmt.Sprintf("gps-%d_%s.nzb", time.Now().Unix(), altnzbpath)
}
if _, err := os.Stat(nzbpath); err == nil {
log.Warning("Nzbfile already exists: %s", nzbpath)
nzbpath = fmt.Sprintf("gps-%d_%s.nzb", time.Now().Unix(), altnzbpath)
log.Info("Using alternative filename: %s", nzbpath)
}
nzb := Nzb{}
// Add some metadata
if len(*nzbMetaPass) > 0 {
nzb.Head = append(nzb.Head, Meta{Type: "password", Value: *nzbMetaPass})
}
for i, _ := range nzbinfo {
n := nzbinfo[i]
nzb.File = append(nzb.File, NzbFile{
Poster: n.Poster,
Date: n.Date,
Subject: n.Subject,
Groups: n.Groups,
Segments: segs[i],
})
}
err := CreateNzb(nzbpath, &nzb)
if err != nil {
log.Warning("Error while creating Nzb: %s", err)
}
log.Info("Generated Nzb file: %s", nzbpath)
statusTicker.Stop()
}
// math.Min wants float64s, zzz
func min(a, b int64) int64 {
if a < b {
return a
} else {
return b
}
}