|
1 | 1 | package graphql |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "reflect" |
6 | | - "sync" |
7 | 5 | ) |
8 | 6 |
|
9 | | -// map of subscription ID to subscription |
| 7 | +// subscriptionMap is a map of subscription ID to subscription. |
| 8 | +// It is NOT thread-safe and must be protected by the caller's lock. |
10 | 9 | type subscriptionMap struct { |
11 | 10 | map_ map[string]subscription |
12 | | - sync.RWMutex |
13 | 11 | } |
14 | 12 |
|
15 | 13 | type subscription struct { |
16 | | - interfaceChan interface{} |
17 | | - forwardDataFunc ForwardDataFunction |
18 | | - id string |
19 | | - hasBeenUnsubscribed bool |
| 14 | + interfaceChan interface{} |
| 15 | + forwardDataFunc ForwardDataFunction |
| 16 | + id string |
| 17 | + closed bool // true if the channel has been closed |
20 | 18 | } |
21 | 19 |
|
22 | | -func (s *subscriptionMap) Create(subscriptionID string, interfaceChan interface{}, forwardDataFunc ForwardDataFunction) { |
23 | | - s.Lock() |
24 | | - defer s.Unlock() |
| 20 | +// create adds a new subscription to the map. |
| 21 | +// The caller must hold the webSocketClient lock. |
| 22 | +func (s *subscriptionMap) create(subscriptionID string, interfaceChan interface{}, forwardDataFunc ForwardDataFunction) { |
25 | 23 | s.map_[subscriptionID] = subscription{ |
26 | | - id: subscriptionID, |
27 | | - interfaceChan: interfaceChan, |
28 | | - forwardDataFunc: forwardDataFunc, |
29 | | - hasBeenUnsubscribed: false, |
| 24 | + id: subscriptionID, |
| 25 | + interfaceChan: interfaceChan, |
| 26 | + forwardDataFunc: forwardDataFunc, |
| 27 | + closed: false, |
30 | 28 | } |
31 | 29 | } |
32 | 30 |
|
33 | | -func (s *subscriptionMap) Unsubscribe(subscriptionID string) error { |
34 | | - s.Lock() |
35 | | - defer s.Unlock() |
36 | | - unsub, success := s.map_[subscriptionID] |
37 | | - if !success { |
38 | | - return fmt.Errorf("tried to unsubscribe from unknown subscription with ID '%s'", subscriptionID) |
| 31 | +// get retrieves a subscription by ID. |
| 32 | +// The caller must hold the webSocketClient lock. |
| 33 | +// Returns nil if not found. |
| 34 | +func (s *subscriptionMap) get(subscriptionID string) *subscription { |
| 35 | + sub, ok := s.map_[subscriptionID] |
| 36 | + if !ok { |
| 37 | + return nil |
39 | 38 | } |
40 | | - hasBeenUnsubscribed := unsub.hasBeenUnsubscribed |
41 | | - unsub.hasBeenUnsubscribed = true |
42 | | - s.map_[subscriptionID] = unsub |
| 39 | + return &sub |
| 40 | +} |
43 | 41 |
|
44 | | - if !hasBeenUnsubscribed { |
45 | | - reflect.ValueOf(s.map_[subscriptionID].interfaceChan).Close() |
46 | | - } |
47 | | - return nil |
| 42 | +// update updates a subscription in the map. |
| 43 | +// The caller must hold the webSocketClient lock. |
| 44 | +func (s *subscriptionMap) update(subscriptionID string, sub subscription) { |
| 45 | + s.map_[subscriptionID] = sub |
48 | 46 | } |
49 | 47 |
|
50 | | -func (s *subscriptionMap) GetAllIDs() (subscriptionIDs []string) { |
51 | | - s.RLock() |
52 | | - defer s.RUnlock() |
| 48 | +// getAllIDs returns all subscription IDs. |
| 49 | +// The caller must hold the webSocketClient lock. |
| 50 | +func (s *subscriptionMap) getAllIDs() []string { |
| 51 | + subscriptionIDs := make([]string, 0, len(s.map_)) |
53 | 52 | for subID := range s.map_ { |
54 | 53 | subscriptionIDs = append(subscriptionIDs, subID) |
55 | 54 | } |
56 | 55 | return subscriptionIDs |
57 | 56 | } |
58 | 57 |
|
59 | | -func (s *subscriptionMap) Delete(subscriptionID string) { |
60 | | - s.Lock() |
61 | | - defer s.Unlock() |
| 58 | +// delete removes a subscription from the map. |
| 59 | +// The caller must hold the webSocketClient lock. |
| 60 | +func (s *subscriptionMap) delete(subscriptionID string) { |
62 | 61 | delete(s.map_, subscriptionID) |
63 | 62 | } |
| 63 | + |
| 64 | +// closeChannel closes a subscription's channel if it hasn't been closed yet. |
| 65 | +// The caller must hold the webSocketClient lock. |
| 66 | +// Returns true if the channel was closed, false if it was already closed. |
| 67 | +func (s *subscriptionMap) closeChannel(subscriptionID string) bool { |
| 68 | + sub := s.get(subscriptionID) |
| 69 | + if sub == nil || sub.closed { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + // Mark as closed before actually closing to prevent double-close |
| 74 | + sub.closed = true |
| 75 | + s.update(subscriptionID, *sub) |
| 76 | + |
| 77 | + // Close the channel |
| 78 | + reflect.ValueOf(sub.interfaceChan).Close() |
| 79 | + return true |
| 80 | +} |
0 commit comments