Skip to content

Commit

Permalink
update events for ephemeral consumer support (#92)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <[email protected]>
  • Loading branch information
rizzza authored Jan 29, 2024
1 parent f3a53ea commit 53adf7f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 540 deletions.
30 changes: 4 additions & 26 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -127,16 +125,15 @@ func run(cmdCtx context.Context, v *viper.Viper) error {
mgr.LBClient = lbapi.NewClient(viper.GetString("loadbalancerapi.url"))
}

// generate a random queuegroup name
// this is to prevent multiple instances of this service from receiving the same message
// and processing it
config.AppConfig.Events.NATS.QueueGroup = generateQueueGroupName()

events, err := events.NewConnection(config.AppConfig.Events, events.WithLogger(logger))
if err != nil {
logger.Fatalw("failed to create events connection", "error", err)
}

defer func() {
_ = events.Shutdown(ctx)
}()

// init events subscriber
subscriber := pubsub.NewSubscriber(
ctx,
Expand All @@ -155,10 +152,6 @@ func run(cmdCtx context.Context, v *viper.Viper) error {
}
}

defer func() {
_ = events.Shutdown(ctx)
}()

if err := mgr.Run(); err != nil {
logger.Fatalw("failed starting manager", "error", err)
}
Expand Down Expand Up @@ -192,18 +185,3 @@ func validateMandatoryFlags() error {

return errors.Join(errs...) //nolint:goerr113
}

// generateQueueGroupName generates a random queue group name with prefix lbmanager-haproxy-
func generateQueueGroupName() string {
const rlen = 10

alphaNum := []rune("abcdefghijklmnopqrstuvwxyz1234567890")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]rune, rlen)

for i := range b {
b[i] = alphaNum[r.Intn(len(alphaNum))]
}

return fmt.Sprintf("lbmanager-haproxy-%s-", string(b))
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.infratographer.com/load-balancer-api v0.1.3
go.infratographer.com/x v0.3.9
go.infratographer.com/x v0.3.10-0.20240122202348-a5142b682ef6
go.uber.org/zap v1.26.0
)

Expand All @@ -27,7 +27,6 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/renameio v1.0.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/haproxytech/go-logger v1.1.0 // indirect
Expand Down Expand Up @@ -67,7 +66,7 @@ require (
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
Expand Down
Loading

0 comments on commit 53adf7f

Please sign in to comment.