-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.go
239 lines (220 loc) · 5.9 KB
/
client.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
package s3
import (
"context"
"fmt"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/kubemq-hub/builder/connector/common"
"github.com/kubemq-io/kubemq-sources/config"
"github.com/kubemq-io/kubemq-sources/middleware"
"github.com/kubemq-io/kubemq-sources/pkg/logger"
)
type Client struct {
log *logger.Logger
opts options
client *s3.S3
downloader *s3manager.Downloader
waiting sync.Map
inProgress sync.Map
completed sync.Map
sendCh chan *SourceFile
ctx context.Context
cancelFunc context.CancelFunc
scanFolders map[string]string
}
func New() *Client {
return &Client{
waiting: sync.Map{},
inProgress: sync.Map{},
completed: sync.Map{},
scanFolders: map[string]string{},
}
}
func (c *Client) Connector() *common.Connector {
return Connector()
}
func (c *Client) Init(ctx context.Context, cfg config.Spec, log *logger.Logger) error {
c.log = log
if c.log == nil {
c.log = logger.NewLogger(cfg.Kind)
}
var err error
c.opts, err = parseOptions(cfg)
if err != nil {
return err
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(c.opts.region),
Credentials: credentials.NewStaticCredentials(c.opts.awsKey, c.opts.awsSecretKey, c.opts.token),
})
if err != nil {
return err
}
svc := s3.New(sess)
c.client = svc
c.downloader = s3manager.NewDownloader(sess)
for _, folder := range c.opts.folders {
unixFolder := unixNormalize(folder)
c.scanFolders[unixFolder] = unixFolder
}
buckets, err := c.client.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
return err
}
for _, bucket := range buckets.Buckets {
if *bucket.Name == c.opts.bucketName {
return nil
}
}
return fmt.Errorf("scanned bucket name not found")
}
func (c *Client) Start(ctx context.Context, target middleware.Middleware) error {
c.ctx, c.cancelFunc = context.WithCancel(ctx)
c.sendCh = make(chan *SourceFile)
go c.scan(c.ctx)
for i := 0; i < c.opts.concurrency; i++ {
go c.senderFunc(c.ctx, target)
}
go c.send(c.ctx)
return nil
}
func (c *Client) inPipe(ctx context.Context, file *SourceFile) bool {
if _, ok := c.waiting.Load(file.FullPath()); ok {
return true
}
if _, ok := c.inProgress.Load(file.FullPath()); ok {
return true
}
if val, ok := c.completed.Load(file.FullPath()); ok {
current := val.(*SourceFile)
if current.Hash() == file.Hash() {
c.log.Infof("file %s already sent and will be deleted", file.FullPath())
if err := file.Delete(ctx); err != nil {
c.log.Errorf("error during delete a file %s,%s, will try again", file.FullPath(), err.Error())
}
return true
} else {
c.log.Infof("file %s already sent but a new content has been detected, resending", file.FullPath())
}
}
return false
}
func (c *Client) walk(ctx context.Context) error {
var list []*SourceFile
objList, err := c.client.ListObjectsV2WithContext(ctx, &s3.ListObjectsV2Input{Bucket: aws.String(c.opts.bucketName)})
if err != nil {
return err
}
var objects []*s3.Object
objects = append(objects, objList.Contents...)
for _, object := range objects {
srcFile := NewSourceFile(c.client, c.downloader, c.opts.bucketName, object)
_, ok := c.scanFolders["/"]
if ok {
list = append(list, srcFile)
continue
}
_, ok = c.scanFolders[srcFile.RootDir()]
if ok {
list = append(list, srcFile)
}
}
added := 0
for _, file := range list {
if !c.inPipe(ctx, file) {
c.waiting.Store(file.FullPath(), file)
added++
}
}
if added > 0 {
c.log.Debugf("%d new files added to sending waiting list", added)
}
return nil
}
func (c *Client) senderFunc(ctx context.Context, sender middleware.Middleware) {
for {
select {
case file := <-c.sendCh:
c.inProgress.Store(file.FullPath(), file)
c.waiting.Delete(file.FullPath())
req, err := file.Request(ctx, c.opts.targetType, c.opts.bucketName)
if err != nil {
c.log.Errorf("error during creating file requests %s, %s", file.FullPath(), err.Error())
c.waiting.Store(file.FullPath(), file)
c.inProgress.Delete(file.FullPath())
continue
}
c.log.Infof("sending %s started ", file.Metadata())
resp, err := sender.Do(ctx, req)
if err != nil {
c.log.Errorf("error during sending file %s, %s", file.FileName(), err.Error())
c.waiting.Store(file.FullPath(), file)
c.inProgress.Delete(file.FullPath())
continue
}
if resp.IsError {
c.log.Errorf("error on sending file %s response, %s", file.FileName(), resp.Error)
c.waiting.Store(file.FullPath(), file)
c.inProgress.Delete(file.FullPath())
continue
}
if err := file.Do(ctx); err != nil {
c.log.Errorf("error during delete/moving a file %s, %s,file will be resend", file.FileName(), err.Error())
c.waiting.Store(file.FullPath(), file)
} else {
c.completed.Store(file.FullPath(), file)
}
c.inProgress.Delete(file.FullPath())
c.log.Infof("sending %s completed: ", file.Metadata())
case <-ctx.Done():
return
}
}
}
func (c *Client) scan(ctx context.Context) {
for {
select {
case <-time.After(time.Duration(c.opts.scanInterval) * time.Second):
err := c.walk(ctx)
if err != nil {
c.log.Errorf("error during scan files in bucket %s, %s", c.opts.bucketName, err.Error())
}
case <-ctx.Done():
return
}
}
}
func (c *Client) send(ctx context.Context) {
for {
select {
case <-time.After(time.Second):
var list []*SourceFile
c.waiting.Range(func(key, value interface{}) bool {
list = append(list, value.(*SourceFile))
return true
})
for _, file := range list {
if _, ok := c.inProgress.Load(file.FullPath()); ok {
continue
}
select {
case c.sendCh <- file:
case <-ctx.Done():
return
}
}
case <-ctx.Done():
return
}
}
}
func (c *Client) Stop() error {
c.cancelFunc()
c.waiting = sync.Map{}
return nil
}