utils

package
v0.0.0-...-c345e67 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyIPMask

func ApplyIPMask(ipStr string) (maskedIP string, ok bool)

ApplyIPMask will apply a /24 bit mask to IPv4 addresses and a /64 bit mask to IPv6 Will return the input string along with ok == false if there is any error while masking

func ClientIPAddr

func ClientIPAddr(c *gin.Context) netip.Addr

Equivalent to c.ClientIP() except that it returns netip.Addr instead of net.IP

If the IP string is invalid, it returns netip.Addr{}. Use netip.Addr.ISValid() to check the validity of the returned value

func CopyHeader

func CopyHeader(dst, src http.Header)

Copy headers from proxied src to dst, removing those defined by HTTP as "hop-by-hop" and not to be forwarded (see https://www.rfc-editor.org/rfc/rfc9110#field.connection)

func ExtractAndMaskIP

func ExtractAndMaskIP(ipStr string) (maskedIP string, ok bool)

ExtractAndMaskIP will extract an IP address from a leading "[" and trailing "]". Then the function will apply the ApplyIPMask function

func ExtractProjectFromUserAgent

func ExtractProjectFromUserAgent(userAgents []string) string

Helper function to extract project from User-Agent Will return an empty string if no project is found

func ExtractVersionAndServiceFromUserAgent

func ExtractVersionAndServiceFromUserAgent(userAgent string) (reqVer, service string)

ExtractVersionAndServiceFromUserAgent will extract the Pelican version and service from the user agent. It will return empty strings if the provided userAgent failes to match against the parser

func GetJwks

func GetJwks(ctx context.Context, tr *http.Transport, location string) (jwk.Set, error)

func GetPreferredCaches

func GetPreferredCaches(preferredCaches string) (caches []*url.URL, err error)

GetPreferredCaches parses the caches it is given and returns it as a list of url's

func HasContentType

func HasContentType(r *http.Response, mimetype string) bool

Determine whether the response `content-type` includes a server-acceptable mime-type

Failure should yield an HTTP 415 (`http.StatusUnsupportedMediaType`)

Source: https://gist.github.com/rjz/fe283b02cbaa50c5991e1ba921adf7c9

func HeaderParser

func HeaderParser(values string) (retMap map[string]string)

Simple parser to that takes a "values" string from a header and turns it into a map of key/value pairs

func LaunchPeriodicWriteCABundle

func LaunchPeriodicWriteCABundle(ctx context.Context, egrpKey string, filename string, sleepTime time.Duration) (count int, err error)

Periodically write out the system CAs, updating them if the system updates. Returns an error if the first attempt at writing fails. Otherwise, it will launch a goroutine and update the entire CA bundle every specified duration.

If we're on a platform (Mac, Windows) that does not provide a CA bundle, we return a count of 0 and do not launch the go routine.

func MakeRequest

func MakeRequest(ctx context.Context, tr *http.Transport, url string, method string, data map[string]interface{}, headers map[string]string) ([]byte, error)

MakeRequest makes an http request with our custom http client. It acts similarly to the http.NewRequest but it only takes json as the request data.

func MapToSlice

func MapToSlice[K comparable, V any](m map[K]V) []V

Convert map to slice of values

func SnakeCaseToCamelCase

func SnakeCaseToCamelCase(input string) string

snakeCaseToCamelCase converts a snake case string to camel case.

func SnakeCaseToHumanReadable

func SnakeCaseToHumanReadable(input string) string

snakeCaseToSnakeCase converts a snake_case string to Snake Case (CamelCase with spaces).

func WriteCABundle

func WriteCABundle(filename string) (int, error)

Write out all the trusted CAs as a CA bundle on disk. This is useful for components that do not use go's trusted CA store

Types

type Group

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

A Group is a collection of goroutines working on subtasks that are part of the same overall task.

A zero Group is valid, has no limit on the number of active goroutines, and does not cancel on error.

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext returns a new Group and an associated Context derived from ctx.

The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.

func (*Group) Go

func (g *Group) Go(f func() error)

Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.

The first call to return a non-nil error cancels the group's context, if the group was created by calling WithContext. The error will be returned by Wait.

func (*Group) SetLimit

func (g *Group) SetLimit(n int)

SetLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit.

Any subsequent call to the Go method will block until it can add an active goroutine without exceeding the configured limit.

The limit must not be modified while any goroutines in the group are active.

func (*Group) TryGo

func (g *Group) TryGo(f func() error) bool

TryGo calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit.

It will block until the provided context is cancelled.

The return value reports whether the goroutine was started.

func (*Group) TryGoUntil

func (g *Group) TryGoUntil(ctx context.Context, f func() error) bool

TryGoUntil calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit.

If the group is at the configured limit, the call will block until the provided context is cancelled.

The return value reports whether the goroutine was started.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

Jump to

Keyboard shortcuts

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