client_registy.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clients
  15. import (
  16. "fmt"
  17. "strings"
  18. "sync"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. )
  22. type clientRegistry struct {
  23. Lock sync.Mutex
  24. supportedClientType []string
  25. clientFactory map[string]ClientFactoryFunc
  26. shareClientStore map[string]ClientWrapper
  27. }
  28. var gClientRegistry = clientRegistry{
  29. clientFactory: make(map[string]ClientFactoryFunc),
  30. Lock: sync.Mutex{},
  31. supportedClientType: make([]string, 0),
  32. shareClientStore: make(map[string]ClientWrapper),
  33. }
  34. func RegisterClientFactory(clientType string, creatorFunc ClientFactoryFunc) {
  35. gClientRegistry.Lock.Lock()
  36. gClientRegistry.clientFactory[clientType] = creatorFunc
  37. gClientRegistry.supportedClientType = append(gClientRegistry.supportedClientType, clientType)
  38. gClientRegistry.Lock.Unlock()
  39. }
  40. func getConnectionSelector(props map[string]interface{}) (ConnectionSelector string, err error) {
  41. for key, v := range props {
  42. if strings.EqualFold(key, "connectionSelector") {
  43. if conVal, ok := v.(string); ok {
  44. return strings.ToLower(conVal), nil
  45. } else {
  46. return "", fmt.Errorf("connectionSelector value: %v is not string", v)
  47. }
  48. }
  49. }
  50. return "", nil
  51. }
  52. func GetClient(connectionType string, props map[string]interface{}) (api.MessageClient, error) {
  53. gClientRegistry.Lock.Lock()
  54. defer gClientRegistry.Lock.Unlock()
  55. connectSelector, err := getConnectionSelector(props)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if connectSelector != "" {
  60. if cliWpr, found := gClientRegistry.shareClientStore[connectSelector]; found {
  61. cliWpr.AddRef()
  62. return cliWpr, nil
  63. }
  64. }
  65. clientCreator, ok := gClientRegistry.clientFactory[connectionType]
  66. if !ok {
  67. conf.Log.Errorf("can not find clientCreator for connection type : %s. only support %s", connectionType, gClientRegistry.supportedClientType)
  68. return nil, fmt.Errorf("can not find clientCreator for connection type : %s. only support %s", connectionType, gClientRegistry.supportedClientType)
  69. }
  70. if connectSelector != "" {
  71. selectCfg := &conf.ConSelector{
  72. ConnSelectorStr: connectSelector,
  73. }
  74. if err := selectCfg.Init(); err != nil {
  75. return nil, err
  76. }
  77. cf, err := selectCfg.ReadCfgFromYaml()
  78. if err != nil {
  79. return nil, err
  80. }
  81. cliWpr, err := clientCreator(cf)
  82. if err != nil {
  83. conf.Log.Errorf("can not create client for connection selector : %s have error %s", connectSelector, err)
  84. return nil, err
  85. }
  86. cliWpr.SetConnectionSelector(connectSelector)
  87. conf.Log.Infof("Init client wrapper for client type %s and connection selector %s", connectionType, connectSelector)
  88. gClientRegistry.shareClientStore[connectSelector] = cliWpr
  89. return cliWpr, nil
  90. } else {
  91. cliWpr, err := clientCreator(props)
  92. if err != nil {
  93. conf.Log.Errorf("can not create client for cfg : %v have error %s", conf.Printable(props), err)
  94. return nil, err
  95. }
  96. conf.Log.Infof("Init client wrapper for client type %s", connectionType)
  97. return cliWpr, nil
  98. }
  99. }
  100. func ReleaseClient(ctx api.StreamContext, cli api.MessageClient) {
  101. log := ctx.GetLogger()
  102. wrapper := cli.(ClientWrapper)
  103. sel := wrapper.GetConnectionSelector()
  104. ok := wrapper.Release(ctx)
  105. if sel != "" && ok {
  106. log.Infof("remove mqtt client wrapper for connection selector %s", sel)
  107. gClientRegistry.Lock.Lock()
  108. delete(gClientRegistry.shareClientStore, sel)
  109. gClientRegistry.Lock.Unlock()
  110. }
  111. }