diff --git a/chihaya.go b/chihaya.go index 1c51899..096427a 100644 --- a/chihaya.go +++ b/chihaya.go @@ -11,7 +11,10 @@ import ( "github.com/chihaya/chihaya/pkg/event" ) +// PeerID represents a peer ID. type PeerID string + +// InfoHash represents an infohash in hexadecimal notation. type InfoHash string // AnnounceRequest represents the parsed parameters from an announce request. diff --git a/server/http/server.go b/server/http/server.go index b2ae231..5d710aa 100644 --- a/server/http/server.go +++ b/server/http/server.go @@ -40,6 +40,9 @@ type httpServer struct { grace *graceful.Server } +// Start runs the server and blocks until it has exited. +// +// It panics if the server exits unexpectedly. func (s *httpServer) Start() { s.grace = &graceful.Server{ Server: &http.Server{ @@ -81,6 +84,7 @@ func (s *httpServer) Start() { log.Println("HTTP server shut down cleanly") } +// Stop stops the server and blocks until the server has exited. func (s *httpServer) Stop() { s.grace.Stop(s.grace.Timeout) <-s.grace.StopChan() diff --git a/server/prometheus/prometheus.go b/server/prometheus/prometheus.go index 0bd9329..1dbf3f1 100644 --- a/server/prometheus/prometheus.go +++ b/server/prometheus/prometheus.go @@ -71,6 +71,9 @@ type Server struct { var _ server.Server = &Server{} +// Start starts the prometheus server and blocks until it exits. +// +// It panics if the server exits unexpectedly. func (s *Server) Start() { s.grace = &graceful.Server{ Server: &http.Server{ @@ -93,6 +96,7 @@ func (s *Server) Start() { log.Println("Prometheus server shut down cleanly") } +// Stop stops the prometheus server and blocks until it exits. func (s *Server) Stop() { s.grace.Stop(s.cfg.ShutdownTimeout) <-s.grace.StopChan() diff --git a/server/server.go b/server/server.go index b20bd36..1adcb16 100644 --- a/server/server.go +++ b/server/server.go @@ -46,6 +46,11 @@ func New(cfg *chihaya.ServerConfig, tkr *tracker.Tracker) (Server, error) { // Server represents one instance of a server accessing the tracker. type Server interface { + // Start starts a server and blocks until the server exits. + // + // It should panic if the server exits unexpectedly. Start() + + // Stop stops a server and blocks until the server exits. Stop() } diff --git a/server/store/memory/peer_store_test.go b/server/store/memory/peer_store_test.go index 62e350f..8233ced 100644 --- a/server/store/memory/peer_store_test.go +++ b/server/store/memory/peer_store_test.go @@ -50,8 +50,8 @@ func TestPeerStoreAPI(t *testing.T) { 1, } config = store.DriverConfig{ - "memory", - unmarshalledConfig, + Name: "memory", + Config: unmarshalledConfig, } d = &peerStoreDriver{} ) @@ -62,9 +62,9 @@ func TestPeerStoreAPI(t *testing.T) { for _, p := range peers { // Construct chihaya.Peer from test data. peer := chihaya.Peer{ - chihaya.PeerID(p.peerID), - net.ParseIP(p.ip), - p.port, + ID: chihaya.PeerID(p.peerID), + IP: net.ParseIP(p.ip), + Port: p.port, } if p.seeder { @@ -95,9 +95,9 @@ func TestPeerStoreAPI(t *testing.T) { for _, p := range peers { // Construct chihaya.Peer from test data. peer := chihaya.Peer{ - chihaya.PeerID(p.peerID), - net.ParseIP(p.ip), - p.port, + ID: chihaya.PeerID(p.peerID), + IP: net.ParseIP(p.ip), + Port: p.port, } if p.seeder { @@ -121,9 +121,9 @@ func TestPeerStoreAPI(t *testing.T) { for _, p := range peers { // Construct chihaya.Peer from test data. peer := chihaya.Peer{ - chihaya.PeerID(p.peerID), - net.ParseIP(p.ip), - p.port, + ID: chihaya.PeerID(p.peerID), + IP: net.ParseIP(p.ip), + Port: p.port, } if p.seeder { s.PutSeeder(hash, peer) @@ -136,9 +136,9 @@ func TestPeerStoreAPI(t *testing.T) { assert.Equal(t, 6, s.NumSeeders(hash)) assert.Equal(t, 4, s.NumLeechers(hash)) peer := chihaya.Peer{ - chihaya.PeerID(peers[0].peerID), - net.ParseIP(peers[0].ip), - peers[0].port, + ID: chihaya.PeerID(peers[0].peerID), + IP: net.ParseIP(peers[0].ip), + Port: peers[0].port, } err = s.GraduateLeecher(hash, peer) assert.Nil(t, err) diff --git a/server/store/store.go b/server/store/store.go index e67ad18..ff8a811 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -57,6 +57,7 @@ func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Ser return theStore, nil } +// Config represents the configuration for the store. type Config struct { Addr string `yaml:"addr"` RequestTimeout time.Duration `yaml:"request_timeout"` @@ -68,6 +69,7 @@ type Config struct { StringStore DriverConfig `yaml:"string_store"` } +// DriverConfig represents the configuration for a store driver. type DriverConfig struct { Name string `yaml:"name"` Config interface{} `yaml:"config"` @@ -99,6 +101,7 @@ func MustGetStore() *Store { return theStore } +// Store provides storage for a tracker. type Store struct { cfg *Config tkr *tracker.Tracker @@ -110,12 +113,14 @@ type Store struct { StringStore } +// Start starts the store drivers and blocks until all of them exit. func (s *Store) Start() { <-s.shutdown s.wg.Wait() log.Println("Store server shut down cleanly") } +// Stop stops the store drivers and waits for them to exit. func (s *Store) Stop() { close(s.shutdown) s.wg.Wait() diff --git a/server/store/string_store.go b/server/store/string_store.go index 4c28f23..8620053 100644 --- a/server/store/string_store.go +++ b/server/store/string_store.go @@ -16,7 +16,7 @@ type StringStore interface { } // StringStoreDriver represents an interface for creating a handle to the -// storage of swarms. +// storage of strings. type StringStoreDriver interface { New(*DriverConfig) (StringStore, error) }