Skip to content

Commit

Permalink
feat: don't fail publish event (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Dec 16, 2024
1 parent 3db2831 commit 919422f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
9 changes: 3 additions & 6 deletions server/bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ func New(log *zap.Logger) *Bus {
}
}

var errChannelNotFound = fmt.Errorf("channel not found for event")

func (b *Bus) Publish(event string, payload any) error {
func (b *Bus) Publish(event string, payload any) {
b.mu.RLock()
channel, found := b.channels[event]
b.mu.RUnlock()

if !found {
// TODO: Log error and return.
return fmt.Errorf("%w: %s", errChannelNotFound, event)
b.log.Error("channel not found", zap.String("event", event))
return
}

channel <- payload
return nil
}

const (
Expand Down
6 changes: 2 additions & 4 deletions server/pkg/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ func (m *Tracer) Trace(uri string) *Trace {
start: time.Now().UTC(),
onEnd: func(duration time.Duration, statusCode int) {
m.log.Info("trace", zap.String("uri", uri), zap.Duration("duration", duration), zap.Int("status_code", statusCode))
if err := m.bus.Publish(events.RequestTraced, &payloads.RequestTraced{
m.bus.Publish(events.RequestTraced, &payloads.RequestTraced{
Request: uri,
DurationMS: int(duration.Milliseconds()),
StatusCode: statusCode,
}); err != nil {
m.log.Error("publishing trace event failed", zap.Error(err))
}
})
},
}
}
Expand Down
7 changes: 2 additions & 5 deletions server/rpc/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,10 @@ func (h *userHandler) FollowUser(ctx context.Context, req *connect.Request[v1.Fo
return nil, connect.NewError(connect.CodeInternal, nil)
}

if err := h.bus.Publish(events.UserFollowed, &payloads.UserFollowed{
h.bus.Publish(events.UserFollowed, &payloads.UserFollowed{
FollowerID: userID,
FolloweeID: req.Msg.GetFollowId(),
}); err != nil {
log.Error("failed to publish user followed event", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
}
})

return &connect.Response[v1.FollowUserResponse]{
Msg: &v1.FollowUserResponse{},
Expand Down
7 changes: 2 additions & 5 deletions server/rpc/v1/workout.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,9 @@ func (h *workoutHandler) PostComment(ctx context.Context, req *connect.Request[v
return nil, connect.NewError(connect.CodeInternal, nil)
}

if err = h.bus.Publish(events.WorkoutCommentPosted, &payloads.WorkoutCommentPosted{
h.bus.Publish(events.WorkoutCommentPosted, &payloads.WorkoutCommentPosted{
CommentID: comment.ID,
}); err != nil {
log.Error("failed to publish event", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
}
})

log.Info("workout comment posted")
return &connect.Response[v1.PostCommentResponse]{
Expand Down

0 comments on commit 919422f

Please sign in to comment.