Replaced legacy `*.orly` module imports with `next.orly.dev/pkg` paths across the codebase for consistency. Removed legacy `go.mod` files from sub-packages, consolidating dependency management. Added Dockerfiles and configurations for benchmarking environments.
42 lines
962 B
Go
42 lines
962 B
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/coder/websocket"
|
|
"lol.mleku.dev/chk"
|
|
"next.orly.dev/pkg/utils/atomic"
|
|
)
|
|
|
|
type Listener struct {
|
|
*Server
|
|
conn *websocket.Conn
|
|
ctx context.Context
|
|
remote string
|
|
req *http.Request
|
|
challenge atomic.Bytes
|
|
authedPubkey atomic.Bytes
|
|
}
|
|
|
|
// Ctx returns the listener's context, but creates a new context for each operation
|
|
// to prevent cancellation from affecting subsequent operations
|
|
func (l *Listener) Ctx() context.Context {
|
|
return l.ctx
|
|
}
|
|
|
|
func (l *Listener) Write(p []byte) (n int, err error) {
|
|
// Use a separate context with timeout for writes to prevent race conditions
|
|
// where the main connection context gets cancelled while writing events
|
|
writeCtx, cancel := context.WithTimeout(
|
|
context.Background(), DefaultWriteTimeout,
|
|
)
|
|
defer cancel()
|
|
|
|
if err = l.conn.Write(writeCtx, websocket.MessageText, p); chk.E(err) {
|
|
return
|
|
}
|
|
n = len(p)
|
|
return
|
|
}
|