This commit is contained in:
Graham McIntire 2026-02-11 10:07:27 -06:00
parent 769ac1f623
commit 37f411ec55
No known key found for this signature in database
2 changed files with 19 additions and 19 deletions

View file

@ -17,15 +17,15 @@ import (
"google.golang.org/protobuf/proto"
)
// phoenixMsg is the Phoenix channel message format (JSON wrapper around binary protobuf).
type phoenixMsg struct {
// channelMsg is the WebSocket channel message format (JSON wrapper around binary protobuf).
type channelMsg struct {
Topic string `json:"topic"`
Event string `json:"event"`
Payload json.RawMessage `json:"payload"`
Ref *string `json:"ref"`
}
// runAgent connects to the Phoenix server and runs the event loop with reconnect.
// runAgent connects to the server and runs the event loop with reconnect.
func runAgent(ctx context.Context, wsURL, token string) {
baseURL := strings.TrimRight(wsURL, "/")
retryDelay := time.Second
@ -91,7 +91,7 @@ func runSession(ctx context.Context, baseURL, token string) error {
}
sendMsg := func(event string, payload json.RawMessage) {
msg := phoenixMsg{
msg := channelMsg{
Topic: topic,
Event: event,
Payload: payload,
@ -120,7 +120,7 @@ func runSession(ctx context.Context, baseURL, token string) error {
// Join channel
joinPayload, _ := json.Marshal(map[string]string{"token": token})
joinMsg := phoenixMsg{
joinMsg := channelMsg{
Topic: topic,
Event: "phx_join",
Payload: joinPayload,
@ -161,8 +161,8 @@ func runSession(ctx context.Context, baseURL, token string) error {
heartbeatTicker := time.NewTicker(60 * time.Second)
defer heartbeatTicker.Stop()
phxHeartbeatTicker := time.NewTicker(25 * time.Second)
defer phxHeartbeatTicker.Stop()
channelHeartbeatTicker := time.NewTicker(25 * time.Second)
defer channelHeartbeatTicker.Stop()
startTime := time.Now()
defer func() {
@ -180,7 +180,7 @@ func runSession(ctx context.Context, baseURL, token string) error {
return fmt.Errorf("read: %w", err)
case data := <-msgCh:
var msg phoenixMsg
var msg channelMsg
if err := json.Unmarshal(data, &msg); err != nil {
slog.Warn("invalid message", "error", err)
continue
@ -212,9 +212,9 @@ func runSession(ctx context.Context, baseURL, token string) error {
sendBinaryResult("heartbeat", hb)
slog.Debug("sent heartbeat")
case <-phxHeartbeatTicker.C:
case <-channelHeartbeatTicker.C:
ref := nextRef()
msg := phoenixMsg{
msg := channelMsg{
Topic: "phoenix",
Event: "heartbeat",
Payload: json.RawMessage(`{}`),
@ -225,14 +225,14 @@ func runSession(ctx context.Context, baseURL, token string) error {
case writeCh <- data:
default:
}
slog.Debug("sent phoenix heartbeat", "ref", ref)
slog.Debug("sent channel heartbeat", "ref", ref)
}
}
}
// handleMessage dispatches incoming Phoenix channel messages.
// handleMessage dispatches incoming channel messages.
func handleMessage(
msg phoenixMsg,
msg channelMsg,
snmpResultCh chan<- *pb.SnmpResult,
mikrotikResultCh chan<- *pb.MikrotikResult,
credTestResultCh chan<- *pb.CredentialTestResult,

View file

@ -5,8 +5,8 @@ import (
"testing"
)
func TestPhoenixMsgSerialization(t *testing.T) {
msg := phoenixMsg{
func TestChannelMsgSerialization(t *testing.T) {
msg := channelMsg{
Topic: "agent:123",
Event: "phx_join",
Payload: json.RawMessage(`{"token":"test"}`),
@ -27,9 +27,9 @@ func TestPhoenixMsgSerialization(t *testing.T) {
}
}
func TestPhoenixMsgDeserialization(t *testing.T) {
func TestChannelMsgDeserialization(t *testing.T) {
raw := `{"topic":"agent:123","event":"phx_reply","payload":{"status":"ok"},"ref":"1"}`
var msg phoenixMsg
var msg channelMsg
if err := json.Unmarshal([]byte(raw), &msg); err != nil {
t.Fatal(err)
}
@ -44,9 +44,9 @@ func TestPhoenixMsgDeserialization(t *testing.T) {
}
}
func TestPhoenixMsgNullRef(t *testing.T) {
func TestChannelMsgNullRef(t *testing.T) {
raw := `{"topic":"agent:123","event":"job","payload":{},"ref":null}`
var msg phoenixMsg
var msg channelMsg
if err := json.Unmarshal([]byte(raw), &msg); err != nil {
t.Fatal(err)
}