mirror of
https://github.com/LBRYFoundation/tracker.git
synced 2025-09-02 02:05:17 +00:00
commit
91715229f1
2 changed files with 42 additions and 9 deletions
|
@ -84,8 +84,13 @@ func NewFrontend(logic frontend.TrackerLogic, cfg Config) (*Frontend, error) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := f.listen()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := f.listenAndServe(); err != nil {
|
if err := f.serve(); err != nil {
|
||||||
log.Fatal("failed while serving udp", log.Err(err))
|
log.Fatal("failed while serving udp", log.Err(err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -112,19 +117,19 @@ func (t *Frontend) Stop() stop.Result {
|
||||||
return c.Result()
|
return c.Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
// listenAndServe blocks while listening and serving UDP BitTorrent requests
|
// listen resolves the address and binds the server socket.
|
||||||
// until Stop() is called or an error is returned.
|
func (t *Frontend) listen() error {
|
||||||
func (t *Frontend) listenAndServe() error {
|
|
||||||
udpAddr, err := net.ResolveUDPAddr("udp", t.Addr)
|
udpAddr, err := net.ResolveUDPAddr("udp", t.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.socket, err = net.ListenUDP("udp", udpAddr)
|
t.socket, err = net.ListenUDP("udp", udpAddr)
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// serve blocks while listening and serving UDP BitTorrent requests
|
||||||
|
// until Stop() is called or an error is returned.
|
||||||
|
func (t *Frontend) serve() error {
|
||||||
pool := bytepool.New(2048)
|
pool := bytepool.New(2048)
|
||||||
|
|
||||||
t.wg.Add(1)
|
t.wg.Add(1)
|
||||||
|
@ -134,7 +139,7 @@ func (t *Frontend) listenAndServe() error {
|
||||||
// Check to see if we need to shutdown.
|
// Check to see if we need to shutdown.
|
||||||
select {
|
select {
|
||||||
case <-t.closing:
|
case <-t.closing:
|
||||||
log.Debug("udp listenAndServe() received shutdown signal")
|
log.Debug("udp serve() received shutdown signal")
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
28
frontend/udp/frontend_test.go
Normal file
28
frontend/udp/frontend_test.go
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package udp_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/chihaya/chihaya/frontend/udp"
|
||||||
|
"github.com/chihaya/chihaya/middleware"
|
||||||
|
"github.com/chihaya/chihaya/storage"
|
||||||
|
_ "github.com/chihaya/chihaya/storage/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStartStopRaceIssue437(t *testing.T) {
|
||||||
|
ps, err := storage.NewPeerStore("memory", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var responseConfig middleware.ResponseConfig
|
||||||
|
lgc := middleware.NewLogic(responseConfig, ps, nil, nil)
|
||||||
|
fe, err := udp.NewFrontend(lgc, udp.Config{Addr: "127.0.0.1:0"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
errC := fe.Stop()
|
||||||
|
errs := <-errC
|
||||||
|
if len(errs) != 0 {
|
||||||
|
t.Fatal(errs[0])
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue