Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

FilesizeFetcher -> MetainfoFetcher

Cette révision appartient à :
ElegantMonkey 2017-05-14 08:20:34 -03:00
Parent 80034bb52b
révision 2da2ad4214
6 fichiers modifiés avec 29 ajouts et 29 suppressions

Voir le fichier

@ -32,12 +32,12 @@ type Config struct {
// optional i2p configuration // optional i2p configuration
I2P *I2PConfig `json:"i2p"` I2P *I2PConfig `json:"i2p"`
// filesize fetcher config // filesize fetcher config
FilesizeFetcher FilesizeFetcherConfig `json:"filesize_fetcher"` MetainfoFetcher MetainfoFetcherConfig `json:"metainfo_fetcher"`
// internationalization config // internationalization config
I18n I18nConfig `json:"i18n"` I18n I18nConfig `json:"i18n"`
} }
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", "default", DefaultScraperConfig, DefaultCacheConfig, DefaultSearchConfig, nil, DefaultFilesizeFetcherConfig, DefaultI18nConfig} var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", "default", DefaultScraperConfig, DefaultCacheConfig, DefaultSearchConfig, nil, DefaultMetainfoFetcherConfig, DefaultI18nConfig}
var allowedDatabaseTypes = map[string]bool{ var allowedDatabaseTypes = map[string]bool{
@ -62,7 +62,7 @@ func New() *Config {
config.DBLogMode = Defaults.DBLogMode config.DBLogMode = Defaults.DBLogMode
config.Scrape = Defaults.Scrape config.Scrape = Defaults.Scrape
config.Cache = Defaults.Cache config.Cache = Defaults.Cache
config.FilesizeFetcher = Defaults.FilesizeFetcher config.MetainfoFetcher = Defaults.MetainfoFetcher
config.I18n = Defaults.I18n config.I18n = Defaults.I18n
return &config return &config
} }

Voir le fichier

@ -1,13 +1,13 @@
package config package config
type FilesizeFetcherConfig struct { type MetainfoFetcherConfig struct {
QueueSize int `json:"queue_size"` QueueSize int `json:"queue_size"`
Timeout int `json:"timeout"` Timeout int `json:"timeout"`
MaxDays int `json:"max_days"` MaxDays int `json:"max_days"`
WakeUpInterval int `json:"wake_up_interval"` WakeUpInterval int `json:"wake_up_interval"`
} }
var DefaultFilesizeFetcherConfig = FilesizeFetcherConfig{ var DefaultMetainfoFetcherConfig = MetainfoFetcherConfig{
QueueSize: 10, QueueSize: 10,
Timeout: 120, // 2 min Timeout: 120, // 2 min
MaxDays: 90, MaxDays: 90,

12
main.go
Voir le fichier

@ -14,7 +14,7 @@ import (
"github.com/ewhal/nyaa/network" "github.com/ewhal/nyaa/network"
"github.com/ewhal/nyaa/router" "github.com/ewhal/nyaa/router"
"github.com/ewhal/nyaa/service/scraper" "github.com/ewhal/nyaa/service/scraper"
"github.com/ewhal/nyaa/service/torrent/filesizeFetcher" "github.com/ewhal/nyaa/service/torrent/metainfoFetcher"
"github.com/ewhal/nyaa/util/languages" "github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/log"
"github.com/ewhal/nyaa/util/search" "github.com/ewhal/nyaa/util/search"
@ -83,9 +83,9 @@ func RunScraper(conf *config.Config) {
scraper.Wait() scraper.Wait()
} }
// RunFilesizeFetcher runs the database filesize fetcher main loop // RunMetainfoFetcher runs the database filesize fetcher main loop
func RunFilesizeFetcher(conf *config.Config) { func RunMetainfoFetcher(conf *config.Config) {
fetcher, err := filesizeFetcher.New(&conf.FilesizeFetcher) fetcher, err := metainfoFetcher.New(&conf.MetainfoFetcher)
if err != nil { if err != nil {
log.Fatalf("failed to start fetcher, %s", err) log.Fatalf("failed to start fetcher, %s", err)
return return
@ -147,8 +147,8 @@ func main() {
RunScraper(conf) RunScraper(conf)
} else if *mode == "webapp" { } else if *mode == "webapp" {
RunServer(conf) RunServer(conf)
} else if *mode == "filesize_fetcher" { } else if *mode == "metainfo_fetcher" {
RunFilesizeFetcher(conf) RunMetainfoFetcher(conf)
} else { } else {
log.Fatalf("invalid runtime mode: %s", *mode) log.Fatalf("invalid runtime mode: %s", *mode)
} }

Voir le fichier

@ -1,4 +1,4 @@
package filesizeFetcher; package metainfoFetcher;
import ( import (
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
@ -13,7 +13,7 @@ import (
"time" "time"
) )
type FilesizeFetcher struct { type MetainfoFetcher struct {
torrentClient *torrent.Client torrentClient *torrent.Client
results chan Result results chan Result
queueSize int queueSize int
@ -27,9 +27,9 @@ type FilesizeFetcher struct {
wg sync.WaitGroup wg sync.WaitGroup
} }
func New(fetcherConfig *config.FilesizeFetcherConfig) (fetcher *FilesizeFetcher, err error) { func New(fetcherConfig *config.MetainfoFetcherConfig) (fetcher *MetainfoFetcher, err error) {
client, err := torrent.NewClient(nil) client, err := torrent.NewClient(nil)
fetcher = &FilesizeFetcher{ fetcher = &MetainfoFetcher{
torrentClient: client, torrentClient: client,
results: make(chan Result, fetcherConfig.QueueSize), results: make(chan Result, fetcherConfig.QueueSize),
queueSize: fetcherConfig.QueueSize, queueSize: fetcherConfig.QueueSize,
@ -43,7 +43,7 @@ func New(fetcherConfig *config.FilesizeFetcherConfig) (fetcher *FilesizeFetcher,
return return
} }
func (fetcher *FilesizeFetcher) isFetchingOrFailed(t model.Torrent) bool { func (fetcher *MetainfoFetcher) isFetchingOrFailed(t model.Torrent) bool {
for _, op := range fetcher.queue { for _, op := range fetcher.queue {
if op.torrent.ID == t.ID { if op.torrent.ID == t.ID {
return true return true
@ -54,7 +54,7 @@ func (fetcher *FilesizeFetcher) isFetchingOrFailed(t model.Torrent) bool {
return ok return ok
} }
func (fetcher *FilesizeFetcher) addToQueue(op *FetchOperation) bool { func (fetcher *MetainfoFetcher) addToQueue(op *FetchOperation) bool {
fetcher.queueMutex.Lock() fetcher.queueMutex.Lock()
defer fetcher.queueMutex.Unlock() defer fetcher.queueMutex.Unlock()
@ -67,7 +67,7 @@ func (fetcher *FilesizeFetcher) addToQueue(op *FetchOperation) bool {
} }
func (fetcher *FilesizeFetcher) removeFromQueue(op *FetchOperation) bool { func (fetcher *MetainfoFetcher) removeFromQueue(op *FetchOperation) bool {
fetcher.queueMutex.Lock() fetcher.queueMutex.Lock()
defer fetcher.queueMutex.Unlock() defer fetcher.queueMutex.Unlock()
@ -111,7 +111,7 @@ func updateFileList(dbEntry model.Torrent, info *metainfo.Info) error {
return nil return nil
} }
func (fetcher *FilesizeFetcher) gotResult(r Result) { func (fetcher *MetainfoFetcher) gotResult(r Result) {
updatedSuccessfully := false updatedSuccessfully := false
if r.err != nil { if r.err != nil {
log.Infof("Failed to get torrent filesize (TID: %d), err %v", r.operation.torrent.ID, r.err) log.Infof("Failed to get torrent filesize (TID: %d), err %v", r.operation.torrent.ID, r.err)
@ -141,7 +141,7 @@ func (fetcher *FilesizeFetcher) gotResult(r Result) {
fetcher.removeFromQueue(r.operation) fetcher.removeFromQueue(r.operation)
} }
func (fetcher *FilesizeFetcher) fillQueue() { func (fetcher *MetainfoFetcher) fillQueue() {
toFill := fetcher.queueSize - len(fetcher.queue) toFill := fetcher.queueSize - len(fetcher.queue)
if toFill <= 0 { if toFill <= 0 {
@ -180,7 +180,7 @@ func (fetcher *FilesizeFetcher) fillQueue() {
} }
} }
func (fetcher *FilesizeFetcher) run() { func (fetcher *MetainfoFetcher) run() {
var result Result var result Result
defer fetcher.wg.Done() defer fetcher.wg.Done()
@ -202,13 +202,13 @@ func (fetcher *FilesizeFetcher) run() {
} }
} }
func (fetcher *FilesizeFetcher) RunAsync() { func (fetcher *MetainfoFetcher) RunAsync() {
fetcher.wg.Add(1) fetcher.wg.Add(1)
go fetcher.run() go fetcher.run()
} }
func (fetcher *FilesizeFetcher) Close() error { func (fetcher *MetainfoFetcher) Close() error {
fetcher.queueMutex.Lock() fetcher.queueMutex.Lock()
defer fetcher.queueMutex.Unlock() defer fetcher.queueMutex.Unlock()
@ -222,7 +222,7 @@ func (fetcher *FilesizeFetcher) Close() error {
return nil return nil
} }
func (fetcher *FilesizeFetcher) Wait() { func (fetcher *MetainfoFetcher) Wait() {
fetcher.wg.Wait() fetcher.wg.Wait()
} }

Voir le fichier

@ -1,4 +1,4 @@
package filesizeFetcher; package metainfoFetcher;
import ( import (
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
@ -11,7 +11,7 @@ import (
) )
type FetchOperation struct { type FetchOperation struct {
fetcher *FilesizeFetcher fetcher *MetainfoFetcher
torrent model.Torrent torrent model.Torrent
done chan int done chan int
} }
@ -22,7 +22,7 @@ type Result struct {
info *metainfo.Info info *metainfo.Info
} }
func NewFetchOperation(fetcher *FilesizeFetcher, dbEntry model.Torrent) (op *FetchOperation) { func NewFetchOperation(fetcher *MetainfoFetcher, dbEntry model.Torrent) (op *FetchOperation) {
op = &FetchOperation{ op = &FetchOperation{
fetcher: fetcher, fetcher: fetcher,
torrent: dbEntry, torrent: dbEntry,

Voir le fichier

@ -1,4 +1,4 @@
package filesizeFetcher; package metainfoFetcher;
import ( import (
"testing" "testing"
@ -13,7 +13,7 @@ func TestInvalidHash(t *testing.T) {
t.Skipf("Failed to create client, with err %v. Skipping.", err) t.Skipf("Failed to create client, with err %v. Skipping.", err)
} }
fetcher := &FilesizeFetcher{ fetcher := &MetainfoFetcher{
timeout: 5, timeout: 5,
torrentClient: client, torrentClient: client,
results: make(chan Result, 1), results: make(chan Result, 1),