2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-20 19:21:41 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-14 20:17:30 +00:00
|
|
|
"context"
|
2018-12-18 20:41:31 +00:00
|
|
|
"fmt"
|
2019-04-26 13:15:06 +01:00
|
|
|
"time"
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2018-12-18 20:41:31 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-02-14 21:55:21 +00:00
|
|
|
"go.uber.org/zap"
|
2018-08-20 19:21:41 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-12-18 20:41:31 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
2018-08-20 19:21:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cacheConfig struct {
|
2018-12-18 20:41:31 +00:00
|
|
|
NodesPath string `help:"the path to a JSON file containing an object with IP keys and nodeID values"`
|
|
|
|
Database string `help:"overlay database connection string" default:"sqlite3://$CONFDIR/master.db"`
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 20:41:31 +00:00
|
|
|
func (c cacheConfig) open(ctx context.Context) (cache *overlay.Cache, dbClose func(), err error) {
|
2019-02-14 21:55:21 +00:00
|
|
|
database, err := satellitedb.New(zap.L().Named("db"), c.Database)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
2018-12-18 20:41:31 +00:00
|
|
|
return nil, nil, errs.New("error connecting to database: %+v", err)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
2018-12-18 20:41:31 +00:00
|
|
|
dbClose = func() {
|
|
|
|
err := database.Close()
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
2018-12-18 20:41:31 +00:00
|
|
|
fmt.Printf("error closing connection to database: %+v\n", err)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
2018-12-04 20:18:26 +00:00
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2019-04-26 13:15:06 +01:00
|
|
|
return overlay.NewCache(zap.L(), database.OverlayCache(), overlay.NodeSelectionConfig{OnlineWindow: time.Hour}), dbClose, nil
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|