Skip to content

Commit

Permalink
feat: add internal events
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofnds committed May 25, 2023
1 parent 90a1b3b commit ac2e00c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
32 changes: 32 additions & 0 deletions internal/event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package event

type Event[T any] struct {
cap int
listeners []chan T
}

func NewEvent[T any](cap int) *Event[T] {
return &Event[T]{cap: cap}
}

func (e *Event[T]) Close() {
for _, c := range e.listeners {
close(c)
}
}

func (e *Event[T]) Listen() <-chan T {
c := make(chan T, e.cap)
e.listeners = append(e.listeners, c)
return c
}

func (e *Event[T]) Send(t T) {
go func() {
defer func() { _ = recover() }()

for _, c := range e.listeners {
c <- t
}
}()
}
7 changes: 5 additions & 2 deletions user/adapter/prom_probe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adapter

import (
"app/internal/event"
"app/user"
"context"

Expand All @@ -11,13 +12,15 @@ import (

type PromProbe struct {
logger *zap.Logger
userCreatedEvent *event.Event[user.UserCreated]
usersCreated prometheus.Counter
usersCreateFailed prometheus.Counter
}

func NewPromProbe(logger *zap.Logger) *PromProbe {
func NewPromProbe(logger *zap.Logger, userCreatedEvent *event.Event[user.UserCreated]) *PromProbe {
return &PromProbe{
logger: logger,
userCreatedEvent: userCreatedEvent,
usersCreated: promauto.NewCounter(prometheus.CounterOpts{Name: "users_created"}),
usersCreateFailed: promauto.NewCounter(prometheus.CounterOpts{Name: "users_create_fail"}),
}
Expand Down Expand Up @@ -45,6 +48,6 @@ func (p *PromProbe) FailedToEnqueue(err error) {
}

func (p *PromProbe) UserCreated(_ context.Context, u user.User) {
p.logger.Info("user created", zap.String("name", u.Name))
p.usersCreated.Inc()
p.userCreatedEvent.Send(user.NewUserCreated(u))
}
9 changes: 9 additions & 0 deletions user/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package user

type UserCreated struct {
User User `json:"user"`
}

func NewUserCreated(u User) UserCreated {
return UserCreated{User: u}
}
21 changes: 21 additions & 0 deletions user/module/module.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package user

import (
"app/internal/event"
"app/user"
"app/user/adapter"
"app/user/http"
"app/user/queue"
"context"

"go.uber.org/fx"
)
Expand All @@ -25,4 +27,23 @@ var Module = fx.Module(
fx.Provide(func(probe *adapter.PromProbe) user.Probe { return probe }),

fx.Provide(http.NewController),

fx.Provide(func(lifecycle fx.Lifecycle) *event.Event[user.UserCreated] {
e := event.NewEvent[user.UserCreated](10)
lifecycle.Append(fx.Hook{
OnStop: func(_ context.Context) error {
e.Close()
return nil
},
})
return e
}),

fx.Invoke(func(event *event.Event[user.UserCreated], greeter *queue.Greeter) {
go func() {
for userCreatedEvent := range event.Listen() {
_ = greeter.Enqueue(userCreatedEvent.User.Name)
}
}()
}),
)
15 changes: 4 additions & 11 deletions user/service.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package user

import (
"app/user/queue"

"context"
)

type Service struct {
repo Repository
greeter *queue.Greeter
probe Probe
repo Repository
probe Probe
}

func NewUserService(repo Repository, greeter *queue.Greeter, probe Probe) *Service {
return &Service{repo: repo, greeter: greeter, probe: probe}
func NewUserService(repo Repository, probe Probe) *Service {
return &Service{repo: repo, probe: probe}
}

func (service *Service) CreateUser(ctx context.Context, name string) (User, error) {
Expand All @@ -25,10 +22,6 @@ func (service *Service) CreateUser(ctx context.Context, name string) (User, erro
}
service.probe.UserCreated(ctx, user)

if err := service.greeter.Enqueue(name); err != nil {
service.probe.FailedToEnqueue(err)
}

return user, err
}

Expand Down

0 comments on commit ac2e00c

Please sign in to comment.