diff --git a/dist/example_config.yaml b/dist/example_config.yaml index d733116..372f52d 100644 --- a/dist/example_config.yaml +++ b/dist/example_config.yaml @@ -208,7 +208,7 @@ prehooks: # initial_source: list # Save data provided by source in specific storage. If name is empty or 'internal', provided above 'storage' # is used, but another storage may be provided (configuration is the same as for 'storage' above) -# store: +# storage: # name: internal # config: # configuration: diff --git a/middleware/torrentapproval/torrentapproval.go b/middleware/torrentapproval/torrentapproval.go index 29f8c7b..e4fa142 100644 --- a/middleware/torrentapproval/torrentapproval.go +++ b/middleware/torrentapproval/torrentapproval.go @@ -12,12 +12,13 @@ import ( "github.com/sot-tech/mochi/middleware" "github.com/sot-tech/mochi/middleware/torrentapproval/container" "github.com/sot-tech/mochi/pkg/conf" + "github.com/sot-tech/mochi/storage" + // import directory watcher to enable appropriate support _ "github.com/sot-tech/mochi/middleware/torrentapproval/container/directory" // import static list to enable appropriate support _ "github.com/sot-tech/mochi/middleware/torrentapproval/container/list" - "github.com/sot-tech/mochi/storage" ) // Name is the name by which this middleware is registered with Conf. @@ -32,10 +33,10 @@ func init() { type baseConfig struct { // Source - name of container for initial values Source string `cfg:"initial_source"` - // Deprecated: use Store parameter + // Deprecated: use Storage parameter Preserve bool - // Store where to hold provided data by Source - Store conf.NamedMapConfig + // Storage where to hold provided data by Source + Storage conf.NamedMapConfig // Configuration depends on used container Configuration conf.MapConfig } @@ -58,16 +59,18 @@ func build(config conf.MapConfig, st storage.PeerStorage) (h middleware.Hook, er return nil, errors.New("preserve option is deprecated, use store parameter") } - var ds storage.DataStorage - if len(cfg.Store.Name) == 0 || cfg.Store.Name == internalStore { + var ds, dsc storage.DataStorage + if len(cfg.Storage.Name) == 0 || cfg.Storage.Name == internalStore { ds = st - } else if ds, err = storage.NewDataStorage(cfg.Store); err != nil { + } else if ds, err = storage.NewDataStorage(cfg.Storage); err == nil { + dsc = ds + } else { return } var c container.Container if c, err = container.GetContainer(cfg.Source, cfg.Configuration, ds); err == nil { - h = &hook{c} + h = &hook{c, dsc} } return h, err } @@ -76,7 +79,8 @@ func build(config conf.MapConfig, st storage.PeerStorage) (h middleware.Hook, er var ErrTorrentUnapproved = bittorrent.ClientError("torrent not allowed by mochi") type hook struct { - hashContainer container.Container + hashContainer container.Container + providedStorage storage.DataStorage } func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, _ *bittorrent.AnnounceResponse) (context.Context, error) { @@ -98,5 +102,8 @@ func (h *hook) Close() (err error) { if cl, isOk := h.hashContainer.(io.Closer); isOk { err = cl.Close() } + if stErr := h.providedStorage.Close(); stErr != nil { + err = errors.Join(err, stErr) + } return err } diff --git a/storage/mdb/storage.go b/storage/mdb/storage.go index a67212f..7654a96 100644 --- a/storage/mdb/storage.go +++ b/storage/mdb/storage.go @@ -1,5 +1,6 @@ //go:build cgo +// Package mdb implements LMDB data and peer storage package mdb import ( @@ -7,14 +8,15 @@ import ( "context" "encoding/binary" "errors" - "github.com/PowerDNS/lmdb-go/exp/lmdbsync" "net/netip" "os" "sync" "time" + "github.com/PowerDNS/lmdb-go/exp/lmdbsync" "github.com/PowerDNS/lmdb-go/lmdb" "github.com/PowerDNS/lmdb-go/lmdbscan" + "github.com/sot-tech/mochi/bittorrent" "github.com/sot-tech/mochi/pkg/conf" "github.com/sot-tech/mochi/pkg/log" @@ -78,12 +80,10 @@ func (cfg config) validate() (config, error) { validCfg := cfg if len(cfg.Path) == 0 { return cfg, errPathNotProvided - } else { - if stat, err := os.Stat(cfg.Path); err != nil { - return cfg, err - } else if !stat.IsDir() { - return cfg, errPathNotDirectory - } + } else if stat, err := os.Stat(cfg.Path); err != nil { + return cfg, err + } else if !stat.IsDir() { + return cfg, errPathNotDirectory } if cfg.Mode == 0 { validCfg.Mode = defaultMode @@ -314,7 +314,6 @@ func packPeer(peer bittorrent.Peer, out []byte) { a := peer.Addr().As16() copy(out[bittorrent.PeerIDLen:], a[:]) binary.BigEndian.PutUint16(out[bittorrent.PeerIDLen+ipLen:], peer.Port()) - return } func unpackPeer(arr []byte) (peer bittorrent.Peer) { diff --git a/storage/mdb/storage_test.go b/storage/mdb/storage_test.go index 6084997..cede7d3 100644 --- a/storage/mdb/storage_test.go +++ b/storage/mdb/storage_test.go @@ -2,10 +2,11 @@ package mdb import ( "fmt" - s "github.com/sot-tech/mochi/storage" - "github.com/sot-tech/mochi/storage/test" "os" "testing" + + s "github.com/sot-tech/mochi/storage" + "github.com/sot-tech/mochi/storage/test" ) const tmpPath = "" @@ -37,9 +38,7 @@ func TestStorage(t *testing.T) { t.Error(err) } t.Cleanup(func() { - err := os.RemoveAll(tmpDir) - if err != nil { - } + _ = os.RemoveAll(tmpDir) }) cfg.Path = tmpDir test.RunTests(t, createNew()) @@ -51,9 +50,7 @@ func BenchmarkStorage(b *testing.B) { b.Error(err) } b.Cleanup(func() { - err := os.RemoveAll(tmpDir) - if err != nil { - } + _ = os.RemoveAll(tmpDir) }) cfg.Path = tmpDir test.RunBenchmarks(b, createNew) diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 61a5d7c..d5f3d93 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -35,12 +35,15 @@ func init() { storage.RegisterDriver(Name, Builder{}) } +// Builder is structure to create new in-memory peer or data storage type Builder struct{} +// NewDataStorage creates new in-memory KV storage. Does not need configuration func (Builder) NewDataStorage(conf.MapConfig) (storage.DataStorage, error) { return dataStorage(), nil } +// NewPeerStorage creates new in-memory peer storage func (Builder) NewPeerStorage(icfg conf.MapConfig) (storage.PeerStorage, error) { var cfg config if err := icfg.Unmarshal(&cfg); err != nil { diff --git a/storage/redis/storage.go b/storage/redis/storage.go index 99fb1b5..3f6454f 100644 --- a/storage/redis/storage.go +++ b/storage/redis/storage.go @@ -35,6 +35,7 @@ import ( "time" "github.com/redis/go-redis/v9" + "github.com/sot-tech/mochi/pkg/str2bytes" "github.com/sot-tech/mochi/bittorrent" @@ -99,6 +100,7 @@ func (b builder) NewDataStorage(icfg conf.MapConfig) (storage.DataStorage, error return b.NewPeerStorage(icfg) } +// NewStore creates new redis peer storage with provided configuration structure func NewStore(cfg Config) (storage.PeerStorage, error) { cfg, err := cfg.Validate() if err != nil { diff --git a/storage/storage.go b/storage/storage.go index 411d3bb..a2d0279 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -85,7 +85,11 @@ type Entry struct { // Driver is the interface used to initialize a new DataStorage or PeerStorage // with provided configuration. type Driver interface { + // NewDataStorage function prototype for creating new instance of data (KV) storage + // with provided configuration NewDataStorage(cfg conf.MapConfig) (DataStorage, error) + // NewPeerStorage function prototype for creating new instance of peer storage + // with provided configuration NewPeerStorage(cfg conf.MapConfig) (PeerStorage, error) }