Use dedicated type for "ClientType".

This commit is contained in:
Joachim Bauch 2025-09-09 12:00:34 +02:00
commit c00fc82777
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
9 changed files with 28 additions and 26 deletions

View file

@ -57,7 +57,7 @@ type AsyncRoomMessage struct {
Type string `json:"type"`
SessionId PublicSessionId `json:"sessionid,omitempty"`
ClientType string `json:"clienttype,omitempty"`
ClientType ClientType `json:"clienttype,omitempty"`
}
type SendOfferMessage struct {

View file

@ -342,12 +342,14 @@ func (m *WelcomeServerMessage) HasFeature(feature string) bool {
return false
}
const (
HelloClientTypeClient = "client"
HelloClientTypeInternal = "internal"
HelloClientTypeFederation = "federation"
type ClientType string
HelloClientTypeVirtual = "virtual"
const (
HelloClientTypeClient = ClientType("client")
HelloClientTypeInternal = ClientType("internal")
HelloClientTypeFederation = ClientType("federation")
HelloClientTypeVirtual = ClientType("virtual")
)
func hasStandardPort(u *url.URL) bool {
@ -441,7 +443,7 @@ func (c *FederationTokenClaims) GetUserData() json.RawMessage {
type HelloClientMessageAuth struct {
// The client type that is connecting. Leave empty to use the default
// "HelloClientTypeClient"
Type string `json:"type,omitempty"`
Type ClientType `json:"type,omitempty"`
Params json.RawMessage `json:"params"`

View file

@ -61,7 +61,7 @@ type ClientSession struct {
ctx context.Context
closeFunc context.CancelFunc
clientType string
clientType ClientType
features []string
userId string
userData json.RawMessage
@ -162,7 +162,7 @@ func (s *ClientSession) Data() *SessionIdData {
return s.data
}
func (s *ClientSession) ClientType() string {
func (s *ClientSession) ClientType() ClientType {
return s.clientType
}

12
hub.go
View file

@ -802,7 +802,7 @@ func (h *Hub) removeSession(session Session) (removed bool) {
delete(h.clients, data.Sid)
if _, found := h.sessions[data.Sid]; found {
delete(h.sessions, data.Sid)
statsHubSessionsCurrent.WithLabelValues(session.Backend().Id(), session.ClientType()).Dec()
statsHubSessionsCurrent.WithLabelValues(session.Backend().Id(), string(session.ClientType())).Dec()
removed = true
}
}
@ -1026,8 +1026,8 @@ func (h *Hub) processRegister(c HandlerClient, message *ClientMessage, backend *
if country := client.Country(); IsValidCountry(country) {
statsClientCountries.WithLabelValues(country).Inc()
}
statsHubSessionsCurrent.WithLabelValues(backend.Id(), session.ClientType()).Inc()
statsHubSessionsTotal.WithLabelValues(backend.Id(), session.ClientType()).Inc()
statsHubSessionsCurrent.WithLabelValues(backend.Id(), string(session.ClientType())).Inc()
statsHubSessionsTotal.WithLabelValues(backend.Id(), string(session.ClientType())).Inc()
h.setDecodedPrivateSessionId(privateSessionId, sessionIdData)
h.setDecodedPublicSessionId(publicSessionId, sessionIdData)
@ -1299,7 +1299,7 @@ func (h *Hub) processHello(client HandlerClient, message *ClientMessage) {
log.Printf("Resume session from %s in %s (%s) %s (private=%s)", client.RemoteAddr(), client.Country(), client.UserAgent(), session.PublicId(), session.PrivateId())
statsHubSessionsResumedTotal.WithLabelValues(clientSession.Backend().Id(), clientSession.ClientType()).Inc()
statsHubSessionsResumedTotal.WithLabelValues(clientSession.Backend().Id(), string(clientSession.ClientType())).Inc()
h.sendHelloResponse(clientSession, message)
clientSession.NotifySessionResumed(client)
return
@ -2449,8 +2449,8 @@ func (h *Hub) processInternalMsg(sess Session, message *ClientMessage) {
h.sessions[sessionIdData.Sid] = sess
h.virtualSessions[virtualSessionId] = sessionIdData.Sid
h.mu.Unlock()
statsHubSessionsCurrent.WithLabelValues(session.Backend().Id(), sess.ClientType()).Inc()
statsHubSessionsTotal.WithLabelValues(session.Backend().Id(), sess.ClientType()).Inc()
statsHubSessionsCurrent.WithLabelValues(session.Backend().Id(), string(sess.ClientType())).Inc()
statsHubSessionsTotal.WithLabelValues(session.Backend().Id(), string(sess.ClientType())).Inc()
log.Printf("Session %s added virtual session %s with initial flags %d", session.PublicId(), sess.PublicId(), sess.Flags())
session.AddVirtualSession(sess)
sess.SetRoom(room)

16
room.go
View file

@ -201,9 +201,9 @@ func (r *Room) Close() []Session {
result = append(result, s)
}
r.sessions = nil
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": HelloClientTypeClient})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": HelloClientTypeInternal})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": HelloClientTypeVirtual})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": string(HelloClientTypeClient)})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": string(HelloClientTypeInternal)})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": string(HelloClientTypeVirtual)})
r.mu.Unlock()
return result
}
@ -287,7 +287,7 @@ func (r *Room) AddSession(session Session, sessionData json.RawMessage) {
_, found := r.sessions[sid]
r.sessions[sid] = session
if !found {
r.statsRoomSessionsCurrent.With(prometheus.Labels{"clienttype": session.ClientType()}).Inc()
r.statsRoomSessionsCurrent.With(prometheus.Labels{"clienttype": string(session.ClientType())}).Inc()
}
var publishUsersChanged bool
switch session.ClientType() {
@ -454,7 +454,7 @@ func (r *Room) RemoveSession(session Session) bool {
}
sid := session.PublicId()
r.statsRoomSessionsCurrent.With(prometheus.Labels{"clienttype": session.ClientType()}).Dec()
r.statsRoomSessionsCurrent.With(prometheus.Labels{"clienttype": string(session.ClientType())}).Dec()
delete(r.sessions, sid)
if virtualSession, ok := session.(*VirtualSession); ok {
delete(r.virtualSessions, virtualSession)
@ -482,9 +482,9 @@ func (r *Room) RemoveSession(session Session) bool {
}
r.hub.removeRoom(r)
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": HelloClientTypeClient})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": HelloClientTypeInternal})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": HelloClientTypeVirtual})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": string(HelloClientTypeClient)})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": string(HelloClientTypeInternal)})
r.statsRoomSessionsCurrent.Delete(prometheus.Labels{"clienttype": string(HelloClientTypeVirtual)})
r.unsubscribeBackend()
r.doClose()
r.mu.Unlock()

View file

@ -47,7 +47,7 @@ func (s *DummySession) PublicId() PublicSessionId {
return s.publicId
}
func (s *DummySession) ClientType() string {
func (s *DummySession) ClientType() ClientType {
return ""
}

View file

@ -51,7 +51,7 @@ type Session interface {
Context() context.Context
PrivateId() PrivateSessionId
PublicId() PublicSessionId
ClientType() string
ClientType() ClientType
Data() *SessionIdData
UserId() string

View file

@ -492,7 +492,7 @@ func (c *TestClient) SendHelloInternalWithFeatures(features []string) error {
return c.SendHelloParams("", HelloVersionV1, "internal", features, params)
}
func (c *TestClient) SendHelloParams(url string, version string, clientType string, features []string, params any) error {
func (c *TestClient) SendHelloParams(url string, version string, clientType ClientType, features []string, params any) error {
data, err := json.Marshal(params)
require.NoError(c.t, err)

View file

@ -100,7 +100,7 @@ func (s *VirtualSession) PublicId() PublicSessionId {
return s.publicId
}
func (s *VirtualSession) ClientType() string {
func (s *VirtualSession) ClientType() ClientType {
return HelloClientTypeVirtual
}