Skip to content

Commit

Permalink
fix: wait for first heartbeat before starting networking loops
Browse files Browse the repository at this point in the history
  • Loading branch information
felixauringer committed Oct 15, 2024
1 parent 097bf3c commit 1e4e317
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/bee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/netip"
"os"
"os/signal"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -65,7 +66,8 @@ func run(bindAddress netip.Addr, disableNftables bool, beekeeperBasePath string)
}
defer forwarder.Close()

heartbeat := heartbeat.NewHeartbeat(bee, forwarder, bindAddress)
var configReceived sync.WaitGroup
heartbeat := heartbeat.NewHeartbeat(bee, forwarder, bindAddress, &configReceived)

signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
Expand All @@ -74,6 +76,7 @@ func run(bindAddress netip.Addr, disableNftables bool, beekeeperBasePath string)

go func() {
defer recoverPanic(signalChannel)
configReceived.Wait()
for {
if err := forwarder.AttackerToBeehiveLoop(ctx); err != nil {
log.WithError(err).Error("Attacker to Beehive loop failed. Restarting.")
Expand All @@ -88,6 +91,7 @@ func run(bindAddress netip.Addr, disableNftables bool, beekeeperBasePath string)

go func() {
defer recoverPanic(signalChannel)
configReceived.Wait()
for {
if err := forwarder.BeehiveToAttackerLoop(ctx); err != nil {
log.WithError(err).Error("Beehive to Attacker loop failed. Restarting.")
Expand Down
17 changes: 13 additions & 4 deletions internal/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"net/netip"
"sync"
"time"

"github.com/Cybersecurity-and-Enterprise-Security/bee/internal/apibee"
Expand All @@ -18,23 +19,31 @@ const (
)

type Heartbeat struct {
bee *apibee.Bee
forwarder *forward.Forwarder
bindAddress netip.Addr
bee *apibee.Bee
forwarder *forward.Forwarder
bindAddress netip.Addr
startupComplete bool
configReceived *sync.WaitGroup
}

func NewHeartbeat(bee *apibee.Bee, forwarder *forward.Forwarder, bindAddress netip.Addr) *Heartbeat {
func NewHeartbeat(bee *apibee.Bee, forwarder *forward.Forwarder, bindAddress netip.Addr, configReceived *sync.WaitGroup) *Heartbeat {
return &Heartbeat{
bee,
forwarder,
bindAddress,
false,
configReceived,
}
}

func (h *Heartbeat) Run(ctx context.Context) error {
for {
h.ReportStats(ctx)
h.UpdateForwardings(ctx)
if !h.startupComplete {
h.startupComplete = true
h.configReceived.Done()
}

select {
case <-ctx.Done():
Expand Down

0 comments on commit 1e4e317

Please sign in to comment.