Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package processing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"messenger/internal/broker"
|
||||
"messenger/internal/pipeline"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/telemetry"
|
||||
"messenger/internal/platform/tracectx"
|
||||
)
|
||||
|
||||
func retryPolicy(cfg config.Jobs) pipeline.RetryPolicy {
|
||||
return pipeline.RetryPolicy{MaxAttempts: cfg.MaxAttempts, BaseDelay: time.Second, MaxDelay: time.Minute}
|
||||
}
|
||||
|
||||
func pause(ctx context.Context, d time.Duration) {
|
||||
timer := time.NewTimer(d)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
|
||||
func logRetry(ctx context.Context, log *slog.Logger, role string, err error) {
|
||||
reason := "processing_error"
|
||||
var brokerCode broker.FailureCode
|
||||
if errors.As(err, &brokerCode) {
|
||||
reason = string(brokerCode)
|
||||
}
|
||||
log.WarnContext(ctx, "attachment session will retry", "role", role, "error_code", reason)
|
||||
telemetry.Event(ctx, "attachment.retry", attribute.String("outcome", role), attribute.String("error_code", reason))
|
||||
}
|
||||
|
||||
func Dispatch(ctx context.Context, log *slog.Logger, store *pipeline.Store, cfg config.Jobs, barrier func(context.Context, pipeline.Job, string) error) {
|
||||
for ctx.Err() == nil {
|
||||
err := dispatchSession(ctx, store, cfg, barrier)
|
||||
if ctx.Err() == nil {
|
||||
logRetry(ctx, log, "dispatcher", err)
|
||||
pause(ctx, time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func dispatchSession(ctx context.Context, store *pipeline.Store, cfg config.Jobs, barrier func(context.Context, pipeline.Job, string) error) error {
|
||||
var publisher *broker.Publisher
|
||||
publish := func(ctx context.Context, j pipeline.Job) (publishErr error) {
|
||||
ctx = tracectx.Restore(ctx, j.Traceparent, j.Tracestate)
|
||||
ctx, span := otel.Tracer("messenger/processing").Start(ctx, "attachment.publish", trace.WithSpanKind(trace.SpanKindProducer))
|
||||
defer func() {
|
||||
outcome := "published"
|
||||
if publishErr != nil {
|
||||
outcome = "error"
|
||||
span.SetStatus(codes.Error, "publish failed")
|
||||
}
|
||||
telemetry.Event(ctx, "attachment.publish", attribute.String("outcome", outcome), attribute.Int("attempt", j.Attempt))
|
||||
span.End()
|
||||
}()
|
||||
j.Traceparent, j.Tracestate = tracectx.Fields(ctx)
|
||||
body, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := publisher.Publish(ctx, body); err != nil {
|
||||
return err
|
||||
}
|
||||
if barrier != nil {
|
||||
return barrier(ctx, j, "published")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for ctx.Err() == nil {
|
||||
if _, err := store.RecoverOne(ctx, retryPolicy(cfg)); err != nil {
|
||||
return err
|
||||
}
|
||||
if publisher == nil {
|
||||
var err error
|
||||
publisher, err = broker.NewPublisher(ctx, cfg.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer publisher.Close()
|
||||
}
|
||||
sent, err := store.PublishOne(ctx, publish)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !sent {
|
||||
pause(ctx, 250*time.Millisecond)
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user