diff --git a/sonic_data_client/mixed_db_client.go b/sonic_data_client/mixed_db_client.go index 4fb5cc66..269db046 100644 --- a/sonic_data_client/mixed_db_client.go +++ b/sonic_data_client/mixed_db_client.go @@ -71,6 +71,29 @@ type MixedDbClient struct { mu sync.RWMutex // Mutex for data protection among routines for DbClient } +var mixedDbClientMap = map[string]MixedDbClient{} + +func getMixedDbClient(zmqAddress string) (MixedDbClient) { + client, ok := mixedDbClientMap[zmqAddress] + if !ok { + client = MixedDbClient { + applDB : swsscommon.NewDBConnector(APPL_DB_NAME, SWSS_TIMEOUT, false), + tableMap : map[string]swsscommon.ProducerStateTable{}, + } + + // enable ZMQ by zmqAddress parameter + if zmqAddress != "" { + client.zmqClient = swsscommon.NewZmqClient(zmqAddress) + } else { + client.zmqClient = nil + } + + mixedDbClientMap[zmqAddress] = client + } + + return client +} + func parseJson(str []byte) (interface{}, error) { var res interface{} err := json.Unmarshal(str, &res) @@ -196,7 +219,6 @@ func (c *MixedDbClient) DbDelTable(table string, key string) error { } func NewMixedDbClient(paths []*gnmipb.Path, prefix *gnmipb.Path, origin string, zmqAddress string) (Client, error) { - var client MixedDbClient var err error // Testing program may ask to use redis local tcp connection @@ -204,6 +226,7 @@ func NewMixedDbClient(paths []*gnmipb.Path, prefix *gnmipb.Path, origin string, useRedisTcpClient() } + var client = getMixedDbClient(zmqAddress) client.prefix = prefix client.target = "" client.origin = origin @@ -229,8 +252,6 @@ func NewMixedDbClient(paths []*gnmipb.Path, prefix *gnmipb.Path, origin string, } client.paths = paths client.workPath = common_utils.GNMI_WORK_PATH - client.applDB = swsscommon.NewDBConnector(APPL_DB, REDIS_SOCK, SWSS_TIMEOUT) - client.tableMap = map[string]swsscommon.ProducerStateTable{} return &client, nil } @@ -1129,20 +1150,7 @@ func (c *MixedDbClient) Capabilities() []gnmipb.ModelData { } func (c *MixedDbClient) Close() error { - for _, pt := range c.tableMap { - zpt, ok := pt.(swsscommon.ZmqProducerStateTable) - if ok { - swsscommon.DeleteZmqProducerStateTable(zpt) - } else { - swsscommon.DeleteProducerStateTable(pt) - } - } - - if c.zmqClient != nil { - swsscommon.DeleteZmqClient(c.zmqClient) - } - - swsscommon.DeleteDBConnector(c.applDB) + // Do nothing here, because MixedDbClient will be cache in mixedDbClientMap and reuse return nil }