From 308a3e6b343f2652526e03530c5c0a42b8b580c2 Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Wed, 18 Dec 2024 14:43:49 +0800 Subject: [PATCH 1/5] fix(zentao): make sure connection uncacheable --- backend/core/plugin/plugin_datasource.go | 5 +++++ .../helpers/pluginhelper/api/ds_remote_api_proxy_api.go | 8 ++++++-- backend/plugins/zentao/models/connection.go | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/core/plugin/plugin_datasource.go b/backend/core/plugin/plugin_datasource.go index d40bfc98008..4d438dabb53 100644 --- a/backend/core/plugin/plugin_datasource.go +++ b/backend/core/plugin/plugin_datasource.go @@ -41,6 +41,11 @@ type CacheableConnection interface { GetHash() string } +type UnCacheableConnection interface { + ApiConnection + UncCacheable() bool +} + // ApiAuthenticator is to be implemented by a Concreate Connection if Authorization is required type ApiAuthenticator interface { // SetupAuthentication is a hook function for connection to set up authentication for the HTTP request diff --git a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go index 9ea8b5d2a43..6c757ba9fd2 100644 --- a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go +++ b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go @@ -67,11 +67,15 @@ func (rap *DsRemoteApiProxyHelper[C]) prepare(input *plugin.ApiResourceInput) (* func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, errors.Error) { c := interface{}(connection) key := "" + var cacheable bool = false + if unCacheableConnection, ok := c.(plugin.UnCacheableConnection); ok { + cacheable = !unCacheableConnection.UncCacheable() + } if cacheableConn, ok := c.(plugin.CacheableConnection); ok { key = cacheableConn.GetHash() } // try to reuse api client - if key != "" { + if key != "" && cacheable { rap.httpClientCacheMutex.RLock() client, ok := rap.httpClientCache[key] rap.httpClientCacheMutex.RUnlock() @@ -86,7 +90,7 @@ func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, e return nil, err } // cache the client if key is not empty - if key != "" { + if key != "" && cacheable { rap.httpClientCacheMutex.Lock() rap.httpClientCache[key] = client rap.httpClientCacheMutex.Unlock() diff --git a/backend/plugins/zentao/models/connection.go b/backend/plugins/zentao/models/connection.go index 0554450e7ae..385d4e5a511 100644 --- a/backend/plugins/zentao/models/connection.go +++ b/backend/plugins/zentao/models/connection.go @@ -73,6 +73,11 @@ type ZentaoConn struct { DbMaxConns int `json:"dbMaxConns" mapstructure:"dbMaxConns"` } +func (connection ZentaoConn) UncCacheable() bool { + // zentao's token will expire after about 24min, so api client cannot be cached. + return true +} + func (connection ZentaoConn) Sanitize() ZentaoConn { connection.Password = "" if connection.DbUrl != "" { @@ -106,6 +111,10 @@ func (connection ZentaoConn) SanitizeDbUrl() string { return dbUrl } +func (connection ZentaoConnection) UncCacheable() bool { + return connection.ZentaoConn.UncCacheable() +} + func (connection ZentaoConnection) Sanitize() ZentaoConnection { connection.ZentaoConn = connection.ZentaoConn.Sanitize() return connection From e216a23ac1fdb0cb2297fc1bfb8e88abb0da9a90 Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Wed, 18 Dec 2024 14:48:05 +0800 Subject: [PATCH 2/5] fix(zentao): fix lint errors --- backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go index 6c757ba9fd2..df8bda33274 100644 --- a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go +++ b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go @@ -40,7 +40,7 @@ type DsRemoteApiProxyHelper[C plugin.ToolLayerApiConnection] struct { // NewDsRemoteApiProxyHelper creates a new DsRemoteApiProxyHelper func NewDsRemoteApiProxyHelper[ - C plugin.ToolLayerApiConnection, +C plugin.ToolLayerApiConnection, ]( modelApiHelper *ModelApiHelper[C], ) *DsRemoteApiProxyHelper[C] { @@ -67,7 +67,7 @@ func (rap *DsRemoteApiProxyHelper[C]) prepare(input *plugin.ApiResourceInput) (* func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, errors.Error) { c := interface{}(connection) key := "" - var cacheable bool = false + cacheable := true if unCacheableConnection, ok := c.(plugin.UnCacheableConnection); ok { cacheable = !unCacheableConnection.UncCacheable() } From 80a83ec496b0eb76605110a76013a6acd7039a51 Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Wed, 18 Dec 2024 14:58:53 +0800 Subject: [PATCH 3/5] Revert "fix(zentao): fix lint errors" This reverts commit e216a23ac1fdb0cb2297fc1bfb8e88abb0da9a90. --- backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go index df8bda33274..6c757ba9fd2 100644 --- a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go +++ b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go @@ -40,7 +40,7 @@ type DsRemoteApiProxyHelper[C plugin.ToolLayerApiConnection] struct { // NewDsRemoteApiProxyHelper creates a new DsRemoteApiProxyHelper func NewDsRemoteApiProxyHelper[ -C plugin.ToolLayerApiConnection, + C plugin.ToolLayerApiConnection, ]( modelApiHelper *ModelApiHelper[C], ) *DsRemoteApiProxyHelper[C] { @@ -67,7 +67,7 @@ func (rap *DsRemoteApiProxyHelper[C]) prepare(input *plugin.ApiResourceInput) (* func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, errors.Error) { c := interface{}(connection) key := "" - cacheable := true + var cacheable bool = false if unCacheableConnection, ok := c.(plugin.UnCacheableConnection); ok { cacheable = !unCacheableConnection.UncCacheable() } From ac5a611f1f6877249ecd0c6c064b0ca3f9aa195b Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Wed, 18 Dec 2024 14:59:39 +0800 Subject: [PATCH 4/5] fix(zentao): fix lint errors --- .../helpers/pluginhelper/api/ds_remote_api_proxy_api.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go index 6c757ba9fd2..cdbb29c90fd 100644 --- a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go +++ b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go @@ -39,11 +39,7 @@ type DsRemoteApiProxyHelper[C plugin.ToolLayerApiConnection] struct { } // NewDsRemoteApiProxyHelper creates a new DsRemoteApiProxyHelper -func NewDsRemoteApiProxyHelper[ - C plugin.ToolLayerApiConnection, -]( - modelApiHelper *ModelApiHelper[C], -) *DsRemoteApiProxyHelper[C] { +func NewDsRemoteApiProxyHelper[C plugin.ToolLayerApiConnection](modelApiHelper *ModelApiHelper[C]) *DsRemoteApiProxyHelper[C] { return &DsRemoteApiProxyHelper[C]{ ModelApiHelper: modelApiHelper, logger: modelApiHelper.basicRes.GetLogger().Nested("remote_api_helper"), @@ -67,7 +63,7 @@ func (rap *DsRemoteApiProxyHelper[C]) prepare(input *plugin.ApiResourceInput) (* func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, errors.Error) { c := interface{}(connection) key := "" - var cacheable bool = false + var cacheable = false if unCacheableConnection, ok := c.(plugin.UnCacheableConnection); ok { cacheable = !unCacheableConnection.UncCacheable() } From 0073b3a9b28cf97b6857f5d952adb1931c3222ae Mon Sep 17 00:00:00 2001 From: d4x1 <1507509064@qq.com> Date: Wed, 18 Dec 2024 15:42:16 +0800 Subject: [PATCH 5/5] fix(zentao): remove UnCacheableConnetion interface, try to fix it with another way --- backend/core/plugin/plugin_datasource.go | 5 ----- .../helpers/pluginhelper/api/ds_remote_api_proxy_api.go | 8 ++------ backend/plugins/zentao/models/connection.go | 8 ++++---- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/backend/core/plugin/plugin_datasource.go b/backend/core/plugin/plugin_datasource.go index 4d438dabb53..d40bfc98008 100644 --- a/backend/core/plugin/plugin_datasource.go +++ b/backend/core/plugin/plugin_datasource.go @@ -41,11 +41,6 @@ type CacheableConnection interface { GetHash() string } -type UnCacheableConnection interface { - ApiConnection - UncCacheable() bool -} - // ApiAuthenticator is to be implemented by a Concreate Connection if Authorization is required type ApiAuthenticator interface { // SetupAuthentication is a hook function for connection to set up authentication for the HTTP request diff --git a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go index cdbb29c90fd..b0c4b0439c2 100644 --- a/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go +++ b/backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go @@ -63,15 +63,11 @@ func (rap *DsRemoteApiProxyHelper[C]) prepare(input *plugin.ApiResourceInput) (* func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, errors.Error) { c := interface{}(connection) key := "" - var cacheable = false - if unCacheableConnection, ok := c.(plugin.UnCacheableConnection); ok { - cacheable = !unCacheableConnection.UncCacheable() - } if cacheableConn, ok := c.(plugin.CacheableConnection); ok { key = cacheableConn.GetHash() } // try to reuse api client - if key != "" && cacheable { + if key != "" { rap.httpClientCacheMutex.RLock() client, ok := rap.httpClientCache[key] rap.httpClientCacheMutex.RUnlock() @@ -86,7 +82,7 @@ func (rap *DsRemoteApiProxyHelper[C]) getApiClient(connection *C) (*ApiClient, e return nil, err } // cache the client if key is not empty - if key != "" && cacheable { + if key != "" { rap.httpClientCacheMutex.Lock() rap.httpClientCache[key] = client rap.httpClientCacheMutex.Unlock() diff --git a/backend/plugins/zentao/models/connection.go b/backend/plugins/zentao/models/connection.go index 385d4e5a511..949dd36d2b4 100644 --- a/backend/plugins/zentao/models/connection.go +++ b/backend/plugins/zentao/models/connection.go @@ -73,9 +73,9 @@ type ZentaoConn struct { DbMaxConns int `json:"dbMaxConns" mapstructure:"dbMaxConns"` } -func (connection ZentaoConn) UncCacheable() bool { +func (connection ZentaoConn) GetHash() string { // zentao's token will expire after about 24min, so api client cannot be cached. - return true + return "" } func (connection ZentaoConn) Sanitize() ZentaoConn { @@ -111,8 +111,8 @@ func (connection ZentaoConn) SanitizeDbUrl() string { return dbUrl } -func (connection ZentaoConnection) UncCacheable() bool { - return connection.ZentaoConn.UncCacheable() +func (connection ZentaoConnection) GetHash() string { + return connection.ZentaoConn.GetHash() } func (connection ZentaoConnection) Sanitize() ZentaoConnection {