コード例 #1
0
ファイル: subscriptions.go プロジェクト: mm3/Sia
// threadedSendUpdates sends updates to a specific subscriber as updates become
// available. If the subscriber deadlocks, this thread will deadlock, however
// that will not affect any of the other threads in the transaction pool.
func (tp *TransactionPool) threadedSendUpdates(update chan struct{}, subscriber modules.TransactionPoolSubscriber) {
	// Updates must be sent in order. This is achieved by having all of the
	// updates stored in the transaction pool in a specific order, and then
	// making blocking calls to 'ReceiveTransactionPoolUpates' until all of the
	// updates have been sent.
	i := 0
	for {
		// Determine how many total updates there are to send.
		id := tp.mu.RLock()
		updateCount := len(tp.consensusChanges)
		tp.mu.RUnlock(id)

		// Send each of the updates in order, starting from the first update
		// that has not yet been sent to the subscriber.
		for i < updateCount {
			id := tp.mu.RLock()
			cc := tp.consensusChanges[i]
			unconfirmedTransactions := tp.unconfirmedTransactions[i]
			unconfirmedDiffs := tp.unconfirmedSiacoinDiffs[i]
			tp.mu.RUnlock(id)
			subscriber.ReceiveTransactionPoolUpdate(cc, unconfirmedTransactions, unconfirmedDiffs)
			i++
		}

		// Wait until there has been another update.
		<-update
	}
}