-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (123 loc) · 3 KB
/
main.go
File metadata and controls
145 lines (123 loc) · 3 KB
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
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"io"
"log"
"os"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type cmd string
const (
cmdGet = cmd("get")
cmdPut = cmd("put")
cmdClose = cmd("close")
)
type request struct {
ID int64 `json:"ID"`
Command cmd `json:"Command"`
ActionID []byte `json:"ActionID,omitempty"`
OutputID []byte `json:"OutputID,omitempty"`
BodySize int64 `json:"BodySize,omitempty"`
body []byte
}
type response struct {
ID int64 `json:"ID"`
Err string `json:"Err,omitempty"`
KnownCommands []cmd `json:"KnownCommands,omitempty"`
Miss bool `json:"Miss,omitempty"`
OutputID []byte `json:"OutputID,omitempty"`
Size int64 `json:"Size,omitempty"`
Time *time.Time `json:"Time,omitempty"`
DiskPath string `json:"DiskPath,omitempty"`
}
func main() {
log.SetPrefix("gocache-s3: ")
log.SetFlags(0)
bucket := flag.String("bucket", os.Getenv("GOCACHE_S3_BUCKET"), "S3 bucket name")
prefix := flag.String("prefix", os.Getenv("GOCACHE_S3_PREFIX"), "S3 key prefix")
pathStyle := flag.Bool("path-style", os.Getenv("GOCACHE_S3_PATH_STYLE") == "true" || os.Getenv("GOCACHE_S3_PATH_STYLE") == "1", "use path-style S3 addressing")
flag.Parse()
if *bucket == "" {
log.Fatal("bucket is required: set -bucket flag or GOCACHE_S3_BUCKET env var")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
awsCfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatalf("load AWS config: %v", err)
}
client := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.UsePathStyle = *pathStyle
})
tmpDir, err := os.MkdirTemp("", "gocache-s3-*")
if err != nil {
log.Fatalf("create temp dir: %v", err)
}
c := &cache{
client: client,
bucket: *bucket,
prefix: *prefix,
tmpDir: tmpDir,
}
defer c.close()
enc := json.NewEncoder(os.Stdout)
var mu sync.Mutex
send := func(r response) {
mu.Lock()
defer mu.Unlock()
enc.Encode(r)
}
send(response{
ID: 0,
KnownCommands: []cmd{cmdGet, cmdPut, cmdClose},
})
var wg sync.WaitGroup
reader := bufio.NewReaderSize(os.Stdin, 1<<20)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
log.Fatalf("read stdin: %v", err)
}
var req request
if err := json.Unmarshal(line, &req); err != nil {
log.Fatalf("unmarshal request: %v", err)
}
if req.BodySize > 0 {
bodyLine, err := reader.ReadBytes('\n')
if err != nil {
log.Fatalf("read body: %v", err)
}
if err := json.Unmarshal(bodyLine, &req.body); err != nil {
log.Fatalf("unmarshal body: %v", err)
}
}
switch req.Command {
case cmdGet:
wg.Add(1)
go func() {
defer wg.Done()
send(c.get(ctx, req))
}()
case cmdPut:
wg.Add(1)
go func() {
defer wg.Done()
send(c.put(ctx, req))
}()
case cmdClose:
wg.Wait()
send(response{ID: req.ID})
return
}
}
wg.Wait()
}