feat: game lobby service
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// Package metricsintentpub wraps a ports.IntentPublisher with the
|
||||
// `lobby.notification.publish_attempts` counter from
|
||||
// `lobby/README.md` §Observability.
|
||||
package metricsintentpub
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"galaxy/lobby/internal/ports"
|
||||
"galaxy/lobby/internal/telemetry"
|
||||
"galaxy/notificationintent"
|
||||
)
|
||||
|
||||
// Publisher decorates an inner ports.IntentPublisher and increments
|
||||
// `lobby.notification.publish_attempts` after each call.
|
||||
type Publisher struct {
|
||||
inner ports.IntentPublisher
|
||||
telemetry *telemetry.Runtime
|
||||
}
|
||||
|
||||
// New constructs one Publisher around inner. When telemetryRuntime is nil,
|
||||
// the wrapper still delegates Publish but does not record metrics.
|
||||
func New(inner ports.IntentPublisher, telemetryRuntime *telemetry.Runtime) *Publisher {
|
||||
return &Publisher{inner: inner, telemetry: telemetryRuntime}
|
||||
}
|
||||
|
||||
// Publish forwards intent to the inner publisher and records the attempt
|
||||
// outcome under the frozen `result` attribute (`ok`/`error`).
|
||||
func (publisher *Publisher) Publish(ctx context.Context, intent notificationintent.Intent) (string, error) {
|
||||
if publisher == nil || publisher.inner == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
id, err := publisher.inner.Publish(ctx, intent)
|
||||
result := "ok"
|
||||
if err != nil {
|
||||
result = "error"
|
||||
}
|
||||
publisher.telemetry.RecordNotificationPublish(ctx, string(intent.NotificationType), result)
|
||||
return id, err
|
||||
}
|
||||
|
||||
// Compile-time interface assertion.
|
||||
var _ ports.IntentPublisher = (*Publisher)(nil)
|
||||
Reference in New Issue
Block a user