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