Bug fixing

This commit is contained in:
Alexei Anoshenko 2026-11-22 16:31:59 +03:00
parent 013fcbbbe8
commit f7f0594fbb
1 changed files with 19 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import (
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"golang.org/x/crypto/acme/autocert" "golang.org/x/crypto/acme/autocert"
@ -44,6 +45,7 @@ type application struct {
params AppParams params AppParams
createContentFunc func(Session) SessionContent createContentFunc func(Session) SessionContent
sessions map[int]sessionInfo sessions map[int]sessionInfo
sessionsMutex sync.Mutex
usedSessionIds []int usedSessionIds []int
} }
@ -66,6 +68,7 @@ func (app *application) Params() AppParams {
} }
func (app *application) Finish() { func (app *application) Finish() {
app.sessionsMutex.Lock()
for _, session := range app.sessions { for _, session := range app.sessions {
session.session.close() session.session.close()
if session.response != nil { if session.response != nil {
@ -73,6 +76,7 @@ func (app *application) Finish() {
session.response = nil session.response = nil
} }
} }
app.sessionsMutex.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
@ -102,6 +106,9 @@ func (app *application) nextSessionID() int {
} }
func (app *application) removeSession(id int) { func (app *application) removeSession(id int) {
app.sessionsMutex.Lock()
defer app.sessionsMutex.Unlock()
if info, ok := app.sessions[id]; ok { if info, ok := app.sessions[id]; ok {
if info.response != nil { if info.response != nil {
close(info.response) close(info.response)
@ -206,10 +213,13 @@ func (app *application) postHandler(w http.ResponseWriter, req *http.Request) {
var session Session = nil var session Session = nil
var response chan string = nil var response chan string = nil
app.sessionsMutex.Lock()
if info, ok := app.sessions[sessionID]; ok && info.response != nil { if info, ok := app.sessions[sessionID]; ok && info.response != nil {
response = info.response response = info.response
session = info.session session = info.session
} }
app.sessionsMutex.Unlock()
command := obj.Tag() command := obj.Tag()
@ -265,9 +275,11 @@ func (app *application) sseHandler(w http.ResponseWriter, req *http.Request) {
return return
} }
app.sessionsMutex.Lock()
if sessionInfo, ok := app.sessions[sessionID]; ok { if sessionInfo, ok := app.sessions[sessionID]; ok {
} }
app.sessionsMutex.Unlock()
*/ */
} }
@ -318,7 +330,11 @@ func (app *application) socketReader(bridge *wsBridge) {
case "reconnect": case "reconnect":
session = nil session = nil
if sessionID, ok := getSessionID(obj); ok { if sessionID, ok := getSessionID(obj); ok {
if info, ok := app.sessions[sessionID]; ok { app.sessionsMutex.Lock()
info, ok := app.sessions[sessionID]
app.sessionsMutex.Unlock()
if ok {
session = info.session session = info.session
session.setBridge(events, bridge) session.setBridge(events, bridge)
@ -378,10 +394,12 @@ func (app *application) createSession(params DataObject, events chan DataObject,
session := newSession(app, sessionID, "", params) session := newSession(app, sessionID, "", params)
session.setBridge(events, bridge) session.setBridge(events, bridge)
app.sessionsMutex.Lock()
app.sessions[sessionID] = sessionInfo{ app.sessions[sessionID] = sessionInfo{
session: session, session: session,
response: response, response: response,
} }
app.sessionsMutex.Unlock()
return session return session
} }