Skip to content

Commit 9d7a48d

Browse files
dancing-monkeychengyi
andauthored
Invalidate cache when enable endpoint. (#472)
Co-authored-by: chengyi <chengyi02@corp.netease.com>
1 parent 96532cb commit 9d7a48d

3 files changed

Lines changed: 43 additions & 27 deletions

File tree

internal/server/endpoint.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package server
22

33
import (
44
"errors"
5-
"strings"
65
"time"
76

87
"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
@@ -45,13 +44,7 @@ func (app *App) Endpoint(config *app.Config) func(c *gin.Context) {
4544
}
4645

4746
func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTime time.Duration, path string) {
48-
endpointCacheKey := strings.Join(
49-
[]string{
50-
"hook_id",
51-
hookId,
52-
},
53-
":",
54-
)
47+
endpointCacheKey := helper.EndpointCacheKey(hookId)
5548
endpoint, err := cache.AutoGetWithGetter[models.Endpoint](
5649
endpointCacheKey,
5750
func() (*models.Endpoint, error) {
@@ -60,7 +53,8 @@ func (app *App) EndpointHandler(ctx *gin.Context, hookId string, maxExecutionTim
6053
)
6154
return &v, err
6255
})
63-
if err == db.ErrDatabaseNotFound {
56+
57+
if errors.Is(err, db.ErrDatabaseNotFound) {
6458
ctx.JSON(404, exception.BadRequestError(errors.New("endpoint not found")).ToResponse())
6559
return
6660
}

internal/service/endpoint.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
2525
"github.com/langgenius/dify-plugin-daemon/internal/utils/cache/helper"
2626
"github.com/langgenius/dify-plugin-daemon/internal/utils/encryption"
27+
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
2728
"github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
2829
"github.com/langgenius/dify-plugin-daemon/pkg/entities"
2930
"github.com/langgenius/dify-plugin-daemon/pkg/entities/endpoint_entities"
@@ -219,28 +220,45 @@ func Endpoint(
219220
}
220221
}
221222

222-
func EnableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
223-
224-
if err := install_service.EnabledEndpoint(endpoint_id, tenant_id); err != nil {
225-
return exception.InternalServerError(errors.New("failed to enable endpoint")).ToResponse()
223+
func EnableEndpoint(endpointID string, tenantID string) *entities.Response {
224+
endpoint, err := install_service.EnabledEndpoint(endpointID, tenantID)
225+
if err != nil {
226+
return handleEndpointStateError(err, "enable")
226227
}
227228

229+
invalidateEndpointCache(endpoint.HookID)
230+
228231
return entities.NewSuccessResponse(true)
229232
}
230233

231-
func DisableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
232-
endpoint, err := install_service.DisabledEndpoint(endpoint_id, tenant_id)
234+
func DisableEndpoint(endpointID string, tenantID string) *entities.Response {
235+
endpoint, err := install_service.DisabledEndpoint(endpointID, tenantID)
233236
if err != nil {
234-
return exception.InternalServerError(errors.New("failed to disable endpoint")).ToResponse()
237+
return handleEndpointStateError(err, "disable")
235238
}
236239

237-
// invalidate endpoint cache
238-
endpointCacheKey := helper.EndpointCacheKey(endpoint.HookID)
239-
_, _ = cache.AutoDelete[models.Endpoint](endpointCacheKey)
240+
invalidateEndpointCache(endpoint.HookID)
240241

241242
return entities.NewSuccessResponse(true)
242243
}
243244

245+
func handleEndpointStateError(err error, action string) *entities.Response {
246+
if err == nil {
247+
return nil
248+
}
249+
if errors.Is(err, db.ErrDatabaseNotFound) {
250+
return exception.NotFoundError(errors.New("endpoint not found")).ToResponse()
251+
}
252+
return exception.InternalServerError(fmt.Errorf("failed to %s endpoint: %w", action, err)).ToResponse()
253+
}
254+
255+
func invalidateEndpointCache(hookID string) {
256+
endpointCacheKey := helper.EndpointCacheKey(hookID)
257+
if _, err := cache.AutoDelete[models.Endpoint](endpointCacheKey); err != nil {
258+
log.Warn("failed to invalidate endpoint cache for hookID %s: %v", hookID, err)
259+
}
260+
}
261+
244262
func ListEndpoints(tenant_id string, page int, page_size int) *entities.Response {
245263
endpoints, err := db.GetAll[models.Endpoint](
246264
db.Equal("tenant_id", tenant_id),

internal/service/install_service/state.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,21 @@ func UninstallEndpoint(endpoint_id string, tenant_id string) (*models.Endpoint,
166166
return &endpoint, err
167167
}
168168

169-
func EnabledEndpoint(endpoint_id string, tenant_id string) error {
170-
return db.WithTransaction(func(tx *gorm.DB) error {
171-
endpoint, err := db.GetOne[models.Endpoint](
169+
func EnabledEndpoint(endpointID string, tenantID string) (*models.Endpoint, error) {
170+
var endpoint *models.Endpoint
171+
err := db.WithTransaction(func(tx *gorm.DB) error {
172+
e, err := db.GetOne[models.Endpoint](
172173
db.WithTransactionContext(tx),
173-
db.Equal("id", endpoint_id),
174-
db.Equal("tenant_id", tenant_id),
174+
db.Equal("id", endpointID),
175+
db.Equal("tenant_id", tenantID),
175176
db.WLock(),
176177
)
177178
if err != nil {
178179
return err
179180
}
180181

182+
endpoint = &e
183+
181184
if endpoint.Enabled {
182185
return nil
183186
}
@@ -198,16 +201,17 @@ func EnabledEndpoint(endpoint_id string, tenant_id string) error {
198201
}),
199202
)
200203
})
204+
return endpoint, err
201205
}
202206

203-
func DisabledEndpoint(endpoint_id string, tenant_id string) (*models.Endpoint, error) {
207+
func DisabledEndpoint(endpointID string, tenantID string) (*models.Endpoint, error) {
204208
var endpoint models.Endpoint
205209
err := db.WithTransaction(func(tx *gorm.DB) error {
206210
var err error
207211
endpoint, err = db.GetOne[models.Endpoint](
208212
db.WithTransactionContext(tx),
209-
db.Equal("id", endpoint_id),
210-
db.Equal("tenant_id", tenant_id),
213+
db.Equal("id", endpointID),
214+
db.Equal("tenant_id", tenantID),
211215
db.WLock(),
212216
)
213217
if err != nil {

0 commit comments

Comments
 (0)