1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
15 // IDGenerator generates alphanumeric strings suitable for use as
16 // unique IDs (a given IDGenerator will never return the same ID
18 type IDGenerator struct {
19 // Prefix is prepended to each returned ID.
26 // Next returns a new ID string. It is safe to call Next from multiple
28 func (g *IDGenerator) Next() string {
32 g.src = rand.NewSource(time.Now().UnixNano())
34 a, b := g.src.Int63(), g.src.Int63()
35 id := strconv.FormatInt(a, 36) + strconv.FormatInt(b, 36)
42 // AddRequestIDs wraps an http.Handler, adding an X-Request-Id header
43 // to each request that doesn't already have one.
44 func AddRequestIDs(h http.Handler) http.Handler {
45 gen := &IDGenerator{Prefix: "req-"}
46 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
47 if req.Header.Get("X-Request-Id") == "" {
48 if req.Header == nil {
49 req.Header = http.Header{}
51 req.Header.Set("X-Request-Id", gen.Next())