mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
The three websocket handlers move onto the fasthttp upgrader, which was already in the module graph — gorilla's own fork for this server, same Conn, same TextMessage, so the guacamole layer under the tunnel changed by its import line and nothing else. The shape does change, and it is the whole point. gorilla RETURNS a connection and lets the handler carry on; fasthttp hands it to a CALLBACK, because an upgrade detaches the socket from the request — the session runs after the handler's frame is gone. So the session lives in the callback, and so does everything whose lifetime is the connection's. That is not a formality. DevBridge takes a global seat and a per-user seat before upgrading and released both with `defer`. Left there, those defers now run while the connection is still live: every accepted socket frees its own seat immediately, both limits stop counting, and the pod accepts without bound — exactly the failure the limits exist to prevent, and invisible until something falls over. The seats are released by the CONNECTION now, through one `release` that the session calls on the way out or the failed upgrade calls instead. Exactly one of the two runs. The pre-upgrade refusals answer through the context, which is the only thing left that can still set a status: past the upgrade there is no response to write. Zen's relay already took the callback's writer and its call site never supplied one; it now runs inside the callback, with the status and headers set before it, where they can still be set. Down to the /v1/responses writer swap, which needs the completion to hand back its non-streaming body rather than have it captured — the last thing here that reaches into the context instead of being passed a value.
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/fasthttp/websocket"
|
|
"github.com/hanzoai/ai/util/guacamole"
|
|
)
|
|
|
|
type GuacamoleHandler struct {
|
|
ws *websocket.Conn
|
|
tunnel *guacamole.Tunnel
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
func NewGuacamoleHandler(ws *websocket.Conn, tunnel *guacamole.Tunnel) *GuacamoleHandler {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
return &GuacamoleHandler{
|
|
ws: ws,
|
|
tunnel: tunnel,
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
}
|
|
}
|
|
|
|
func (r GuacamoleHandler) Start() {
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-r.ctx.Done():
|
|
return
|
|
default:
|
|
instruction, err := r.tunnel.Read()
|
|
if err != nil {
|
|
guacamole.Disconnect(r.ws, TunnelClosed, "Remote connection shut down")
|
|
return
|
|
}
|
|
if len(instruction) == 0 {
|
|
continue
|
|
}
|
|
err = r.ws.WriteMessage(websocket.TextMessage, instruction)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (r GuacamoleHandler) Stop() {
|
|
r.cancel()
|
|
}
|