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