Files
threshold/internal/session/service_worker_status_report.go
T
Timothy Messier 79670180a7 refact(session): Move domain logic of Worker Status into domain service
This creates a domain service function `session.WorkerStatusReport`. The
service performs two functions:

1. Checks the status for all of the reported sessions, returning a
   report of the sessions that are in a "non-active" state, i.e.
   "cacneling" or "terminated".
2. Looks for any "orphaned" session connections, i.e. connections that
   are active but were not reported by the worker. These connections are
   marked as closed.

This also removes the "ShouldCloseConnectionsOnWorker" logic, as this
was determined to not be a possible state, and was unnecessary.
2022-03-15 13:51:15 -04:00

49 lines
1.8 KiB
Go

package session
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/observability/event"
)
// StateReport is used to report on the state of a Session.
type StateReport struct {
SessionId string
Status Status
ConnectionIds []string
}
// WorkerStatusReport is a domain service function that compares the state of
// sessions and connections as reported by a Worker, to the known state in the
// repositories. It returns a []StateReport for each session that is in the
// canceling or terminated state. It also will check for any orphaned
// connections, which is defined as a connection that is in an active state,
// but was not reported by worker. Any orphaned connections will be marked as
// closed.
func WorkerStatusReport(ctx context.Context, repo *Repository, connRepo *ConnectionRepository, workerId string, report []StateReport) ([]StateReport, error) {
const op = "session.WorkerStatusReport"
reportedConnections := make([]string, 0)
reportedSessions := make([]string, 0, len(report))
for _, r := range report {
reportedSessions = append(reportedSessions, r.SessionId)
reportedConnections = append(reportedConnections, r.ConnectionIds...)
}
notActive, err := repo.checkIfNoLongerActive(ctx, reportedSessions)
if err != nil {
return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("Error checking session state for worker %s: %v", workerId, err))
}
closed, err := connRepo.closeOrphanedConnections(ctx, workerId, reportedConnections)
if err != nil {
return notActive, errors.New(ctx, errors.Internal, op, fmt.Sprintf("Error closing orphaned connections for worker %s: %v", workerId, err))
}
if len(closed) > 0 {
event.WriteSysEvent(ctx, op, "marked unclaimed connections as closed", "server_id", workerId, "count", len(closed))
}
return notActive, err
}