initial architecture overhaul

This commit is contained in:
Jimmy Zelinskie
2013-08-23 15:39:42 -04:00
parent d756de9127
commit d3f3c81c61
13 changed files with 103 additions and 129 deletions
+5 -5
View File
@@ -12,7 +12,7 @@ import (
"strconv"
"time"
"github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/models"
)
func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
@@ -24,7 +24,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
}
// Retry failed transactions a specified number of times
for i := 0; i < s.conf.Storage.TxRetries; i++ {
for i := 0; i < s.conf.Cache.TxRetries; i++ {
// Start a transaction
tx, err := s.dbConnPool.Get()
@@ -69,7 +69,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
}
// Create a new peer object from the request
peer := &storage.Peer{
peer := &models.Peer{
ID: peerID,
UserID: user.ID,
TorrentID: torrent.ID,
@@ -332,7 +332,7 @@ func minInt(a, b int) int {
return b
}
func writeSeeders(w http.ResponseWriter, t *storage.Torrent, count, numWant int, compact bool) {
func writeSeeders(w http.ResponseWriter, t *models.Torrent, count, numWant int, compact bool) {
for _, seed := range t.Seeders {
if count >= numWant {
break
@@ -353,7 +353,7 @@ func writeSeeders(w http.ResponseWriter, t *storage.Torrent, count, numWant int,
}
}
func writeLeechers(w http.ResponseWriter, t *storage.Torrent, count, numWant int, compact bool) {
func writeLeechers(w http.ResponseWriter, t *models.Torrent, count, numWant int, compact bool) {
for _, leech := range t.Leechers {
if count >= numWant {
break
-28
View File
@@ -1,28 +0,0 @@
// Copyright 2013 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package server
import (
zmq "github.com/alecthomas/gozmq"
)
func (s *Server) publishQueue() {
context, err := zmq.NewContext()
if err != nil {
panic(err)
}
defer context.Close()
socket, err := context.NewSocket(zmq.PUB)
if err != nil {
panic(err)
}
defer socket.Close()
socket.Bind(s.conf.PubAddr)
for msg := range s.pubChan {
socket.Send([]byte(msg), 0)
}
}
+2 -2
View File
@@ -11,7 +11,7 @@ import (
"net/http"
"path"
"github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/models"
)
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
@@ -68,7 +68,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
w.(http.Flusher).Flush()
}
func writeScrapeInfo(w io.Writer, torrent *storage.Torrent) {
func writeScrapeInfo(w io.Writer, torrent *models.Torrent) {
io.WriteString(w, "d")
writeBencoded(w, "complete")
writeBencoded(w, len(torrent.Seeders))
+5 -5
View File
@@ -17,14 +17,15 @@ import (
"sync/atomic"
"time"
"github.com/pushrax/chihaya/cache"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/models"
)
type Server struct {
conf *config.Config
listener net.Listener
dbConnPool storage.Pool
dbConnPool cache.Pool
serving bool
startTime time.Time
@@ -40,7 +41,7 @@ type Server struct {
}
func New(conf *config.Config) (*Server, error) {
pool, err := storage.Open(&conf.Storage)
pool, err := cache.Open(&conf.Cache)
if err != nil {
return nil, err
}
@@ -69,7 +70,6 @@ func (s *Server) ListenAndServe() error {
s.startTime = time.Now()
go s.updateStats()
go s.publishQueue()
s.Serve(s.listener)
s.waitgroup.Wait()
@@ -126,7 +126,7 @@ func fail(err error, w http.ResponseWriter, r *http.Request) {
w.(http.Flusher).Flush()
}
func validateUser(tx storage.Tx, dir string) (*storage.User, error) {
func validateUser(tx cache.Tx, dir string) (*models.User, error) {
if len(dir) != 34 {
return nil, errors.New("Passkey is invalid")
}
-3
View File
@@ -47,6 +47,3 @@ func writeBencoded(w io.Writer, data interface{}) {
panic("Tried to bencode an unsupported type!")
}
}
func compact() {
}