2011-03-25 04:07:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-01-20 17:52:03 +00:00
|
|
|
// Package cacher provides various blobref fetching caching mechanisms.
|
2011-08-07 01:46:37 +00:00
|
|
|
package cacher
|
2011-03-25 04:07:49 +00:00
|
|
|
|
|
|
|
import (
|
2011-06-04 16:12:02 +00:00
|
|
|
"io"
|
2013-01-20 17:52:03 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2011-06-04 16:12:02 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2013-01-20 17:52:03 +00:00
|
|
|
"camlistore.org/pkg/blobserver/localdisk"
|
2013-01-06 06:51:33 +00:00
|
|
|
"camlistore.org/pkg/singleflight"
|
2013-07-29 03:08:55 +00:00
|
|
|
"camlistore.org/pkg/types"
|
2011-03-25 04:07:49 +00:00
|
|
|
)
|
|
|
|
|
2013-01-20 17:52:03 +00:00
|
|
|
// NewCachingFetcher returns a CachingFetcher that fetches from
|
|
|
|
// fetcher and writes to and serves from cache.
|
2013-08-04 02:54:30 +00:00
|
|
|
func NewCachingFetcher(cache blobserver.Cache, fetcher blob.StreamingFetcher) *CachingFetcher {
|
2013-01-20 17:52:03 +00:00
|
|
|
return &CachingFetcher{c: cache, sf: fetcher}
|
2011-03-25 04:07:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
// A CachingFetcher is a blob.StreamingFetcher and a blob.SeekFetcher.
|
2011-03-25 04:07:49 +00:00
|
|
|
type CachingFetcher struct {
|
|
|
|
c blobserver.Cache
|
2013-08-04 02:54:30 +00:00
|
|
|
sf blob.StreamingFetcher
|
2013-01-06 06:51:33 +00:00
|
|
|
|
|
|
|
g singleflight.Group
|
2011-03-25 04:07:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (cf *CachingFetcher) FetchStreaming(br blob.Ref) (file io.ReadCloser, size int64, err error) {
|
2011-06-04 16:12:02 +00:00
|
|
|
file, size, err = cf.c.Fetch(br)
|
2011-07-02 16:09:50 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-05 01:05:20 +00:00
|
|
|
if err = cf.faultIn(br); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-06-04 16:12:02 +00:00
|
|
|
return cf.c.Fetch(br)
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (cf *CachingFetcher) Fetch(br blob.Ref) (file types.ReadSeekCloser, size int64, err error) {
|
2011-03-25 04:07:49 +00:00
|
|
|
file, size, err = cf.c.Fetch(br)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-05 01:05:20 +00:00
|
|
|
if err = cf.faultIn(br); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-06-04 16:12:02 +00:00
|
|
|
return cf.c.Fetch(br)
|
|
|
|
}
|
2011-03-25 04:07:49 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (cf *CachingFetcher) faultIn(br blob.Ref) error {
|
2013-01-06 06:51:33 +00:00
|
|
|
_, err := cf.g.Do(br.String(), func() (interface{}, error) {
|
|
|
|
sblob, _, err := cf.sf.FetchStreaming(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer sblob.Close()
|
|
|
|
_, err = cf.c.ReceiveBlob(br, sblob)
|
|
|
|
return nil, err
|
|
|
|
})
|
2011-06-04 16:12:02 +00:00
|
|
|
return err
|
2011-03-25 04:07:49 +00:00
|
|
|
}
|
2013-01-20 17:52:03 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
// A DiskCache is a blob.StreamingFetcher and blob.SeekFetcher
|
2013-01-20 17:52:03 +00:00
|
|
|
// that serves from a local temp directory and is backed by a another
|
2013-08-04 02:54:30 +00:00
|
|
|
// blob.StreamingFetcher (usually the pkg/client HTTP client).
|
2013-01-20 17:52:03 +00:00
|
|
|
type DiskCache struct {
|
|
|
|
*CachingFetcher
|
|
|
|
|
|
|
|
// Root is the temp directory being used to store files.
|
|
|
|
// It is available mostly for debug printing.
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiskCache returns a new DiskCache from a StreamingFetcher, which
|
|
|
|
// is usually the pkg/client HTTP client (which typically has much
|
|
|
|
// higher latency and lower bandwidth than local disk).
|
2013-08-04 02:54:30 +00:00
|
|
|
func NewDiskCache(fetcher blob.StreamingFetcher) (*DiskCache, error) {
|
2013-01-20 17:52:03 +00:00
|
|
|
// TODO: max disk size, keep LRU of access, smarter cleaning,
|
|
|
|
// persistent directory per-user, etc.
|
|
|
|
|
|
|
|
cacheDir, err := ioutil.TempDir("", "camlicache")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
diskcache, err := localdisk.New(cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dc := &DiskCache{
|
|
|
|
CachingFetcher: NewCachingFetcher(diskcache, fetcher),
|
|
|
|
Root: cacheDir,
|
|
|
|
}
|
|
|
|
return dc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean cleans some or all of the DiskCache.
|
|
|
|
func (dc *DiskCache) Clean() {
|
|
|
|
// TODO: something less aggressive?
|
|
|
|
os.RemoveAll(dc.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2013-08-04 02:54:30 +00:00
|
|
|
_ blob.StreamingFetcher = (*CachingFetcher)(nil)
|
|
|
|
_ blob.SeekFetcher = (*CachingFetcher)(nil)
|
|
|
|
_ blob.StreamingFetcher = (*DiskCache)(nil)
|
|
|
|
_ blob.SeekFetcher = (*DiskCache)(nil)
|
2013-01-20 17:52:03 +00:00
|
|
|
)
|