Fix register session after close

This commit is contained in:
Ingo Oppermann 2023-07-04 12:30:43 +02:00
parent 3ff5251eba
commit 352289f759
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E
3 changed files with 52 additions and 2 deletions

View File

@ -431,6 +431,13 @@ func (c *collector) stop() {
c.sessionsWG.Wait()
}
func (c *collector) isRunning() bool {
c.lock.run.Lock()
defer c.lock.run.Unlock()
return c.running
}
type historySnapshot struct {
data []byte
}
@ -507,11 +514,19 @@ func (c *collector) IsKnownSession(id string) bool {
}
func (c *collector) RegisterAndActivate(id, reference, location, peer string) {
if !c.isRunning() {
return
}
c.Register(id, reference, location, peer)
c.Activate(id)
}
func (c *collector) Register(id, reference, location, peer string) {
if !c.isRunning() {
return
}
c.lock.session.Lock()
defer c.lock.session.Unlock()

View File

@ -407,8 +407,6 @@ func (r *registry) UnregisterAll() {
for id := range r.collector {
r.unregister(id)
}
r.collector = make(map[string]*collector)
}
func (r *registry) Summary(id string) Summary {

View File

@ -1,6 +1,7 @@
package session
import (
"strconv"
"testing"
"time"
@ -300,3 +301,39 @@ func TestPersistSessionBuffer(t *testing.T) {
require.NoError(t, err)
require.Greater(t, info.Size(), int64(0))
}
func TestRegisterAfterCloseWithPersist(t *testing.T) {
memfs, err := fs.NewMemFilesystem(fs.MemConfig{})
require.NoError(t, err)
pattern := "/log/%Y-%m-%d.log"
r, err := New(Config{
PersistFS: memfs,
LogPattern: pattern,
LogBufferDuration: 5 * time.Second,
})
require.NoError(t, err)
t.Cleanup(func() {
r.Close()
})
c, err := r.Register("foobar", CollectorConfig{
SessionTimeout: 3 * time.Second,
})
require.NoError(t, err)
for i := 0; i < 1000; i++ {
c.RegisterAndActivate("foo_"+strconv.Itoa(i), "ref", "location", "peer")
c.Egress("foo", int64(i))
}
r.Close()
c.RegisterAndActivate("foo_XXX", "ref", "location", "peer")
c.Egress("foo", 42)
time.Sleep(5 * time.Second)
require.Equal(t, int64(2), memfs.Files())
}