1
1
// Copyright (c) Tailscale Inc & AUTHORS
2
2
// SPDX-License-Identifier: BSD-3-Clause
3
3
4
- // Package s3proxy implements components of a Go module proxy that caches files
5
- // locally on disk, backed by objects in an S3 bucket.
6
- package s3proxy
4
+ // Package modproxy implements components of a Go module proxy that caches
5
+ // files locally on disk, backed by objects in an S3 bucket.
6
+ package modproxy
7
7
8
8
import (
9
9
"bytes"
@@ -28,10 +28,10 @@ import (
28
28
"golang.org/x/sync/semaphore"
29
29
)
30
30
31
- var _ goproxy.Cacher = (* Cacher )(nil )
31
+ var _ goproxy.Cacher = (* S3Cacher )(nil )
32
32
33
- // Cacher implements the [github.com/goproxy/goproxy.Cacher] interface using a
34
- // local disk cache backed by an S3 bucket.
33
+ // S3Cacher implements the [github.com/goproxy/goproxy.Cacher] interface using
34
+ // a local disk cache backed by an S3 bucket.
35
35
//
36
36
// # Cache Layout
37
37
//
@@ -48,7 +48,7 @@ var _ goproxy.Cacher = (*Cacher)(nil)
48
48
// the specified key prefix instead:
49
49
//
50
50
// <key-prefix>/module/16/0db4d719252162c87a9169e26deda33d2340770d0d540fd4c580c55008b2d6
51
- type Cacher struct {
51
+ type S3Cacher struct {
52
52
// Local is the path of a local cache directory where modules are cached.
53
53
// It must be non-empty.
54
54
Local string
@@ -114,7 +114,7 @@ type Cacher struct {
114
114
putS3Bytes expvar.Int // put: total bytes written to S3
115
115
}
116
116
117
- func (c * Cacher ) init () {
117
+ func (c * S3Cacher ) init () {
118
118
c .initOnce .Do (func () {
119
119
nt := c .MaxTasks
120
120
if nt <= 0 {
@@ -127,7 +127,7 @@ func (c *Cacher) init() {
127
127
128
128
// Get implements a method of the goproxy.Cacher interface. It reports cache
129
129
// hits out of the local directory if available, or faults in from S3.
130
- func (c * Cacher ) Get (ctx context.Context , name string ) (_ io.ReadCloser , oerr error ) {
130
+ func (c * S3Cacher ) Get (ctx context.Context , name string ) (_ io.ReadCloser , oerr error ) {
131
131
c .init ()
132
132
c .getRequest .Add (1 )
133
133
start := time .Now ()
@@ -179,7 +179,7 @@ func (c *Cacher) Get(ctx context.Context, name string) (_ io.ReadCloser, oerr er
179
179
180
180
// putLocal reports whether the specified path already exists in the local
181
181
// cache, and if not, writes data atomically into the path.
182
- func (c * Cacher ) putLocal (ctx context.Context , name , path string , data io.Reader ) (bool , error ) {
182
+ func (c * S3Cacher ) putLocal (ctx context.Context , name , path string , data io.Reader ) (bool , error ) {
183
183
if _ , err := os .Stat (path ); err == nil {
184
184
return true , nil
185
185
}
@@ -193,7 +193,7 @@ func (c *Cacher) putLocal(ctx context.Context, name, path string, data io.Reader
193
193
194
194
// Put implements a method of the goproxy.Cacher interface. It stores data into
195
195
// the local directory and then writes it back to S3 in the background.
196
- func (c * Cacher ) Put (ctx context.Context , name string , data io.ReadSeeker ) (oerr error ) {
196
+ func (c * S3Cacher ) Put (ctx context.Context , name string , data io.ReadSeeker ) (oerr error ) {
197
197
c .init ()
198
198
c .putRequest .Add (1 )
199
199
start := time .Now ()
@@ -240,14 +240,14 @@ func (c *Cacher) Put(ctx context.Context, name string, data io.ReadSeeker) (oerr
240
240
}
241
241
242
242
// Close waits until all background updates are complete.
243
- func (c * Cacher ) Close () error {
243
+ func (c * S3Cacher ) Close () error {
244
244
c .init ()
245
245
return c .tasks .Wait ()
246
246
}
247
247
248
248
// Metrics returns a map of cacher metrics. The caller is responsible for
249
249
// publishing these metrics.
250
- func (c * Cacher ) Metrics () * expvar.Map {
250
+ func (c * S3Cacher ) Metrics () * expvar.Map {
251
251
m := new (expvar.Map )
252
252
m .Set ("path_error" , & c .pathError )
253
253
m .Set ("get_request" , & c .getRequest )
@@ -273,13 +273,13 @@ func hashName(name string) string {
273
273
274
274
// makeKey assembles a complete S3 key from the specified parts, including the
275
275
// key prefix if one is defined.
276
- func (c * Cacher ) makeKey (hash string ) string {
276
+ func (c * S3Cacher ) makeKey (hash string ) string {
277
277
return path .Join (c .KeyPrefix , hash [:2 ], hash )
278
278
}
279
279
280
280
// makePath assembles a complete local cache path for the given name, creating
281
281
// the enclosing directory if needed.
282
- func (c * Cacher ) makePath (name string ) (hash , path string , err error ) {
282
+ func (c * S3Cacher ) makePath (name string ) (hash , path string , err error ) {
283
283
hash = hashName (name )
284
284
path = filepath .Join (c .Local , hash [:2 ], hash )
285
285
err = os .MkdirAll (filepath .Dir (path ), 0700 )
@@ -289,13 +289,13 @@ func (c *Cacher) makePath(name string) (hash, path string, err error) {
289
289
return hash , path , err
290
290
}
291
291
292
- func (c * Cacher ) logf (msg string , args ... any ) {
292
+ func (c * S3Cacher ) logf (msg string , args ... any ) {
293
293
if c .Logf != nil {
294
294
c .Logf (msg , args ... )
295
295
}
296
296
}
297
297
298
- func (c * Cacher ) vlogf (msg string , args ... any ) {
298
+ func (c * S3Cacher ) vlogf (msg string , args ... any ) {
299
299
if c .LogRequests {
300
300
c .logf (msg , args ... )
301
301
}
0 commit comments