@@ -156,8 +156,8 @@ type Server struct {
156156
157157 config * Config
158158
159- // connCounts tracks active sessions per session type, created on demand .
160- connCounts syncmap.Map [string , * atomic.Int64 ]
159+ // sessionCounts tracks active sessions per session type.
160+ sessionCounts * syncmap.Map [string , * atomic.Int64 ]
161161
162162 metrics * sshServerMetrics
163163}
@@ -200,13 +200,14 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
200200
201201 metrics := newSSHServerMetrics (prometheusRegistry )
202202 s := & Server {
203- Execer : execer ,
204- listeners : make (map [net.Listener ]struct {}),
205- fs : fs ,
206- conns : make (map [net.Conn ]struct {}),
207- sessions : make (map [ssh.Session ]struct {}),
208- processes : make (map [* os.Process ]struct {}),
209- logger : logger ,
203+ Execer : execer ,
204+ listeners : make (map [net.Listener ]struct {}),
205+ fs : fs ,
206+ conns : make (map [net.Conn ]struct {}),
207+ sessions : make (map [ssh.Session ]struct {}),
208+ processes : make (map [* os.Process ]struct {}),
209+ sessionCounts : syncmap .New [string , * atomic.Int64 ](),
210+ logger : logger ,
210211
211212 config : config ,
212213
@@ -228,7 +229,7 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
228229 },
229230 }
230231
231- jetbrainsCounter := s .getOrCreateConnCounter (MagicSessionTypeJetBrains )
232+ jetbrainsCounter := s .getOrCreateSessionCounter (MagicSessionTypeJetBrains )
232233 srv := & ssh.Server {
233234 ChannelHandlers : map [string ]ssh.ChannelHandler {
234235 "direct-tcpip" : func (srv * ssh.Server , conn * gossh.ServerConn , newChan gossh.NewChannel , ctx ssh.Context ) {
@@ -325,35 +326,38 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
325326 return s , nil
326327}
327328
328- // maxSessionTypes bounds distinct client-supplied session-type counters;
329- // overflow shares the unknown counter, mirroring the server-side cap.
330- const maxSessionTypes = 64
331-
332- // getOrCreateConnCounter returns the active session counter for the given
333- // session type, creating it on first use.
334- func (s * Server ) getOrCreateConnCounter (sessionType MagicSessionType ) * atomic.Int64 {
329+ // getOrCreateSessionCounter returns the active session counter for the
330+ // given session type, creating it on first use. Known-family types always
331+ // get their own counter; unrecognized types past the cap share the unknown
332+ // counter, mirroring the server-side cap.
333+ func (s * Server ) getOrCreateSessionCounter (sessionType MagicSessionType ) * atomic.Int64 {
335334 key := string (sessionType )
336- if counter , ok := s .connCounts .Load (key ); ok {
335+ if counter , ok := s .sessionCounts .Load (key ); ok {
337336 return counter
338337 }
339- var size int
340- s .connCounts .Range (func (string , * atomic.Int64 ) bool {
341- size ++
342- return true
343- })
344- // Racing first-uses can slightly overshoot the cap.
345- if size >= maxSessionTypes {
346- key = idemetadata .AppNameUnknown
338+ if idemetadata .Family (key ) == idemetadata .AppNameUnknown {
339+ var size int
340+ s .sessionCounts .Range (func (string , * atomic.Int64 ) bool {
341+ size ++
342+ return true
343+ })
344+ // Racing first-uses can slightly overshoot the cap.
345+ if size >= idemetadata .MaxSessionCountEntries {
346+ s .logger .Debug (context .Background (), "session type counter cap reached, counting under unknown" ,
347+ slog .F ("session_type" , key ),
348+ )
349+ key = idemetadata .AppNameUnknown
350+ }
347351 }
348- counter , _ := s .connCounts .LoadOrStore (key , & atomic.Int64 {})
352+ counter , _ := s .sessionCounts .LoadOrStore (key , & atomic.Int64 {})
349353 return counter
350354}
351355
352356// SessionCounts returns a snapshot of active sessions per session type,
353357// omitting idle types.
354358func (s * Server ) SessionCounts () map [string ]int64 {
355359 stats := make (map [string ]int64 )
356- s .connCounts .Range (func (key string , value * atomic.Int64 ) bool {
360+ s .sessionCounts .Range (func (key string , value * atomic.Int64 ) bool {
357361 if count := value .Load (); count > 0 {
358362 stats [key ] = count
359363 }
@@ -483,7 +487,7 @@ func (s *Server) sessionHandler(session ssh.Session) {
483487 // channel.
484488 reportSession = false
485489 } else {
486- counter := s .getOrCreateConnCounter (magicType )
490+ counter := s .getOrCreateSessionCounter (magicType )
487491 counter .Add (1 )
488492 defer counter .Add (- 1 )
489493 }
0 commit comments