-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_current.go
55 lines (48 loc) · 1.72 KB
/
context_current.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package runtime
import (
"fmt"
"git.golaxy.org/core/internal/concurrent"
"git.golaxy.org/core/internal/exception"
"git.golaxy.org/core/service"
"git.golaxy.org/core/util/iface"
)
type (
CurrentContextProvider = concurrent.CurrentContextProvider // 当前上下文提供者
ConcurrentContextProvider = concurrent.ConcurrentContextProvider // 多线程安全的上下文提供者
)
// Current 获取当前运行时上下文
func Current(provider CurrentContextProvider) Context {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrContext, exception.ErrArgs))
}
return iface.Cache2Iface[Context](provider.GetCurrentContext())
}
// Concurrent 获取多线程安全的运行时上下文
func Concurrent(provider ConcurrentContextProvider) ConcurrentContext {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrContext, exception.ErrArgs))
}
return iface.Cache2Iface[Context](provider.GetConcurrentContext())
}
func getCaller(provider ConcurrentContextProvider) concurrent.Caller {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrContext, exception.ErrArgs))
}
return iface.Cache2Iface[Context](provider.GetConcurrentContext())
}
func getRuntimeContext(provider CurrentContextProvider) Context {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrContext, exception.ErrArgs))
}
return iface.Cache2Iface[Context](provider.GetCurrentContext())
}
func getServiceContext(provider ConcurrentContextProvider) service.Context {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrContext, exception.ErrArgs))
}
ctx := iface.Cache2Iface[Context](provider.GetConcurrentContext())
if ctx == nil {
return nil
}
return ctx.getServiceCtx()
}