Skip to content

Commit df095e3

Browse files
committed
fix: barry quick fix, 2025-06-08 11:14:40
1 parent ff5e93f commit df095e3

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

dixinternal/resolver.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ func SetLog(setter func(logger log.Logger) log.Logger) {
2525
type ResolverImpl struct {
2626
providers map[reflect.Type][]Provider
2727
objects map[reflect.Type]map[string][]reflect.Value
28+
resolving map[reflect.Type]bool // 用于防止递归解析同一类型
2829
}
2930

3031
// NewResolver 创建新的解析器
3132
func NewResolver() *ResolverImpl {
3233
return &ResolverImpl{
3334
providers: make(map[reflect.Type][]Provider),
3435
objects: make(map[reflect.Type]map[string][]reflect.Value),
36+
resolving: make(map[reflect.Type]bool),
3537
}
3638
}
3739

@@ -198,11 +200,27 @@ func (r *ResolverImpl) resolveAsList(dep Dependency, opts Options) (reflect.Valu
198200

199201
// getTypeValues 获取类型的所有值
200202
func (r *ResolverImpl) getTypeValues(typ reflect.Type, opts Options) (map[string][]reflect.Value, error) {
203+
// 防止递归解析同一类型
204+
if r.resolving[typ] {
205+
return map[string][]reflect.Value{}, nil // 返回空结果,避免死循环
206+
}
207+
201208
// 检查是否已有缓存的对象
202209
if r.objects[typ] == nil {
203210
r.objects[typ] = make(map[string][]reflect.Value)
204211
}
205212

213+
// 如果已有缓存,直接返回
214+
if len(r.objects[typ][defaultKey]) > 0 {
215+
return r.objects[typ], nil
216+
}
217+
218+
// 标记正在解析
219+
r.resolving[typ] = true
220+
defer func() {
221+
delete(r.resolving, typ)
222+
}()
223+
206224
// 首先尝试调用直接的提供者
207225
if len(r.providers[typ]) > 0 {
208226
err := r.invokeDirectProviders(typ, opts)
@@ -235,13 +253,9 @@ func (r *ResolverImpl) invokeDirectProviders(typ reflect.Type, opts Options) err
235253
for _, provider := range r.providers[typ] {
236254
// 检查 provider 是否能提供该类型
237255
if provider.CanProvide(typ) {
238-
// 对于多类型 provider,即使已初始化,也可能需要为新类型提供实例
239-
if provider.IsInitialized() {
240-
// 检查是否已经有该类型的缓存
241-
if r.objects[typ] != nil && len(r.objects[typ][defaultKey]) > 0 {
242-
continue // 已经有缓存,跳过
243-
}
244-
// 没有缓存,需要重新提供
256+
// 对于多类型 provider,检查是否已经有该类型的缓存
257+
if r.objects[typ] != nil && len(r.objects[typ][defaultKey]) > 0 {
258+
continue // 已经有缓存,跳过
245259
}
246260

247261
err := r.invokeProviderForType(provider, typ, opts)

0 commit comments

Comments
 (0)