Skip to content

Commit 7c14eff

Browse files
authored
[#107]: fix(collects): prevent collects from calling twice
[#107]: fix(collects): prevent collects from calling twice
2 parents 8890672 + 651c32c commit 7c14eff

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

pkg/container/init.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,34 @@ func (e *Endure) callInitFn(init reflect.Method, vrtx *vertex.Vertex) error {
9797
return errors.E(op, errors.ArgType, errors.Str("0 or less parameters for Init"))
9898
}
9999

100-
if len(vrtx.Meta.CollectorEntries) > 0 {
101-
for i := 0; i < len(vrtx.Meta.CollectorEntries); i++ {
102-
// try to find nil IN args and get it from global
103-
for j := 0; j < len(vrtx.Meta.CollectorEntries[i].In); j++ {
104-
if vrtx.Meta.CollectorEntries[i].In[j].In.IsZero() {
105-
global, ok := e.graph.Providers[vrtx.Meta.CollectorEntries[i].In[j].Dep]
106-
if !ok {
107-
e.logger.Error("can't find in arg to Call Collects on the vertex", zap.String("id", vrtx.ID))
108-
return errors.E(op, errors.Errorf("id: %s", vrtx.ID))
100+
// do not call collectors twice
101+
if _, ok := e.initialized[vrtx.ID]; !ok {
102+
if len(vrtx.Meta.CollectorEntries) > 0 {
103+
for i := 0; i < len(vrtx.Meta.CollectorEntries); i++ {
104+
// try to find nil IN args and get it from global
105+
for j := 0; j < len(vrtx.Meta.CollectorEntries[i].In); j++ {
106+
if vrtx.Meta.CollectorEntries[i].In[j].In.IsZero() {
107+
global, ok := e.graph.Providers[vrtx.Meta.CollectorEntries[i].In[j].Dep]
108+
if !ok {
109+
e.logger.Error("can't find in arg to Call Collects on the vertex", zap.String("id", vrtx.ID))
110+
return errors.E(op, errors.Errorf("id: %s", vrtx.ID))
111+
}
112+
vrtx.Meta.CollectorEntries[i].In[j].In = global
109113
}
110-
vrtx.Meta.CollectorEntries[i].In[j].In = global
111114
}
112-
}
113115

114-
in := make([]reflect.Value, 0, len(vrtx.Meta.CollectorEntries[i].In))
115-
for _, v := range vrtx.Meta.CollectorEntries[i].In {
116-
in = append(in, v.In)
117-
}
118-
err = e.fnCallCollectors(vrtx, in, vrtx.Meta.CollectorEntries[i].Fn)
119-
if err != nil {
120-
return errors.E(op, errors.Traverse, err)
116+
in := make([]reflect.Value, 0, len(vrtx.Meta.CollectorEntries[i].In))
117+
for _, v := range vrtx.Meta.CollectorEntries[i].In {
118+
in = append(in, v.In)
119+
}
120+
err = e.fnCallCollectors(vrtx, in, vrtx.Meta.CollectorEntries[i].Fn)
121+
if err != nil {
122+
return errors.E(op, errors.Traverse, err)
123+
}
121124
}
122125
}
123126
}
127+
124128
return nil
125129
}
126130

0 commit comments

Comments
 (0)