Created
November 1, 2019 14:58
-
-
Save manhdaovan/284bb7087a04cd0b7974a776669f58ae to your computer and use it in GitHub Desktop.
Revisions
-
manhdaovan created this gist
Nov 1, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ type sequenceIDGenerator struct { idMgrClient IDManagerClient // client for getting IDPrefix preGenerateIDs chan string // pre-generated ids } // Pre-generate id and send to channel. // This method should be called once and only once when init generator func (g *sequenceIDGenerator) preGenIDs() { var idBase, id string var seq uint64 for { if idBase == "" || seq == 0 { // first call or over max uint 64 idBase = g.idMgrClient.GetNewIDPrefix("ServiceA") } id = fmt.Sprintf("%s-%d", idBase, strconv.FormatUint(seq, 32)) g.preGenerateIDs <- id seq++ } } // NewSequenceIDGenerator returns new id generator func NewSequenceIDGenerator(imc IDManagerClient) *sequenceIDGenerator { g := &sequenceIDGenerator{ idMgrClient: imc, preGenerateIDs: make(chan string, 10000), } go g.preGenIDs() return g } // NextID returns next id. func (g *sequenceIDGenerator) NextID() string { return <-g.preGenerateIDs }