generated from bep/golibtemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
219 lines (178 loc) · 4.8 KB
/
server.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
package s3rpc
import (
"context"
"errors"
"fmt"
"os"
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"golang.org/x/sync/errgroup"
)
// NewServer creates a new server.
func NewServer(opts ServerOptions) (*Server, error) {
if err := opts.init(); err != nil {
return nil, err
}
awsCfg := aws.Config{
Region: opts.Region,
Credentials: credentials.NewStaticCredentialsProvider(opts.AccessKeyID, opts.SecretAccessKey, ""),
}
if opts.PollInterval == 0 {
opts.PollInterval = 10 * time.Second
}
if opts.Infof == nil {
opts.Infof = func(format string, args ...interface{}) {
fmt.Println("server: " + fmt.Sprintf(format, args...))
}
}
tempDir, err := os.MkdirTemp("", "s3rpc_server")
if err != nil {
return nil, err
}
return &Server{
handlers: opts.Handlers,
pollIntervall: opts.PollInterval,
quit: make(chan struct{}),
common: &common{
bucket: opts.Bucket,
queue: opts.Queue,
s3Client: s3.NewFromConfig(awsCfg),
sqsClient: sqs.NewFromConfig(awsCfg),
tempDir: tempDir,
infof: opts.Infof,
},
}, nil
}
// Output is the result of a handler invocation.
type Output struct {
Filename string
Metadata map[string]string
}
// Input is the input to a handler invocation.
type Input struct {
Filename string
Metadata map[string]string
}
// Handlers is a map of operation names to handler functions.
type Handlers map[string]func(ctx context.Context, input Input) (Output, error)
// Server is a server that processes files from an S3 bucket.
type Server struct {
handlers Handlers
pollIntervall time.Duration
quit chan struct{}
*common
}
// Close closes the server.
func (s *Server) Close() error {
var err error
s.closeOnce.Do(func() {
close(s.quit)
err = os.RemoveAll(s.tempDir)
})
return err
}
// ListenAndServe listens for messages and processes them.
// It blocks until the server is closed.
func (s *Server) ListenAndServe(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
for {
select {
case <-s.quit:
s.infof("Closed")
return nil
case <-ctx.Done():
return nil
default:
s.infof("Checking queue %q for new messages", s.queue)
ms, err := s.Receive(ctx)
if err != nil {
return err
}
for _, m := range ms {
if m.Bucket != s.bucket {
return fmt.Errorf("expected bucket %q, got %q", s.bucket, m.Bucket)
}
s.infof("Got message with key %q", m.Key)
op := strings.Split(m.Key, "/")[1]
handle := s.handlers[op]
if handle == nil {
if err := s.releaseMessage(ctx, m.ReceiptHandle); err != nil {
return err
}
continue
}
// We have a handler for this operation, so we can process the file.
// Delete the message from the queue before the visibility timeout expires.
if err := s.deleteMessage(ctx, m.ReceiptHandle); err != nil {
return err
}
baseKey := path.Base(m.Key)
err = func() error {
f, err := os.CreateTemp(s.tempDir, "*_"+baseKey)
if err != nil {
return fmt.Errorf("tempfile: %w", err)
}
defer f.Close()
defer os.Remove(f.Name())
metaData, err := s.getObject(ctx, f, m.Key)
if err != nil {
return err
}
result, err := handle(ctx, Input{Filename: f.Name(), Metadata: metaData})
if err != nil {
return fmt.Errorf("handle: %w", err)
}
// The client uses an UUID in the base name of the file to identify the
// message in the output quueue, so we need to preserve that.
// With that, we also know that it's unique.
key := toClient + "/" + op + "/" + baseKey
if err := s.upload(result.Filename, key, result.Metadata); err != nil {
return err
}
return err
}()
if err != nil {
return err
}
}
time.Sleep(s.pollIntervall)
}
}
})
return g.Wait()
}
// ServerOptions are options for the server.
type ServerOptions struct {
// Handlers maps an operation to a handler.
// The operation is also the first path segment below in/out in the bucket.
Handlers Handlers
// The in queue to poll for new messages.
Queue string
// PollInterval is the interval between polling for new messages.
PollInterval time.Duration
// Infof logs info messages.
Infof func(format string, args ...interface{})
// The AWS config.
AWSConfig
}
func (opts *ServerOptions) init() error {
if opts.Region == "" {
opts.Region = defaultRegion
}
if opts.AccessKeyID == "" {
return errors.New("access key id is required")
}
if opts.SecretAccessKey == "" {
return errors.New("secret access key is required")
}
if opts.Queue == "" {
return fmt.Errorf("queue is required")
}
return nil
}