forked from 0xPolygon/cdk-erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
140 lines (116 loc) · 2.52 KB
/
Copy pathutils.go
File metadata and controls
140 lines (116 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package zk
import (
"errors"
"fmt"
"time"
"github.com/ledgerwatch/log/v3"
"sync"
)
var ErrLimboState = errors.New("Calculating limbo state")
// prints progress every 10 seconds
// returns a channel to send progress to, and a function to stop the printer routine
func ProgressPrinter(message string, total uint64, quiet bool) (chan uint64, func()) {
progress := make(chan uint64)
ctDone := make(chan bool)
var once sync.Once
cleanup := func() {
once.Do(func() {
close(ctDone)
})
}
go func() {
defer close(progress)
defer cleanup()
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var pc uint64
var pct uint64
for {
select {
case newPc := <-progress:
pc += newPc
if total > 0 {
pct = (pc * 100) / total
}
if pct == 100 {
log.Info(fmt.Sprintf("%s: %d/%d (%d%%)", message, pc, total, pct))
}
case <-ticker.C:
if pc > 0 && !quiet {
log.Info(fmt.Sprintf("%s: %d/%d (%d%%)", message, pc, total, pct))
}
case <-ctDone:
return
}
}
}()
return progress, cleanup
}
// prints progress every 10 seconds
// returns a channel to send progress to, and a function to stop the printer routine
func ProgressPrinterWithoutTotal(message string) (chan uint64, func()) {
progress := make(chan uint64)
ctDone := make(chan bool)
var once sync.Once
cleanup := func() {
once.Do(func() {
close(ctDone)
})
}
go func() {
defer close(progress)
defer cleanup()
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var pc uint64
for {
select {
case newPc := <-progress:
pc = newPc
case <-ticker.C:
if pc > 0 {
log.Info(fmt.Sprintf("%s: %d", message, pc))
}
case <-ctDone:
return
}
}
}()
return progress, cleanup
}
// prints progress every 10 seconds
// returns a channel to send progress to, and a function to stop the printer routine
func ProgressPrinterWithoutValues(message string, total uint64) (chan uint64, func()) {
progress := make(chan uint64)
ctDone := make(chan bool)
var once sync.Once
cleanup := func() {
once.Do(func() {
close(ctDone)
})
}
go func() {
defer close(progress)
defer cleanup()
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var pc uint64
var pct uint64
for {
select {
case newPc := <-progress:
pc = newPc
if total > 0 {
pct = (pc * 100) / total
}
case <-ticker.C:
if pc > 0 {
log.Info(fmt.Sprintf("%s: (%d%%)", message, pct))
}
case <-ctDone:
return
}
}
}()
return progress, cleanup
}