mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 22:44:24 +00:00
go fmt
This commit is contained in:
+31
-28
@@ -1,45 +1,48 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"time"
|
||||
"sync"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
/* Debouncer */
|
||||
type Debouncer struct {
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
dur time.Duration
|
||||
mtx sync.Mutex
|
||||
timer *time.Timer
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
dur time.Duration
|
||||
mtx sync.Mutex
|
||||
timer *time.Timer
|
||||
}
|
||||
|
||||
func NewDebouncer(dur time.Duration) *Debouncer {
|
||||
var timer *time.Timer
|
||||
var ch = make(chan struct{})
|
||||
var quit = make(chan struct{})
|
||||
var mtx sync.Mutex
|
||||
fire := func() {
|
||||
go func() {
|
||||
select {
|
||||
case ch <- struct{}{}:
|
||||
case <-quit:
|
||||
}
|
||||
}()
|
||||
mtx.Lock(); defer mtx.Unlock()
|
||||
timer.Reset(dur)
|
||||
}
|
||||
timer = time.AfterFunc(dur, fire)
|
||||
return &Debouncer{Ch:ch, dur:dur, quit:quit, mtx:mtx, timer:timer}
|
||||
var timer *time.Timer
|
||||
var ch = make(chan struct{})
|
||||
var quit = make(chan struct{})
|
||||
var mtx sync.Mutex
|
||||
fire := func() {
|
||||
go func() {
|
||||
select {
|
||||
case ch <- struct{}{}:
|
||||
case <-quit:
|
||||
}
|
||||
}()
|
||||
mtx.Lock()
|
||||
defer mtx.Unlock()
|
||||
timer.Reset(dur)
|
||||
}
|
||||
timer = time.AfterFunc(dur, fire)
|
||||
return &Debouncer{Ch: ch, dur: dur, quit: quit, mtx: mtx, timer: timer}
|
||||
}
|
||||
|
||||
func (d *Debouncer) Reset() {
|
||||
d.mtx.Lock(); defer d.mtx.Unlock()
|
||||
d.timer.Reset(d.dur)
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
d.timer.Reset(d.dur)
|
||||
}
|
||||
|
||||
func (d *Debouncer) Stop() bool {
|
||||
d.mtx.Lock(); defer d.mtx.Unlock()
|
||||
close(d.quit)
|
||||
return d.timer.Stop()
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
close(d.quit)
|
||||
return d.timer.Stop()
|
||||
}
|
||||
|
||||
+28
-29
@@ -1,28 +1,28 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"container/heap"
|
||||
)
|
||||
|
||||
type Heap struct {
|
||||
pq priorityQueue
|
||||
pq priorityQueue
|
||||
}
|
||||
|
||||
func NewHeap() *Heap {
|
||||
return &Heap{pq:make([]*pqItem, 0)}
|
||||
return &Heap{pq: make([]*pqItem, 0)}
|
||||
}
|
||||
|
||||
func (h *Heap) Len() int {
|
||||
return len(h.pq)
|
||||
return len(h.pq)
|
||||
}
|
||||
|
||||
func (h *Heap) Push(value interface{}, priority int) {
|
||||
heap.Push(&h.pq, &pqItem{value:value, priority:priority})
|
||||
heap.Push(&h.pq, &pqItem{value: value, priority: priority})
|
||||
}
|
||||
|
||||
func (h *Heap) Pop() interface{} {
|
||||
item := heap.Pop(&h.pq).(*pqItem)
|
||||
return item.value
|
||||
item := heap.Pop(&h.pq).(*pqItem)
|
||||
return item.value
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -43,9 +43,9 @@ func main() {
|
||||
// From: http://golang.org/pkg/container/heap/#example__priorityQueue
|
||||
|
||||
type pqItem struct {
|
||||
value interface{}
|
||||
priority int
|
||||
index int
|
||||
value interface{}
|
||||
priority int
|
||||
index int
|
||||
}
|
||||
|
||||
type priorityQueue []*pqItem
|
||||
@@ -53,35 +53,34 @@ type priorityQueue []*pqItem
|
||||
func (pq priorityQueue) Len() int { return len(pq) }
|
||||
|
||||
func (pq priorityQueue) Less(i, j int) bool {
|
||||
return pq[i].priority < pq[j].priority
|
||||
return pq[i].priority < pq[j].priority
|
||||
}
|
||||
|
||||
func (pq priorityQueue) Swap(i, j int) {
|
||||
pq[i], pq[j] = pq[j], pq[i]
|
||||
pq[i].index = i
|
||||
pq[j].index = j
|
||||
pq[i], pq[j] = pq[j], pq[i]
|
||||
pq[i].index = i
|
||||
pq[j].index = j
|
||||
}
|
||||
|
||||
func (pq *priorityQueue) Push(x interface{}) {
|
||||
n := len(*pq)
|
||||
item := x.(*pqItem)
|
||||
item.index = n
|
||||
*pq = append(*pq, item)
|
||||
n := len(*pq)
|
||||
item := x.(*pqItem)
|
||||
item.index = n
|
||||
*pq = append(*pq, item)
|
||||
}
|
||||
|
||||
func (pq *priorityQueue) Pop() interface{} {
|
||||
old := *pq
|
||||
n := len(old)
|
||||
item := old[n-1]
|
||||
item.index = -1 // for safety
|
||||
*pq = old[0 : n-1]
|
||||
return item
|
||||
old := *pq
|
||||
n := len(old)
|
||||
item := old[n-1]
|
||||
item.index = -1 // for safety
|
||||
*pq = old[0 : n-1]
|
||||
return item
|
||||
}
|
||||
|
||||
func (pq *priorityQueue) Update(item *pqItem, value interface{}, priority int) {
|
||||
heap.Remove(pq, item.index)
|
||||
item.value = value
|
||||
item.priority = priority
|
||||
heap.Push(pq, item)
|
||||
heap.Remove(pq, item.index)
|
||||
item.value = value
|
||||
item.priority = priority
|
||||
heap.Push(pq, item)
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Panicf(s string, args ...interface{}) {
|
||||
panic(fmt.Sprintf(s, args...))
|
||||
panic(fmt.Sprintf(s, args...))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user