visitor_manager.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2018 fatedier, fatedier@gmail.com
  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 client
  15. import (
  16. "context"
  17. "sync"
  18. "time"
  19. "github.com/fatedier/frp/pkg/config"
  20. "github.com/fatedier/frp/pkg/util/xlog"
  21. )
  22. type VisitorManager struct {
  23. ctl *Control
  24. cfgs map[string]config.VisitorConf
  25. visitors map[string]Visitor
  26. checkInterval time.Duration
  27. mu sync.Mutex
  28. ctx context.Context
  29. stopCh chan struct{}
  30. }
  31. func NewVisitorManager(ctx context.Context, ctl *Control) *VisitorManager {
  32. return &VisitorManager{
  33. ctl: ctl,
  34. cfgs: make(map[string]config.VisitorConf),
  35. visitors: make(map[string]Visitor),
  36. checkInterval: 10 * time.Second,
  37. ctx: ctx,
  38. stopCh: make(chan struct{}),
  39. }
  40. }
  41. func (vm *VisitorManager) Run() {
  42. xl := xlog.FromContextSafe(vm.ctx)
  43. ticker := time.NewTicker(vm.checkInterval)
  44. defer ticker.Stop()
  45. for {
  46. select {
  47. case <-vm.stopCh:
  48. xl.Info("gracefully shutdown visitor manager")
  49. return
  50. case <-ticker.C:
  51. vm.mu.Lock()
  52. for _, cfg := range vm.cfgs {
  53. name := cfg.GetBaseInfo().ProxyName
  54. if _, exist := vm.visitors[name]; !exist {
  55. xl.Info("try to start visitor [%s]", name)
  56. vm.startVisitor(cfg)
  57. }
  58. }
  59. vm.mu.Unlock()
  60. }
  61. }
  62. }
  63. // Hold lock before calling this function.
  64. func (vm *VisitorManager) startVisitor(cfg config.VisitorConf) (err error) {
  65. xl := xlog.FromContextSafe(vm.ctx)
  66. name := cfg.GetBaseInfo().ProxyName
  67. visitor := NewVisitor(vm.ctx, vm.ctl, cfg)
  68. err = visitor.Run()
  69. if err != nil {
  70. xl.Warn("start error: %v", err)
  71. } else {
  72. vm.visitors[name] = visitor
  73. xl.Info("start visitor success")
  74. }
  75. return
  76. }
  77. func (vm *VisitorManager) Reload(cfgs map[string]config.VisitorConf) {
  78. xl := xlog.FromContextSafe(vm.ctx)
  79. vm.mu.Lock()
  80. defer vm.mu.Unlock()
  81. delNames := make([]string, 0)
  82. for name, oldCfg := range vm.cfgs {
  83. del := false
  84. cfg, ok := cfgs[name]
  85. if !ok {
  86. del = true
  87. } else {
  88. if !oldCfg.Compare(cfg) {
  89. del = true
  90. }
  91. }
  92. if del {
  93. delNames = append(delNames, name)
  94. delete(vm.cfgs, name)
  95. if visitor, ok := vm.visitors[name]; ok {
  96. visitor.Close()
  97. }
  98. delete(vm.visitors, name)
  99. }
  100. }
  101. if len(delNames) > 0 {
  102. xl.Info("visitor removed: %v", delNames)
  103. }
  104. addNames := make([]string, 0)
  105. for name, cfg := range cfgs {
  106. if _, ok := vm.cfgs[name]; !ok {
  107. vm.cfgs[name] = cfg
  108. addNames = append(addNames, name)
  109. vm.startVisitor(cfg)
  110. }
  111. }
  112. if len(addNames) > 0 {
  113. xl.Info("visitor added: %v", addNames)
  114. }
  115. return
  116. }
  117. func (vm *VisitorManager) Close() {
  118. vm.mu.Lock()
  119. defer vm.mu.Unlock()
  120. for _, v := range vm.visitors {
  121. v.Close()
  122. }
  123. select {
  124. case <-vm.stopCh:
  125. default:
  126. close(vm.stopCh)
  127. }
  128. }