From 667eaa1562a4b201c29ac37fc9cd174bba61d1ce Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 2 Oct 2013 21:16:38 -0500 Subject: [PATCH] Move to a callback for notifications. Rather than using a channel for notifictions, use a callback instead. There are several issues that arise with async notifications via a channel, so use a callback instead. The caller can always make the notifications async by using channels in the provided callback if needed. --- chain.go | 14 +++++++------- notifications.go | 16 ++++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/chain.go b/chain.go index 7c4c6a08..157f02a2 100644 --- a/chain.go +++ b/chain.go @@ -144,7 +144,7 @@ func removeChildNode(children []*blockNode, node *blockNode) []*blockNode { type BlockChain struct { db btcdb.Db btcnet btcwire.BitcoinNet - notifications chan *Notification + notifications NotificationCallback root *blockNode bestChain *blockNode index map[btcwire.ShaHash]*blockNode @@ -960,12 +960,12 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err } // New returns a BlockChain instance for the passed bitcoin network using the -// provided backing database. It accepts a channel on which asynchronous -// notifications will be sent when various events take place. See the -// documentation for Notification and NotificationType for details on the -// types and contents of notifications. The provided channel can be nil if the -// caller is not interested in receiving notifications. -func New(db btcdb.Db, btcnet btcwire.BitcoinNet, c chan *Notification) *BlockChain { +// provided backing database. It accepts a callback on which notifications +// will be sent when various events take place. See the documentation for +// Notification and NotificationType for details on the types and contents of +// notifications. The provided callback can be nil if the caller is not +// interested in receiving notifications. +func New(db btcdb.Db, btcnet btcwire.BitcoinNet, c NotificationCallback) *BlockChain { b := BlockChain{ db: db, btcnet: btcnet, diff --git a/notifications.go b/notifications.go index 51ac0c8a..a4ba98b1 100644 --- a/notifications.go +++ b/notifications.go @@ -11,6 +11,10 @@ import ( // NotificationType represents the type of a notification message. type NotificationType int +// NotificationCallback is used for a caller to provide a callback for +// notifications about various chain events. +type NotificationCallback func(*Notification) + // Constants for the type of a notification message. const ( // NTOrphanBlock indicates an orphan block was processed and the @@ -50,10 +54,9 @@ func (n NotificationType) String() string { return fmt.Sprintf("Unknown Notification Type (%d)", int(n)) } -// Notification defines an asynchronous notification that is sent to the caller -// over the notification channel provided during the call to New and consists -// of a notification type as well as associated data that depends on the type as -// follows: +// Notification defines notification that is sent to the caller via the callback +// function provided during the call to New and consists of a notification type +// as well as associated data that depends on the type as follows: // - NTOrphanBlock: *btcwire.ShaHash // - NTBlockAccepted: *btcutil.Block // - NTBlockConnected: *btcutil.Block @@ -64,7 +67,8 @@ type Notification struct { } // sendNotification sends a notification with the passed type and data if the -// caller requested notifications by providing a channel in the call to New. +// caller requested notifications by providing a callback function in the call +// to New. func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) { // Ignore it if the caller didn't request notifications. if b.notifications == nil { @@ -73,5 +77,5 @@ func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) { // Generate and send the notification. n := Notification{Type: typ, Data: data} - b.notifications <- &n + b.notifications(&n) }