(tested) complete replace logrus with zerolog

* remove cobra dependency and split execs to mochi and e2e

* add log init synchronization
This commit is contained in:
Lawrence, Rendall
2022-05-02 03:13:58 +03:00
parent 4d646f7c09
commit c50a532181
36 changed files with 753 additions and 707 deletions
+1 -8
View File
@@ -5,7 +5,6 @@ import (
"errors"
"github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/log"
"github.com/sot-tech/mochi/storage"
)
@@ -185,16 +184,10 @@ func (h *responseHook) appendPeers(req *bittorrent.AnnounceRequest, resp *bittor
resp.IPv4Peers = append(resp.IPv4Peers, p)
uniquePeers[p] = nil
} else {
log.Warn("received invalid peer from storage", log.Fields{"peer": p})
logger.Warn().Object("peer", p).Msg("received invalid peer from storage")
}
}
}
log.Debug("responseHook announce peers", log.Fields{
"infoHash": req.InfoHash,
"requestPeer": req.RequestPeer,
"ipv4Peers": resp.IPv4Peers,
"ipv6Peers": resp.IPv6Peers,
})
return
}
+2
View File
@@ -38,6 +38,7 @@ type Logic struct {
// HandleAnnounce generates a response for an Announce.
func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (_ context.Context, resp *bittorrent.AnnounceResponse, err error) {
logger.Debug().Object("request", req).Msg("new announce request")
resp = &bittorrent.AnnounceResponse{
Interval: l.announceInterval,
MinInterval: l.minAnnounceInterval,
@@ -70,6 +71,7 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque
// HandleScrape generates a response for a Scrape.
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (_ context.Context, resp *bittorrent.ScrapeResponse, err error) {
logger.Debug().Object("request", req).Msg("new scrape request")
resp = &bittorrent.ScrapeResponse{
Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)),
}
+5
View File
@@ -9,8 +9,13 @@ import (
"github.com/stretchr/testify/require"
"github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/log"
)
func init() {
_ = log.ConfigureLogger("", "warn", false, false)
}
// nopHook is a Hook to measure the overhead of a no-operation Hook through
// benchmarks.
type nopHook struct{}
+5 -5
View File
@@ -47,11 +47,11 @@ func RegisterBuilder(name string, d Builder) {
drivers[name] = d
}
// New attempts to initialize a new middleware instance from the
// NewHook attempts to initialize a new middleware instance from the
// list of registered Builders.
//
// If a driver does not exist, returns ErrBuilderDoesNotExist.
func New(name string, options conf.MapConfig, storage storage.PeerStorage) (Hook, error) {
func NewHook(name string, options conf.MapConfig, storage storage.PeerStorage) (Hook, error) {
driversM.RLock()
defer driversM.RUnlock()
@@ -70,9 +70,9 @@ type Config struct {
Options conf.MapConfig
}
// HooksFromHookConfigs is a utility function for initializing Hooks in bulk.
// NewHooks is a utility function for initializing Hooks in bulk.
// each element of configs must contain pairs `name` - string and `options` - map[string]any
func HooksFromHookConfigs(configs []conf.MapConfig, storage storage.PeerStorage) (hooks []Hook, err error) {
func NewHooks(configs []conf.MapConfig, storage storage.PeerStorage) (hooks []Hook, err error) {
for _, cfg := range configs {
var c Config
@@ -81,7 +81,7 @@ func HooksFromHookConfigs(configs []conf.MapConfig, storage storage.PeerStorage)
}
var h Hook
h, err = New(c.Name, c.Options, storage)
h, err = NewHook(c.Name, c.Options, storage)
if err != nil {
break
}
@@ -57,36 +57,37 @@ func build(conf conf.MapConfig, st storage.DataStorage) (container.Container, er
}
d.watcher = w
if len(d.StorageCtx) == 0 {
log.Info("storage context not set, using default value: " + container.DefaultStorageCtxName)
logger.Warn().
Str("name", "StorageCtx").
Str("provided", d.StorageCtx).
Str("default", container.DefaultStorageCtxName).
Msg("falling back to default configuration")
d.StorageCtx = container.DefaultStorageCtxName
}
go func() {
for event := range d.watcher.Events {
var mi *metainfo.MetaInfo
lf := log.Fields{
"file": event.TorrentFilePath,
"v1hash": event.InfoHash,
}
if mi, err = metainfo.LoadFromFile(event.TorrentFilePath); err == nil {
s256 := sha256.New()
s256.Write(mi.InfoBytes)
v2hash, _ := bittorrent.NewInfoHash(s256.Sum(nil))
lf["v2hash"] = v2hash
lf["v2to1hash"] = v2hash.TruncateV1()
switch event.Change {
case dirwatch.Added:
var name string
if info, err := mi.UnmarshalInfo(); err == nil {
name = info.Name
} else {
lf["error"] = err
log.Warn("unable to unmarshal torrent info", lf)
delete(lf, "error")
logger.Error().
Err(err).
Str("file", event.TorrentFilePath).
Stringer("infoHash", event.InfoHash).
Stringer("infoHashV2", v2hash).
Msg("unable to unmarshal torrent info")
}
if len(name) == 0 {
name = list.DUMMY
}
if err := d.Storage.Put(d.StorageCtx,
logger.Err(d.Storage.Put(d.StorageCtx,
storage.Entry{
Key: event.InfoHash.AsString(),
Value: name,
@@ -96,23 +97,28 @@ func build(conf conf.MapConfig, st storage.DataStorage) (container.Container, er
}, storage.Entry{
Key: v2hash.TruncateV1().RawString(),
Value: name,
}); err != nil {
lf["error"] = err
}
log.Debug("approval torrent added", lf)
})).
Str("action", "add").
Str("file", event.TorrentFilePath).
Stringer("infoHash", event.InfoHash).
Stringer("infoHashV2", v2hash).
Msg("approval torrent watcher event")
case dirwatch.Removed:
if err := d.Storage.Delete(c.StorageCtx,
logger.Err(d.Storage.Delete(c.StorageCtx,
event.InfoHash.AsString(),
v2hash.RawString(),
v2hash.TruncateV1().RawString(),
); err != nil {
lf["error"] = err
}
log.Debug("approval torrent deleted", lf)
)).
Str("action", "delete").
Str("file", event.TorrentFilePath).
Stringer("infoHash", event.InfoHash).
Stringer("infoHashV2", v2hash).
Msg("approval torrent watcher event")
}
} else {
lf["error"] = err
log.Error("unable to load torrent file", lf)
logger.Error().Err(err).
Str("file", event.TorrentFilePath).
Msg("unable to load torrent file")
}
}
}()
@@ -126,6 +132,8 @@ type directory struct {
// Stop closes watching of torrent directory
func (d *directory) Stop() stop.Result {
st := make(stop.Channel)
d.watcher.Close()
return stop.AlreadyStopped
st.Done()
return st.Result()
}
@@ -15,6 +15,8 @@ import (
// Name of this container for registry.
const Name = "list"
var logger = log.NewLogger("torrent approval list")
func init() {
container.Register(Name, build)
}
@@ -45,7 +47,11 @@ func build(conf conf.MapConfig, st storage.DataStorage) (container.Container, er
}
if len(l.StorageCtx) == 0 {
log.Info("Storage context not set, using default value: " + container.DefaultStorageCtxName)
logger.Warn().
Str("name", "StorageCtx").
Str("provided", l.StorageCtx).
Str("default", container.DefaultStorageCtxName).
Msg("falling back to default configuration")
l.StorageCtx = container.DefaultStorageCtxName
}
@@ -93,7 +99,7 @@ func (l *List) Approved(hash bittorrent.InfoHash) (contains bool) {
}
}
if err != nil {
log.Err(err)
logger.Error().Err(err).Stringer("infoHash", hash).Msg("unable load hash information from storage")
}
return contains != l.Invert
}
@@ -9,9 +9,14 @@ import (
"github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/conf"
"github.com/sot-tech/mochi/pkg/log"
"github.com/sot-tech/mochi/storage/memory"
)
func init() {
_ = log.ConfigureLogger("", "warn", false, false)
}
var cases = []struct {
cfg baseConfig
ih string