util

package
v9.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package util provides general helpers used by Relay code.

Index

Constants

View Source
const RedactedPlaceholder = "xxxxx"

RedactedPlaceholder is substituted for any part of a URL that could contain a credential.

Variables

This section is empty.

Functions

func DecompressGzipData

func DecompressGzipData(data []byte) ([]byte, error)

func ErrorJSONMsg

func ErrorJSONMsg(msg string) (j []byte)

ErrorJSONMsg returns a json-encoded error message

func ErrorJSONMsgf

func ErrorJSONMsgf(fmtStr string, args ...interface{}) []byte

ErrorJSONMsgf returns a json-encoded error message using the printf formatter

func NewReader

func NewReader(r io.ReadCloser, isGzipped bool, maxInboundPayloadSize ct.OptBase2Bytes) (io.ReadCloser, error)

NewReader creates a new reader

func PtrOrNil

func PtrOrNil[T comparable](v T) *T

PtrOrNil returns a pointer to v, or nil when v is the zero value for its type. It is used to model optional fields where the zero value (e.g. an empty string or the zero time.Time) means "absent".

func RedactURL

func RedactURL(inputURL string) string

RedactURL replaces the credential-bearing components of a URL string - the userinfo section, the query, and the fragment - with RedactedPlaceholder, while preserving the scheme, host, port and path so the result is still useful for diagnostics. Each component is replaced only when it is actually present.

The path is deliberately preserved, so the result is not safe for a URL that embeds a credential in a path segment.

func SanitizeUTF8

func SanitizeUTF8(v string) string

SanitizeUTF8 removes invalid UTF-8 byte sequences from v.

OpenTelemetry serializes attribute values into OTLP protobuf string fields, and proto3 requires those to be valid UTF-8. An invalid byte does not merely spoil the value: proto.Marshal fails, so the entire export batch is dropped rather than just the offending span or data point. For metrics that is unrecoverable, because the poisoned series is cumulative and gets re-collected on every interval until the process restarts.

This is reachable from ordinary request data. HTTP header values are not restricted to ASCII -- RFC 7230 permits obs-text, and Go's parser passes those bytes through unchanged -- and a percent-encoded URL path decodes to arbitrary bytes. Any attribute value derived from either has to pass through here.

Types

type CleanupTasks

type CleanupTasks []func()

CleanupTasks accumulates a list of things which should be done at the end of a method unless Clear() is called. Intended usage:

var cleanup CleanupTasks
defer cleanup.Run()

thing1 := createThing1()
cleanup.AddCloser(thing1)

err := doSomethingElse()
if err != nil {
	return err // thing1 will be disposed of automatically
}

cleanup.Clear() // everything succeeded so we don't want thing1 to be disposed of

func (*CleanupTasks) AddCloser

func (t *CleanupTasks) AddCloser(c io.Closer)

AddCloser adds a task for calling Close on an object

func (*CleanupTasks) AddFunc

func (t *CleanupTasks) AddFunc(f func())

AddFunc adds a task.

func (*CleanupTasks) Clear

func (t *CleanupTasks) Clear()

Clear clears all tasks that were added.

func (*CleanupTasks) Run

func (t *CleanupTasks) Run()

Run runs all of the tasks.

type PayloadReader

type PayloadReader struct {
	IsGzipped bool
	MaxBytes  int64
	// contains filtered or unexported fields
}

PayloadReader is an implementation of io.Reader that reads bytes off the request body optionally decompresses them, and has a limit attached. If the limit is reached, an error will be returned and the underlying stream will be closed.

Note: limit is applied to *both* compressed and uncompressed number of bytes. This protects us from potential zipbombs

func (*PayloadReader) Close

func (pr *PayloadReader) Close() error

func (*PayloadReader) GetBytesRead

func (pr *PayloadReader) GetBytesRead() int64

GetBytesRead returns the total number of bytes read off the original stream

func (*PayloadReader) GetUncompressedBytesRead

func (pr *PayloadReader) GetUncompressedBytesRead() int64

GetUncompressedBytesRead Total number of Bytes in the uncompressed stream.

GetBytesRead and  GetUncompressedBytesRead will return the same value if the stream is uncompressed.

func (*PayloadReader) Read

func (pr *PayloadReader) Read(p []byte) (int, error)

type StringMemoizer

type StringMemoizer struct {
	// contains filtered or unexported fields
}

StringMemoizer is a simple encapsulation of a lazily evaluated-only-once string function.

func NewStringMemoizer

func NewStringMemoizer(computeFn func() string) *StringMemoizer

NewStringMemoizer creates a new uninitialized StringMemoizer.

func (*StringMemoizer) Get

func (m *StringMemoizer) Get() string

Get returns the result of the computeFn, calling it only if it has not already been called.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL