fix lint warnings

This commit is contained in:
Lawrence, Rendall
2024-06-24 14:01:46 +03:00
parent 13d5444ca4
commit e61045833a
7 changed files with 38 additions and 26 deletions

View File

@@ -208,7 +208,7 @@ prehooks:
# initial_source: list # initial_source: list
# Save data provided by source in specific storage. If name is empty or 'internal', provided above 'storage' # 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) # is used, but another storage may be provided (configuration is the same as for 'storage' above)
# store: # storage:
# name: internal # name: internal
# config: # config:
# configuration: # configuration:

View File

@@ -12,12 +12,13 @@ import (
"github.com/sot-tech/mochi/middleware" "github.com/sot-tech/mochi/middleware"
"github.com/sot-tech/mochi/middleware/torrentapproval/container" "github.com/sot-tech/mochi/middleware/torrentapproval/container"
"github.com/sot-tech/mochi/pkg/conf" "github.com/sot-tech/mochi/pkg/conf"
"github.com/sot-tech/mochi/storage"
// import directory watcher to enable appropriate support // import directory watcher to enable appropriate support
_ "github.com/sot-tech/mochi/middleware/torrentapproval/container/directory" _ "github.com/sot-tech/mochi/middleware/torrentapproval/container/directory"
// import static list to enable appropriate support // import static list to enable appropriate support
_ "github.com/sot-tech/mochi/middleware/torrentapproval/container/list" _ "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. // Name is the name by which this middleware is registered with Conf.
@@ -32,10 +33,10 @@ func init() {
type baseConfig struct { type baseConfig struct {
// Source - name of container for initial values // Source - name of container for initial values
Source string `cfg:"initial_source"` Source string `cfg:"initial_source"`
// Deprecated: use Store parameter // Deprecated: use Storage parameter
Preserve bool Preserve bool
// Store where to hold provided data by Source // Storage where to hold provided data by Source
Store conf.NamedMapConfig Storage conf.NamedMapConfig
// Configuration depends on used container // Configuration depends on used container
Configuration conf.MapConfig 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") return nil, errors.New("preserve option is deprecated, use store parameter")
} }
var ds storage.DataStorage var ds, dsc storage.DataStorage
if len(cfg.Store.Name) == 0 || cfg.Store.Name == internalStore { if len(cfg.Storage.Name) == 0 || cfg.Storage.Name == internalStore {
ds = st 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 return
} }
var c container.Container var c container.Container
if c, err = container.GetContainer(cfg.Source, cfg.Configuration, ds); err == nil { if c, err = container.GetContainer(cfg.Source, cfg.Configuration, ds); err == nil {
h = &hook{c} h = &hook{c, dsc}
} }
return h, err 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") var ErrTorrentUnapproved = bittorrent.ClientError("torrent not allowed by mochi")
type hook struct { 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) { 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 { if cl, isOk := h.hashContainer.(io.Closer); isOk {
err = cl.Close() err = cl.Close()
} }
if stErr := h.providedStorage.Close(); stErr != nil {
err = errors.Join(err, stErr)
}
return err return err
} }

View File

@@ -1,5 +1,6 @@
//go:build cgo //go:build cgo
// Package mdb implements LMDB data and peer storage
package mdb package mdb
import ( import (
@@ -7,14 +8,15 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"errors" "errors"
"github.com/PowerDNS/lmdb-go/exp/lmdbsync"
"net/netip" "net/netip"
"os" "os"
"sync" "sync"
"time" "time"
"github.com/PowerDNS/lmdb-go/exp/lmdbsync"
"github.com/PowerDNS/lmdb-go/lmdb" "github.com/PowerDNS/lmdb-go/lmdb"
"github.com/PowerDNS/lmdb-go/lmdbscan" "github.com/PowerDNS/lmdb-go/lmdbscan"
"github.com/sot-tech/mochi/bittorrent" "github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/conf" "github.com/sot-tech/mochi/pkg/conf"
"github.com/sot-tech/mochi/pkg/log" "github.com/sot-tech/mochi/pkg/log"
@@ -78,12 +80,10 @@ func (cfg config) validate() (config, error) {
validCfg := cfg validCfg := cfg
if len(cfg.Path) == 0 { if len(cfg.Path) == 0 {
return cfg, errPathNotProvided return cfg, errPathNotProvided
} else { } else if stat, err := os.Stat(cfg.Path); err != nil {
if stat, err := os.Stat(cfg.Path); err != nil { return cfg, err
return cfg, err } else if !stat.IsDir() {
} else if !stat.IsDir() { return cfg, errPathNotDirectory
return cfg, errPathNotDirectory
}
} }
if cfg.Mode == 0 { if cfg.Mode == 0 {
validCfg.Mode = defaultMode validCfg.Mode = defaultMode
@@ -314,7 +314,6 @@ func packPeer(peer bittorrent.Peer, out []byte) {
a := peer.Addr().As16() a := peer.Addr().As16()
copy(out[bittorrent.PeerIDLen:], a[:]) copy(out[bittorrent.PeerIDLen:], a[:])
binary.BigEndian.PutUint16(out[bittorrent.PeerIDLen+ipLen:], peer.Port()) binary.BigEndian.PutUint16(out[bittorrent.PeerIDLen+ipLen:], peer.Port())
return
} }
func unpackPeer(arr []byte) (peer bittorrent.Peer) { func unpackPeer(arr []byte) (peer bittorrent.Peer) {

View File

@@ -2,10 +2,11 @@ package mdb
import ( import (
"fmt" "fmt"
s "github.com/sot-tech/mochi/storage"
"github.com/sot-tech/mochi/storage/test"
"os" "os"
"testing" "testing"
s "github.com/sot-tech/mochi/storage"
"github.com/sot-tech/mochi/storage/test"
) )
const tmpPath = "" const tmpPath = ""
@@ -37,9 +38,7 @@ func TestStorage(t *testing.T) {
t.Error(err) t.Error(err)
} }
t.Cleanup(func() { t.Cleanup(func() {
err := os.RemoveAll(tmpDir) _ = os.RemoveAll(tmpDir)
if err != nil {
}
}) })
cfg.Path = tmpDir cfg.Path = tmpDir
test.RunTests(t, createNew()) test.RunTests(t, createNew())
@@ -51,9 +50,7 @@ func BenchmarkStorage(b *testing.B) {
b.Error(err) b.Error(err)
} }
b.Cleanup(func() { b.Cleanup(func() {
err := os.RemoveAll(tmpDir) _ = os.RemoveAll(tmpDir)
if err != nil {
}
}) })
cfg.Path = tmpDir cfg.Path = tmpDir
test.RunBenchmarks(b, createNew) test.RunBenchmarks(b, createNew)

View File

@@ -35,12 +35,15 @@ func init() {
storage.RegisterDriver(Name, Builder{}) storage.RegisterDriver(Name, Builder{})
} }
// Builder is structure to create new in-memory peer or data storage
type Builder struct{} type Builder struct{}
// NewDataStorage creates new in-memory KV storage. Does not need configuration
func (Builder) NewDataStorage(conf.MapConfig) (storage.DataStorage, error) { func (Builder) NewDataStorage(conf.MapConfig) (storage.DataStorage, error) {
return dataStorage(), nil return dataStorage(), nil
} }
// NewPeerStorage creates new in-memory peer storage
func (Builder) NewPeerStorage(icfg conf.MapConfig) (storage.PeerStorage, error) { func (Builder) NewPeerStorage(icfg conf.MapConfig) (storage.PeerStorage, error) {
var cfg config var cfg config
if err := icfg.Unmarshal(&cfg); err != nil { if err := icfg.Unmarshal(&cfg); err != nil {

View File

@@ -35,6 +35,7 @@ import (
"time" "time"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"github.com/sot-tech/mochi/pkg/str2bytes" "github.com/sot-tech/mochi/pkg/str2bytes"
"github.com/sot-tech/mochi/bittorrent" "github.com/sot-tech/mochi/bittorrent"
@@ -99,6 +100,7 @@ func (b builder) NewDataStorage(icfg conf.MapConfig) (storage.DataStorage, error
return b.NewPeerStorage(icfg) return b.NewPeerStorage(icfg)
} }
// NewStore creates new redis peer storage with provided configuration structure
func NewStore(cfg Config) (storage.PeerStorage, error) { func NewStore(cfg Config) (storage.PeerStorage, error) {
cfg, err := cfg.Validate() cfg, err := cfg.Validate()
if err != nil { if err != nil {

View File

@@ -85,7 +85,11 @@ type Entry struct {
// Driver is the interface used to initialize a new DataStorage or PeerStorage // Driver is the interface used to initialize a new DataStorage or PeerStorage
// with provided configuration. // with provided configuration.
type Driver interface { type Driver interface {
// NewDataStorage function prototype for creating new instance of data (KV) storage
// with provided configuration
NewDataStorage(cfg conf.MapConfig) (DataStorage, error) 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) NewPeerStorage(cfg conf.MapConfig) (PeerStorage, error)
} }