1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

refactor environment context

This commit is contained in:
Shelikhoo 2021-09-05 15:36:57 +01:00
parent 6a4364f37a
commit 141cd30ee3
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
7 changed files with 42 additions and 23 deletions

View File

@ -1,7 +1,7 @@
package environment
import (
"github.com/v2fly/v2ray-core/v4/features/extension"
"github.com/v2fly/v2ray-core/v4/features/extension/storage"
)
type AppEnvironmentCapabilitySet interface {
@ -10,8 +10,8 @@ type AppEnvironmentCapabilitySet interface {
InstanceNetworkCapabilitySet
FileSystemCapabilitySet
PersistentStorage() extension.ScopedPersistentStorage
TransientStorage() extension.ScopedTransientStorage
PersistentStorage() storage.ScopedPersistentStorage
TransientStorage() storage.ScopedTransientStorage
}
type AppEnvironment interface {

View File

@ -1,8 +1,8 @@
package environment
import (
"github.com/v2fly/v2ray-core/v4/common/environment/filesystemcap"
"github.com/v2fly/v2ray-core/v4/common/log"
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem/fsifce"
"github.com/v2fly/v2ray-core/v4/transport/internet"
"github.com/v2fly/v2ray-core/v4/transport/internet/tagged"
)
@ -35,7 +35,5 @@ type LogCapabilitySet interface {
}
type FileSystemCapabilitySet interface {
OpenFileForReadSeek() fsifce.FileSeekerFunc
OpenFileForRead() fsifce.FileReaderFunc
OpenFileForWrite() fsifce.FileWriterFunc
filesystemcap.FileSystemCapabilitySet
}

View File

@ -0,0 +1,20 @@
package envctx
import "context"
type environmentContextKey int
const (
environmentKey environmentContextKey = iota
)
func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context {
return context.WithValue(ctx, environmentKey, environment)
}
func EnvironmentFromContext(ctx context.Context) interface{} {
if environment, ok := ctx.Value(environmentKey).(interface{}); ok {
return environment
}
return nil
}

View File

@ -0,0 +1,9 @@
package filesystemcap
import "github.com/v2fly/v2ray-core/v4/common/platform/filesystem/fsifce"
type FileSystemCapabilitySet interface {
OpenFileForReadSeek() fsifce.FileSeekerFunc
OpenFileForRead() fsifce.FileReaderFunc
OpenFileForWrite() fsifce.FileWriterFunc
}

View File

@ -1,12 +1,14 @@
package environment
import "github.com/v2fly/v2ray-core/v4/features/extension"
import (
"github.com/v2fly/v2ray-core/v4/features/extension/storage"
)
type ProxyEnvironmentCapabilitySet interface {
BaseEnvironmentCapabilitySet
InstanceNetworkCapabilitySet
TransientStorage() extension.ScopedTransientStorage
TransientStorage() storage.ScopedTransientStorage
}
type ProxyEnvironment interface {

View File

@ -1,13 +1,15 @@
package environment
import "github.com/v2fly/v2ray-core/v4/features/extension"
import (
"github.com/v2fly/v2ray-core/v4/features/extension/storage"
)
type TransportEnvironmentCapacitySet interface {
BaseEnvironmentCapabilitySet
SystemNetworkCapabilitySet
InstanceNetworkCapabilitySet
TransientStorage() extension.ScopedTransientStorage
TransientStorage() storage.ScopedTransientStorage
}
type TransportEnvironment interface {

View File

@ -15,7 +15,6 @@ const (
sockoptSessionKey
trackedConnectionErrorKey
handlerSessionKey
environmentKey
)
// ContextWithID returns a new context with the given ID.
@ -134,14 +133,3 @@ func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
}
func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context {
return context.WithValue(ctx, environmentKey, environment)
}
func EnvironmentFromContext(ctx context.Context) interface{} {
if environment, ok := ctx.Value(environmentKey).(interface{}); ok {
return environment
}
return nil
}