5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-09 23:20:28 +00:00

more cleanup and fix a busyloop when the admin socket is shut down

This commit is contained in:
Arceliar 2021-05-29 21:37:13 -05:00
parent e25ad9ed21
commit 1db7437b80
5 changed files with 36 additions and 15 deletions

View File

@ -25,7 +25,7 @@ type AdminSocket struct {
listenaddr string listenaddr string
listener net.Listener listener net.Listener
handlers map[string]handler handlers map[string]handler
started bool done chan struct{}
} }
type AdminSocketResponse struct { type AdminSocketResponse struct {
@ -69,6 +69,8 @@ func (a *AdminSocket) Init(c *core.Core, state *config.NodeState, log *log.Logge
a.handlers = make(map[string]handler) a.handlers = make(map[string]handler)
current := state.GetCurrent() current := state.GetCurrent()
a.listenaddr = current.AdminListen a.listenaddr = current.AdminListen
a.done = make(chan struct{})
close(a.done) // Start in a done / not-started state
_ = a.AddHandler("list", []string{}, func(_ json.RawMessage) (interface{}, error) { _ = a.AddHandler("list", []string{}, func(_ json.RawMessage) (interface{}, error) {
res := &ListResponse{ res := &ListResponse{
List: map[string]ListEntry{}, List: map[string]ListEntry{},
@ -144,21 +146,32 @@ func (a *AdminSocket) SetupAdminHandlers(na *AdminSocket) {
// Start runs the admin API socket to listen for / respond to admin API calls. // Start runs the admin API socket to listen for / respond to admin API calls.
func (a *AdminSocket) Start() error { func (a *AdminSocket) Start() error {
if a.listenaddr != "none" && a.listenaddr != "" { if a.listenaddr != "none" && a.listenaddr != "" {
a.done = make(chan struct{})
go a.listen() go a.listen()
a.started = true
} }
return nil return nil
} }
// IsStarted returns true if the module has been started. // IsStarted returns true if the module has been started.
func (a *AdminSocket) IsStarted() bool { func (a *AdminSocket) IsStarted() bool {
return a.started select {
case <-a.done:
// Not blocking, so we're not currently running
return false
default:
// Blocked, so we must have started
return true
}
} }
// Stop will stop the admin API and close the socket. // Stop will stop the admin API and close the socket.
func (a *AdminSocket) Stop() error { func (a *AdminSocket) Stop() error {
if a.listener != nil { if a.listener != nil {
a.started = false select {
case <-a.done:
default:
close(a.done)
}
return a.listener.Close() return a.listener.Close()
} }
return nil return nil
@ -215,6 +228,14 @@ func (a *AdminSocket) listen() {
conn, err := a.listener.Accept() conn, err := a.listener.Accept()
if err == nil { if err == nil {
go a.handleRequest(conn) go a.handleRequest(conn)
} else {
select {
case <-a.done:
// Not blocked, so we havent started or already stopped
return
default:
// Blocked, so we're supposed to keep running
}
} }
} }
} }

View File

@ -95,10 +95,12 @@ func (c *Core) _addPeerLoop() {
} }
} }
if c.addPeerTimer != nil {
c.addPeerTimer = time.AfterFunc(time.Minute, func() { c.addPeerTimer = time.AfterFunc(time.Minute, func() {
c.Act(nil, c._addPeerLoop) c.Act(nil, c._addPeerLoop)
}) })
} }
}
// Start starts up Yggdrasil using the provided config.NodeConfig, and outputs // Start starts up Yggdrasil using the provided config.NodeConfig, and outputs
// debug logging through the provided log.Logger. The started stack will include // debug logging through the provided log.Logger. The started stack will include
@ -149,7 +151,9 @@ func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState
// return nil, err // return nil, err
//} //}
c.Act(c, c._addPeerLoop) c.addPeerTimer = time.AfterFunc(0, func() {
c.Act(nil, c._addPeerLoop)
})
c.log.Infoln("Startup complete") c.log.Infoln("Startup complete")
return &c.config, nil return &c.config, nil
@ -166,6 +170,7 @@ func (c *Core) _stop() {
c.log.Infoln("Stopping...") c.log.Infoln("Stopping...")
if c.addPeerTimer != nil { if c.addPeerTimer != nil {
c.addPeerTimer.Stop() c.addPeerTimer.Stop()
c.addPeerTimer = nil
} }
c.links.stop() c.links.stop()
/* FIXME this deadlocks, need a waitgroup or something to coordinate shutdown /* FIXME this deadlocks, need a waitgroup or something to coordinate shutdown

View File

@ -142,7 +142,7 @@ func TestCore_Start_Transfer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if bytes.Compare(msg, buf) != 0 { if !bytes.Equal(msg, buf) {
t.Fatal("expected echo") t.Fatal("expected echo")
} }
<-done <-done

View File

@ -31,7 +31,6 @@ import (
) )
const default_timeout = 6 * time.Second const default_timeout = 6 * time.Second
const tcp_ping_interval = (default_timeout * 2 / 3)
// The TCP listener and information about active TCP connections, to avoid duplication. // The TCP listener and information about active TCP connections, to avoid duplication.
type tcp struct { type tcp struct {

View File

@ -17,11 +17,7 @@ import (
"golang.org/x/net/ipv6" "golang.org/x/net/ipv6"
) )
const len_ETHER = 14 type ICMPv6 struct{}
type ICMPv6 struct {
tun *TunAdapter
}
// Marshal returns the binary encoding of h. // Marshal returns the binary encoding of h.
func ipv6Header_Marshal(h *ipv6.Header) ([]byte, error) { func ipv6Header_Marshal(h *ipv6.Header) ([]byte, error) {