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