aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2016-04-16 00:05:24 +0800
committerFelix Lange <fjl@twurst.com>2016-04-16 00:10:52 +0800
commita40e61b4ac44a4f64f057a4220a26cfe4b9dcf03 (patch)
tree401218a5f3ed5b4fac88baf87551bd3f57f3c34b
parent6197fbf8d70c1aa96c3e87de91ff3f46f454c1ea (diff)
downloaddexon-a40e61b4ac44a4f64f057a4220a26cfe4b9dcf03.tar.gz
dexon-a40e61b4ac44a4f64f057a4220a26cfe4b9dcf03.tar.zst
dexon-a40e61b4ac44a4f64f057a4220a26cfe4b9dcf03.zip
rpc: remove NotifierContextKey
Context keys must have a unique type in order to prevent any unintented clashes. The code used int(1) as key. Fix it by implementing the pattern recommended by package context.
-rw-r--r--eth/api.go4
-rw-r--r--eth/downloader/api.go2
-rw-r--r--eth/filters/api.go2
-rw-r--r--rpc/notification.go9
-rw-r--r--rpc/notification_test.go2
-rw-r--r--rpc/server.go11
6 files changed, 18 insertions, 12 deletions
diff --git a/eth/api.go b/eth/api.go
index 41387c27f..a0b1f8ac2 100644
--- a/eth/api.go
+++ b/eth/api.go
@@ -603,7 +603,7 @@ type NewBlocksArgs struct {
// NewBlocks triggers a new block event each time a block is appended to the chain. It accepts an argument which allows
// the caller to specify whether the output should contain transactions and in what format.
func (s *PublicBlockChainAPI) NewBlocks(ctx context.Context, args NewBlocksArgs) (rpc.Subscription, error) {
- notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
+ notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return nil, rpc.ErrNotificationsUnsupported
}
@@ -1345,7 +1345,7 @@ func (s *PublicTransactionPoolAPI) PendingTransactions() []*RPCTransaction {
// NewPendingTransaction creates a subscription that is triggered each time a transaction enters the transaction pool
// and is send from one of the transactions this nodes manages.
func (s *PublicTransactionPoolAPI) NewPendingTransactions(ctx context.Context) (rpc.Subscription, error) {
- notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
+ notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return nil, rpc.ErrNotificationsUnsupported
}
diff --git a/eth/downloader/api.go b/eth/downloader/api.go
index 670a3ade3..94cd6515f 100644
--- a/eth/downloader/api.go
+++ b/eth/downloader/api.go
@@ -85,7 +85,7 @@ type SyncingResult struct {
// Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (rpc.Subscription, error) {
- notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
+ notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return nil, rpc.ErrNotificationsUnsupported
}
diff --git a/eth/filters/api.go b/eth/filters/api.go
index 9e95ebd83..d9bd4d4b7 100644
--- a/eth/filters/api.go
+++ b/eth/filters/api.go
@@ -234,7 +234,7 @@ func (s *PublicFilterAPI) newLogFilter(earliest, latest int64, addresses []commo
}
func (s *PublicFilterAPI) Logs(ctx context.Context, args NewFilterArgs) (rpc.Subscription, error) {
- notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
+ notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return nil, rpc.ErrNotificationsUnsupported
}
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
pan>/+1 * Moved kdemultimedia3 to multimedia category.alane2002-12-091-1/+1 * -frontpage-pt_BR ports/www/frontpage-pt_BRlioux2002-12-071-5/+5 * Ok, done.obrien2002-12-051-1/+0 * I was wrong.obrien2002-12-051-0/+1 * Done.obrien2002-12-051-1/+0 * GCC cleanupobrien2002-12-051-0/+1 * The following ports where missing the "ports/" path prefix:olgeni2002-12-041-9/+9 * Done now.obrien2002-12-021-1/+0 * Missed one in the BU import.obrien2002-12-021-0/+1 * Done in contrib/binutils.obrien2002-12-021-1/+0 * Exclude src/contrib/binutils from $FreeBSD$ while I finish up in contrib.obrien2002-12-021-0/+1 * I'm done for now.obrien2002-12-021-1/+0 * Exclude src/contrib/binutils from $FreeBSD$ checking for the time beingobrien2002-12-021-0/+1 * -avifile ports/graphics/avifilelioux2002-11-291-32/+33 * Unlink pspell-ispell.knu2002-11-281-1/+0 * Realize how addport spoiled people and sort entries in alphabeticalknu2002-11-281-11/+11 * ruby-gconf2 -> devel/ruby-gconf2knu2002-11-281-0/+9 * mplayerxp --> ports/multimedia/mplayerxplioux2002-11-281-0/+1 * ko-netscape48-communicator -> ports/korean/netscape48-communicatorcjh2002-11-271-0/+2 * reallyslick --> ports/graphics/reallyslicklioux2002-11-251-0/+1 * euchre --> ports/games/euchrewill2002-11-251-0/+1 * mkultra --> ports/x11-wm/mkultraarved2002-11-251-0/+1 * Add a module for the linux-ibm-jdk14 port.glewis2002-11-251-0/+1 * Add entry for new port of gecc.trevor2002-11-251-0/+1 * grhino --> ports/games/grhinoarved2002-11-251-0/+1 * pinball --> ports/games/pinballarved2002-11-251-0/+1 * netspades --> ports/games/netspadesarved2002-11-251-0/+1 * knights --> ports/games/knightsarved2002-11-251-0/+1 * freebsd-games --> ports/games/freebsd-gamesmarkm2002-11-251-0/+1 * linux-runrev --> ports/devel/linux-runrevlioux2002-11-241-0/+1 * xarchon --> ports/games/xarchonlioux2002-11-241-0/+1 * Remove xcopilot, port supersceded by palm/pose.markp2002-11-241-1/+0 * fortune-mod-futurama --> ports/misc/fortune-mod-futuramaedwin2002-11-241-0/+1 * whitespace.el --> ports/textproc/whitespace.eledwin2002-11-241-0/+1 * ja-ruby-refe --> ports/japanese/ruby-refeknu2002-11-241-0/+1 * linux-ungif --> ports/graphics/linux-ungifmaho2002-11-241-0/+1 * ko-man-doc -> korean/man-docperky2002-11-241-0/+1 * remove libxpg4-nssada2002-11-241-1/+0 * arpd --> ports/net/arpdedwin2002-11-241-0/+1 * gkrellkam2 --> ports/graphics/gkrellkam2edwin2002-11-241-0/+1 * gkrellweather2 --> ports/misc/gkrellweather2edwin2002-11-241-0/+1 * gkrellmmailwatch2 --> ports/mail/gkrellmmailwatch2edwin2002-11-241-0/+1 * gkrellmfmonitor2 --> ports/misc/gkrellmfmonitor2edwin2002-11-241-0/+1 * ruby-rmagick --> ports/graphics/ruby-rmagickknu2002-11-241-0/+1 * pornview --> ports/graphics/pornviewarved2002-11-241-0/+1 * libgringotts --> ports/security/libgringottsanders2002-11-241-0/+1 * bins --> ports/www/binslioux2002-11-241-0/+1 * kgpg --> ports/security/kgpgarved2002-11-241-0/+1 * p5-XML-XQL --> ports/textproc/p5-XML-XQLlioux2002-11-241-0/+1 * Remove french/cups* in preparation for update to cups-1.1.16.alane2002-11-241-3/+0 * Remove ko-gaim. It can be replaced by net/gaim.cjh2002-11-231-1/+0 * geheimnis --> ports/security/geheimnisarved2002-11-231-0/+1 * aap --> ports/devel/aaparved2002-11-231-0/+1 * pup --> ports/print/puparved2002-11-231-0/+1 * remove ko-w3mcjh2002-11-231-1/+0 * gbib --> ports/databases/gbibedwin2002-11-231-0/+1 * bibletime --> ports/misc/bibletimeedwin2002-11-231-0/+1 * bibletime-doc --> ports/misc/bibletime-docedwin2002-11-231-0/+1 * privoxy --> ports/www/privoxynork2002-11-231-0/+1 * p5-Text-Reflow --> ports/textproc/p5-Text-Reflownork2002-11-231-0/+1 * p5-HTML-Widgets-SelectLayers --> ports/www/p5-HTML-Widgets-SelectLayersnork2002-11-231-0/+1 * p5-DBIx-DBSchema --> ports/databases/p5-DBIx-DBSchemanork2002-11-231-0/+1 * ruby-cache --> ports/devel/ruby-cacheknu2002-11-231-0/+1 * -br-ispell --> ports/textproc/br-ispelllioux2002-11-221-1/+1 * gfax --> ports/comms/gfaxmarcus2002-11-221-0/+1 * ksubeditor --> ports/multimedia/ksubeditorlioux2002-11-221-0/+1 * gtksubtitler --> ports/multimedia/gtksubtitlerlioux2002-11-221-0/+1 * fmirror --> ports/ftp/fmirroredwin2002-11-221-0/+1 * Add entries for libgnome and libgnomecanvas.adamw2002-11-221-0/+2 * Add ports/net/ruby-spread.seanc2002-11-221-0/+1 * -p5-Math-Financial ports/math/p5-Math-Financialknu2002-11-221-3/+3 * qhacc moved to financearved2002-11-221-1/+1 * gauche-gtk --> ports/x11-toolkits/gauche-gtknork2002-11-221-0/+1 * ports_finance -> ports/financeknu2002-11-221-0/+3 * Unlink ja-tvi.knu2002-11-221-1/+0 * Add hammerhead.vanilla2002-11-211-0/+1 * p5-Rcs --> ports/devel/p5-Rcstobez2002-11-211-0/+1 * Remove ports/sysutils/pkgview.marcus2002-11-211-1/+0 * gnome-pkgview --> ports/sysutils/gnome-pkgviewmarcus2002-11-211-0/+1 * ccmsn --> ports/net/ccmsnijliao2002-11-201-0/+1 * rpl --> ports/misc/rplijliao2002-11-201-0/+1 * pager --> ports/x11-wm/pagerobraun2002-11-201-0/+1 * bubblegum --> ports/security/bubblegumijliao2002-11-201-0/+1 * treepuzzle --> ports/biology/treepuzzleijliao2002-11-201-0/+1 * galax --> ports/textproc/galaxobraun2002-11-201-0/+1 * xtypo --> ports/misc/xtypoijliao2002-11-191-0/+1 * yape --> ports/emulators/yapeijliao2002-11-191-0/+1 * lgeneral --> ports/games/lgeneralijliao2002-11-191-0/+1 * hu-ispell -> ports/hungarian/ispellknu2002-11-191-0/+2 * ja-mozilla-jlp-devel -> ports/japanese/mozilla-jlp-develknu2002-11-191-0/+1 * libwpcg --> ports/graphics/libwpcgijliao2002-11-191-0/+1 * Correct entry for cdrdao after repo-move.netchild2002-11-181-1/+1 * libsidplay2 -> ports/audio/libsidplay2anders2002-11-181-0/+1 * resid --> ports/audio/residanders2002-11-181-0/+1 * Remove linux_mesakris2002-11-181-1/+0 * gtkam --> ports/graphics/gtkamanders2002-11-181-0/+1 * galeon2 --> ports/www/galeon2marcus2002-11-181-0/+1 * mozilla-devel-gtk2 --> ports/www/mozilla-devel-gtk2marcus2002-11-181-0/+1 * add leo because addport barfed on itselfalane2002-11-181-0/+1 * gtklp --> ports/print/gtklpmarcus2002-11-181-0/+1 * remove ja-mhonarcsada2002-11-171-1/+0 * remove netscape47 and earlierssada2002-11-171-30/+0 * add belowsada2002-11-171-0/+9 * irrtoolset --> ports/net/irrtoolsetsada2002-11-161-1/+1 * xmms-mailnotify --> ports/audio/xmms-mailnotifyarved2002-11-161-0/+1 * simicq --> ports/net/simicqarved2002-11-161-0/+1 * ruby-bz2 -> ports/archivers/ruby-bz2knu2002-11-161-0/+1 * swhplugins --> ports/audio/swhpluginsarved2002-11-161-0/+1 * xbubble --> ports/games/xbubblearved2002-11-161-0/+1 * cy-aspell --> ports/textproc/cy-aspellsada2002-11-151-0/+1 * el-aspell --> ports/textproc/el-aspellsada2002-11-151-0/+1 * uk-aspell --> ports/ukrainian/aspellsada2002-11-151-0/+1 * startup-notification --> ports/x11/startup-notificationmarcus2002-11-151-0/+1 * Fix typo: s/x11-font/x11-fonts/ for terminus-font :)olgeni2002-11-141-1/+1 * jakarta-commons-logging --> ports/java/jakarta-commons-loggingznerd2002-11-141-0/+1 * jakarta-commons-beanutils --> ports/java/jakarta-commons-beanutilsznerd2002-11-141-0/+1 * Add terminus-fonts.vanilla2002-11-141-0/+1 * jakarta-commons-cli --> ports/java/jakarta-commons-cliznerd2002-11-141-0/+1 * pkgview --> ports/sysutils/pkgviewmarcus2002-11-141-0/+1 * Add:adamw2002-11-141-0/+1 * atlc --> ports/cad/atlclioux2002-11-141-0/+1 * flashpluginwrapper --> ports/www/flashpluginwrapperpetef2002-11-141-0/+1 * minivmac --> ports/emulators/minivmaclioux2002-11-141-0/+1 * cascade --> ports/cad/cascadelioux2002-11-141-0/+1 * gob2 --> ports/devel/gob2marcus2002-11-141-0/+1 * fr-plgrenouille --> ports/french/plgrenouillenaddy2002-11-141-0/+1 * xapply --> ports/sysutils/xapplypetef2002-11-141-0/+1 * flock --> ports/sysutils/flockpetef2002-11-141-0/+1 * curly --> ports/sysutils/curlypetef2002-11-131-0/+1 * Tee --> ports/sysutils/Teepetef2002-11-131-0/+1 * Skip the CVSweb icon files.knu2002-11-131-0/+3 * vi-vi2vn --> ports/vietnamese/vi2vnobrien2002-11-131-0/+1 * hlserver-trainhunters --> ports/games/hlserver-trainhunterslioux2002-11-131-0/+1 * ruby-tserver --> ports/net/ruby-tserverknu2002-11-131-0/+1 * p5-HTML-FromText --> ports/www/p5-HTML-FromTextnaddy2002-11-131-0/+1 * hlserver-ns --> ports/games/hlserver-nslioux2002-11-121-0/+1 * Remove ports/japanese/pine.max2002-11-121-1/+0 * bogofilter --> ports/mail/bogofilterijliao2002-11-121-0/+1 * Add devel/distel.olgeni2002-11-111-0/+1 * Add py_otp_interface (addport complained).olgeni2002-11-111-0/+1 * Add usermin entry by hand.olgeni2002-11-111-0/+1 * ruby-radius --> ports/net/ruby-radiusknu2002-11-111-0/+1 * ruby-amrita --> ports/textproc/ruby-amritaknu2002-11-111-0/+1 * ruby-amatch --> ports/textproc/ruby-amatchknu2002-11-111-0/+1 * ruby-raspell --> ports/textproc/ruby-raspellknu2002-11-111-0/+1 * Add jarl and qssl modules (ports) since addport (modulesupdate, Idd2002-11-111-0/+2 * Unlock the ports tree now that Mario is finished converting PORTCOMMENTwill2002-11-111-1/+1 * Temporarily lock out the ports collection so lioux can revert thekris2002-11-101-1/+1 * notftp --> ports/www/notftpsada2002-11-101-0/+1 * ruby-nodedump --> ports/devel/ruby-nodedumpknu2002-11-101-0/+1 * netcomponents --> ports/java/netcomponentsznerd2002-11-091-0/+1 * ruby-robjectteam --> ports/devel/ruby-robjectteamknu2002-11-091-0/+1 * picasm --> ports/devel/picasmnaddy2002-11-091-0/+1 * Add p5-Proc-PIDFile module (addport failed on this step).kbyanc2002-11-091-0/+1 * Add an entry for net/yaz++ port.demon2002-11-091-0/+1 * pkill --> ports/sysutils/pkilllioux2002-11-081-0/+1 * xmms-gdancer moved from audio to graphics categorytom2002-11-081-1/+1 * gqlplus --> ports/databases/gqlplusleeym2002-11-081-0/+1 * ircservices --> ports/irc/ircservicesijliao2002-11-081-0/+1 * Add whole bunch of p5- modules that were not added at the time of commit duesobomax2002-11-071-0/+18 * Add msp430-libc port.jkoshy2002-11-071-0/+1 * jumpnbump --> ports/games/jumpnbumpadamw2002-11-071-0/+1 * zh-cdict5 --> ports/chinese/cdict5ijliao2002-11-061-0/+1 * Add MSP430 modules.jkoshy2002-11-061-0/+3 * emacs-wget -> ports/ftp/emacs-wgetyoichi2002-11-061-0/+1 * Create port for xlog, an amateur radio logging app. The addport scriptpatrick2002-11-061-0/+1 * Removed textproc/kdoc, which should have died a long long time ago.alane2002-11-051-1/+0 * New MIT Kerberos V beta, version 1.2.7-beta1.cy2002-11-051-0/+1 * apotheke --> ports/x11-fm/apothekemarcus2002-11-051-0/+1 * Unlink pruby and ruby-dbd_sybase.knu2002-11-051-2/+0 * libical --> ports/devel/libicalmarcus2002-11-051-0/+1 * jdb --> ports/databases/jdbnork2002-11-041-0/+1 * cal.rb --> ports/japanese/cal.rbnork2002-11-041-0/+1 * Add tomekris2002-11-041-0/+1 * p5-xmltv --> ports/textproc/p5-xmltvlioux2002-11-041-0/+1 * p5-Tk-TableMatrix --> ports/x11-toolkits/p5-Tk-TableMatrixlioux2002-11-041-0/+1 * Add pekwm.vanilla2002-11-041-0/+1 * netspoc --> ports/net/netspocijliao2002-11-031-0/+1 * frozenbubble --> ports/games/frozenbubblearved2002-11-031-0/+1