forked from dashjay/baize
/
main.go
140 lines (127 loc) · 3.58 KB
/
main.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
package main
import (
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/btwiuse/baize/pkg/utils/healthchecker"
repb "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/btwiuse/baize/pkg/caches"
"github.com/btwiuse/baize/pkg/config"
"github.com/btwiuse/baize/pkg/interfaces"
rc "github.com/btwiuse/baize/pkg/utils/remotecacheutils"
)
const (
ac = "ac"
cas = "cas"
)
func readFromCache(w http.ResponseWriter, r *http.Request, d *repb.Digest, cache interfaces.Cache) {
w.Header().Set("Content-Encoding", "gzip")
c, err := cache.Reader(r.Context(), d, 0)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
defer c.Close()
_, err = io.Copy(w, c)
if err != nil {
cache.Delete(r.Context(), d)
_, _ = w.Write([]byte(fmt.Sprintf("copy body error: %s", err)))
w.WriteHeader(http.StatusInternalServerError)
}
}
func read(w http.ResponseWriter, r *http.Request, cache interfaces.Cache) {
cacheAction := rc.Parse(r.RequestURI)
if cacheAction.CacheType == cas {
isoCache, _ := cache.WithIsolation(r.Context(), interfaces.CASCacheType, cacheAction.InstanceName)
readFromCache(w, r, &repb.Digest{Hash: cacheAction.Digest}, isoCache)
}
if cacheAction.CacheType == ac {
acCache, _ := cache.WithIsolation(r.Context(), interfaces.ActionCacheType, cacheAction.InstanceName)
readFromCache(w, r, &repb.Digest{Hash: cacheAction.Digest}, acCache)
}
}
func writeToCache(w http.ResponseWriter, r *http.Request, d *repb.Digest, cache interfaces.Cache) {
var err error
defer func() {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("writeToCache error: %s", err)))
cache.Delete(r.Context(), d)
}
}()
wc, err := cache.Writer(r.Context(), d)
if err != nil {
return
}
gw := gzip.NewWriter(wc)
_, err = io.Copy(gw, r.Body)
if err != nil {
return
}
err = gw.Close()
if err != nil {
return
}
err = wc.Close()
if err != nil {
return
}
}
func write(w http.ResponseWriter, r *http.Request, cache interfaces.Cache) {
cacheAction := rc.Parse(r.RequestURI)
if cacheAction.CacheType == cas {
isoCache, _ := cache.WithIsolation(r.Context(), interfaces.CASCacheType, cacheAction.InstanceName)
writeToCache(w, r, &repb.Digest{Hash: cacheAction.Digest}, isoCache)
}
if cacheAction.CacheType == ac {
acCache, _ := cache.WithIsolation(r.Context(), interfaces.ActionCacheType, cacheAction.InstanceName)
writeToCache(w, r, &repb.Digest{Hash: cacheAction.Digest}, acCache)
}
}
type Handler struct {
cache interfaces.Cache
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
read(w, r, h.cache)
}
if r.Method == http.MethodPut {
write(w, r, h.cache)
}
}
func NewRemoteCacheCommand() *cobra.Command {
cmd := &cobra.Command{}
cfgPath := cmd.Flags().String("config", "/config.toml", "config file to use")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
cfg, err := config.NewConfigFromFile(*cfgPath)
if err != nil {
return err
}
l, err := logrus.ParseLevel(cfg.GetDebugConfig().LogLevel)
if err != nil {
return err
}
logrus.SetLevel(l)
cacheCfg := cfg.GetCacheConfig()
cache := caches.GenerateCacheFromConfig(cacheCfg)
if cache == nil {
return errors.New("no cache enabled")
}
hc := healthchecker.NewHealthchecker()
hc.AddChecker(cache.Check, time.Second*60)
hc.Start()
return http.ListenAndServe(cacheCfg.ListenAddr, &Handler{cache: cache})
}
return cmd
}
func main() {
err := NewRemoteCacheCommand().Execute()
if err != nil {
panic(err)
}
}