pirsch-analytics / pirsch

Pirsch is a drop-in, server-side, no-cookie, and privacy-focused analytics solution for Go.
https://pirsch.io
GNU Affero General Public License v3.0
928 stars 43 forks source link

v3.5 #229

Closed Kugelschieber closed 2 years ago

Kugelschieber commented 2 years ago

Should make sure it's not full before adding more sessions. The buffer can be expanded indefinitely, in theory.

func (tracker *Tracker) aggregateSessions(ctx context.Context) {
    sessions := make([]Session, 0, tracker.workerBufferSize*2)
    timer := time.NewTimer(tracker.workerTimeout)
    defer timer.Stop()

    for {
        timer.Reset(tracker.workerTimeout)

        select {
        case session := <-tracker.sessions:
            if session.Cancel != nil {
                sessions = append(sessions, *session.Cancel)
            }

            sessions = append(sessions, session.State)

            if len(sessions) >= tracker.workerBufferSize*2 {
                tracker.saveSessions(sessions)
                sessions = sessions[:0]
            }
        case <-timer.C:
            tracker.saveSessions(sessions)
            sessions = sessions[:0]
        case <-ctx.Done():
            tracker.saveSessions(sessions)
            tracker.workerDone <- true
            return
        }
    }
}