split listenAndServe into 2 functions

This commit is contained in:
Cenk Alti 2018-12-26 18:15:05 +03:00
parent 2a26215f2a
commit b345eb3899
No known key found for this signature in database
GPG key ID: 2DB2EA6FD1BF1761

View file

@ -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,18 @@ func (t *Frontend) Stop() stop.Result {
return c.Result() return c.Result()
} }
// listenAndServe blocks while listening and serving UDP BitTorrent requests func (t *Frontend) listen() error {
// until Stop() is called or an error is returned.
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 }
}
// listenAndServe 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)