mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-06-09 17:30:44 +00:00
Feat: core.ToContext(ctx, v) for ctx initialization (#852)
This commit is contained in:
parent
0dcd1f4992
commit
aa40b8b835
16
context.go
16
context.go
|
@ -27,3 +27,19 @@ func MustFromContext(ctx context.Context) *Instance {
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToContext returns ctx from the given context, or creates an Instance if the context doesn't find that.
|
||||||
|
func ToContext(ctx context.Context, v *Instance) context.Context {
|
||||||
|
if FromContext(ctx) != v {
|
||||||
|
ctx = context.WithValue(ctx, v2rayKey, v)
|
||||||
|
}
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustToContext returns ctx from the given context, or panics if not found that.
|
||||||
|
func MustToContext(ctx context.Context, v *Instance) context.Context {
|
||||||
|
if c := ToContext(ctx, v); c != ctx {
|
||||||
|
panic("V is not in context.")
|
||||||
|
}
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
. "github.com/v2fly/v2ray-core/v4"
|
. "github.com/v2fly/v2ray-core/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestContextPanic(t *testing.T) {
|
func TestFromContextPanic(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
r := recover()
|
r := recover()
|
||||||
if r == nil {
|
if r == nil {
|
||||||
|
@ -17,3 +17,14 @@ func TestContextPanic(t *testing.T) {
|
||||||
|
|
||||||
MustFromContext(context.Background())
|
MustFromContext(context.Background())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToContextPanic(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
r := recover()
|
||||||
|
if r == nil {
|
||||||
|
t.Error("expect panic, but nil")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
MustToContext(context.Background(), &Instance{})
|
||||||
|
}
|
||||||
|
|
10
functions.go
10
functions.go
|
@ -16,7 +16,7 @@ import (
|
||||||
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
if v != nil {
|
if v != nil {
|
||||||
ctx = context.WithValue(v.ctx, v2rayKey, v)
|
ctx = ToContext(v.ctx, v)
|
||||||
}
|
}
|
||||||
return common.CreateObject(ctx, config)
|
return common.CreateObject(ctx, config)
|
||||||
}
|
}
|
||||||
|
@ -47,9 +47,7 @@ func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
|
||||||
//
|
//
|
||||||
// v2ray:api:stable
|
// v2ray:api:stable
|
||||||
func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
|
func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
|
||||||
if FromContext(ctx) == nil {
|
ctx = ToContext(ctx, v)
|
||||||
ctx = context.WithValue(ctx, v2rayKey, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatcher := v.GetFeature(routing.DispatcherType())
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
|
@ -76,9 +74,7 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err
|
||||||
//
|
//
|
||||||
// v2ray:api:beta
|
// v2ray:api:beta
|
||||||
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
||||||
if FromContext(ctx) == nil {
|
ctx = ToContext(ctx, v)
|
||||||
ctx = context.WithValue(ctx, v2rayKey, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatcher := v.GetFeature(routing.DispatcherType())
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user