Add profiling, fix race condition with WaitGroup
This commit is contained in:
15
src/main.go
15
src/main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@@ -25,7 +26,7 @@ var (
|
|||||||
})
|
})
|
||||||
r *Redis = &Redis{}
|
r *Redis = &Redis{}
|
||||||
conf *Config = DefaultConfig
|
conf *Config = DefaultConfig
|
||||||
instanceID uint16
|
instanceID uint16 = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -80,6 +81,18 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if v := os.Getenv("PROFILE"); len(v) > 0 {
|
||||||
|
port, err := strconv.ParseUint(v, 10, 16)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go Profile(uint16(port))
|
||||||
|
|
||||||
|
log.Printf("Profiler is listening on :%d\n", port)
|
||||||
|
}
|
||||||
|
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
go ListenAndServe(conf.Host, conf.Port+instanceID)
|
go ListenAndServe(conf.Host, conf.Port+instanceID)
|
||||||
|
|||||||
15
src/profiler.go
Normal file
15
src/profiler.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/pprof"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Profile(port uint16) {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", pprof.Profile)
|
||||||
|
|
||||||
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mux))
|
||||||
|
}
|
||||||
@@ -235,31 +235,37 @@ func GetServerIcon(host string, port uint16) ([]byte, time.Duration, error) {
|
|||||||
func FetchJavaStatus(host string, port uint16, enableQuery bool) JavaStatusResponse {
|
func FetchJavaStatus(host string, port uint16, enableQuery bool) JavaStatusResponse {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
if enableQuery {
|
||||||
|
wg.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
var status interface{} = nil
|
var status interface{} = nil
|
||||||
var query *response.FullQuery = nil
|
var query *response.FullQuery = nil
|
||||||
|
|
||||||
|
// Status
|
||||||
|
{
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
if result, _ := mcutil.Status(host, port); result != nil {
|
if result, _ := mcutil.Status(host, port); result != nil {
|
||||||
status = result
|
status = result
|
||||||
} else if result, _ := mcutil.StatusLegacy(host, port); result != nil {
|
} else if result, _ := mcutil.StatusLegacy(host, port); result != nil {
|
||||||
status = result
|
status = result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
wg.Add(1)
|
// Query
|
||||||
|
|
||||||
if enableQuery {
|
if enableQuery {
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
query, _ = mcutil.FullQuery(host, port, options.Query{
|
query, _ = mcutil.FullQuery(host, port, options.Query{
|
||||||
Timeout: time.Second,
|
Timeout: time.Second,
|
||||||
})
|
})
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Done()
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|||||||
Reference in New Issue
Block a user