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. active map[ID]*Subscription
  49. inactive map[ID]*Subscription
  50. }
  51. // newNotifier creates a new notifier that can be used to send subscription
  52. // notifications to the client.
  53. func newNotifier(codec ServerCodec) *Notifier {
  54. return &Notifier{
  55. codec: codec,
  56. active: make(map[ID]*Subscription),
  57. inactive: make(map[ID]*Subscription),
  58. }
  59. }
  60. // NotifierFromContext returns the Notifier value stored in ctx, if any.
  61. func NotifierFromContext(ctx context.Context) (*Notifier, bool) {
  62. n, ok := ctx.Value(notifierKey{}).(*Notifier)
  63. return n, ok
  64. }
  65. // CreateSubscription returns a new subscription that is coupled to the
  66. // RPC connection. By default subscriptions are inactive and notifications
  67. // are dropped until the subscription is marked as active. This is done
  68. // by the RPC server after the subscription ID is send to the client.
  69. func (n *Notifier) CreateSubscription() *Subscription {
  70. s := &Subscription{ID: NewID(), err: make(chan error)}
  71. n.subMu.Lock()
  72. n.inactive[s.ID] = s
  73. n.subMu.Unlock()
  74. return s
  75. }
  76. // Notify sends a notification to the client with the given data as payload.
  77. // If an error occurs the RPC connection is closed and the error is returned.
  78. func (n *Notifier) Notify(id ID, data interface{}) error {
  79. n.subMu.RLock()
  80. defer n.subMu.RUnlock()
  81. sub, active := n.active[id]
  82. if active {
  83. notification := n.codec.CreateNotification(string(id), sub.namespace, data)
  84. if err := n.codec.Write(notification); err != nil {
  85. n.codec.Close()
  86. return err
  87. }
  88. }
  89. return nil
  90. }
  91. // Closed returns a channel that is closed when the RPC connection is closed.
  92. func (n *Notifier) Closed() <-chan interface{} {
  93. return n.codec.Closed()
  94. }
  95. // unsubscribe a subscription.
  96. // If the subscription could not be found ErrSubscriptionNotFound is returned.
  97. func (n *Notifier) unsubscribe(id ID) error {
  98. n.subMu.Lock()
  99. defer n.subMu.Unlock()
  100. if s, found := n.active[id]; found {
  101. close(s.err)
  102. delete(n.active, id)
  103. return nil
  104. }
  105. return ErrSubscriptionNotFound
  106. }
  107. // activate enables a subscription. Until a subscription is enabled all
  108. // notifications are dropped. This method is called by the RPC server after
  109. // the subscription ID was sent to client. This prevents notifications being
  110. // send to the client before the subscription ID is send to the client.
  111. func (n *Notifier) activate(id ID, namespace string) {
  112. n.subMu.Lock()
  113. defer n.subMu.Unlock()
  114. if sub, found := n.inactive[id]; found {
  115. sub.namespace = namespace
  116. n.active[id] = sub
  117. delete(n.inactive, id)
  118. }
  119. }