Skip to content

Commit a6ca8fd

Browse files
committed
Merge pull request #2463 from fjl/rpc-context-key
rpc: remove NotifierContextKey
2 parents 27116bd + a40e61b commit a6ca8fd

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

eth/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ type NewBlocksArgs struct {
603603
// NewBlocks triggers a new block event each time a block is appended to the chain. It accepts an argument which allows
604604
// the caller to specify whether the output should contain transactions and in what format.
605605
func (s *PublicBlockChainAPI) NewBlocks(ctx context.Context, args NewBlocksArgs) (rpc.Subscription, error) {
606-
notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
606+
notifier, supported := rpc.NotifierFromContext(ctx)
607607
if !supported {
608608
return nil, rpc.ErrNotificationsUnsupported
609609
}
@@ -1345,7 +1345,7 @@ func (s *PublicTransactionPoolAPI) PendingTransactions() []*RPCTransaction {
13451345
// NewPendingTransaction creates a subscription that is triggered each time a transaction enters the transaction pool
13461346
// and is send from one of the transactions this nodes manages.
13471347
func (s *PublicTransactionPoolAPI) NewPendingTransactions(ctx context.Context) (rpc.Subscription, error) {
1348-
notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
1348+
notifier, supported := rpc.NotifierFromContext(ctx)
13491349
if !supported {
13501350
return nil, rpc.ErrNotificationsUnsupported
13511351
}

eth/downloader/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type SyncingResult struct {
8585

8686
// Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
8787
func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (rpc.Subscription, error) {
88-
notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
88+
notifier, supported := rpc.NotifierFromContext(ctx)
8989
if !supported {
9090
return nil, rpc.ErrNotificationsUnsupported
9191
}

eth/filters/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (s *PublicFilterAPI) newLogFilter(earliest, latest int64, addresses []commo
234234
}
235235

236236
func (s *PublicFilterAPI) Logs(ctx context.Context, args NewFilterArgs) (rpc.Subscription, error) {
237-
notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
237+
notifier, supported := rpc.NotifierFromContext(ctx)
238238
if !supported {
239239
return nil, rpc.ErrNotificationsUnsupported
240240
}

rpc/notification.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/logger"
2525
"github.com/ethereum/go-ethereum/logger/glog"
26+
"golang.org/x/net/context"
2627
)
2728

2829
var (
@@ -62,6 +63,14 @@ type Notifier interface {
6263
Unsubscribe(id string) error
6364
}
6465

66+
type notifierKey struct{}
67+
68+
// NotifierFromContext returns the Notifier value stored in ctx, if any.
69+
func NotifierFromContext(ctx context.Context) (Notifier, bool) {
70+
n, ok := ctx.Value(notifierKey{}).(Notifier)
71+
return n, ok
72+
}
73+
6574
// Subscription defines the interface for objects that can notify subscribers
6675
type Subscription interface {
6776
// Inform client of an event

rpc/notification_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (s *NotificationTestService) Unsubscribe(subid string) {
3636
}
3737

3838
func (s *NotificationTestService) SomeSubscription(ctx context.Context, n, val int) (Subscription, error) {
39-
notifier, supported := ctx.Value(NotifierContextKey).(Notifier)
39+
notifier, supported := NotifierFromContext(ctx)
4040
if !supported {
4141
return nil, ErrNotificationsUnsupported
4242
}

rpc/server.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ import (
3232
const (
3333
stopPendingRequestTimeout = 3 * time.Second // give pending requests stopPendingRequestTimeout the time to finish when the server is stopped
3434

35-
// NotifierContextKey is the key where the notifier associated with the codec is stored in the context
36-
NotifierContextKey = 1
37-
3835
notificationBufferSize = 10000 // max buffered notifications before codec is closed
3936

4037
DefaultIPCApis = "admin,eth,debug,miner,net,shh,txpool,personal,web3"
@@ -171,7 +168,7 @@ func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecO
171168
// to send notification to clients. It is thight to the codec/connection. If the
172169
// connection is closed the notifier will stop and cancels all active subscriptions.
173170
if options&OptionSubscriptions == OptionSubscriptions {
174-
ctx = context.WithValue(ctx, NotifierContextKey, newBufferedNotifier(codec, notificationBufferSize))
171+
ctx = context.WithValue(ctx, notifierKey{}, newBufferedNotifier(codec, notificationBufferSize))
175172
}
176173
s.codecsMu.Lock()
177174
if atomic.LoadInt32(&s.run) != 1 { // server stopped
@@ -275,7 +272,7 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
275272

276273
if req.isUnsubscribe { // cancel subscription, first param must be the subscription id
277274
if len(req.args) >= 1 && req.args[0].Kind() == reflect.String {
278-
notifier, supported := ctx.Value(NotifierContextKey).(*bufferedNotifier)
275+
notifier, supported := NotifierFromContext(ctx)
279276
if !supported { // interface doesn't support subscriptions (e.g. http)
280277
return codec.CreateErrorResponse(&req.id, &callbackError{ErrNotificationsUnsupported.Error()}), nil
281278
}
@@ -298,8 +295,8 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
298295

299296
// active the subscription after the sub id was successful sent to the client
300297
activateSub := func() {
301-
notifier, _ := ctx.Value(NotifierContextKey).(*bufferedNotifier)
302-
notifier.activate(subid)
298+
notifier, _ := NotifierFromContext(ctx)
299+
notifier.(*bufferedNotifier).activate(subid)
303300
}
304301

305302
return codec.CreateResponse(req.id, subid), activateSub

0 commit comments

Comments
 (0)