@@ -5,6 +5,7 @@ package cacher
5
5
6
6
import (
7
7
"bytes"
8
+ "context"
8
9
"io/ioutil"
9
10
"net/http"
10
11
"net/url"
@@ -18,8 +19,6 @@ import (
18
19
"github.com/cybozu-go/aptutil/apt"
19
20
"github.com/cybozu-go/log"
20
21
"github.com/pkg/errors"
21
- "golang.org/x/net/context"
22
- "golang.org/x/net/context/ctxhttp"
23
22
)
24
23
25
24
const (
@@ -60,15 +59,11 @@ type Cacher struct {
60
59
61
60
// NewCacher constructs Cacher.
62
61
func NewCacher (ctx context.Context , config * Config ) (* Cacher , error ) {
63
- checkInterval := time .Duration (config .CheckInterval ) * time .Second
64
- if checkInterval == 0 {
65
- checkInterval = defaultCheckInterval * time .Second
62
+ if config .CheckInterval == 0 {
63
+ return nil , errors .New ("invaild check_interval" )
66
64
}
67
-
65
+ checkInterval := time . Duration ( config . CheckInterval ) * time . Second
68
66
cachePeriod := time .Duration (config .CachePeriod ) * time .Second
69
- if cachePeriod == 0 {
70
- cachePeriod = defaultCachePeriod * time .Second
71
- }
72
67
73
68
metaDir := filepath .Clean (config .MetaDirectory )
74
69
if ! filepath .IsAbs (metaDir ) {
@@ -84,10 +79,10 @@ func NewCacher(ctx context.Context, config *Config) (*Cacher, error) {
84
79
return nil , errors .New ("meta_dir and cache_dir must be different" )
85
80
}
86
81
87
- capacity := uint64 (config .CacheCapacity ) * gib
88
- if capacity == 0 {
89
- capacity = defaultCacheCapacity * gib
82
+ if config .CacheCapacity <= 0 {
83
+ return nil , errors .New ("cache_capacity must be > 0" )
90
84
}
85
+ capacity := uint64 (config .CacheCapacity ) * gib
91
86
92
87
meta := NewStorage (metaDir , 0 )
93
88
cache := NewStorage (cacheDir , capacity )
@@ -206,7 +201,7 @@ func (c *Cacher) maintRelease(p string, withGPG bool) {
206
201
207
202
if log .Enabled (log .LvDebug ) {
208
203
log .Debug ("maintRelease" , map [string ]interface {}{
209
- "_path " : p ,
204
+ "path " : p ,
210
205
})
211
206
}
212
207
@@ -287,11 +282,19 @@ func (c *Cacher) download(p string, u *url.URL, valid *apt.FileInfo) {
287
282
ctx , cancel := context .WithTimeout (c .ctx , requestTimeout )
288
283
defer cancel ()
289
284
290
- resp , err := ctxhttp .Get (ctx , c .client , u .String ())
285
+ req := & http.Request {
286
+ Method : "GET" ,
287
+ URL : u ,
288
+ Proto : "HTTP/1.1" ,
289
+ ProtoMajor : 1 ,
290
+ ProtoMinor : 1 ,
291
+ Header : make (http.Header ),
292
+ }
293
+ resp , err := c .client .Do (req .WithContext (ctx ))
291
294
if err != nil {
292
295
log .Warn ("GET failed" , map [string ]interface {}{
293
- "_url" : u .String (),
294
- "_err " : err .Error (),
296
+ "url" : u .String (),
297
+ "error " : err .Error (),
295
298
})
296
299
return
297
300
}
@@ -305,16 +308,16 @@ func (c *Cacher) download(p string, u *url.URL, valid *apt.FileInfo) {
305
308
306
309
if err != nil {
307
310
log .Warn ("GET failed" , map [string ]interface {}{
308
- "_url" : u .String (),
309
- "_err " : err .Error (),
311
+ "url" : u .String (),
312
+ "error " : err .Error (),
310
313
})
311
314
return
312
315
}
313
316
314
317
fi := apt .MakeFileInfo (p , body )
315
318
if valid != nil && ! valid .Same (fi ) {
316
319
log .Warn ("downloaded data is not valid" , map [string ]interface {}{
317
- "_url " : u .String (),
320
+ "url " : u .String (),
318
321
})
319
322
return
320
323
}
@@ -331,8 +334,8 @@ func (c *Cacher) download(p string, u *url.URL, valid *apt.FileInfo) {
331
334
fil , err = apt .ExtractFileInfo (t [1 ], bytes .NewReader (body ))
332
335
if err != nil {
333
336
log .Error ("invalid meta data" , map [string ]interface {}{
334
- "_path" : p ,
335
- "_err" : err .Error (),
337
+ "path" : p ,
338
+ "error" : err .Error (),
336
339
})
337
340
// do not return; we accept broken meta data as is.
338
341
}
@@ -344,8 +347,8 @@ func (c *Cacher) download(p string, u *url.URL, valid *apt.FileInfo) {
344
347
345
348
if err := storage .Insert (body , fi ); err != nil {
346
349
log .Error ("could not save an item" , map [string ]interface {}{
347
- "_path" : p ,
348
- "_err" : err .Error (),
350
+ "path" : p ,
351
+ "error" : err .Error (),
349
352
})
350
353
// panic because go-apt-cacher cannot continue working
351
354
panic (err )
@@ -363,7 +366,7 @@ func (c *Cacher) download(p string, u *url.URL, valid *apt.FileInfo) {
363
366
}
364
367
c .info [p ] = fi
365
368
log .Info ("downloaded and cached" , map [string ]interface {}{
366
- "_path " : p ,
369
+ "path " : p ,
367
370
})
368
371
}
369
372
@@ -401,7 +404,7 @@ RETRY:
401
404
case ErrNotFound :
402
405
default :
403
406
log .Error ("lookup failure" , map [string ]interface {}{
404
- "_err " : err .Error (),
407
+ "error " : err .Error (),
405
408
})
406
409
return http .StatusInternalServerError , nil , err
407
410
}
0 commit comments