8784: Fix test for latest firefox.
[arvados.git] / sdk / go / httpserver / id_generator.go
1 package httpserver
2
3 import (
4         "strconv"
5         "sync"
6         "time"
7 )
8
9 // IDGenerator generates alphanumeric strings suitable for use as
10 // unique IDs (a given IDGenerator will never return the same ID
11 // twice).
12 type IDGenerator struct {
13         // Prefix is prepended to each returned ID.
14         Prefix string
15
16         lastID int64
17         mtx    sync.Mutex
18 }
19
20 // Next returns a new ID string. It is safe to call Next from multiple
21 // goroutines.
22 func (g *IDGenerator) Next() string {
23         id := time.Now().UnixNano()
24         g.mtx.Lock()
25         if id <= g.lastID {
26                 id = g.lastID + 1
27         }
28         g.lastID = id
29         g.mtx.Unlock()
30         return g.Prefix + strconv.FormatInt(id, 36)
31 }