Skip to content

Commit 6a22859

Browse files
committed
improvement: retain a single instance when registering duplicate endpoints
1 parent d851be6 commit 6a22859

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

plugin/managed/server/instance.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func (h APIHandler) registerInstance(w http.ResponseWriter, req *http.Request, p
8585
err := h.DecodeJSON(req, obj)
8686
if err != nil {
8787
h.WriteError(w, err.Error(), http.StatusInternalServerError)
88+
return
89+
}
90+
if obj.Endpoint == "" {
91+
h.WriteError(w, "empty endpoint", http.StatusInternalServerError)
92+
return
8893
}
8994

9095
oldInst := &model.Instance{}
@@ -95,8 +100,28 @@ func (h APIHandler) registerInstance(w http.ResponseWriter, req *http.Request, p
95100
h.WriteError(w, errMsg, http.StatusInternalServerError)
96101
return
97102
}
98-
99-
err = orm.Create(nil, obj)
103+
err, result := orm.GetBy("endpoint", obj.Endpoint, oldInst)
104+
if err != nil {
105+
log.Error(err)
106+
h.WriteError(w, err.Error(), http.StatusInternalServerError)
107+
return
108+
}
109+
if len(result.Result) > 0 {
110+
buf := util.MustToJSONBytes(result.Result[0])
111+
util.MustFromJSONBytes(buf, &oldInst)
112+
if oldInst.ID != "" {
113+
//keep old created time
114+
obj.Created = oldInst.Created
115+
log.Infof("remove old instance [%s] with the same endpoint %s", oldInst.ID, oldInst.Endpoint)
116+
err = orm.Delete(nil, oldInst)
117+
if err != nil {
118+
log.Error(err)
119+
h.WriteError(w, err.Error(), http.StatusInternalServerError)
120+
return
121+
}
122+
}
123+
}
124+
err = orm.Save(nil, obj)
100125
if err != nil {
101126
h.WriteError(w, err.Error(), http.StatusInternalServerError)
102127
return

0 commit comments

Comments
 (0)