-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from SiaFoundation/nate/add-event-reporter
Add session reporter
- Loading branch information
Showing
15 changed files
with
604 additions
and
517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"go.sia.tech/hostd/rhp" | ||
"go.sia.tech/jape" | ||
"go.uber.org/zap" | ||
"nhooyr.io/websocket" | ||
) | ||
|
||
type rhpSessionSubscriber struct { | ||
conn *websocket.Conn | ||
} | ||
|
||
func (rs *rhpSessionSubscriber) ReceiveSessionEvent(event rhp.SessionEvent) { | ||
buf, err := json.Marshal(event) | ||
if err != nil { | ||
return | ||
} | ||
rs.conn.Write(context.Background(), websocket.MessageText, buf) | ||
} | ||
|
||
func (a *api) handleGETSessions(c jape.Context) { | ||
c.Encode(a.sessions.Active()) | ||
} | ||
|
||
func (a *api) handleGETSessionsSubscribe(c jape.Context) { | ||
wsc, err := websocket.Accept(c.ResponseWriter, c.Request, &websocket.AcceptOptions{ | ||
OriginPatterns: []string{"*"}, | ||
}) | ||
if err != nil { | ||
a.log.Warn("failed to accept websocket connection", zap.Error(err)) | ||
return | ||
} | ||
defer wsc.Close(websocket.StatusNormalClosure, "") | ||
|
||
// subscribe the websocket conn | ||
sub := &rhpSessionSubscriber{ | ||
conn: wsc, | ||
} | ||
a.sessions.Subscribe(sub) | ||
defer a.sessions.Unsubscribe(sub) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.