Automatically replenish when the number of ID pools is reduced to half

My computer develop environment

1
2
3
4
5
6
7
$ sw_vers 
ProductName: Mac OS X
ProductVersion: 10.12.6
BuildVersion: 16G29

$ go version
go version go1.8.3 darwin/amd64

I’m using the [twitter Snowflake] (https://github.com/twitter/snowflake) to build a uuid service.

I want to maintain a id pool to provide this service which can be auto filled id up when the number of id is half the reset

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
package main

import (
"fmt"
"time"
)

var ids []int
var idsLen = 10
var needFill = make(chan bool)

func main() {

go fillIds()

for {
if len(ids) < idsLen/2 {
needFill <- true
} else {
needFill <- false
}

ids = ids[1:]

fmt.Printf("%v\n", ids)
time.Sleep(time.Second * 2)
}
}

func fillIds() {
for {
if <-needFill {

ids = append(ids, 1, 2, 3, 4, 5)
}
}
}
Share