manager.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package server
  2. import (
  3. "fmt"
  4. "sync"
  5. )
  6. type ControlManager struct {
  7. // controls indexed by run id
  8. ctlsByRunId map[string]*Control
  9. mu sync.RWMutex
  10. }
  11. func NewControlManager() *ControlManager {
  12. return &ControlManager{
  13. ctlsByRunId: make(map[string]*Control),
  14. }
  15. }
  16. func (cm *ControlManager) Add(runId string, ctl *Control) (oldCtl *Control) {
  17. cm.mu.Lock()
  18. defer cm.mu.Unlock()
  19. oldCtl, ok := cm.ctlsByRunId[runId]
  20. if ok {
  21. oldCtl.Replaced(ctl)
  22. }
  23. cm.ctlsByRunId[runId] = ctl
  24. return
  25. }
  26. func (cm *ControlManager) GetById(runId string) (ctl *Control, ok bool) {
  27. cm.mu.RLock()
  28. defer cm.mu.RUnlock()
  29. ctl, ok = cm.ctlsByRunId[runId]
  30. return
  31. }
  32. type ProxyManager struct {
  33. // proxies indexed by proxy name
  34. pxys map[string]Proxy
  35. mu sync.RWMutex
  36. }
  37. func NewProxyManager() *ProxyManager {
  38. return &ProxyManager{
  39. pxys: make(map[string]Proxy),
  40. }
  41. }
  42. func (pm *ProxyManager) Add(name string, pxy Proxy) error {
  43. pm.mu.Lock()
  44. defer pm.mu.Unlock()
  45. if _, ok := pm.pxys[name]; ok {
  46. return fmt.Errorf("proxy name [%s] is already in use", name)
  47. }
  48. pm.pxys[name] = pxy
  49. return nil
  50. }
  51. func (pm *ProxyManager) Del(name string) {
  52. pm.mu.Lock()
  53. defer pm.mu.Unlock()
  54. delete(pm.pxys, name)
  55. }
  56. func (pm *ProxyManager) GetByName(name string) (pxy Proxy, ok bool) {
  57. pm.mu.RLock()
  58. defer pm.mu.RUnlock()
  59. pxy, ok = pm.pxys[name]
  60. return
  61. }