kis-flow/conn/kis_connector.go

102 lines
2.3 KiB
Go
Raw Normal View History

2024-01-09 17:30:58 +08:00
package conn
import (
"context"
2024-04-16 15:11:23 +08:00
"sync"
2024-03-26 14:54:50 +08:00
"github.com/aceld/kis-flow/common"
"github.com/aceld/kis-flow/config"
"github.com/aceld/kis-flow/id"
"github.com/aceld/kis-flow/kis"
2024-01-09 17:30:58 +08:00
)
2024-04-15 17:50:02 +08:00
// KisConnector represents a KisConnector instance
2024-01-09 17:30:58 +08:00
type KisConnector struct {
// Connector ID
CId string
// Connector Name
CName string
// Connector Config
Conf *config.KisConnConfig
// Connector Init
onceInit sync.Once
2024-01-26 17:27:29 +08:00
2024-04-15 17:50:02 +08:00
// KisConnector's custom temporary data
2024-01-26 17:27:29 +08:00
metaData map[string]interface{}
2024-04-15 17:50:02 +08:00
// Lock for reading and writing metaData
2024-01-26 17:27:29 +08:00
mLock sync.RWMutex
2024-01-09 17:30:58 +08:00
}
2024-04-15 17:50:02 +08:00
// NewKisConnector creates a KisConnector based on the given configuration
2024-01-09 17:30:58 +08:00
func NewKisConnector(config *config.KisConnConfig) *KisConnector {
conn := new(KisConnector)
2024-04-15 17:50:02 +08:00
conn.CId = id.KisID(common.KisIDTypeConnector)
2024-01-09 17:30:58 +08:00
conn.CName = config.CName
conn.Conf = config
2024-01-26 17:27:29 +08:00
conn.metaData = make(map[string]interface{})
2024-01-09 17:30:58 +08:00
return conn
}
2024-04-15 17:50:02 +08:00
// Init initializes the connection to the associated storage engine of the Connector
2024-01-09 17:30:58 +08:00
func (conn *KisConnector) Init() error {
var err error
2024-04-15 17:50:02 +08:00
// The initialization business of a Connector can only be executed once
2024-01-09 17:30:58 +08:00
conn.onceInit.Do(func() {
err = kis.Pool().CallConnInit(conn)
})
return err
}
2024-04-15 17:50:02 +08:00
// Call invokes the read-write operations of the external storage logic through the Connector
func (conn *KisConnector) Call(ctx context.Context, flow kis.Flow, args interface{}) (interface{}, error) {
var result interface{}
var err error
result, err = kis.Pool().CallConnector(ctx, flow, conn, args)
if err != nil {
return nil, err
2024-01-09 17:30:58 +08:00
}
return result, nil
2024-01-09 17:30:58 +08:00
}
2024-04-15 17:50:02 +08:00
// GetName returns the name of the Connector
2024-01-09 17:30:58 +08:00
func (conn *KisConnector) GetName() string {
return conn.CName
}
2024-04-15 17:50:02 +08:00
// GetConfig returns the configuration of the Connector
2024-01-09 17:30:58 +08:00
func (conn *KisConnector) GetConfig() *config.KisConnConfig {
return conn.Conf
}
2024-04-15 17:50:02 +08:00
// GetID returns the ID of the Connector
func (conn *KisConnector) GetID() string {
2024-01-09 17:30:58 +08:00
return conn.CId
}
2024-01-26 17:27:29 +08:00
2024-04-15 17:50:02 +08:00
// GetMetaData gets the temporary data of the current Connector
2024-01-26 17:27:29 +08:00
func (conn *KisConnector) GetMetaData(key string) interface{} {
conn.mLock.RLock()
defer conn.mLock.RUnlock()
data, ok := conn.metaData[key]
if !ok {
return nil
}
return data
}
2024-04-15 17:50:02 +08:00
// SetMetaData sets the temporary data of the current Connector
2024-01-26 17:27:29 +08:00
func (conn *KisConnector) SetMetaData(key string, value interface{}) {
conn.mLock.Lock()
defer conn.mLock.Unlock()
conn.metaData[key] = value
}