mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-26 01:18:10 -07:00
storage reorganized around a pool and transactions
This commit is contained in:
+10
-10
@@ -23,16 +23,22 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Start a transaction
|
||||
tx, err := s.dbConnPool.Get()
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
|
||||
// Validate the user's passkey
|
||||
passkey, _ := path.Split(r.URL.Path)
|
||||
user, err := s.FindUser(passkey)
|
||||
user, err := validateUser(tx, passkey)
|
||||
if err != nil {
|
||||
fail(err, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the user's client is whitelisted
|
||||
whitelisted, err := s.dataStore.ClientWhitelisted(peerID)
|
||||
whitelisted, err := tx.ClientWhitelisted(peerID)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
@@ -42,7 +48,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Find the specified torrent
|
||||
torrent, exists, err := s.dataStore.FindTorrent(infohash)
|
||||
torrent, exists, err := tx.FindTorrent(infohash)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
@@ -51,15 +57,9 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Begin a data store transaction
|
||||
tx, err := s.dataStore.Begin()
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
|
||||
// If the torrent was pruned and the user is seeding, unprune it
|
||||
if !torrent.Active && left == 0 {
|
||||
err := tx.Active(torrent)
|
||||
err := tx.MarkActive(torrent)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
|
||||
+18
-9
@@ -15,24 +15,32 @@ import (
|
||||
)
|
||||
|
||||
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||
passkey, _ := path.Split(r.URL.Path)
|
||||
_, err := s.FindUser(passkey)
|
||||
if err != nil {
|
||||
fail(err, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the query
|
||||
pq, err := parseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
fail(errors.New("Error parsing query"), w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Start a transaction
|
||||
tx, err := s.dbConnPool.Get()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Find and validate the user
|
||||
passkey, _ := path.Split(r.URL.Path)
|
||||
_, err = validateUser(tx, passkey)
|
||||
if err != nil {
|
||||
fail(err, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
io.WriteString(w, "d")
|
||||
writeBencoded(w, "files")
|
||||
if pq.Infohashes != nil {
|
||||
for _, infohash := range pq.Infohashes {
|
||||
torrent, exists, err := s.dataStore.FindTorrent(infohash)
|
||||
torrent, exists, err := tx.FindTorrent(infohash)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
@@ -42,7 +50,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
} else if infohash, exists := pq.Params["info_hash"]; exists {
|
||||
torrent, exists, err := s.dataStore.FindTorrent(infohash)
|
||||
torrent, exists, err := tx.FindTorrent(infohash)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
@@ -53,6 +61,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
io.WriteString(w, "e")
|
||||
|
||||
// Finish up and write headers
|
||||
r.Close = true
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
w.Header().Add("Connection", "close")
|
||||
|
||||
+9
-9
@@ -22,9 +22,9 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
conf *config.Config
|
||||
listener net.Listener
|
||||
dataStore storage.DS
|
||||
conf *config.Config
|
||||
listener net.Listener
|
||||
dbConnPool storage.Pool
|
||||
|
||||
serving bool
|
||||
startTime time.Time
|
||||
@@ -38,14 +38,14 @@ type Server struct {
|
||||
}
|
||||
|
||||
func New(conf *config.Config) (*Server, error) {
|
||||
ds, err := storage.Open(&conf.Storage)
|
||||
pool, err := storage.Open(&conf.Storage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
conf: conf,
|
||||
dataStore: ds,
|
||||
conf: conf,
|
||||
dbConnPool: pool,
|
||||
Server: http.Server{
|
||||
Addr: conf.Addr,
|
||||
ReadTimeout: conf.ReadTimeout.Duration,
|
||||
@@ -75,7 +75,7 @@ func (s *Server) ListenAndServe() error {
|
||||
func (s *Server) Stop() error {
|
||||
s.serving = false
|
||||
s.waitgroup.Wait()
|
||||
err := s.dataStore.Close()
|
||||
err := s.dbConnPool.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -121,13 +121,13 @@ func fail(err error, w http.ResponseWriter, r *http.Request) {
|
||||
w.(http.Flusher).Flush()
|
||||
}
|
||||
|
||||
func (s *Server) FindUser(dir string) (*storage.User, error) {
|
||||
func validateUser(tx storage.Tx, dir string) (*storage.User, error) {
|
||||
if len(dir) != 34 {
|
||||
return nil, errors.New("Passkey is invalid")
|
||||
}
|
||||
passkey := dir[1:33]
|
||||
|
||||
user, exists, err := s.dataStore.FindUser(passkey)
|
||||
user, exists, err := tx.FindUser(passkey)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user