Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed Jan 7, 2025
1 parent fbfd0ff commit bf2dfbb
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions server/types/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Callbacks struct {
// The handler can examine the request and either accept or reject the connection.
// To accept:
// Return ConnectionResponse with Accept=true. ConnectionCallbacks MUST be set to an
// implementation of the ConnectionCallbacks interface to handle the connection.
// instance of the ConnectionCallbacks struct to handle the connection callbacks.
// HTTPStatusCode and HTTPResponseHeader are ignored.
//
// To reject:
Expand All @@ -31,17 +31,19 @@ type Callbacks struct {
OnConnecting func(request *http.Request) ConnectionResponse
}

func defaultOnConnecting(r *http.Request) ConnectionResponse {
return ConnectionResponse{Accept: true}

Check warning on line 35 in server/types/callbacks.go

View check run for this annotation

Codecov / codecov/patch

server/types/callbacks.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}

func (c *Callbacks) SetDefaults() {
if c.OnConnecting == nil {
c.OnConnecting = func(r *http.Request) ConnectionResponse {
return ConnectionResponse{Accept: true}
}
c.OnConnecting = defaultOnConnecting
}
}

// ConnectionCallbacks receives callbacks for a specific connection. An implementation of
// this interface MUST be set on the ConnectionResponse returned by the OnConnecting
// callback if Accept=true. The implementation can be shared by all connections or can be
// ConnectionCallbacks specifies callbacks for a specific connection. An instance of
// this struct MUST be set on the ConnectionResponse returned by the OnConnecting
// callback if Accept=true. The instance can be shared by all connections or can be
// unique for each connection.
type ConnectionCallbacks struct {
// The following callbacks will never be called concurrently for the same
Expand All @@ -62,23 +64,29 @@ type ConnectionCallbacks struct {
OnConnectionClose func(conn Connection)
}

func defaultOnConnected(ctx context.Context, conn Connection) {}

func defaultOnMessage(
ctx context.Context, conn Connection, message *protobufs.AgentToServer,
) *protobufs.ServerToAgent {
// We will send an empty response since there is no user-defined callback to handle it.
return &protobufs.ServerToAgent{
InstanceUid: message.InstanceUid,
}
}

func defaultOnConnectionClose(conn Connection) {}

func (c *ConnectionCallbacks) SetDefaults() {
if c.OnConnected == nil {
c.OnConnected = func(ctx context.Context, conn Connection) {}
c.OnConnected = defaultOnConnected
}

if c.OnMessage == nil {
c.OnMessage = func(
ctx context.Context, conn Connection, message *protobufs.AgentToServer,
) *protobufs.ServerToAgent {
// We will send an empty response since there is no user-defined callback to handle it.
return &protobufs.ServerToAgent{
InstanceUid: message.InstanceUid,
}
}
c.OnMessage = defaultOnMessage
}

if c.OnConnectionClose == nil {
c.OnConnectionClose = func(conn Connection) {}
c.OnConnectionClose = defaultOnConnectionClose
}
}

0 comments on commit bf2dfbb

Please sign in to comment.