Skip to content

Commit

Permalink
feat: add session
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Sep 4, 2024
1 parent a44f916 commit 4341eab
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package session

import (
"github.com/prometheus-community/pro-bing"
"golang.org/x/sync/errgroup"

"github.com/b4nst/icmperf/pkg/recorder"
)

type Session struct {
// pingers is a list of pingers.
pingers []*probing.Pinger
// record is a session record.
record *recorder.Record
}

// NewSession creates a new session with the given number of pingers.
func NewSession(pingers []*probing.Pinger) *Session {
s := &Session{
pingers: pingers,
record: recorder.NewRecord(),
}
for _, pinger := range pingers {
pinger.OnRecv = func(p *probing.Packet) {
s.record.AddPacket(p)
}
}
return s
}

func (s *Session) Run() error {
wg := new(errgroup.Group)
for _, pinger := range s.pingers {
p := pinger // capture range variable
wg.Go(func() error {
return p.Run()
})
}
return wg.Wait()
}

func (s *Session) Statistics() []*recorder.Stat {
return s.record.Stats()
}

0 comments on commit 4341eab

Please sign in to comment.