2017-05-24 17:34:35 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// packetQueue represent the wrapper around the original queue plus the
|
|
|
|
// functionality for releasing the queue objects in object channel. Such
|
2017-06-17 00:49:38 +03:00
|
|
|
// structures allows storing of all pending object in queue before the moment
|
|
|
|
// of actual releasing.
|
|
|
|
//
|
2017-05-24 17:34:35 +03:00
|
|
|
// TODO(andrew.shvv) structure not preserve the order if object failed second
|
|
|
|
// time.
|
|
|
|
type packetQueue struct {
|
|
|
|
*list.List
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// pending channel is needed in order to send the object which should
|
|
|
|
// be re-proceed.
|
|
|
|
pending chan *htlcPacket
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// grab channel represents the channel-lock which is needed in order to
|
|
|
|
// make "release" goroutines block during other release goroutine
|
2017-05-24 17:34:35 +03:00
|
|
|
// processing.
|
|
|
|
grab chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWaitingQueue() *packetQueue {
|
|
|
|
// Initialize grab channel and release one slot, otherwise release
|
|
|
|
// function will block.
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
done <- struct{}{}
|
|
|
|
|
|
|
|
return &packetQueue{
|
|
|
|
pending: make(chan *htlcPacket),
|
|
|
|
grab: done,
|
|
|
|
List: list.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// consume function take the given packet and store it in queue till release
|
|
|
|
// function will be executed.
|
|
|
|
func (q *packetQueue) consume(packet *htlcPacket) {
|
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
|
|
|
q.PushBack(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
// release function spawn the goroutine which grab the object from pending
|
|
|
|
// queue and pass it in processing channel.
|
|
|
|
func (q *packetQueue) release() {
|
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
|
|
|
if q.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2017-06-17 00:49:38 +03:00
|
|
|
// Grab the pending mutex so that other goroutines waits before
|
|
|
|
// grabbing the object, otherwise the objects will be send in
|
|
|
|
// the pending channel in random sequence.
|
2017-05-24 17:34:35 +03:00
|
|
|
<-q.grab
|
|
|
|
|
|
|
|
defer func() {
|
2017-06-17 00:49:38 +03:00
|
|
|
// Release the channel-lock and give other goroutines
|
|
|
|
// the ability to
|
2017-05-24 17:34:35 +03:00
|
|
|
q.grab <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Fetch object after releasing the pending mutex in order to
|
|
|
|
// preserver the order of the stored objects.
|
|
|
|
q.Lock()
|
|
|
|
e := q.Front()
|
|
|
|
q.Unlock()
|
|
|
|
|
|
|
|
if e != nil {
|
2017-06-17 00:49:38 +03:00
|
|
|
// Send the object in object queue and wait it to be
|
|
|
|
// processed by other side.
|
2017-05-24 17:34:35 +03:00
|
|
|
q.pending <- e.Value.(*htlcPacket)
|
|
|
|
|
|
|
|
// After object have been preprocessed remove it from
|
|
|
|
// the queue.
|
|
|
|
q.Lock()
|
|
|
|
q.Remove(e)
|
|
|
|
q.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// length returns the number of pending htlc packets which is stored in queue.
|
|
|
|
func (q *packetQueue) length() int {
|
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
|
|
|
return q.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// pendingAmount returns the amount of money which is stored in pending queue.
|
2017-08-22 09:36:43 +03:00
|
|
|
func (q *packetQueue) pendingAmount() lnwire.MilliSatoshi {
|
2017-05-24 17:34:35 +03:00
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
var amount lnwire.MilliSatoshi
|
2017-05-24 17:34:35 +03:00
|
|
|
for e := q.Front(); e != nil; e = e.Next() {
|
|
|
|
packet := e.Value.(*htlcPacket)
|
|
|
|
htlc := packet.htlc.(*lnwire.UpdateAddHTLC)
|
|
|
|
amount += htlc.Amount
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount
|
|
|
|
}
|