This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
registry.go
86 lines (73 loc) · 2.07 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Copyright (c) 2012-2018 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package jsonrpc
import "sync"
// DefaultRegistry is package registry, which is used by NewManagedTunnel.
var DefaultRegistry = NewRegistry()
// Save saves a given tunnel is the default registry.
func Save(tun *Tunnel) {
DefaultRegistry.Save(tun)
}
// Rm removes a given tunnel from the default registry.
func Rm(id string) (*Tunnel, bool) {
return DefaultRegistry.Rm(id)
}
// GetTunnels gets tunnels managed by default registry.
func GetTunnels() []*Tunnel {
return DefaultRegistry.GetTunnels()
}
// Get gets a single tunnel managed by default registry.
func Get(id string) (*Tunnel, bool) {
return DefaultRegistry.Get(id)
}
// TunnelRegistry is a simple storage for tunnels.
type TunnelRegistry struct {
sync.RWMutex
tunnels map[string]*Tunnel
}
// NewRegistry creates a new registry.
func NewRegistry() *TunnelRegistry {
return &TunnelRegistry{tunnels: make(map[string]*Tunnel)}
}
// Save saves a tunnel with a given id in this registry, overrides existing one.
func (reg *TunnelRegistry) Save(tunnel *Tunnel) {
reg.Lock()
defer reg.Unlock()
reg.tunnels[tunnel.id] = tunnel
}
// Rm removes tunnel with given id from the registry.
func (reg *TunnelRegistry) Rm(id string) (*Tunnel, bool) {
reg.Lock()
defer reg.Unlock()
tun, ok := reg.tunnels[id]
if ok {
delete(reg.tunnels, id)
}
return tun, ok
}
// GetTunnels returns all the tunnels which the registry keeps.
func (reg *TunnelRegistry) GetTunnels() []*Tunnel {
reg.RLock()
defer reg.RUnlock()
tunnels := make([]*Tunnel, 0)
for _, v := range reg.tunnels {
tunnels = append(tunnels, v)
}
return tunnels
}
// Get returns tunnel with a given id.
func (reg *TunnelRegistry) Get(id string) (*Tunnel, bool) {
reg.RLock()
defer reg.RUnlock()
tun, ok := reg.tunnels[id]
return tun, ok
}