Skip to content

Commit

Permalink
Added established and finished handlers to server
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebires committed Mar 17, 2022
1 parent f072a37 commit 141fffa
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,23 @@ func (srv *Server) handleChannel(ctx context.Context, c *ServerChannel) {
return
}

established := srv.config.Established
if established != nil {
established(ctx, c.remoteNode, c.sessionID)
}

defer func() {
if c.Established() {
// Do not use the shared context since it could be canceled
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_ = c.FinishSession(ctx)
}

finished := srv.config.Finished
if finished != nil {
finished(context.Background(), c.remoteNode)
}
}()

if err = srv.mux.ListenServer(ctx, c); err != nil {
Expand Down Expand Up @@ -176,6 +186,10 @@ type ServerConfig struct {
Authenticate func(context.Context, Identity, Authentication) (*AuthenticationResult, error)
// Register is called for the client Node address registration.
Register func(context.Context, Node, *ServerChannel) (Node, error)
// Established is called when a session with a node is established.
Established func(ctx context.Context, n Node, sessionID string)
// Finished is called when an established session with a node is finished.
Finished func(context.Context, Node)
}

var defaultServerConfig = NewServerConfig()
Expand Down

0 comments on commit 141fffa

Please sign in to comment.