subscription.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "context"
  19. "errors"
  20. "sync"
  21. )
  22. var (
  23. // ErrNotificationsUnsupported is returned when the connection doesn't support notifications
  24. ErrNotificationsUnsupported = errors.New("notifications not supported")
  25. // ErrNotificationNotFound is returned when the notification for the given id is not found
  26. ErrSubscriptionNotFound = errors.New("subscription not found")
  27. )
  28. // ID defines a pseudo random number that is used to identify RPC subscriptions.
  29. type ID string
  30. // a Subscription is created by a notifier and tight to that notifier. The client can use
  31. // this subscription to wait for an unsubscribe request for the client, see Err().
  32. type Subscription struct {
  33. ID ID
  34. namespace string
  35. err chan error // closed on unsubscribe
  36. }
  37. // Err returns a channel that is closed when the client send an unsubscribe request.
  38. func (s *Subscription) Err() <-chan error {
  39. return s.err
  40. }
  41. // notifierKey is used to store a notifier within the connection context.
  42. type notifierKey struct{}
  43. // Notifier is tight to a RPC connection that supports subscriptions.
  44. // Server callbacks use the notifier to send notifications.
  45. type Notifier struct {
  46. codec ServerCodec
  47. subMu sync.RWMutex // guards active and inactive maps
  48. stopped bool
  49. active map[ID]*Subscription
  50. inactive map[ID]*Subscription
  51. }
  52. // newNotifier creates a new notifier that can be used to send subscription
  53. // notifications to the client.
  54. func newNotifier(codec ServerCodec) *Notifier {
  55. return &Notifier{
  56. codec: codec,
  57. active: make(map[ID]*Subscription),
  58. inactive: make(map[ID]*Subscription),
  59. }
  60. }
  61. // NotifierFromContext returns the Notifier value stored in ctx, if any.
  62. func NotifierFromContext(ctx context.Context) (*Notifier, bool) {
  63. n, ok := ctx.Value(notifierKey{}).(*Notifier)
  64. return n, ok
  65. }
  66. // CreateSubscription returns a new subscription that is coupled to the
  67. // RPC connection. By default subscriptions are inactive and notifications
  68. // are dropped until the subscription is marked as active. This is done
  69. // by the RPC server after the subscription ID is send to the client.
  70. func (n *Notifier) CreateSubscription() *Subscription {
  71. s := &Subscription{ID: NewID(), err: make(chan error)}
  72. n.subMu.Lock()
  73. n.inactive[s.ID] = s
  74. n.subMu.Unlock()
  75. return s
  76. }
  77. // Notify sends a notification to the client with the given data as payload.
  78. // If an error occurs the RPC connection is closed and the error is returned.
  79. func (n *Notifier) Notify(id ID, data interface{}) error {
  80. n.subMu.RLock()
  81. defer n.subMu.RUnlock()
  82. sub, active := n.active[id]
  83. if active {
  84. notification := n.codec.CreateNotification(string(id), sub.namespace, data)
  85. if err := n.codec.Write(notification); err != nil {
  86. n.codec.Close()
  87. return err
  88. }
  89. }
  90. return nil
  91. }
  92. // Closed returns a channel that is closed when the RPC connection is closed.
  93. func (n *Notifier) Closed() <-chan interface{} {
  94. return n.codec.Closed()
  95. }
  96. // unsubscribe a subscription.
  97. // If the subscription could not be found ErrSubscriptionNotFound is returned.
  98. func (n *Notifier) unsubscribe(id ID) error {
  99. n.subMu.Lock()
  100. defer n.subMu.Unlock()
  101. if s, found := n.active[id]; found {
  102. close(s.err)
  103. delete(n.active, id)
  104. return nil
  105. }
  106. return ErrSubscriptionNotFound
  107. }
  108. // activate enables a subscription. Until a subscription is enabled all
  109. // notifications are dropped. This method is called by the RPC server after
  110. // the subscription ID was sent to client. This prevents notifications being
  111. // send to the client before the subscription ID is send to the client.
  112. func (n *Notifier) activate(id ID, namespace string) {
  113. n.subMu.Lock()
  114. defer n.subMu.Unlock()
  115. if sub, found := n.inactive[id]; found {
  116. sub.namespace = namespace
  117. n.active[id] = sub
  118. delete(n.inactive, id)
  119. }
  120. }