manager.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2021 INTECH Process Automation 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 shared
  15. import (
  16. "fmt"
  17. "sync"
  18. )
  19. const IdProperty = "id"
  20. type channels struct {
  21. id string
  22. consumers map[string]chan map[string]interface{}
  23. }
  24. var sinkChannels = make(map[string]*channels)
  25. var mu = sync.Mutex{}
  26. func GetSink(props map[string]interface{}) (*sink, error) {
  27. id, err := getId(props)
  28. if err != nil {
  29. return nil, err
  30. }
  31. ch, err := getOrCreateSinkChannels(id)
  32. if err != nil {
  33. return nil, err
  34. }
  35. s := &sink{
  36. id: id,
  37. ch: ch,
  38. }
  39. return s, nil
  40. }
  41. func GetSource() *source {
  42. return &source{}
  43. }
  44. func getOrCreateSinkChannels(sink string) (*channels, error) {
  45. mu.Lock()
  46. defer mu.Unlock()
  47. if c, exists := sinkChannels[sink]; exists {
  48. return c, nil
  49. }
  50. c := createChannels(sink)
  51. sinkChannels[sink] = c
  52. return c, nil
  53. }
  54. func getOrCreateSinkConsumerChannel(sink string, source string) (chan map[string]interface{}, error) {
  55. mu.Lock()
  56. defer mu.Unlock()
  57. var sinkConsumerChannels *channels
  58. if c, exists := sinkChannels[sink]; exists {
  59. sinkConsumerChannels = c
  60. } else {
  61. sinkConsumerChannels = createChannels(sink)
  62. }
  63. var ch chan map[string]interface{}
  64. if sourceChannel, exists := sinkConsumerChannels.consumers[source]; exists {
  65. ch = sourceChannel
  66. } else {
  67. ch = make(chan map[string]interface{})
  68. sinkConsumerChannels.consumers[source] = ch
  69. }
  70. return ch, nil
  71. }
  72. func getId(props map[string]interface{}) (string, error) {
  73. if t, ok := props[IdProperty]; ok {
  74. if id, casted := t.(string); casted {
  75. return id, nil
  76. }
  77. return "", fmt.Errorf("can't cast value %s to string", t)
  78. }
  79. return "", fmt.Errorf("there is no topic property in the memory action")
  80. }
  81. func closeSourceConsumerChannel(sink string, source string) error {
  82. mu.Lock()
  83. defer mu.Unlock()
  84. if sinkConsumerChannels, exists := sinkChannels[sink]; exists {
  85. if sourceChannel, exists := sinkConsumerChannels.consumers[source]; exists {
  86. close(sourceChannel)
  87. delete(sinkConsumerChannels.consumers, source)
  88. }
  89. }
  90. return nil
  91. }
  92. func closeSink(sink string) error {
  93. mu.Lock()
  94. defer mu.Unlock()
  95. if sinkConsumerChannels, exists := sinkChannels[sink]; exists {
  96. for s, c := range sinkConsumerChannels.consumers {
  97. close(c)
  98. delete(sinkConsumerChannels.consumers, s)
  99. }
  100. }
  101. delete(sinkChannels, sink)
  102. return nil
  103. }
  104. func createChannels(sink string) *channels {
  105. return &channels{
  106. id: sink,
  107. consumers: make(map[string]chan map[string]interface{}),
  108. }
  109. }