Skip to content

Commit

Permalink
fix(zentao): make sure connection uncacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
d4x1 committed Dec 18, 2024
1 parent 47b4014 commit 308a3e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions backend/core/plugin/plugin_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions backend/helpers/pluginhelper/api/ds_remote_api_proxy_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions backend/plugins/zentao/models/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 308a3e6

Please sign in to comment.