Changes in server

This commit is contained in:
qowevisa 2024-06-09 07:58:15 +03:00
parent e0c5e6ede4
commit f69cf7911e

View File

@ -10,21 +10,41 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime"
"runtime/pprof"
"sync" "sync"
"syscall" "syscall"
"time"
com "git.qowevisa.me/Qowevisa/gotell/communication" com "git.qowevisa.me/Qowevisa/gotell/communication"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
"git.qowevisa.me/Qowevisa/gotell/profilers"
) )
func atEnd() { func captureHeapProfile() {
profilers.GetMemoryProfiler() currentTime := time.Now().Format("2006_01_02T15_04")
filename := fmt.Sprintf("heap_%s.prof", currentTime)
f, err := os.Create(filename)
if err != nil {
log.Fatalf("could not create memory profile: %v", err)
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatalf("could not write memory profile: %v", err)
}
} }
func main() { func main() {
cpuProfDefer := profilers.GetCPUProfiler() f, err := os.Create("cpu.prof")
defer cpuProfDefer() if err != nil {
panic(err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
userCenter.Init() userCenter.Init()
linkCenter.Init() linkCenter.Init()
connCenter.Init() connCenter.Init()
@ -55,20 +75,25 @@ func main() {
defer listener.Close() defer listener.Close()
log.Printf("server: listening on %s", service) log.Printf("server: listening on %s", service)
defer atEnd()
var wg sync.WaitGroup var wg sync.WaitGroup
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() { go func() {
c := make(chan os.Signal, 1) <-quit
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
log.Println("received shutdown signal") log.Println("received shutdown signal")
listener.Close() listener.Close()
wg.Wait() wg.Wait()
os.Exit(0) os.Exit(0)
}() }()
ticker := time.NewTicker(3 * time.Minute)
go func() {
for range ticker.C {
captureHeapProfile()
}
}()
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {