@@ -13,6 +13,7 @@ import (
13
13
"fmt"
14
14
"iter"
15
15
"log"
16
+ "maps"
16
17
"net/url"
17
18
"path/filepath"
18
19
"slices"
@@ -43,7 +44,7 @@ type Server struct {
43
44
sessions []* ServerSession
44
45
sendingMethodHandler_ MethodHandler [* ServerSession ]
45
46
receivingMethodHandler_ MethodHandler [* ServerSession ]
46
- resourceSubscriptions map [string ]map [string ]bool // uri -> session ID -> bool
47
+ resourceSubscriptions map [string ]map [* ServerSession ]bool // uri -> session -> bool
47
48
}
48
49
49
50
// ServerOptions is used to configure behavior of the server.
@@ -109,7 +110,7 @@ func NewServer(impl *Implementation, opts *ServerOptions) *Server {
109
110
resourceTemplates : newFeatureSet (func (t * serverResourceTemplate ) string { return t .resourceTemplate .URITemplate }),
110
111
sendingMethodHandler_ : defaultSendingMethodHandler [* ServerSession ],
111
112
receivingMethodHandler_ : defaultReceivingMethodHandler [* ServerSession ],
112
- resourceSubscriptions : make (map [string ]map [string ]bool ),
113
+ resourceSubscriptions : make (map [string ]map [* ServerSession ]bool ),
113
114
}
114
115
}
115
116
@@ -442,19 +443,13 @@ func fileResourceHandler(dir string) ResourceHandler {
442
443
}
443
444
}
444
445
446
+ // ResourceUpdated sends a notification to all clients that have subscribed to the
447
+ // resource specified in params. This method is the primary way for a
448
+ // server author to signal that a resource has changed.
445
449
func (s * Server ) ResourceUpdated (ctx context.Context , params * ResourceUpdatedNotificationParams ) error {
446
450
s .mu .Lock ()
447
- subscribedSessionIDs := s .resourceSubscriptions [params .URI ]
448
- if len (subscribedSessionIDs ) == 0 {
449
- s .mu .Unlock ()
450
- return nil
451
- }
452
- var sessions []* ServerSession
453
- for _ , session := range s .sessions {
454
- if _ , ok := subscribedSessionIDs [session .ID ()]; ok {
455
- sessions = append (sessions , session )
456
- }
457
- }
451
+ subscribedSessions := s .resourceSubscriptions [params .URI ]
452
+ sessions := slices .Collect (maps .Keys (subscribedSessions ))
458
453
s .mu .Unlock ()
459
454
notifySessions (sessions , notificationResourceUpdated , params )
460
455
return nil
@@ -467,12 +462,14 @@ func (s *Server) subscribe(ctx context.Context, ss *ServerSession, params *Subsc
467
462
if err := s .opts .SubscribeHandler (ctx , params ); err != nil {
468
463
return nil , err
469
464
}
465
+
470
466
s .mu .Lock ()
471
467
defer s .mu .Unlock ()
472
468
if s .resourceSubscriptions [params .URI ] == nil {
473
- s .resourceSubscriptions [params .URI ] = make (map [string ]bool )
469
+ s .resourceSubscriptions [params .URI ] = make (map [* ServerSession ]bool )
474
470
}
475
- s .resourceSubscriptions [params .URI ][ss .ID ()] = true
471
+ s.resourceSubscriptions [params.URI ][ss ] = true
472
+
476
473
return & emptyResult {}, nil
477
474
}
478
475
@@ -487,10 +484,9 @@ func (s *Server) unsubscribe(ctx context.Context, ss *ServerSession, params *Uns
487
484
488
485
s .mu .Lock ()
489
486
defer s .mu .Unlock ()
490
-
491
- if subscribedSessionIDs , ok := s .resourceSubscriptions [params .URI ]; ok {
492
- delete (subscribedSessionIDs , ss .ID ())
493
- if len (subscribedSessionIDs ) == 0 {
487
+ if subscribedSessions , ok := s .resourceSubscriptions [params .URI ]; ok {
488
+ delete (subscribedSessions , ss )
489
+ if len (subscribedSessions ) == 0 {
494
490
delete (s .resourceSubscriptions , params .URI )
495
491
}
496
492
}
@@ -546,8 +542,8 @@ func (s *Server) disconnect(cc *ServerSession) {
546
542
return cc2 == cc
547
543
})
548
544
549
- for _ , subscribedSessionIDs := range s .resourceSubscriptions {
550
- delete (subscribedSessionIDs , cc . ID () )
545
+ for _ , subscribedSessions := range s .resourceSubscriptions {
546
+ delete (subscribedSessions , cc )
551
547
}
552
548
}
553
549
0 commit comments