Use interface instead of concrete "Hub" class for GRPC server.

This commit is contained in:
Joachim Bauch 2024-04-29 10:53:10 +02:00
parent 90035906d3
commit 70cdc7a1aa
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 28 additions and 6 deletions

View file

@ -55,6 +55,14 @@ func init() {
GrpcServerId = hex.EncodeToString(md.Sum(nil))
}
type GrpcServerHub interface {
GetSessionByResumeId(resumeId string) Session
GetSessionByPublicId(sessionId string) Session
GetSessionIdByRoomSessionId(roomSessionId string) (string, error)
GetBackend(u *url.URL) *Backend
}
type GrpcServer struct {
UnimplementedRpcBackendServer
UnimplementedRpcInternalServer
@ -66,7 +74,7 @@ type GrpcServer struct {
listener net.Listener
serverId string // can be overwritten from tests
hub *Hub
hub GrpcServerHub
}
func NewGrpcServer(config *goconf.ConfigFile) (*GrpcServer, error) {
@ -131,7 +139,7 @@ func (s *GrpcServer) LookupSessionId(ctx context.Context, request *LookupSession
statsGrpcServerCalls.WithLabelValues("LookupSessionId").Inc()
// TODO: Remove debug logging
log.Printf("Lookup session id for room session id %s", request.RoomSessionId)
sid, err := s.hub.roomSessions.GetSessionId(request.RoomSessionId)
sid, err := s.hub.GetSessionIdByRoomSessionId(request.RoomSessionId)
if errors.Is(err, ErrNoSuchRoomSession) {
return nil, status.Error(codes.NotFound, "no such room session id")
} else if err != nil {
@ -221,7 +229,7 @@ func (s *GrpcServer) GetSessionCount(ctx context.Context, request *GetSessionCou
return nil, status.Error(codes.InvalidArgument, "invalid url")
}
backend := s.hub.backend.GetBackend(u)
backend := s.hub.GetBackend(u)
if backend == nil {
return nil, status.Error(codes.NotFound, "no such backend")
}
@ -233,13 +241,18 @@ func (s *GrpcServer) GetSessionCount(ctx context.Context, request *GetSessionCou
func (s *GrpcServer) ProxySession(request RpcSessions_ProxySessionServer) error {
statsGrpcServerCalls.WithLabelValues("ProxySession").Inc()
client, err := newRemoteGrpcClient(s.hub, request)
hub, ok := s.hub.(*Hub)
if !ok {
return status.Error(codes.Internal, "invalid hub type")
}
client, err := newRemoteGrpcClient(hub, request)
if err != nil {
return err
}
sid := s.hub.registerClient(client)
defer s.hub.unregisterClient(sid)
sid := hub.registerClient(client)
defer hub.unregisterClient(sid)
return client.run()
}

9
hub.go
View file

@ -38,6 +38,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"strings"
"sync"
"sync/atomic"
@ -606,6 +607,10 @@ func (h *Hub) GetSessionByResumeId(resumeId string) Session {
return session
}
func (h *Hub) GetSessionIdByRoomSessionId(roomSessionId string) (string, error) {
return h.roomSessions.GetSessionId(roomSessionId)
}
func (h *Hub) GetDialoutSession(roomId string, backend *Backend) *ClientSession {
url := backend.Url()
@ -624,6 +629,10 @@ func (h *Hub) GetDialoutSession(roomId string, backend *Backend) *ClientSession
return nil
}
func (h *Hub) GetBackend(u *url.URL) *Backend {
return h.backend.GetBackend(u)
}
func (h *Hub) checkExpiredSessions(now time.Time) {
for session, expires := range h.expiredSessions {
if now.After(expires) {