udp: replace boolean-synchronization with channels
This commit is contained in:
parent
386e0d2abc
commit
bd546f6eba
1 changed files with 15 additions and 6 deletions
21
udp/udp.go
21
udp/udp.go
|
@ -22,7 +22,7 @@ import (
|
||||||
type Server struct {
|
type Server struct {
|
||||||
config *config.Config
|
config *config.Config
|
||||||
tracker *tracker.Tracker
|
tracker *tracker.Tracker
|
||||||
done bool
|
closing chan struct{}
|
||||||
booting chan struct{}
|
booting chan struct{}
|
||||||
sock *net.UDPConn
|
sock *net.UDPConn
|
||||||
|
|
||||||
|
@ -55,7 +55,12 @@ func (s *Server) serve() error {
|
||||||
s.sock = sock
|
s.sock = sock
|
||||||
close(s.booting)
|
close(s.booting)
|
||||||
|
|
||||||
for !s.done {
|
for {
|
||||||
|
select {
|
||||||
|
case <-s.closing:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
buffer := pool.TakeSlice()
|
buffer := pool.TakeSlice()
|
||||||
sock.SetReadDeadline(time.Now().Add(time.Second))
|
sock.SetReadDeadline(time.Now().Add(time.Second))
|
||||||
n, addr, err := sock.ReadFromUDP(buffer)
|
n, addr, err := sock.ReadFromUDP(buffer)
|
||||||
|
@ -97,11 +102,14 @@ func (s *Server) Serve() {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// Generate a new IV every hour.
|
// Generate a new IV every hour.
|
||||||
for range time.Tick(time.Hour) {
|
t := time.NewTicker(time.Hour)
|
||||||
if s.done {
|
for {
|
||||||
|
select {
|
||||||
|
case <-t.C:
|
||||||
|
s.connIDGen.NewIV()
|
||||||
|
case <-s.closing:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.connIDGen.NewIV()
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -114,7 +122,7 @@ func (s *Server) Serve() {
|
||||||
|
|
||||||
// Stop cleanly shuts down the server.
|
// Stop cleanly shuts down the server.
|
||||||
func (s *Server) Stop() {
|
func (s *Server) Stop() {
|
||||||
s.done = true
|
close(s.closing)
|
||||||
s.sock.SetReadDeadline(time.Now())
|
s.sock.SetReadDeadline(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +137,7 @@ func NewServer(cfg *config.Config, tkr *tracker.Tracker) *Server {
|
||||||
config: cfg,
|
config: cfg,
|
||||||
tracker: tkr,
|
tracker: tkr,
|
||||||
connIDGen: gen,
|
connIDGen: gen,
|
||||||
|
closing: make(chan struct{}),
|
||||||
booting: make(chan struct{}),
|
booting: make(chan struct{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue