AutoInc.go 527 B

12345678910111213141516171819202122232425262728293031323334
  1. package base
  2. type AutoInc struct {
  3. start, step int
  4. queue chan int
  5. running bool
  6. }
  7. func NewAutoInc(start, step int) (ai *AutoInc) {
  8. ai = &AutoInc{
  9. start: start,
  10. step: step,
  11. running: true,
  12. queue: make(chan int, 4),
  13. }
  14. go ai.process()
  15. return
  16. }
  17. func (ai *AutoInc) process() {
  18. defer func() { recover() }()
  19. for i := ai.start; ai.running; i = i + ai.step {
  20. ai.queue <- i
  21. }
  22. }
  23. func (ai *AutoInc) Id() int {
  24. return <-ai.queue
  25. }
  26. func (ai *AutoInc) Close() {
  27. ai.running = false
  28. close(ai.queue)
  29. }