mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-20 23:04:47 -07:00
Gegin work on shared store for handlers
This commit is contained in:
committed by
Lawrence, Rendall
parent
af1cbc543c
commit
566d99fcd7
@@ -57,17 +57,17 @@ func EndToEndRunCmdFunc(cmd *cobra.Command, args []string) error {
|
||||
func generateInfohash() bittorrent.InfoHash {
|
||||
b := make([]byte, 20)
|
||||
rand.Read(b)
|
||||
ih, _ := bittorrent.InfoHashFromBytes(b)
|
||||
ih, _ := bittorrent.NewInfoHash(b)
|
||||
return ih
|
||||
}
|
||||
|
||||
func test(addr string, delay time.Duration) error {
|
||||
ih, _ := generateInfohash().BytesV1()
|
||||
ih := generateInfohash().TruncateV1()
|
||||
return testWithInfohash(ih, addr, delay)
|
||||
}
|
||||
|
||||
func testWithInfohash(infoHash [20]byte, url string, delay time.Duration) error {
|
||||
var ih [20]byte
|
||||
func testWithInfohash(infoHash bittorrent.InfoHash, url string, delay time.Duration) error {
|
||||
var ih [bittorrent.InfoHashV1Len]byte
|
||||
req := tracker.AnnounceRequest{
|
||||
InfoHash: ih,
|
||||
PeerId: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
|
||||
@@ -95,8 +95,9 @@ func testWithInfohash(infoHash [20]byte, url string, delay time.Duration) error
|
||||
|
||||
time.Sleep(delay)
|
||||
|
||||
copy(ih[:], infoHash)
|
||||
req = tracker.AnnounceRequest{
|
||||
InfoHash: infoHash,
|
||||
InfoHash: ih,
|
||||
PeerId: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21},
|
||||
Downloaded: 50,
|
||||
Left: 100,
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
// Run represents the state of a running instance of Chihaya.
|
||||
type Run struct {
|
||||
configFilePath string
|
||||
peerStore storage.PeerStore
|
||||
storage storage.Storage
|
||||
logic *middleware.Logic
|
||||
sg *stop.Group
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func NewRun(configFilePath string) (*Run, error) {
|
||||
// Start begins an instance of Chihaya.
|
||||
// It is optional to provide an instance of the peer store to avoid the
|
||||
// creation of a new one.
|
||||
func (r *Run) Start(ps storage.PeerStore) error {
|
||||
func (r *Run) Start(ps storage.Storage) error {
|
||||
configFile, err := ParseConfigFile(r.configFilePath)
|
||||
if err != nil {
|
||||
return errors.New("failed to read config: " + err.Error())
|
||||
@@ -59,19 +59,19 @@ func (r *Run) Start(ps storage.PeerStore) error {
|
||||
|
||||
if ps == nil {
|
||||
log.Info("starting storage", log.Fields{"name": cfg.Storage.Name})
|
||||
ps, err = storage.NewPeerStore(cfg.Storage.Name, cfg.Storage.Config)
|
||||
ps, err = storage.NewStorage(cfg.Storage.Name, cfg.Storage.Config)
|
||||
if err != nil {
|
||||
return errors.New("failed to create storage: " + err.Error())
|
||||
}
|
||||
log.Info("started storage", ps)
|
||||
}
|
||||
r.peerStore = ps
|
||||
r.storage = ps
|
||||
|
||||
preHooks, err := middleware.HooksFromHookConfigs(cfg.PreHooks)
|
||||
preHooks, err := middleware.HooksFromHookConfigs(cfg.PreHooks, r.storage)
|
||||
if err != nil {
|
||||
return errors.New("failed to validate hook config: " + err.Error())
|
||||
}
|
||||
postHooks, err := middleware.HooksFromHookConfigs(cfg.PostHooks)
|
||||
postHooks, err := middleware.HooksFromHookConfigs(cfg.PostHooks, r.storage)
|
||||
if err != nil {
|
||||
return errors.New("failed to validate hook config: " + err.Error())
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (r *Run) Start(ps storage.PeerStore) error {
|
||||
"prehooks": cfg.PreHookNames(),
|
||||
"posthooks": cfg.PostHookNames(),
|
||||
})
|
||||
r.logic = middleware.NewLogic(cfg.ResponseConfig, r.peerStore, preHooks, postHooks)
|
||||
r.logic = middleware.NewLogic(cfg.ResponseConfig, r.storage, preHooks, postHooks)
|
||||
|
||||
if cfg.HTTPConfig.Addr != "" {
|
||||
log.Info("starting HTTP frontend", cfg.HTTPConfig)
|
||||
@@ -113,7 +113,7 @@ func combineErrors(prefix string, errs []error) error {
|
||||
}
|
||||
|
||||
// Stop shuts down an instance of Chihaya.
|
||||
func (r *Run) Stop(keepPeerStore bool) (storage.PeerStore, error) {
|
||||
func (r *Run) Stop(keepPeerStore bool) (storage.Storage, error) {
|
||||
log.Debug("stopping frontends and metrics server")
|
||||
if errs := r.sg.Stop().Wait(); len(errs) != 0 {
|
||||
return nil, combineErrors("failed while shutting down frontends", errs)
|
||||
@@ -126,13 +126,13 @@ func (r *Run) Stop(keepPeerStore bool) (storage.PeerStore, error) {
|
||||
|
||||
if !keepPeerStore {
|
||||
log.Debug("stopping peer store")
|
||||
if errs := r.peerStore.Stop().Wait(); len(errs) != 0 {
|
||||
if errs := r.storage.Stop().Wait(); len(errs) != 0 {
|
||||
return nil, combineErrors("failed while shutting down peer store", errs)
|
||||
}
|
||||
r.peerStore = nil
|
||||
r.storage = nil
|
||||
}
|
||||
|
||||
return r.peerStore, nil
|
||||
return r.storage, nil
|
||||
}
|
||||
|
||||
// RootRunCmdFunc implements a Cobra command that runs an instance of Chihaya
|
||||
|
||||
Reference in New Issue
Block a user