{"id":461469,"date":"2025-05-30T03:00:20","date_gmt":"2025-05-30T03:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=461469"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=461469","title":{"rendered":"<span>Application Layer \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 Go \u2014 \u0437\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d, \u0435\u0441\u043b\u0438 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u044b?<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u0437\u0430\u0447\u0435\u043c Go-\u043f\u0440\u043e\u0435\u043a\u0442\u0443 \u0441\u043b\u043e\u0439 Application \/ Use-Case: \u043a\u0430\u043a \u043e\u043d \u0433\u0435\u0440\u043c\u0435\u0442\u0438\u0447\u043d\u043e \u0438\u0437\u043e\u043b\u0438\u0440\u0443\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 HTTP, gRPC, Cron-\u0434\u0436\u043e\u0431\u0430\u043c\u0438 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044f\u043c\u0438, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0442\u0435\u0441\u0442\u044b \u0438 \u043d\u0435\u0440\u0432\u043d\u044b\u0435 \u043a\u043b\u0435\u0442\u043a\u0438. <\/p>\n<h3>\u0413\u0434\u0435 \u0436\u0438\u0432\u0451\u0442 \u0441\u043b\u043e\u0439 Application?<\/h3>\n<pre><code class=\"bash\">\/internal     \/domain        \/\/ \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438     \/app           \/\/ &lt;-- \u043d\u0430\u0448\u0438 use cases     \/adapters         \/http      \/\/ delivery         \/cron         \/grpc     \/infra         \/postgres  \/\/ \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, external clients         \/redis<\/code><\/pre>\n<p><strong>Domain<\/strong> \u2014 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u0439 \u0446\u0435\u043d\u0442\u0440: \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 + \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b. <strong>App<\/strong> \u2014 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 API \u0434\u043e\u043c\u0435\u043d\u0430: orchestrates <em>\u0447\u0442\u043e<\/em> \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0442 <em>\u043a\u0430\u043a<\/em>. <strong>Adapters<\/strong> \u2014 \u043f\u043e\u0440\u0442\u044b, \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u044e\u0442\u0441\u044f App. <strong>Infra<\/strong> \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432, \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430\u044f \u043e\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\/SDK.<\/p>\n<h3>\u041a\u0435\u0439\u0441: \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0437\u0430\u043a\u0430\u0437<\/h3>\n<h4>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (\/domain\/order.go)<\/h4>\n<pre><code class=\"go\">type OrderStatus string  const (     StatusNew  OrderStatus = \"new\"     StatusPaid OrderStatus = \"paid\" )  type Order struct {     ID         uuid.UUID     UserID     uuid.UUID     Items      []Item     Status     OrderStatus     CreatedAt  time.Time }  func (o *Order) Total() money.Money {     var sum int64     for _, it := range o.Items {         sum += int64(it.Qty) * it.Price     }     return money.FromMinor(sum, \"RUB\") }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c <strong>\u043d\u0435\u0442<\/strong> \u043d\u0438 SQL-\u0442\u0435\u0433\u043e\u0432, \u043d\u0438 JSON-\u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u2014 \u0434\u043e\u043c\u0435\u043d \u0432\u044b\u0448\u0435 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u0430 \u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430.<\/p>\n<h4>\u041f\u043e\u0440\u0442-\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 (\/domain\/order_repository.go)<\/h4>\n<pre><code class=\"go\">type OrderRepository interface {     Save(ctx context.Context, o Order) error     WithTx(ctx context.Context, fn func(repo OrderRepository) error) error }<\/code><\/pre>\n<p>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0430 <code>WithTx<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 use-case \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439, \u043d\u0435 \u0437\u043d\u0430\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0411\u0414\u0448\u043a\u0438.<\/p>\n<h4>Use Case: CreateOrder<\/h4>\n<pre><code class=\"go\">\/\/ \/app\/create_order.go type CreateOrder struct {     repo OrderRepository     pay  PaymentGateway     log  *zap.Logger }  func NewCreateOrder(r OrderRepository, p PaymentGateway, l *zap.Logger) CreateOrder {     return CreateOrder{repo: r, pay: p, log: l} }  type Input struct {     UserID uuid.UUID     Items  []ItemDTO }  func (uc CreateOrder) Execute(ctx context.Context, in Input) (uuid.UUID, error) {     order := NewOrder(in.UserID, in.Items) \/\/ \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0432 domain     if err := uc.repo.WithTx(ctx, func(r OrderRepository) error {         if err := r.Save(ctx, order); err != nil {             return err         }         return uc.pay.Reserve(ctx, order.ID, order.Total())     }); err != nil {         return uuid.Nil, fmt.Errorf(\"create order: %w\", err)     }     uc.log.Info(\"order created\", zap.String(\"id\", order.ID.String()))     return order.ID, nil }<\/code><\/pre>\n<p><em>\u0411\u0438\u0437\u043d\u0435\u0441-\u0440\u0435\u0448\u0435\u043d\u0438\u044f<\/em> \u0442\u0443\u0442 \u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b: \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f, \u0432\u044b\u0437\u043e\u0432 \u043f\u043b\u0430\u0442\u0435\u0436\u043a\u0438, \u043b\u043e\u0433. \u041d\u0438 JSON, \u043d\u0438 <code>http.Request<\/code>.<\/p>\n<h4>HTTP-\u0430\u0434\u0430\u043f\u0442\u0435\u0440 (\/adapters\/http\/order_handler.go)<\/h4>\n<pre><code class=\"go\">func (h Handler) createOrder(w http.ResponseWriter, r *http.Request) {     var dto struct { Items []ItemDTO `json:\"items\"` }     if err := json.NewDecoder(r.Body).Decode(&amp;dto); err != nil {         respondErr(w, http.StatusBadRequest, err)         return     }      id, err := h.uc.Execute(r.Context(), app.Input{         UserID: userIDFromCtx(r.Context()),         Items:  dto.Items,     })     if err != nil {         respondErr(w, statusFromErr(err), err)         return     }     respondJSON(w, http.StatusCreated, map[string]string{\"id\": id.String()}) }<\/code><\/pre>\n<p>90 % \u043a\u043e\u0434\u0430 \u2014 \u043c\u0430\u0440\u0448\u0430\u043b\u043b\u0438\u043d\u0433 \/ \u0441\u0442\u0430\u0442\u0443\u0441-\u043a\u043e\u0434\u044b. <strong>\u0411\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u043d\u0435\u0442\u0440\u043e\u043d\u0443\u0442\u044b\u043c\u0438<\/strong>.<\/p>\n<h3>\u0414\u0435\u0442\u0430\u043b\u0438<\/h3>\n<h4>Dependency Injection \u0431\u0435\u0437 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430<\/h4>\n<p>\u0412 <code>cmd\/service\/main.go<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"go\">repo := postgres.NewOrderRepo(db) pay  := stripe.NewGateway(httpClient) uc   := app.NewCreateOrder(repo, pay, logger) h    := http.NewHandler(uc)  srv := &amp;http.Server{Handler: h, Addr: cfg.HTTPAddr} log.Fatal(srv.ListenAndServe()) <\/code><\/pre>\n<p>\u041d\u0435 \u043d\u0443\u0436\u0435\u043d Uber FX \u0438\u043b\u0438 Wire, \u043d\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0440\u0430\u0441\u0442\u0451\u0442 \u2014 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u0432, \u0430 use-case \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c.<\/p>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c use-case \u0437\u0430 5 \u043c\u0441<\/h4>\n<pre><code class=\"go\">func TestExecute_Success(t *testing.T) {     repo := mocks.NewOrderRepo(t)     repo.On(\"WithTx\", mock.Anything, mock.Anything).         Run(txFuncSuccess(repo)).Return(nil)     pay := mocks.NewPaymentGateway(t)     pay.On(\"Reserve\", mock.Anything, mock.AnythingOfType(\"uuid.UUID\"), mock.Anything).         Return(nil)      uc := app.NewCreateOrder(repo, pay, zaptest.NewLogger(t))     _, err := uc.Execute(context.Background(), input())     assert.NoError(t, err) }<\/code><\/pre>\n<p>\u041c\u043e\u043a\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u2014 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d Postgres \u0438\u043b\u0438 Stripe. \u042e\u043d\u0438\u0442-\u0442\u0435\u0441\u0442 \u0431\u0435\u0436\u0438\u0442 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e, \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441-\u0432\u0435\u0442\u043a\u0438. <\/p>\n<h4>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u043e\u0441\u0442\u044c<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043a\u0430\u0437-\u0440\u0435\u043d\u044c\u044e\u0435\u0440: \u043a\u0430\u0436\u0434\u044b\u0435 24 \u0447 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0435\u043e\u043f\u043b\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043a\u0430\u0437\u044b \u0438 \u043e\u0442\u043c\u0435\u043d\u044f\u0442\u044c. \u041f\u0438\u0448\u0435\u043c:<\/p>\n<pre><code class=\"go\">func RenewExpired(ctx context.Context, uc app.CancelExpired) {     if err := uc.Execute(ctx); err != nil {         log.Error(\"cancel job\", err)     } }<\/code><\/pre>\n<p>\u0421\u043e\u0431\u0440\u0430\u043b\u0438 \u043f\u0430\u043a\u0435\u0442 <code>\/adapters\/cron<\/code>, \u0432\u044b\u0442\u0430\u0449\u0438\u043b\u0438 \u0442\u043e\u0442 \u0436\u0435 use-case \u2014 0 \u0441\u0442\u0440\u043e\u043a \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438.<\/p>\n<h3>\u041a\u043e\u043d\u0442\u0440\u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442<\/h3>\n<p>\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u0443\u043f\u0440\u0451\u043a \u2014 \u00ab\u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u0441\u043b\u043e\u0439, \u043b\u0438\u0448\u043d\u0438\u0439 \u043a\u043e\u0434\u00bb. \u041d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u044d\u0442\u043e 20\u201340 \u0441\u0442\u0440\u043e\u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043f\u0430\u0441\u0430\u044e\u0442, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u0432\u0443\u0445 \u0432\u0445\u043e\u0434\u043e\u0432 (HTTP, Cron, Kafka). \u0412\u043c\u0435\u0441\u0442\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043a\u0438 \u2014 \u0435\u0434\u0438\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u043e\u0440\u043a\u0435\u0441\u0442\u0440\u0430\u0446\u0438\u0438. \u041e\u0434\u0438\u043d \u0440\u0430\u0437 \u043d\u0430\u043f\u0438\u0441\u0430\u043b use-case \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0448\u044c \u0432 \u0442\u0440\u0451\u0445 \u043c\u0435\u0441\u0442\u0430\u0445.<\/p>\n<p>\u041f\u0440\u043e \u043f\u0435\u0440\u0444\u043e\u0440\u043c\u0430\u043d\u0441: \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0432 Go \u2014 \u044d\u0442\u043e 15\u201320 \u043d\u0430\u043d\u043e\u0441\u0435\u043a\u0443\u043d\u0434. \u0414\u0430\u0436\u0435 \u0441\u0430\u043c\u0430\u044f \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f (\u0442\u043e\u0442 \u0436\u0435, JSON, protobuf) \u2014 \u044d\u0442\u043e \u0442\u044b\u0441\u044f\u0447\u0438 \u0440\u0430\u0437 \u0434\u043e\u043b\u044c\u0448\u0435. \u0415\u0441\u043b\u0438 bottleneck \u0443 \u0432\u0430\u0441 \u0432 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f\u0445, \u0430 \u043d\u0435 \u0432 IO, \u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u044b \u043d\u0435 \u0442\u0443\u0434\u0430 \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435.<\/p>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/oleg-bunin\/articles\/810155\/\">\u0411\u043e\u0439\u043b\u0435\u0440\u043f\u043b\u0435\u0439\u0442 <\/a>\u043b\u0435\u0433\u043a\u043e \u0433\u0430\u0441\u0438\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439: <code>mockery<\/code>, <code>wire<\/code>, <code>go generate<\/code> \u2014 \u0441 \u043d\u0438\u043c\u0438 \u0432\u0441\u044f \u0440\u0443\u0442\u0438\u043d\u043d\u0430\u044f \u043e\u0431\u0432\u044f\u0437\u043a\u0430 \u043f\u0438\u0448\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u0442\u0441\u044f. <\/p>\n<h3>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043c\u0435\u043b\u043e\u0447\u0451\u0432\u043a\u0430, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0430\u0441 \u043f\u043e\u0445\u0432\u0430\u043b\u044f\u0442<\/h3>\n<p><strong>Observability<\/strong>. \u0412 \u043a\u0430\u0436\u0434\u044b\u0439 use-case \u0438\u043d\u0436\u0435\u043a\u0442\u0438\u0440\u0443\u0439\u0442\u0435 <code>trace.Tracer<\/code> \u0438 \u043c\u0435\u0442\u0440\u0438\u043a\u0438-\u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">type Metrics interface {     IncOrdersCreated()     ObserveLatency(start time.Time, op string) }  type CreateOrder struct {     repo OrderRepository     pay  PaymentGateway     mtr  Metrics     trc  trace.Tracer }  func (uc CreateOrder) Execute(ctx context.Context, in Input) (uuid.UUID, error) {     ctx, span := uc.trc.Start(ctx, \"create_order\")     defer span.End()     defer uc.mtr.ObserveLatency(time.Now(), \"create_order\")      \/\/ \u2026 }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043b\u044e\u0431\u043e\u0439 \u0430\u0434\u0430\u043f\u0442\u0435\u0440 \u0441\u0430\u043c \u0440\u0435\u0448\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u2014 OpenTelemetry, Prometheus, Datadog. Use-case \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p><strong>\u041e\u0448\u0438\u0431\u043a\u0438 \u043a\u0430\u043a \u0441\u0438\u0433\u043d\u0430\u043b\u044b<\/strong>. \u0412 \u0434\u043e\u043c\u0435\u043d\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0439\u0442\u0435 sentinels:<\/p>\n<pre><code class=\"go\">var (     ErrInventory = errors.New(\"no inventory\")     ErrPayment   = errors.New(\"payment declined\") )<\/code><\/pre>\n<p>\u0410 \u0432 use-case \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043c\u0435\u043d\u044f\u044f \u043d\u0430\u0440\u0443\u0436\u0443:<\/p>\n<pre><code class=\"go\">return fmt.Errorf(\"reserve items: %w\", domain.ErrInventory)<\/code><\/pre>\n<p>\u041c\u0430\u043f-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u0437 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 HTTP-\u0441\u0442\u0430\u0442\u0443\u0441\u044b \u043b\u0435\u0436\u0438\u0442 \u0432 <code>adapters\/http<\/code>. gRPC-\u0441\u043b\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0442\u0443 \u0436\u0435 \u043a\u0430\u0440\u0442\u0443, \u043d\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u0432 <code>codes.FailedPrecondition<\/code>.<\/p>\n<p><strong>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c<\/strong>. \u0420\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0439 use-case \u043b\u0435\u0433\u043a\u043e \u2014 \u0432\u043d\u0443\u0442\u0440\u0438 <code>Execute<\/code>:<\/p>\n<pre><code class=\"go\">grp, ctx := errgroup.WithContext(ctx)  grp.Go(func() error {     return uc.repo.Save(ctx, order) }) grp.Go(func() error {     return uc.pay.Reserve(ctx, order.ID, order.Total()) })  if err := grp.Wait(); err != nil {     return uuid.Nil, err }<\/code><\/pre>\n<p>\u041d\u0435\u0442 \u0433\u043e\u043d\u043e\u043a: \u0431\u0438\u0437\u043d\u0435\u0441-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0451 \u0435\u0449\u0451 \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e <code>WithTx<\/code>, \u0430 <code>errgroup<\/code> \u0441\u043d\u0438\u043c\u0430\u0435\u0442 \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0431\u043e\u043b\u044c \u0441 \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438.<\/p>\n<p><strong>Zero-downtime \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438<\/strong>. \u041c\u0435\u043d\u044f\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>Order<\/code>? \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 <code>StatusHistory []StatusTransition<\/code> \u0438 \u043f\u0438\u0448\u0435\u043c \u0434\u043e\u043f. use-case <code>BackfillHistory<\/code>. \u041e\u043d \u0438\u0434\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 Cron-\u0430\u0434\u0430\u043f\u0442\u0435\u0440 \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u044f HTTP-\u043f\u0443\u0442\u044c.<\/p>\n<hr\/>\n<p>\u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u043a\u0435\u0439\u0441\u0430\u043c\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0438 \u0437\u0430\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u0443\u0435\u0442\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0436\u0438\u043b\u0430 \u0434\u043e\u043b\u044c\u0448\u0435 MVP \u2014 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 17 \u0438\u044e\u043d\u044f <strong>\u00ab\u041c\u043e\u043d\u043e\u043b\u0438\u0442 \u0438\u043b\u0438 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u044b?\u00bb<\/strong>. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u043a\u0430\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0434\u043e\u0433\u043c: \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u2014 \u0431\u043b\u0430\u0433\u043e, \u0430 \u043a\u043e\u0433\u0434\u0430 \u2014 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0434\u043e\u043b\u0433, \u043a\u0430\u043a \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u0435\u0440\u044f\u043c\u0438 \u0438 \u0447\u0442\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u043e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430\u0447\u043d\u0451\u0442 \u0440\u0430\u0441\u043f\u043e\u043b\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c. \u0412\u0441\u0451 \u043f\u043e \u0434\u0435\u043b\u0443, \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0438 \u0431\u0435\u0437 \u0435\u0432\u0430\u043d\u0433\u0435\u043b\u0438\u0437\u043c\u0430.<\/p>\n<p>\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/7nfQF\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 &#171;Software Architect&#187;.<\/a><\/p>\n<p><em>\u0410 \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0443\u0440\u0441, <\/em><a href=\"https:\/\/otus.pw\/tp1z\/\"><em>\u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 \u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/em><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/913532\/\"> https:\/\/habr.com\/ru\/articles\/913532\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u0437\u0430\u0447\u0435\u043c Go-\u043f\u0440\u043e\u0435\u043a\u0442\u0443 \u0441\u043b\u043e\u0439 Application \/ Use-Case: \u043a\u0430\u043a \u043e\u043d \u0433\u0435\u0440\u043c\u0435\u0442\u0438\u0447\u043d\u043e \u0438\u0437\u043e\u043b\u0438\u0440\u0443\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 HTTP, gRPC, Cron-\u0434\u0436\u043e\u0431\u0430\u043c\u0438 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044f\u043c\u0438, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0442\u0435\u0441\u0442\u044b \u0438 \u043d\u0435\u0440\u0432\u043d\u044b\u0435 \u043a\u043b\u0435\u0442\u043a\u0438. <\/p>\n<h3>\u0413\u0434\u0435 \u0436\u0438\u0432\u0451\u0442 \u0441\u043b\u043e\u0439 Application?<\/h3>\n<pre><code class=\"bash\">\/internal     \/domain        \/\/ \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438     \/app           \/\/ &lt;-- \u043d\u0430\u0448\u0438 use cases     \/adapters         \/http      \/\/ delivery         \/cron         \/grpc     \/infra         \/postgres  \/\/ \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, external clients         \/redis<\/code><\/pre>\n<p><strong>Domain<\/strong> \u2014 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u0439 \u0446\u0435\u043d\u0442\u0440: \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 + \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b. <strong>App<\/strong> \u2014 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 API \u0434\u043e\u043c\u0435\u043d\u0430: orchestrates <em>\u0447\u0442\u043e<\/em> \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043e\u0442 <em>\u043a\u0430\u043a<\/em>. <strong>Adapters<\/strong> \u2014 \u043f\u043e\u0440\u0442\u044b, \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u044e\u0442\u0441\u044f App. <strong>Infra<\/strong> \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432, \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430\u044f \u043e\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\/SDK.<\/p>\n<h3>\u041a\u0435\u0439\u0441: \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0437\u0430\u043a\u0430\u0437<\/h3>\n<h4>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (\/domain\/order.go)<\/h4>\n<pre><code class=\"go\">type OrderStatus string  const (     StatusNew  OrderStatus = \"new\"     StatusPaid OrderStatus = \"paid\" )  type Order struct {     ID         uuid.UUID     UserID     uuid.UUID     Items      []Item     Status     OrderStatus     CreatedAt  time.Time }  func (o *Order) Total() money.Money {     var sum int64     for _, it := range o.Items {         sum += int64(it.Qty) * it.Price     }     return money.FromMinor(sum, \"RUB\") }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c <strong>\u043d\u0435\u0442<\/strong> \u043d\u0438 SQL-\u0442\u0435\u0433\u043e\u0432, \u043d\u0438 JSON-\u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u2014 \u0434\u043e\u043c\u0435\u043d \u0432\u044b\u0448\u0435 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u0430 \u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430.<\/p>\n<h4>\u041f\u043e\u0440\u0442-\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 (\/domain\/order_repository.go)<\/h4>\n<pre><code class=\"go\">type OrderRepository interface {     Save(ctx context.Context, o Order) error     WithTx(ctx context.Context, fn func(repo OrderRepository) error) error }<\/code><\/pre>\n<p>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0430 <code>WithTx<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 use-case \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439, \u043d\u0435 \u0437\u043d\u0430\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0411\u0414\u0448\u043a\u0438.<\/p>\n<h4>Use Case: CreateOrder<\/h4>\n<pre><code class=\"go\">\/\/ \/app\/create_order.go type CreateOrder struct {     repo OrderRepository     pay  PaymentGateway     log  *zap.Logger }  func NewCreateOrder(r OrderRepository, p PaymentGateway, l *zap.Logger) CreateOrder {     return CreateOrder{repo: r, pay: p, log: l} }  type Input struct {     UserID uuid.UUID     Items  []ItemDTO }  func (uc CreateOrder) Execute(ctx context.Context, in Input) (uuid.UUID, error) {     order := NewOrder(in.UserID, in.Items) \/\/ \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0432 domain     if err := uc.repo.WithTx(ctx, func(r OrderRepository) error {         if err := r.Save(ctx, order); err != nil {             return err         }         return uc.pay.Reserve(ctx, order.ID, order.Total())     }); err != nil {         return uuid.Nil, fmt.Errorf(\"create order: %w\", err)     }     uc.log.Info(\"order created\", zap.String(\"id\", order.ID.String()))     return order.ID, nil }<\/code><\/pre>\n<p><em>\u0411\u0438\u0437\u043d\u0435\u0441-\u0440\u0435\u0448\u0435\u043d\u0438\u044f<\/em> \u0442\u0443\u0442 \u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b: \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f, \u0432\u044b\u0437\u043e\u0432 \u043f\u043b\u0430\u0442\u0435\u0436\u043a\u0438, \u043b\u043e\u0433. \u041d\u0438 JSON, \u043d\u0438 <code>http.Request<\/code>.<\/p>\n<h4>HTTP-\u0430\u0434\u0430\u043f\u0442\u0435\u0440 (\/adapters\/http\/order_handler.go)<\/h4>\n<pre><code class=\"go\">func (h Handler) createOrder(w http.ResponseWriter, r *http.Request) {     var dto struct { Items []ItemDTO `json:\"items\"` }     if err := json.NewDecoder(r.Body).Decode(&amp;dto); err != nil {         respondErr(w, http.StatusBadRequest, err)         return     }      id, err := h.uc.Execute(r.Context(), app.Input{         UserID: userIDFromCtx(r.Context()),         Items:  dto.Items,     })     if err != nil {         respondErr(w, statusFromErr(err), err)         return     }     respondJSON(w, http.StatusCreated, map[string]string{\"id\": id.String()}) }<\/code><\/pre>\n<p>90 % \u043a\u043e\u0434\u0430 \u2014 \u043c\u0430\u0440\u0448\u0430\u043b\u043b\u0438\u043d\u0433 \/ \u0441\u0442\u0430\u0442\u0443\u0441-\u043a\u043e\u0434\u044b. <strong>\u0411\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u043d\u0435\u0442\u0440\u043e\u043d\u0443\u0442\u044b\u043c\u0438<\/strong>.<\/p>\n<h3>\u0414\u0435\u0442\u0430\u043b\u0438<\/h3>\n<h4>Dependency Injection \u0431\u0435\u0437 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430<\/h4>\n<p>\u0412 <code>cmd\/service\/main.go<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"go\">repo := postgres.NewOrderRepo(db) pay  := stripe.NewGateway(httpClient) uc   := app.NewCreateOrder(repo, pay, logger) h    := http.NewHandler(uc)  srv := &amp;http.Server{Handler: h, Addr: cfg.HTTPAddr} log.Fatal(srv.ListenAndServe()) <\/code><\/pre>\n<p>\u041d\u0435 \u043d\u0443\u0436\u0435\u043d Uber FX \u0438\u043b\u0438 Wire, \u043d\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0440\u0430\u0441\u0442\u0451\u0442 \u2014 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u0432, \u0430 use-case \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c.<\/p>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c use-case \u0437\u0430 5 \u043c\u0441<\/h4>\n<pre><code class=\"go\">func TestExecute_Success(t *testing.T) {     repo := mocks.NewOrderRepo(t)     repo.On(\"WithTx\", mock.Anything, mock.Anything).         Run(txFuncSuccess(repo)).Return(nil)     pay := mocks.NewPaymentGateway(t)     pay.On(\"Reserve\", mock.Anything, mock.AnythingOfType(\"uuid.UUID\"), mock.Anything).         Return(nil)      uc := app.NewCreateOrder(repo, pay, zaptest.NewLogger(t))     _, err := uc.Execute(context.Background(), input())     assert.NoError(t, err) }<\/code><\/pre>\n<p>\u041c\u043e\u043a\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u2014 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d Postgres \u0438\u043b\u0438 Stripe. \u042e\u043d\u0438\u0442-\u0442\u0435\u0441\u0442 \u0431\u0435\u0436\u0438\u0442 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e, \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441-\u0432\u0435\u0442\u043a\u0438. <\/p>\n<h4>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u043e\u0441\u0442\u044c<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043a\u0430\u0437-\u0440\u0435\u043d\u044c\u044e\u0435\u0440: \u043a\u0430\u0436\u0434\u044b\u0435 24 \u0447 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0435\u043e\u043f\u043b\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043a\u0430\u0437\u044b \u0438 \u043e\u0442\u043c\u0435\u043d\u044f\u0442\u044c. \u041f\u0438\u0448\u0435\u043c:<\/p>\n<pre><code class=\"go\">func RenewExpired(ctx context.Context, uc app.CancelExpired) {     if err := uc.Execute(ctx); err != nil {         log.Error(\"cancel job\", err)     } }<\/code><\/pre>\n<p>\u0421\u043e\u0431\u0440\u0430\u043b\u0438 \u043f\u0430\u043a\u0435\u0442 <code>\/adapters\/cron<\/code>, \u0432\u044b\u0442\u0430\u0449\u0438\u043b\u0438 \u0442\u043e\u0442 \u0436\u0435 use-case \u2014 0 \u0441\u0442\u0440\u043e\u043a \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438.<\/p>\n<h3>\u041a\u043e\u043d\u0442\u0440\u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442<\/h3>\n<p>\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u0443\u043f\u0440\u0451\u043a \u2014 \u00ab\u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u0441\u043b\u043e\u0439, \u043b\u0438\u0448\u043d\u0438\u0439 \u043a\u043e\u0434\u00bb. \u041d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u044d\u0442\u043e 20\u201340 \u0441\u0442\u0440\u043e\u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043f\u0430\u0441\u0430\u044e\u0442, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u0432\u0443\u0445 \u0432\u0445\u043e\u0434\u043e\u0432 (HTTP, Cron, Kafka). \u0412\u043c\u0435\u0441\u0442\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043a\u0438 \u2014 \u0435\u0434\u0438\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u043e\u0440\u043a\u0435\u0441\u0442\u0440\u0430\u0446\u0438\u0438. \u041e\u0434\u0438\u043d \u0440\u0430\u0437 \u043d\u0430\u043f\u0438\u0441\u0430\u043b use-case \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0448\u044c \u0432 \u0442\u0440\u0451\u0445 \u043c\u0435\u0441\u0442\u0430\u0445.<\/p>\n<p>\u041f\u0440\u043e \u043f\u0435\u0440\u0444\u043e\u0440\u043c\u0430\u043d\u0441: \u0432\u044b\u0437\u043e\u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0432 Go \u2014 \u044d\u0442\u043e 15\u201320 \u043d\u0430\u043d\u043e\u0441\u0435\u043a\u0443\u043d\u0434. \u0414\u0430\u0436\u0435 \u0441\u0430\u043c\u0430\u044f \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f (\u0442\u043e\u0442 \u0436\u0435, JSON, protobuf) \u2014 \u044d\u0442\u043e \u0442\u044b\u0441\u044f\u0447\u0438 \u0440\u0430\u0437 \u0434\u043e\u043b\u044c\u0448\u0435. \u0415\u0441\u043b\u0438 bottleneck \u0443 \u0432\u0430\u0441 \u0432 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f\u0445, \u0430 \u043d\u0435 \u0432 IO, \u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u044b \u043d\u0435 \u0442\u0443\u0434\u0430 \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435.<\/p>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/oleg-bunin\/articles\/810155\/\">\u0411\u043e\u0439\u043b\u0435\u0440\u043f\u043b\u0435\u0439\u0442 <\/a>\u043b\u0435\u0433\u043a\u043e \u0433\u0430\u0441\u0438\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439: <code>mockery<\/code>, <code>wire<\/code>, <code>go generate<\/code> \u2014 \u0441 \u043d\u0438\u043c\u0438 \u0432\u0441\u044f \u0440\u0443\u0442\u0438\u043d\u043d\u0430\u044f \u043e\u0431\u0432\u044f\u0437\u043a\u0430 \u043f\u0438\u0448\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u0442\u0441\u044f. <\/p>\n<h3>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043c\u0435\u043b\u043e\u0447\u0451\u0432\u043a\u0430, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0430\u0441 \u043f\u043e\u0445\u0432\u0430\u043b\u044f\u0442<\/h3>\n<p><strong>Observability<\/strong>. \u0412 \u043a\u0430\u0436\u0434\u044b\u0439 use-case \u0438\u043d\u0436\u0435\u043a\u0442\u0438\u0440\u0443\u0439\u0442\u0435 <code>trace.Tracer<\/code> \u0438 \u043c\u0435\u0442\u0440\u0438\u043a\u0438-\u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">type Metrics interface {     IncOrdersCreated()     ObserveLatency(start time.Time, op string) }  type CreateOrder struct {     repo OrderRepository     pay  PaymentGateway     mtr  Metrics     trc  trace.Tracer }  func (uc CreateOrder) Execute(ctx context.Context, in Input) (uuid.UUID, error) {     ctx, span := uc.trc.Start(ctx, \"create_order\")     defer span.End()     defer uc.mtr.ObserveLatency(time.Now(), \"create_order\")      \/\/ \u2026 }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043b\u044e\u0431\u043e\u0439 \u0430\u0434\u0430\u043f\u0442\u0435\u0440 \u0441\u0430\u043c \u0440\u0435\u0448\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u2014 OpenTelemetry, Prometheus, Datadog. Use-case \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p><strong>\u041e\u0448\u0438\u0431\u043a\u0438 \u043a\u0430\u043a \u0441\u0438\u0433\u043d\u0430\u043b\u044b<\/strong>. \u0412 \u0434\u043e\u043c\u0435\u043d\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0439\u0442\u0435 sentinels:<\/p>\n<pre><code class=\"go\">var (     ErrInventory = errors.New(\"no inventory\")     ErrPayment   = errors.New(\"payment declined\") )<\/code><\/pre>\n<p>\u0410 \u0432 use-case \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043c\u0435\u043d\u044f\u044f \u043d\u0430\u0440\u0443\u0436\u0443:<\/p>\n<pre><code class=\"go\">return fmt.Errorf(\"reserve items: %w\", domain.ErrInventory)<\/code><\/pre>\n<p>\u041c\u0430\u043f-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u0437 \u043e\u0448\u0438\u0431\u043e\u043a \u0432 HTTP-\u0441\u0442\u0430\u0442\u0443\u0441\u044b \u043b\u0435\u0436\u0438\u0442 \u0432 <code>adapters\/http<\/code>. gRPC-\u0441\u043b\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0442\u0443 \u0436\u0435 \u043a\u0430\u0440\u0442\u0443, \u043d\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u0432 <code>codes.FailedPrecondition<\/code>.<\/p>\n<p><strong>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c<\/strong>. \u0420\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0439 use-case \u043b\u0435\u0433\u043a\u043e \u2014 \u0432\u043d\u0443\u0442\u0440\u0438 <code>Execute<\/code>:<\/p>\n<pre><code class=\"go\">grp, ctx := errgroup.WithContext(ctx)  grp.Go(func() error {     return uc.repo.Save(ctx, order) }) grp.Go(func() error {     return uc.pay.Reserve(ctx, order.ID, order.Total()) })  if err := grp.Wait(); err != nil {     return uuid.Nil, err }<\/code><\/pre>\n<p>\u041d\u0435\u0442 \u0433\u043e\u043d\u043e\u043a: \u0431\u0438\u0437\u043d\u0435\u0441-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0451 \u0435\u0449\u0451 \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e <code>WithTx<\/code>, \u0430 <code>errgroup<\/code> \u0441\u043d\u0438\u043c\u0430\u0435\u0442 \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0431\u043e\u043b\u044c \u0441 \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438.<\/p>\n<p><strong>Zero-downtime \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438<\/strong>. \u041c\u0435\u043d\u044f\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>Order<\/code>? \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 <code>StatusHistory []StatusTransition<\/code> \u0438 \u043f\u0438\u0448\u0435\u043c \u0434\u043e\u043f. use-case <code>BackfillHistory<\/code>. \u041e\u043d \u0438\u0434\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 Cron-\u0430\u0434\u0430\u043f\u0442\u0435\u0440 \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u044f HTTP-\u043f\u0443\u0442\u044c.<\/p>\n<hr\/>\n<p>\u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u043a\u0435\u0439\u0441\u0430\u043c\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0438 \u0437\u0430\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u0443\u0435\u0442\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0436\u0438\u043b\u0430 \u0434\u043e\u043b\u044c\u0448\u0435 MVP \u2014 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 17 \u0438\u044e\u043d\u044f <strong>\u00ab\u041c\u043e\u043d\u043e\u043b\u0438\u0442 \u0438\u043b\u0438 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u044b?\u00bb<\/strong>. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u043a\u0430\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0434\u043e\u0433\u043c: \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u2014 \u0431\u043b\u0430\u0433\u043e, \u0430 \u043a\u043e\u0433\u0434\u0430 \u2014 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0434\u043e\u043b\u0433, \u043a\u0430\u043a \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u0435\u0440\u044f\u043c\u0438 \u0438 \u0447\u0442\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u043e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430\u0447\u043d\u0451\u0442 \u0440\u0430\u0441\u043f\u043e\u043b\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c. \u0412\u0441\u0451 \u043f\u043e \u0434\u0435\u043b\u0443, \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0438 \u0431\u0435\u0437 \u0435\u0432\u0430\u043d\u0433\u0435\u043b\u0438\u0437\u043c\u0430.<\/p>\n<p>\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/7nfQF\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 &#171;Software Architect&#187;.<\/a><\/p>\n<p><em>\u0410 \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0443\u0440\u0441, <\/em><a href=\"https:\/\/otus.pw\/tp1z\/\"><em>\u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 \u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/em><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/913532\/\"> https:\/\/habr.com\/ru\/articles\/913532\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-461469","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/461469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=461469"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/461469\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=461469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=461469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=461469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}