Skip to content
Snippets Groups Projects
Commit 75eed018 authored by Hans Moog's avatar Hans Moog
Browse files

Fix: fixed shutdown error of dashboard

parent 9362c0fb
No related branches found
No related tags found
No related merge requests found
package dashboard package dashboard
import ( import (
"log" "golang.org/x/net/context"
"net/http" "net/http"
"time"
"github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/packages/events"
...@@ -10,11 +11,18 @@ import ( ...@@ -10,11 +11,18 @@ import (
"github.com/iotaledger/goshimmer/plugins/metrics" "github.com/iotaledger/goshimmer/plugins/metrics"
) )
var server *http.Server
var router *http.ServeMux
var PLUGIN = node.NewPlugin("Dashboard", configure, run) var PLUGIN = node.NewPlugin("Dashboard", configure, run)
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
http.HandleFunc("/dashboard", ServeHome) router = http.NewServeMux()
http.HandleFunc("/ws", ServeWs) server = &http.Server{Addr: ":8081", Handler: router}
router.HandleFunc("/dashboard", ServeHome)
router.HandleFunc("/ws", ServeWs)
// send the sampledTPS to client via websocket, use uint32 to save mem // send the sampledTPS to client via websocket, use uint32 to save mem
metrics.Events.ReceivedTPSUpdated.Attach(events.NewClosure(func(sampledTPS uint64) { metrics.Events.ReceivedTPSUpdated.Attach(events.NewClosure(func(sampledTPS uint64) {
...@@ -23,12 +31,21 @@ func configure(plugin *node.Plugin) { ...@@ -23,12 +31,21 @@ func configure(plugin *node.Plugin) {
TPSQ = TPSQ[1:] TPSQ = TPSQ[1:]
} }
})) }))
daemon.Events.Shutdown.Attach(events.NewClosure(func() {
ctx, cancel := context.WithTimeout(context.Background(), 0*time.Second)
defer cancel()
_ = server.Shutdown(ctx)
}))
} }
func run(plugin *node.Plugin) { func run(plugin *node.Plugin) {
daemon.BackgroundWorker("Dashboard Updater", func() { daemon.BackgroundWorker("Dashboard Updater", func() {
if err := http.ListenAndServe(":8081", nil); err != nil { go func() {
log.Fatal(err) if err := server.ListenAndServe(); err != nil {
} plugin.LogFailure(err.Error())
}
}()
}) })
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment