aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-04-19 15:22:48 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-04-19 15:22:48 +0800
commita6ca8fd26884807c80b649bd2a0e780aa93ced22 (patch)
tree5b0e615a854e31e10e7a606e9e44b51b910529e9 /rpc
parent27116bd46cbc14a8eec9e1818945f1f10b9b834a (diff)
parenta40e61b4ac44a4f64f057a4220a26cfe4b9dcf03 (diff)
downloadgo-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.tar
go-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.tar.gz
go-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.tar.bz2
go-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.tar.lz
go-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.tar.xz
go-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.tar.zst
go-tangerine-a6ca8fd26884807c80b649bd2a0e780aa93ced22.zip
Merge pull request #2463 from fjl/rpc-context-key
rpc: remove NotifierContextKey
Diffstat (limited to 'rpc')
-rw-r--r--rpc/notification.go9
-rw-r--r--rpc/notification_test.go2
-rw-r--r--rpc/server.go11
3 files changed, 14 insertions, 8 deletions
diff --git a/rpc/notification.go b/rpc/notification.go
index 146d785c9..e84e26a58 100644
--- a/rpc/notification.go
+++ b/rpc/notification.go
@@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
+ "golang.org/x/net/context"
)
var (
@@ -62,6 +63,14 @@ type Notifier interface {
Unsubscribe(id string) error
}
+type notifierKey struct{}
+
+// NotifierFromContext returns the Notifier value stored in ctx, if any.
+func NotifierFromContext(ctx context.Context) (Notifier, bool) {
+ n, ok := ctx.Value(notifierKey{}).(Notifier)
+ return n, ok
+}
+
// Subscription defines the interface for objects that can notify subscribers
type Subscription interface {
// Inform client of an event
diff --git a/rpc/notification_test.go b/rpc/notification_test.go
index cd05d73fe..1bcede177 100644
--- a/rpc/notification_test.go
+++ b/rpc/notification_test.go
@@ -36,7 +36,7 @@ func (s *NotificationTestService) Unsubscribe(subid string) {
}
func (s *NotificationTestService) SomeSubscription(ctx context.Context, n, val int) (Subscription, error) {
- notifier, supported := ctx.Value(NotifierContextKey).(Notifier)
+ notifier, supported := NotifierFromContext(ctx)
if !supported {
return nil, ErrNotificationsUnsupported
}
diff --git a/rpc/server.go b/rpc/server.go
index cf90eba02..001107a1b 100644
--- a/rpc/server.go
+++ b/rpc/server.go
@@ -32,9 +32,6 @@ import (
const (
stopPendingRequestTimeout = 3 * time.Second // give pending requests stopPendingRequestTimeout the time to finish when the server is stopped
- // NotifierContextKey is the key where the notifier associated with the codec is stored in the context
- NotifierContextKey = 1
-
notificationBufferSize = 10000 // max buffered notifications before codec is closed
DefaultIPCApis = "admin,eth,debug,miner,net,shh,txpool,personal,web3"
@@ -171,7 +168,7 @@ func (s *Server) serveRequest(codec ServerCodec, singleShot bool, options CodecO
// to send notification to clients. It is thight to the codec/connection. If the
// connection is closed the notifier will stop and cancels all active subscriptions.
if options&OptionSubscriptions == OptionSubscriptions {
- ctx = context.WithValue(ctx, NotifierContextKey, newBufferedNotifier(codec, notificationBufferSize))
+ ctx = context.WithValue(ctx, notifierKey{}, newBufferedNotifier(codec, notificationBufferSize))
}
s.codecsMu.Lock()
if atomic.LoadInt32(&s.run) != 1 { // server stopped
@@ -275,7 +272,7 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
if req.isUnsubscribe { // cancel subscription, first param must be the subscription id
if len(req.args) >= 1 && req.args[0].Kind() == reflect.String {
- notifier, supported := ctx.Value(NotifierContextKey).(*bufferedNotifier)
+ notifier, supported := NotifierFromContext(ctx)
if !supported { // interface doesn't support subscriptions (e.g. http)
return codec.CreateErrorResponse(&req.id, &callbackError{ErrNotificationsUnsupported.Error()}), nil
}
@@ -298,8 +295,8 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
// active the subscription after the sub id was successful sent to the client
activateSub := func() {
- notifier, _ := ctx.Value(NotifierContextKey).(*bufferedNotifier)
- notifier.activate(subid)
+ notifier, _ := NotifierFromContext(ctx)
+ notifier.(*bufferedNotifier).activate(subid)
}
return codec.CreateResponse(req.id, subid), activateSub