/
manager.go
229 lines (189 loc) · 5.66 KB
/
manager.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package proxy
import (
"context"
"maps"
"net"
"sync"
"time"
"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/util/graceful"
"github.com/rs/zerolog"
xmaps "golang.org/x/exp/maps"
)
type ProxyManager struct {
ctx context.Context
cfg config.Config
reloadConfig chan config.Config
metaMu sync.Mutex
metaStore map[Identifier]*Metadata
mountsMu sync.Mutex
mounts map[string]*Mount
cleanup map[string]*time.Timer
}
func NewProxyManager(ctx context.Context, cfg config.Config) (*ProxyManager, error) {
m := &ProxyManager{
ctx: ctx,
cfg: cfg,
reloadConfig: make(chan config.Config),
metaStore: make(map[Identifier]*Metadata),
mounts: make(map[string]*Mount),
cleanup: make(map[string]*time.Timer),
}
return m, nil
}
func (pm *ProxyManager) RemoveMount(mount *Mount) {
pm.mountsMu.Lock()
defer pm.mountsMu.Unlock()
t := pm.cleanup[mount.Name]
if t != nil {
// we're already trying to cleanup apparently, reset the timer
t.Reset(mountTimeout)
return
}
pm.cleanup[mount.Name] = time.AfterFunc(mountTimeout, func() {
pm.mountsMu.Lock()
defer pm.mountsMu.Unlock()
// first see if we're still ment to be cleaning up, the cleanup can
// be canceled by an AddSourceClient call between timer start and
// timer fire, and we keep track of this by the entry in cleanup
//
// note that there is an obvious logic race here where the call to
// AddSourceClient is waiting on mountsMu but we have already acquired
// it, this does mean we've already passed our mountTimeout so just
// proceed with cleaning up and the AddSourceClient can make a new mount
if pm.cleanup[mount.Name] == nil {
return
}
// otherwise we are still fine to cleanup and
// remove the mount from our internal state
delete(pm.mounts, mount.Name)
// then let the mount cleanup whatever it needs to cleanup
err := mount.Close()
if err != nil {
mount.logger.Error().Err(err).Msg("closing mount master connection")
}
})
}
func (pm *ProxyManager) AddSourceClient(source *SourceClient) error {
if source == nil {
panic("nil source client in AddSourceClient")
}
logger := zerolog.Ctx(pm.ctx)
pm.mountsMu.Lock()
defer pm.mountsMu.Unlock()
mount, ok := pm.mounts[source.MountName]
if !ok { // if it didn't exist create one
mount = NewMount(pm.ctx, pm.cfg, pm, source.MountName, source.ContentType, nil)
pm.mounts[source.MountName] = mount
}
// check if our mount was empty and was waiting to clean itself up
if t := pm.cleanup[mount.Name]; t != nil {
// it's possible that we call Stop but the timer has already fired,
// but since we're holding onto mountsMu the removal function can't run
// yet and will cancel itself once it sees we've removed the entry in
// the cleanup map.
t.Stop()
delete(pm.cleanup, mount.Name)
}
if mount.ContentType != source.ContentType {
// log content-type mismatches, but keep going as if it didn't happen.
// This shouldn't really occur unless someone is being silly
logger.Warn().
Str("mount-content-type", mount.ContentType).
Str("source-content-type", source.ContentType).
Msg("mismatching content-type")
}
logger.Info().
Str("mount", source.MountName).
Str("username", source.User.Username).
Str("address", source.conn.RemoteAddr().String()).
Msg("adding source client")
// see if we have any metadata from before this source connected as
// a source client, storing a nil here is fine
source.Metadata.Store(pm.Metadata(source.Identifier))
// add the source to the mount list
mount.AddSource(pm.ctx, source)
return nil
}
func (pm *ProxyManager) SendMetadata(ctx context.Context, metadata *Metadata) error {
if metadata == nil {
panic("nil metadata in SendMetadata")
}
pm.mountsMu.Lock()
mount, ok := pm.mounts[metadata.MountName]
if ok {
pm.mountsMu.Unlock()
mount.SendMetadata(ctx, metadata)
return nil
}
defer pm.mountsMu.Unlock()
// metadata for a mount that doesn't exist, we store it temporarily
// to see if a new source client will appear soon
zerolog.Ctx(ctx).Info().
Str("mount", metadata.MountName).
Str("username", metadata.User.Username).
Str("address", metadata.Addr).
Msg("storing metadata because mount does not exist")
pm.metaMu.Lock()
pm.metaStore[metadata.Identifier] = metadata
pm.metaMu.Unlock()
return nil
}
func (pm *ProxyManager) Metadata(identifier Identifier) *Metadata {
pm.metaMu.Lock()
defer pm.metaMu.Unlock()
defer delete(pm.metaStore, identifier)
return pm.metaStore[identifier]
}
type wireProxy struct {
Mounts []string
Metadata map[Identifier]*Metadata
}
func (pm *ProxyManager) writeSelf(dst *net.UnixConn) error {
pm.mountsMu.Lock()
mounts := maps.Clone(pm.mounts)
pm.mountsMu.Unlock()
pm.metaMu.Lock()
metadata := maps.Clone(pm.metaStore)
pm.metaMu.Unlock()
wp := wireProxy{
Mounts: xmaps.Keys(mounts),
Metadata: metadata,
}
err := graceful.WriteJSON(dst, wp)
if err != nil {
return err
}
for _, mount := range mounts {
err := mount.writeSelf(dst)
if err != nil {
return err
}
}
return nil
}
func (pm *ProxyManager) readSelf(ctx context.Context, cfg config.Config, src *net.UnixConn) error {
var wp wireProxy
zerolog.Ctx(ctx).Info().Msg("resume: reading proxy manager data")
err := graceful.ReadJSON(src, &wp)
if err != nil {
return err
}
zerolog.Ctx(ctx).Info().Any("wire", wp).Msg("resume")
pm.metaMu.Lock()
xmaps.Copy(pm.metaStore, wp.Metadata)
pm.metaMu.Unlock()
mounts := make(map[string]*Mount)
for range wp.Mounts {
mount := new(Mount)
err = mount.readSelf(ctx, cfg, src)
if err != nil {
return err
}
mounts[mount.Name] = mount
}
pm.mountsMu.Lock()
xmaps.Copy(pm.mounts, mounts)
pm.mountsMu.Unlock()
return nil
}