Xray-core/common/session/context.go
yuhan6665 fa5d7a255b
Least load balancer (#2999)
* v5: Health Check & LeastLoad Strategy (rebased from 2c5a71490368500a982018a74a6d519c7e121816)

Some changes will be necessary to integrate it into V2Ray

* Update proto

* parse duration conf with time.Parse()

* moving health ping to observatory as a standalone component

* moving health ping to observatory as a standalone component: auto generated file

* add initialization for health ping

* incorporate changes in router implementation

* support principle target output

* add v4 json support for BurstObservatory & fix balancer reference

* update API command

* remove cancelled API

* return zero length value when observer is not found

* remove duplicated targeted dispatch

* adjust test with updated structure

* bug fix for observer

* fix strategy selector

* fix strategy least load

* Fix ticker usage

ticker.Close does not close ticker.C

* feat: Replace default Health Ping URL to HTTPS (#1991)

* fix selectLeastLoad() returns wrong number of nodes (#2083)

* Test: fix leastload strategy unit test

* fix(router): panic caused by concurrent map read and write (#2678)

* Clean up code

---------

Co-authored-by: Jebbs <qjebbs@gmail.com>
Co-authored-by: Shelikhoo <xiaokangwang@outlook.com>
Co-authored-by: 世界 <i@sekai.icu>
Co-authored-by: Bernd Eichelberger <46166740+4-FLOSS-Free-Libre-Open-Source-Software@users.noreply.github.com>
Co-authored-by: 秋のかえで <autmaple@protonmail.com>
Co-authored-by: Rinka <kujourinka@gmail.com>
2024-02-17 22:51:37 -05:00

164 lines
4.5 KiB
Go

package session
import (
"context"
_ "unsafe"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/features/routing"
)
//go:linkname IndependentCancelCtx context.newCancelCtx
func IndependentCancelCtx(parent context.Context) context.Context
type sessionKey int
const (
idSessionKey sessionKey = iota
inboundSessionKey
outboundSessionKey
contentSessionKey
muxPreferedSessionKey
sockoptSessionKey
trackedConnectionErrorKey
dispatcherKey
timeoutOnlyKey
allowedNetworkKey
handlerSessionKey
)
// ContextWithID returns a new context with the given ID.
func ContextWithID(ctx context.Context, id ID) context.Context {
return context.WithValue(ctx, idSessionKey, id)
}
// IDFromContext returns ID in this context, or 0 if not contained.
func IDFromContext(ctx context.Context) ID {
if id, ok := ctx.Value(idSessionKey).(ID); ok {
return id
}
return 0
}
func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
return context.WithValue(ctx, inboundSessionKey, inbound)
}
func InboundFromContext(ctx context.Context) *Inbound {
if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
return inbound
}
return nil
}
func ContextWithOutbound(ctx context.Context, outbound *Outbound) context.Context {
return context.WithValue(ctx, outboundSessionKey, outbound)
}
func OutboundFromContext(ctx context.Context) *Outbound {
if outbound, ok := ctx.Value(outboundSessionKey).(*Outbound); ok {
return outbound
}
return nil
}
func ContextWithContent(ctx context.Context, content *Content) context.Context {
return context.WithValue(ctx, contentSessionKey, content)
}
func ContentFromContext(ctx context.Context) *Content {
if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
return content
}
return nil
}
// ContextWithMuxPrefered returns a new context with the given bool
func ContextWithMuxPrefered(ctx context.Context, forced bool) context.Context {
return context.WithValue(ctx, muxPreferedSessionKey, forced)
}
// MuxPreferedFromContext returns value in this context, or false if not contained.
func MuxPreferedFromContext(ctx context.Context) bool {
if val, ok := ctx.Value(muxPreferedSessionKey).(bool); ok {
return val
}
return false
}
// ContextWithSockopt returns a new context with Socket configs included
func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
return context.WithValue(ctx, sockoptSessionKey, s)
}
// SockoptFromContext returns Socket configs in this context, or nil if not contained.
func SockoptFromContext(ctx context.Context) *Sockopt {
if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
return sockopt
}
return nil
}
func GetForcedOutboundTagFromContext(ctx context.Context) string {
if ContentFromContext(ctx) == nil {
return ""
}
return ContentFromContext(ctx).Attribute("forcedOutboundTag")
}
func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
ctx = ContextWithContent(ctx, &Content{})
}
ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
return ctx
}
type TrackedRequestErrorFeedback interface {
SubmitError(err error)
}
func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
errorTracker := errorTracker.(TrackedRequestErrorFeedback)
errorTracker.SubmitError(err)
}
}
func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
}
func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
return context.WithValue(ctx, dispatcherKey, dispatcher)
}
func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
return dispatcher
}
return nil
}
func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
return context.WithValue(ctx, timeoutOnlyKey, only)
}
func TimeoutOnlyFromContext(ctx context.Context) bool {
if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
return val
}
return false
}
func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
return context.WithValue(ctx, allowedNetworkKey, network)
}
func AllowedNetworkFromContext(ctx context.Context) net.Network {
if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
return val
}
return net.Network_Unknown
}