Session handles GRPC session from a repository client.
(srv grpcapi.KopiaRepository_SessionServer)
| 82 | |
| 83 | // Session handles GRPC session from a repository client. |
| 84 | func (s *Server) Session(srv grpcapi.KopiaRepository_SessionServer) error { |
| 85 | ctx := srv.Context() |
| 86 | |
| 87 | s.serverMutex.RLock() |
| 88 | dr, ok := s.rep.(repo.DirectRepository) |
| 89 | s.serverMutex.RUnlock() |
| 90 | |
| 91 | if !ok { |
| 92 | return status.Errorf(codes.Unavailable, "not connected to a direct repository") |
| 93 | } |
| 94 | |
| 95 | log := dr.LogManager().NewLogger("grpc-session") |
| 96 | ctx = contentlog.WithParams(ctx, logparam.String("span:server-session", contentlog.RandomSpanID())) |
| 97 | |
| 98 | usernameAtHostname, err := s.authenticateGRPCSession(ctx, dr) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | authz := s.authorizer.Authorize(ctx, dr, usernameAtHostname) |
| 104 | if authz == nil { |
| 105 | authz = auth.NoAccess() |
| 106 | } |
| 107 | |
| 108 | p, ok := peer.FromContext(ctx) |
| 109 | if !ok { |
| 110 | return status.Errorf(codes.PermissionDenied, "peer not found in context") |
| 111 | } |
| 112 | |
| 113 | userLog(ctx).Infof("starting session for user %q from %v", usernameAtHostname, p.Addr) |
| 114 | defer userLog(ctx).Infof("session ended for user %q from %v", usernameAtHostname, p.Addr) |
| 115 | |
| 116 | opt, err := s.handleInitialSessionHandshake(srv, dr) |
| 117 | if err != nil { |
| 118 | userLog(ctx).Errorf("session handshake error: %v", err) |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | //nolint:wrapcheck |
| 123 | return repo.DirectWriteSession(ctx, dr, opt, func(ctx context.Context, dw repo.DirectRepositoryWriter) error { |
| 124 | // channel to which workers will be sending errors, only holds 1 slot and sends are non-blocking. |
| 125 | lastErr := make(chan error, 1) |
| 126 | |
| 127 | for req, err := srv.Recv(); err == nil; req, err = srv.Recv() { |
| 128 | // propagate any error from the goroutines |
| 129 | select { |
| 130 | case err := <-lastErr: |
| 131 | userLog(ctx).Errorf("error handling session request: %v", err) |
| 132 | |
| 133 | contentlog.Log1(ctx, log, |
| 134 | "error handling session request", |
| 135 | logparam.Error("error", err)) |
| 136 | |
| 137 | return err |
| 138 | |
| 139 | default: |
| 140 | } |
| 141 |
nothing calls this directly
no test coverage detected