Skip to content

Commit 8113357

Browse files
authored
fix_: status backend server websocket IO wait (#6154)
1 parent 50933aa commit 8113357

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

cmd/status-backend/server/server.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,26 @@ func (s *Server) signalHandler(data []byte) {
5353
s.lock.Lock()
5454
defer s.lock.Unlock()
5555

56+
deleteConnection := func(connection *websocket.Conn) {
57+
delete(s.connections, connection)
58+
err := connection.Close()
59+
if err != nil {
60+
log.Error("failed to close connection", "error", err)
61+
}
62+
}
63+
5664
for connection := range s.connections {
57-
err := connection.WriteMessage(websocket.TextMessage, data)
65+
err := connection.SetWriteDeadline(time.Now().Add(5 * time.Second))
66+
if err != nil {
67+
log.Error("failed to set write deadline", "error", err)
68+
deleteConnection(connection)
69+
continue
70+
}
71+
72+
err = connection.WriteMessage(websocket.TextMessage, data)
5873
if err != nil {
59-
log.Error("failed to write message: %w", err)
74+
log.Error("failed to write signal message", "error", err)
75+
deleteConnection(connection)
6076
}
6177
}
6278
}
@@ -130,6 +146,7 @@ func (s *Server) signals(w http.ResponseWriter, r *http.Request) {
130146
log.Error("failed to upgrade connection: %w", err)
131147
return
132148
}
149+
log.Debug("new websocket connection")
133150

134151
s.connections[connection] = struct{}{}
135152
}

cmd/statusd/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func main() {
318318

319319
// Check if profiling shall be enabled.
320320
if *pprofEnabled {
321-
profiling.NewProfiler(*pprofPort).Go()
321+
profiling.NewProfiler(fmt.Sprintf(":%d", *pprofPort)).Go()
322322
}
323323

324324
if config.PushNotificationServerConfig.Enabled {

mobile/status.go

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"runtime"
89
"unsafe"
910

1011
"go.uber.org/zap"
@@ -1071,6 +1072,17 @@ func writeHeapProfile(dataDir string) string { //nolint: deadcode
10711072
return makeJSONResponse(err)
10721073
}
10731074

1075+
// StartProfiling starts profiling and HTTP server for pprof
1076+
func StartProfiling(address string) string {
1077+
return callWithResponse(startProfiling, address)
1078+
}
1079+
1080+
func startProfiling(address string) string {
1081+
runtime.SetMutexProfileFraction(5)
1082+
profiling.NewProfiler(address).Go()
1083+
return makeJSONResponse(nil)
1084+
}
1085+
10741086
func makeJSONResponse(err error) string {
10751087
errString := ""
10761088
if err != nil {

profiling/profiler.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package profiling
22

33
import (
4-
"fmt"
54
"net/http"
65
hpprof "net/http/pprof"
76
"time"
@@ -19,7 +18,7 @@ type Profiler struct {
1918

2019
// NewProfiler creates an instance of the profiler with
2120
// the given port.
22-
func NewProfiler(port int) *Profiler {
21+
func NewProfiler(address string) *Profiler {
2322
mux := http.NewServeMux()
2423
mux.HandleFunc("/debug/pprof/", hpprof.Index)
2524
mux.HandleFunc("/debug/pprof/cmdline", hpprof.Cmdline)
@@ -28,7 +27,7 @@ func NewProfiler(port int) *Profiler {
2827
mux.HandleFunc("/debug/pprof/trace", hpprof.Trace)
2928
p := Profiler{
3029
server: &http.Server{
31-
Addr: fmt.Sprintf(":%d", port),
30+
Addr: address,
3231
ReadHeaderTimeout: 5 * time.Second,
3332
Handler: mux,
3433
},

0 commit comments

Comments
 (0)