Wait for backend to start before accepting connections

This commit is contained in:
mkwcat
2024-05-05 20:56:27 -04:00
parent 9f9f3b10f8
commit 22a3e1d98b
7 changed files with 89 additions and 86 deletions

View File

@@ -70,20 +70,22 @@ func StartServer() {
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
logging.Notice("GSTATS", "Listening on", address)
go func() {
// Close the listener when the application closes.
defer l.Close()
logging.Notice("GSTATS", "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
panic(err)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
panic(err)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}()
}
// Handles incoming requests.

View File

@@ -2,19 +2,10 @@ package gpsp
import (
"bufio"
"context"
"net"
"wwfc/common"
"wwfc/gpcm"
"wwfc/logging"
"github.com/jackc/pgx/v4/pgxpool"
)
var (
ctx = context.Background()
pool *pgxpool.Pool
userId int64
)
func StartServer() {
@@ -27,20 +18,22 @@ func StartServer() {
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
logging.Notice("GPSP", "Listening on", address)
go func() {
// Close the listener when the application closes.
defer l.Close()
logging.Notice("GPSP", "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
panic(err)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
panic(err)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}()
}
// Handles incoming requests.
@@ -77,19 +70,15 @@ func handleRequest(conn net.Conn) {
logging.Error(moduleName, "Unknown command:", command.Command)
logging.Error(moduleName, "Raw data:", string(buffer))
replyError(moduleName, conn, gpcm.ErrParse)
break
case "ka":
conn.Write([]byte(`\ka\\final\`))
break
case "otherslist":
conn.Write([]byte(handleOthersList(command)))
break
case "search":
conn.Write([]byte(handleSearch(command)))
break
}
}
}

30
main.go
View File

@@ -63,7 +63,20 @@ func backendMain() {
os.Exit(1)
}
logging.Notice("BACKEND", "Listening on", aurora.BrightCyan(address))
common.ConnectFrontend()
wg := &sync.WaitGroup{}
actions := []func(){nas.StartServer, gpcm.StartServer, qr2.StartServer, gpsp.StartServer, serverbrowser.StartServer, sake.StartServer, natneg.StartServer, api.StartServer, gamestats.StartServer}
wg.Add(len(actions))
for _, action := range actions {
go func(ac func()) {
defer wg.Done()
ac()
}(action)
}
// Wait for all servers to start
wg.Wait()
go func() {
for {
@@ -77,19 +90,10 @@ func backendMain() {
}
}()
// TODO: Wait until the servers are started before allowing in connections
logging.Notice("BACKEND", "Listening on", aurora.BrightCyan(address))
wg := &sync.WaitGroup{}
actions := []func(){nas.StartServer, gpcm.StartServer, qr2.StartServer, gpsp.StartServer, serverbrowser.StartServer, sake.StartServer, natneg.StartServer, api.StartServer, gamestats.StartServer}
wg.Add(len(actions))
for _, action := range actions {
go func(ac func()) {
defer wg.Done()
ac()
}(action)
}
wg.Wait()
// Prevent application from exiting
select {}
}
// RPCPacket.NewConnection is called by the frontend to notify the backend of a new connection

View File

@@ -55,7 +55,9 @@ func StartServer() {
}
logging.Notice("NAS", "Starting HTTP server on", address)
panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest)))
go func() {
panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest)))
}()
}
var regexSakeHost = regexp.MustCompile(`^([a-z\-]+\.)?sake\.gs\.`)

View File

@@ -94,19 +94,21 @@ func StartServer() {
natnegConn = conn
// Close the listener when the application closes.
defer conn.Close()
logging.Notice("NATNEG", "Listening on", address)
go func() {
// Close the listener when the application closes.
defer conn.Close()
logging.Notice("NATNEG", "Listening on", address)
for {
buffer := make([]byte, 1024)
size, addr, err := conn.ReadFrom(buffer)
if err != nil {
continue
for {
buffer := make([]byte, 1024)
size, addr, err := conn.ReadFrom(buffer)
if err != nil {
continue
}
go handleConnection(conn, addr, buffer[:size])
}
go handleConnection(conn, addr, buffer[:size])
}
}()
}
func handleConnection(conn net.PacketConn, addr net.Addr, buffer []byte) {

View File

@@ -40,19 +40,21 @@ func StartServer() {
masterConn = conn
// Close the listener when the application closes.
defer conn.Close()
logging.Notice("QR2", "Listening on", address)
go func() {
// Close the listener when the application closes.
defer conn.Close()
logging.Notice("QR2", "Listening on", address)
for {
buf := make([]byte, 1024)
_, addr, err := conn.ReadFrom(buf)
if err != nil {
continue
for {
buf := make([]byte, 1024)
_, addr, err := conn.ReadFrom(buf)
if err != nil {
continue
}
go handleConnection(conn, addr, buf)
}
go handleConnection(conn, addr, buf)
}
}()
}
func handleConnection(conn net.PacketConn, addr net.Addr, buffer []byte) {

View File

@@ -64,21 +64,23 @@ func StartServer() {
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
logging.Notice(ModuleName, "Listening on", address)
go func() {
// Close the listener when the application closes.
defer l.Close()
logging.Notice(ModuleName, "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}()
}
// Handles incoming requests.