Codebase list golang-github-vbauerster-mpb / 69c9fb4
Refactor progress.go Vladimir Bauer 9 years ago
2 changed file(s) with 34 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
4646 // WaitGroup for internal rendering sync
4747 wg *sync.WaitGroup
4848
49 done chan struct{}
50 ops chan func(*pConf)
51 stopReqCh chan struct{}
52
53 // following is used after (*Progress.done) is closed
54 conf pConf
49 // quit channel to request p.server to quit
50 quit chan struct{}
51 // done channel is receiveable after p.server has been quit
52 done chan struct{}
53 ops chan func(*pConf)
5554 }
5655
5756 // New creates new Progress instance, which orchestrates bars rendering process.
7271 }
7372
7473 p := &Progress{
75 wg: new(sync.WaitGroup),
76 done: make(chan struct{}),
77 ops: make(chan func(*pConf)),
78 stopReqCh: make(chan struct{}),
74 wg: new(sync.WaitGroup),
75 done: make(chan struct{}),
76 ops: make(chan func(*pConf)),
77 quit: make(chan struct{}),
7978 }
8079 go p.server(conf)
8180 return p
9493 select {
9594 case p.ops <- op:
9695 return <-result
97 case <-p.done:
96 case <-p.quit:
9897 return nil
9998 }
10099 }
118117 select {
119118 case p.ops <- op:
120119 return <-result
121 case <-p.done:
120 case <-p.quit:
122121 return false
123122 }
124123 }
132131 select {
133132 case p.ops <- op:
134133 return <-result
135 case <-p.done:
134 case <-p.quit:
136135 return 0
137136 }
138137 }
143142 // cancelation purposes.
144143 func (p *Progress) Stop() {
145144 select {
146 case <-p.done:
145 case <-p.quit:
147146 return
148147 default:
149148 // complete Total unknown bars
154153 }
155154 // wait for all bars to quit
156155 p.wg.Wait()
157 // stop request
158 p.stopReqCh <- struct{}{}
156 // request p.server to quit
157 p.quitRequest()
159158 // wait for p.server to quit
160159 <-p.done
161160 }
162161 }
163162
163 func (p *Progress) quitRequest() {
164 select {
165 case <-p.quit:
166 default:
167 close(p.quit)
168 }
169 }
170
164171 // server monitors underlying channels and renders any progress bars
165172 func (p *Progress) server(conf pConf) {
166173
167174 defer func() {
168 p.conf = conf
175 // p.conf = conf
169176 if conf.shutdownNotifier != nil {
170177 close(conf.shutdownNotifier)
171178 }
172179 close(p.done)
173180 }()
174
175 // recoverFn := func(ch chan []byte) {
176 // if p := recover(); p != nil {
177 // ch <- []byte(fmt.Sprintln(p))
178 // }
179 // close(ch)
180 // }
181181
182182 for {
183183 select {
193193 conf.beforeRender(conf.bars)
194194 }
195195
196 quitWidthSyncCh := make(chan struct{})
196 wSyncTimeout := make(chan struct{})
197197 time.AfterFunc(conf.rr, func() {
198 close(quitWidthSyncCh)
198 close(wSyncTimeout)
199199 })
200200
201201 b0 := conf.bars[0]
202 prependWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfPrependers())
203 appendWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfAppenders())
202 prependWs := newWidthSync(wSyncTimeout, numBars, b0.NumOfPrependers())
203 appendWs := newWidthSync(wSyncTimeout, numBars, b0.NumOfAppenders())
204204
205205 tw, _, _ := cwriter.GetTermSize()
206206
221221 case <-conf.cancel:
222222 conf.ticker.Stop()
223223 conf.cancel = nil
224 case <-p.stopReqCh:
225 conf.ticker.Stop()
224 case <-p.quit:
225 if conf.cancel != nil {
226 conf.ticker.Stop()
227 }
226228 return
227229 }
228230 }
229231 }
230232
231 func newWidthSync(quit <-chan struct{}, numBars, numColumn int) *widthSync {
233 func newWidthSync(timeout <-chan struct{}, numBars, numColumn int) *widthSync {
232234 ws := &widthSync{
233235 listen: make([]chan int, numColumn),
234236 result: make([]chan int, numColumn),
249251 if len(widths) == numBars {
250252 break loop
251253 }
252 case <-quit:
254 case <-timeout:
253255 if len(widths) == 0 {
254256 return
255257 }
278280 return ch
279281 }
280282
281 func updateConf(p *Progress, op func(*pConf)) *Progress {
282 select {
283 case p.ops <- op:
284 return p
285 case <-p.done:
286 return nil
287 }
288 }
289
290283 func max(slice []int) int {
291284 max := slice[0]
292285
+0
-15
progress_go1.7.go less more
0 //+build go1.7
1
2 package mpb
3
4 import "context"
5
6 // WithContext Deprecated, use mpb.WithContext
7 func (p *Progress) WithContext(ctx context.Context) *Progress {
8 if ctx == nil {
9 panic("nil context")
10 }
11 return updateConf(p, func(c *pConf) {
12 c.cancel = ctx.Done()
13 })
14 }