/
worker.go
183 lines (169 loc) · 6.32 KB
/
worker.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
package client
import (
"io"
"os"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"github.com/sushshring/torrxfer/pkg/common"
"github.com/sushshring/torrxfer/pkg/crypto"
"github.com/sushshring/torrxfer/pkg/net"
)
// ServerTransferJob holds the attributes needed to perform unit of work.
type ServerTransferJob struct {
ID uuid.UUID
Delay time.Duration
ServerConnection *ServerConnection
File *File
TransferNotifications chan ServerNotification
}
// NewServerTransferWorker creates takes a numeric id and a channel w/ worker pool.
func NewServerTransferWorker(id int, workerPool chan chan ServerTransferJob) ServerTransferWorker {
return ServerTransferWorker{
id: id,
jobQueue: make(chan ServerTransferJob),
workerPool: workerPool,
}
}
// ServerTransferWorker performs the work necessary to transfer a file request to a server
type ServerTransferWorker struct {
id int
jobQueue chan ServerTransferJob
workerPool chan chan ServerTransferJob
}
func (w ServerTransferWorker) doFileTransferJob(job ServerTransferJob) {
file := job.File
// Prime the server for the file.
file.TransferTime = time.Now()
log.Trace().Str("File Path", file.Path).Str("Media Prefix", file.MediaPrefix).Str("Job ID", job.ID.String()).Msg("Starting job")
remoteFileInfo, err := job.ServerConnection.rpcConnection.QueryFile(file.Path, file.MediaPrefix, job.ID.String())
if err != nil {
log.Trace().Err(err).Msg("Query file failed")
job.sendConnectionNotification(ConnectionNotificationTypeQueryError, 0, err)
return
}
// File was already fully transmitted
// Verify based on data hash
fileHash, err := crypto.HashFile(file.Path)
if err != nil {
// If hashing local file failed due to an transient error, just check for file size.
// If file size is different, attempt to transfer again.
fileHash = ""
}
if fileHash == remoteFileInfo.GetDataHash() || file.Size == remoteFileInfo.GetRemoteSize() {
func() {
job.ServerConnection.Lock()
defer job.ServerConnection.Unlock()
job.ServerConnection.filesTransferred[file.Path] = file
job.ServerConnection.fileTransferStatus[file] = remoteFileInfo.GetSize()
job.sendConnectionNotification(ConnectionNotificationTypeCompleted, 0)
}()
return
}
// Server connection is primed for this file. Start sending now
bytesReader, bytesWriter := io.Pipe()
offset := remoteFileInfo.GetRemoteSize()
// Open the file for reading
fileOnDisk, err := os.Open(file.Path)
if err != nil {
log.Debug().Err(err).Msg("Failed to open provided file")
return
}
defer fileOnDisk.Close()
// Continue transmission of file from last sent point if the data hash so far matches
if offset > 0 {
currentHash, err := crypto.HashReader(io.LimitReader(fileOnDisk, int64(offset)))
if err != nil {
// If error while generating hash, transfer the full file even though this was a local error
common.LogErrorStack(err, "Could not generate hash of file to remote offset")
currentHash = ""
}
if currentHash == remoteFileInfo.GetDataHash() {
if _, err := fileOnDisk.Seek(int64(offset), 0); err != nil {
// Could not seek to location locally. Transfer full file
offset = 0
// Best effort seek to 0. If failed,
if _, err := fileOnDisk.Seek(0, 0); err != nil {
log.Trace().Err(err).Uint64("offset", offset).Msg("Seek to 0 failed after seek to specific offset failed")
job.sendConnectionNotification(ConnectionNotificationTypeFatalError, 0, err)
}
}
}
} else {
offset = 0
if _, err := fileOnDisk.Seek(0, 0); err != nil {
log.Trace().Err(err).Msg("Seek to 0 failed")
job.sendConnectionNotification(ConnectionNotificationTypeFatalError, 0, err)
}
}
// Setup the file transfer channels. There may be one blocking call but it should be fairly trivial
fileSummaryChan, err := job.ServerConnection.rpcConnection.TransferFile(bytesReader, common.DefaultBlockSize, offset, job.ID.String())
if err != nil {
log.Trace().Err(err).Msg("Transfer file failed")
job.sendConnectionNotification(ConnectionNotificationTypeTransferError, 0, err)
return
}
func() {
job.ServerConnection.Lock()
defer job.ServerConnection.Unlock()
job.ServerConnection.filesTransferred[file.Path] = file
job.ServerConnection.fileTransferStatus[file] = offset
}()
go func(summaryChannel chan net.FileTransferNotification, server *ServerConnection, file *File) {
for summary := range summaryChannel {
switch summary.NotificationType {
case net.TransferNotificationTypeError:
log.Trace().Err(err).Msg("Error during bytes transfer")
job.sendConnectionNotification(ConnectionNotificationTypeTransferError, 0, summary.Error)
continue
case net.TransferNotificationTypeBytes:
func() {
server.Lock()
defer server.Unlock()
server.bytesTransferred += summary.LastTransferred
server.fileTransferStatus[file] += summary.LastTransferred
job.sendConnectionNotification(ConnectionNotificationTypeFilesUpdated, summary.LastTransferred)
}()
case net.TransferNotificationTypeClosed:
job.sendConnectionNotification(ConnectionNotificationTypeCompleted, 0)
}
}
}(fileSummaryChan, job.ServerConnection, file)
func(dataChannel *io.PipeWriter, path string) {
defer dataChannel.Close()
n, err := io.Copy(dataChannel, fileOnDisk)
if err != nil {
common.LogErrorStack(err, "Failed to pipe from file")
job.sendConnectionNotification(ConnectionNotificationTypeTransferError, 0, err)
}
log.Trace().Int64("Written bytes", n).Msg("File transfer complete")
}(bytesWriter, file.Path)
}
func (w ServerTransferJob) sendConnectionNotification(n ConnectionNotificationType, lastBlockSize uint64, err ...error) {
serverNotif := ServerNotification{
NotificationType: n,
Error: nil,
Connection: w.ServerConnection,
SentFile: w.File,
LastSentSize: lastBlockSize,
}
if len(err) != 0 {
serverNotif.Error = err[0]
}
w.TransferNotifications <- serverNotif
}
func (w ServerTransferWorker) start() {
for {
// Add my jobQueue to the worker pool.
w.workerPool <- w.jobQueue
if job, ok := <-w.jobQueue; ok {
// Dispatcher has added a job to my jobQueue.
log.Trace().Int("ID", w.id).Str("Job ID", job.ID.String()).Dur("Delay", job.Delay).Msg("Worker started")
time.Sleep(job.Delay)
w.doFileTransferJob(job)
} else {
// Job queue was closed. Exit
return
}
}
}