fix: add plugin id conflict checker (#78)
This commit is contained in:
@@ -27,6 +27,7 @@ import (
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
|
||||
"github.com/coze-dev/coze-studio/backend/api/model/plugin_develop_common"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/query"
|
||||
@@ -88,7 +89,7 @@ func (p *PluginDraftDAO) getSelected(opt *PluginSelectedOption) (selected []fiel
|
||||
}
|
||||
|
||||
func (p *PluginDraftDAO) Create(ctx context.Context, plugin *entity.PluginInfo) (pluginID int64, err error) {
|
||||
id, err := p.idGen.GenID(ctx)
|
||||
id, err := p.genPluginID(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -117,6 +118,25 @@ func (p *PluginDraftDAO) Create(ctx context.Context, plugin *entity.PluginInfo)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (p *PluginDraftDAO) genPluginID(ctx context.Context) (id int64, err error) {
|
||||
|
||||
retryTimes := 5
|
||||
for i := 0; i < retryTimes; i++ {
|
||||
id, err = p.idGen.GenID(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, ok := conf.GetPluginProduct(id); !ok {
|
||||
break
|
||||
}
|
||||
if i == retryTimes-1 {
|
||||
return 0, fmt.Errorf("id %d is confilict with product plugin id.", id)
|
||||
}
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (p *PluginDraftDAO) Get(ctx context.Context, pluginID int64, opt *PluginSelectedOption) (plugin *entity.PluginInfo, exist bool, err error) {
|
||||
table := p.query.PluginDraft
|
||||
pl, err := table.WithContext(ctx).
|
||||
@@ -262,7 +282,7 @@ func (p *PluginDraftDAO) Update(ctx context.Context, plugin *entity.PluginInfo)
|
||||
}
|
||||
|
||||
func (p *PluginDraftDAO) CreateWithTX(ctx context.Context, tx *query.QueryTx, plugin *entity.PluginInfo) (pluginID int64, err error) {
|
||||
id, err := p.idGen.GenID(ctx)
|
||||
id, err := p.genPluginID(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
|
||||
common "github.com/coze-dev/coze-studio/backend/api/model/plugin_develop_common"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/query"
|
||||
@@ -90,7 +91,8 @@ func (t *ToolDraftDAO) getSelected(opt *ToolSelectedOption) (selected []field.Ex
|
||||
}
|
||||
|
||||
func (t *ToolDraftDAO) Create(ctx context.Context, tool *entity.ToolInfo) (toolID int64, err error) {
|
||||
id, err := t.idGen.GenID(ctx)
|
||||
|
||||
id, err := t.genToolID(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -111,6 +113,27 @@ func (t *ToolDraftDAO) Create(ctx context.Context, tool *entity.ToolInfo) (toolI
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (t *ToolDraftDAO) genToolID(ctx context.Context) (id int64, err error) {
|
||||
|
||||
retryTimes := 5
|
||||
|
||||
for i := 0; i < retryTimes; i++ {
|
||||
id, err = t.idGen.GenID(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if _, ok := conf.GetToolProduct(id); !ok {
|
||||
break
|
||||
}
|
||||
if i == retryTimes-1 {
|
||||
return 0, fmt.Errorf("id %d is confilict with product tool id.", id)
|
||||
}
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (t *ToolDraftDAO) Get(ctx context.Context, toolID int64) (tool *entity.ToolInfo, exist bool, err error) {
|
||||
table := t.query.ToolDraft
|
||||
tl, err := table.WithContext(ctx).
|
||||
@@ -335,7 +358,7 @@ func (t *ToolDraftDAO) BatchCreateWithTX(ctx context.Context, tx *query.QueryTx,
|
||||
tls := make([]*model.ToolDraft, 0, len(tools))
|
||||
|
||||
for _, tool := range tools {
|
||||
id, err := t.idGen.GenID(ctx)
|
||||
id, err := t.genToolID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user