Skip to content

Commit d292acd

Browse files
chore: re-order websocket accept
1 parent 042b159 commit d292acd

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

coderd/inboxnotifications.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,6 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
9494
return
9595
}
9696

97-
conn, err := websocket.Accept(rw, r, nil)
98-
if err != nil {
99-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
100-
Message: "Failed to upgrade connection to websocket.",
101-
Detail: err.Error(),
102-
})
103-
return
104-
}
105-
106-
go httpapi.Heartbeat(ctx, conn)
107-
defer conn.Close(websocket.StatusNormalClosure, "connection closed")
108-
10997
notificationCh := make(chan codersdk.InboxNotification, 10)
11098

11199
closeInboxNotificationsSubscriber, err := api.Pubsub.SubscribeWithErr(pubsub.InboxNotificationForOwnerEventChannel(apikey.UserID),
@@ -161,9 +149,20 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
161149
api.Logger.Error(ctx, "subscribe to inbox notification event", slog.Error(err))
162150
return
163151
}
164-
165152
defer closeInboxNotificationsSubscriber()
166153

154+
conn, err := websocket.Accept(rw, r, nil)
155+
if err != nil {
156+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
157+
Message: "Failed to upgrade connection to websocket.",
158+
Detail: err.Error(),
159+
})
160+
return
161+
}
162+
163+
go httpapi.Heartbeat(ctx, conn)
164+
defer conn.Close(websocket.StatusNormalClosure, "connection closed")
165+
167166
encoder := wsjson.NewEncoder[codersdk.GetInboxNotificationResponse](conn, websocket.MessageText)
168167
defer encoder.Close(websocket.StatusNormalClosure)
169168

0 commit comments

Comments
 (0)