<commit_before>// Package reqid creates request IDs and stores them in Contexts.
package reqid

import (
	"crypto/rand"
	"encoding/hex"
	"log"

	"golang.org/x/net/context"
)

const Unknown = "unknown_req_id"

// New generates a random request ID.
func New() string {
	// Given n IDs of length b bits, the probability that there will be a collision is bounded by
	// the number of pairs of IDs multiplied by the probability that any pair might collide:
	// p ≤ n(n - 1)/2 * 1/(2^b)
	//
	// We assume an upper bound of 1000 req/sec, which means that in a week there will be
	// n = 1000 * 604800 requests. If l = 10, b = 8*10, then p ≤ 1.512e-7, which is a suitably
	// low probability.
	l := 10
	b := make([]byte, l)
	_, err := rand.Read(b)
	if err != nil {
		log.Println("error making reqID")
	}
	return hex.EncodeToString(b)
}

// NewContext returns a new Context that carries reqid.
func NewContext(ctx context.Context, reqid string) context.Context {
	return context.WithValue(ctx, "requestID", reqid)
}

// FromContext returns the request ID stored in ctx,
// or Unknown, if there is none.
func FromContext(ctx context.Context) string {
	reqID, ok := (ctx.Value("requestID")).(string)
	if !ok {
		return Unknown<commit_msg>net/http/reqid: use typed context key idiom for request IDs<commit_after>// Package reqid creates request IDs and stores them in Contexts.
package reqid

import (
	"crypto/rand"
	"encoding/hex"
	"log"

	"golang.org/x/net/context"
)

// key is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int

// reqIDKey is the key for request IDs in Contexts.  It is
// unexported; clients use NewContext and FromContext
// instead of using this key directly.
var reqIDKey key = 0

const Unknown = "unknown_req_id"

// New generates a random request ID.
func New() string {
	// Given n IDs of length b bits, the probability that there will be a collision is bounded by
	// the number of pairs of IDs multiplied by the probability that any pair might collide:
	// p ≤ n(n - 1)/2 * 1/(2^b)
	//
	// We assume an upper bound of 1000 req/sec, which means that in a week there will be
	// n = 1000 * 604800 requests. If l = 10, b = 8*10, then p ≤ 1.512e-7, which is a suitably
	// low probability.
	l := 10
	b := make([]byte, l)
	_, err := rand.Read(b)
	if err != nil {
		log.Println("error making reqID")
	}
	return hex.EncodeToString(b)
}

// NewContext returns a new Context that carries reqid.
func NewContext(ctx context.Context, reqid string) context.Context {
	return context.WithValue(ctx, reqIDKey, reqid)
}

// FromContext returns the request ID stored in ctx,
// or Unknown, if there is none.
func FromContext(ctx context.Context) string {
	reqID, ok := ctx.Value(reqIDKey).(string)
	if !ok {
		return Unknown
{'fraction_non_alphanumeric': 0.10648148148148148, 'fraction_numerical': 0.021723646723646725, 'mean_word_length': 3.4166666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2191151', 'n_tokens_mistral': 973, 'n_tokens_neox': 888, 'n_words': 381}