minor: move EwmaSetCurrent close to EwmaIncrInt64
Vladimir Bauer
1 year, 10 months ago
| 243 | 243 | } |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | // Increment is a shorthand for b.IncrInt64(1). | |
| 247 | func (b *Bar) Increment() { | |
| 248 | b.IncrInt64(1) | |
| 249 | } | |
| 250 | ||
| 251 | // IncrBy is a shorthand for b.IncrInt64(int64(n)). | |
| 252 | func (b *Bar) IncrBy(n int) { | |
| 253 | b.IncrInt64(int64(n)) | |
| 254 | } | |
| 255 | ||
| 256 | // IncrInt64 increments progress by amount of n. | |
| 257 | func (b *Bar) IncrInt64(n int64) { | |
| 258 | select { | |
| 259 | case b.operateState <- func(s *bState) { | |
| 260 | s.current += n | |
| 261 | if s.triggerComplete && s.current >= s.total { | |
| 262 | s.current = s.total | |
| 263 | s.triggerCompletion(b) | |
| 264 | } | |
| 265 | }: | |
| 266 | case <-b.ctx.Done(): | |
| 267 | } | |
| 268 | } | |
| 269 | ||
| 270 | // EwmaIncrement is a shorthand for b.EwmaIncrInt64(1, iterDur). | |
| 271 | func (b *Bar) EwmaIncrement(iterDur time.Duration) { | |
| 272 | b.EwmaIncrInt64(1, iterDur) | |
| 273 | } | |
| 274 | ||
| 275 | // EwmaIncrBy is a shorthand for b.EwmaIncrInt64(int64(n), iterDur). | |
| 276 | func (b *Bar) EwmaIncrBy(n int, iterDur time.Duration) { | |
| 277 | b.EwmaIncrInt64(int64(n), iterDur) | |
| 278 | } | |
| 279 | ||
| 280 | // EwmaIncrInt64 increments progress by amount of n and updates EWMA based | |
| 281 | // decorators by dur of a single iteration. | |
| 282 | func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) { | |
| 283 | var wg sync.WaitGroup | |
| 284 | iter := make(chan decor.EwmaDecorator) | |
| 285 | select { | |
| 286 | case b.operateState <- func(s *bState) { | |
| 287 | wg.Add(len(s.ewmaDecorators)) | |
| 288 | for _, d := range s.ewmaDecorators { | |
| 289 | iter <- d | |
| 290 | } | |
| 291 | close(iter) | |
| 292 | s.current += n | |
| 293 | if s.triggerComplete && s.current >= s.total { | |
| 294 | s.current = s.total | |
| 295 | s.triggerCompletion(b) | |
| 296 | } | |
| 297 | wg.Wait() | |
| 298 | }: | |
| 299 | for d := range iter { | |
| 300 | d := d | |
| 301 | go func() { | |
| 302 | d.EwmaUpdate(n, iterDur) | |
| 303 | wg.Done() | |
| 304 | }() | |
| 305 | } | |
| 306 | case <-b.ctx.Done(): | |
| 307 | } | |
| 308 | } | |
| 309 | ||
| 246 | 310 | // EwmaSetCurrent sets progress' current to an arbitrary value and updates |
| 247 | 311 | // EWMA based decorators by dur of a single iteration. |
| 248 | 312 | func (b *Bar) EwmaSetCurrent(current int64, iterDur time.Duration) { |
| 274 | 338 | d := d |
| 275 | 339 | go func() { |
| 276 | 340 | d.EwmaUpdate(d.n, iterDur) |
| 277 | wg.Done() | |
| 278 | }() | |
| 279 | } | |
| 280 | case <-b.ctx.Done(): | |
| 281 | } | |
| 282 | } | |
| 283 | ||
| 284 | // Increment is a shorthand for b.IncrInt64(1). | |
| 285 | func (b *Bar) Increment() { | |
| 286 | b.IncrInt64(1) | |
| 287 | } | |
| 288 | ||
| 289 | // IncrBy is a shorthand for b.IncrInt64(int64(n)). | |
| 290 | func (b *Bar) IncrBy(n int) { | |
| 291 | b.IncrInt64(int64(n)) | |
| 292 | } | |
| 293 | ||
| 294 | // IncrInt64 increments progress by amount of n. | |
| 295 | func (b *Bar) IncrInt64(n int64) { | |
| 296 | select { | |
| 297 | case b.operateState <- func(s *bState) { | |
| 298 | s.current += n | |
| 299 | if s.triggerComplete && s.current >= s.total { | |
| 300 | s.current = s.total | |
| 301 | s.triggerCompletion(b) | |
| 302 | } | |
| 303 | }: | |
| 304 | case <-b.ctx.Done(): | |
| 305 | } | |
| 306 | } | |
| 307 | ||
| 308 | // EwmaIncrement is a shorthand for b.EwmaIncrInt64(1, iterDur). | |
| 309 | func (b *Bar) EwmaIncrement(iterDur time.Duration) { | |
| 310 | b.EwmaIncrInt64(1, iterDur) | |
| 311 | } | |
| 312 | ||
| 313 | // EwmaIncrBy is a shorthand for b.EwmaIncrInt64(int64(n), iterDur). | |
| 314 | func (b *Bar) EwmaIncrBy(n int, iterDur time.Duration) { | |
| 315 | b.EwmaIncrInt64(int64(n), iterDur) | |
| 316 | } | |
| 317 | ||
| 318 | // EwmaIncrInt64 increments progress by amount of n and updates EWMA based | |
| 319 | // decorators by dur of a single iteration. | |
| 320 | func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) { | |
| 321 | var wg sync.WaitGroup | |
| 322 | iter := make(chan decor.EwmaDecorator) | |
| 323 | select { | |
| 324 | case b.operateState <- func(s *bState) { | |
| 325 | wg.Add(len(s.ewmaDecorators)) | |
| 326 | for _, d := range s.ewmaDecorators { | |
| 327 | iter <- d | |
| 328 | } | |
| 329 | close(iter) | |
| 330 | s.current += n | |
| 331 | if s.triggerComplete && s.current >= s.total { | |
| 332 | s.current = s.total | |
| 333 | s.triggerCompletion(b) | |
| 334 | } | |
| 335 | wg.Wait() | |
| 336 | }: | |
| 337 | for d := range iter { | |
| 338 | d := d | |
| 339 | go func() { | |
| 340 | d.EwmaUpdate(n, iterDur) | |
| 341 | 341 | wg.Done() |
| 342 | 342 | }() |
| 343 | 343 | } |