{"id":397265,"date":"2024-06-29T13:20:50","date_gmt":"2024-06-29T13:20:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=397265"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=397265","title":{"rendered":"<span>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0432 Go \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/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>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u042f \u0418\u043b\u044c\u044f \u0421\u0435\u0440\u0433\u0443\u043d\u0438\u043d, \u0432\u0435\u0431-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438\u0437 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0410\u0432\u0438\u0442\u043e. \u041c\u044b \u043f\u0438\u0448\u0435\u043c \u043d\u0430 Go \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0432\u044b\u043a\u0443\u043f\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432, \u043f\u0440\u043e MLP \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0436\u0435 \u043f\u0438\u0441\u0430\u043b\u0438 \u0432 <a href=\"https:\/\/habr.com\/ru\/companies\/avito\/articles\/700784\/\">\u0431\u043b\u043e\u0433\u0435<\/a>. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435\u043c \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442-\u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. <\/p>\n<p><strong>\u0412\u0430\u0436\u043d\u043e\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435: <\/strong>\u0432 Go \u043d\u0443\u0436\u043d\u043e \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u044f \u0441\u043a\u0440\u044b\u0432\u0430\u044e \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043b\u0435\u0433\u0447\u0435 \u0447\u0438\u0442\u0430\u043b\u0438\u0441\u044c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6fa\/515\/289\/6fa5152890b15a65b22b17e8b13eb959.png\" width=\"1560\" height=\"881\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6fa\/515\/289\/6fa5152890b15a65b22b17e8b13eb959.png\"\/><\/figure>\n<h3>\u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/h3>\n<p>\u041f\u0430\u0442\u0442\u0435\u0440\u043d Repository \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d \u041c\u0430\u0440\u0442\u0438\u043d\u043e\u043c \u0424\u0430\u0443\u043b\u0435\u0440\u043e\u043c \u0432 \u043a\u043d\u0438\u0433\u0435 \u00ab\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439\u00bb. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u042d\u0440\u0438\u043a \u042d\u0432\u0430\u043d\u0441 \u0432 \u043a\u043d\u0438\u0433\u0435 \u00ab\u041f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (DDD). \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u00bb. <\/p>\n<p>Repository \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0430 \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u041a\u0430\u0436\u0434\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0434\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e. Repository \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b. <\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0443 \u043d\u0435\u0433\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438: \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8d6\/331\/2cd\/8d63312cd6ed1f1227fcb4d50ebd75f9.png\" width=\"1434\" height=\"678\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8d6\/331\/2cd\/8d63312cd6ed1f1227fcb4d50ebd75f9.png\"\/><\/figure>\n<p>User \u043c\u043e\u0436\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432 \u0441\u0435\u0431\u044f \u043d\u0438\u043a\u043d\u0435\u0439\u043c, \u043f\u0430\u0440\u043e\u043b\u044c, \u0438\u043c\u044f, \u0444\u0430\u043c\u0438\u043b\u0438\u044e, \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439 \u0438 \u0442.\u0434. \u0412\u0441\u0435 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u0440\u0430\u0437\u043d\u043e\u043c \u0432\u0438\u0434\u0435. <\/p>\n<p>\u0418\u043c\u044f \u0438 \u0444\u0430\u043c\u0438\u043b\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u044b \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0435\u0441\u043b\u0438 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0434\u0438 \u0438 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u0432\u0438\u0434\u0435 json-\u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0418\u0445 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. Repository \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438, \u0430 \u043d\u0435 \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u044b:<\/p>\n<pre><code class=\"go\">\/\/ \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 func (r *userRepo) GetByID(id domain.UserID) (*domain.User, error) { query := `SELECT * FROM \"user\" WHERE id = ?;` uRow := userRow{}  err := r.db.Get(&amp;uRow, r.db.Rebind(query), id)  return r.toModel(uRow), nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 func (r *userRepo) Save(u *domain.User) error { query := `INSERT INTO \"user\" (username, password, notification) VALUES (:username, :password, :notification) ON CONFLICT (id)     DO UPDATE SET excluded.username     = :username,                   excluded.password     = :password,                   excluded.notification = :notification RETURNING id;`  rows, err := r.db.Query(query, r.toRow(u))  defer rows.Close() if !rows.Next() { return rows.Err() }  err = rows.Scan(&amp;u.ID)  return err }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0430\u0445  <code>toModel<\/code> \u0438 <code>toRow<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e.<\/p>\n<p>\u041c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u043b\u044e\u0431\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445 \u0438 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0430\u0445 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u043e\u0441\u0442\u0430\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c.\u00a0<\/p>\n<h3>\u0428\u0430\u0433 1 \u2014 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u00a0<\/h3>\n<p>\u041a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c:<\/p>\n<pre><code class=\"go\">func (u *Register) Handle(in In) (user *domain.User, err error) { user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(user)  return user, nil }<\/code><\/pre>\n<p>\u041c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"go\">func (u *Register) Handle(in In) (user *domain.User, err error) { user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(user)      \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414 err = u.queue.Publish(domain.Registered{ID: user.ID})  return user, nil }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c, \u0447\u0442\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0441\u0431\u043e\u0439, \u0438 \u0442\u043e\u0433\u0434\u0430 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0435 \u043e \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043d\u0435 \u0434\u043e\u0439\u0434\u0435\u0442 \u0434\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438 \u044f\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0435\u0451 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c:<\/p>\n<pre><code class=\"go\">func (u *Register) Handle(in In) (user *domain.User, err error) {     \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414     tr, err = u.db.Beginx()  user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(tr, user) err = u.queue.Publish(tr, domain.Registered{ID: user.ID})      \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414 err = tr.Commit() \/\/ tr.Rollback()  return user, nil }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0442\u0442\u0435\u0440\u043d transaction outbox, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0418 \u0442\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0437\u043d\u0430\u043b \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"go\">\/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 func (r *userRepo) Save(tr domain.Tr, u *domain.User) error { query := `INSERT INTO \"user\" ...;`      \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414 if tr != nil { tr = r.db }  rows, err := sqlx.NamedQuery(tr, query, r.toRow(u))  defer rows.Close() if !rows.Next() { return rows.Err() }  err = rows.Scan(&amp;u.ID)  return err }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043d\u043e \u0432\u0437\u0430\u043c\u0435\u043d \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f:\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ac2\/c46\/ad9\/ac2c46ad9bf06cb875ce9ead53e584d3.png\" alt=\"\" title=\"\" width=\"1524\" height=\"660\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ac2\/c46\/ad9\/ac2c46ad9bf06cb875ce9ead53e584d3.png\"\/><\/figure>\n<h3>\u0428\u0430\u0433 2 \u2014 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0437\u0430\u043a\u0430\u0437 \u0438 \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u0432 \u043e\u0434\u0438\u043d \u043a\u043b\u0438\u043a \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u00a0<\/h3>\n<p>\u0423 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043a\u0430\u0437. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0432\u0442\u043e\u0440\u043e\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4fa\/4a0\/7e8\/4fa4a07e829cf45b4d954eec5d29a13b.png\" width=\"1044\" height=\"512\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4fa\/4a0\/7e8\/4fa4a07e829cf45b4d954eec5d29a13b.png\"\/><\/figure>\n<p>\u041f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0437\u0430\u043a\u0430\u0437\u0430 \u0438 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"go\">func (u *Purchase) Handle(tx *sqlx.Tx, in In) (order *domain.Order, err error) {     tr, err = u.db.Beginx();  order, err = domain.NewOrder(in.UserID, in.ProductID, in.Quantity)  err = u.orderRepo.Save(tr, order)  u.queue.Publish(tr, domain.Purchased{ID: order.ID})  err = tr.Commit() \/\/ tr.Rollback()  return order, nil }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0434\u0430\u0436 \u0434\u0430\u0434\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"go\">func (u *FastPurchase) Handle(in In) (out Out, err error) { out.User, err = u.register.Handle(tx, register.In{ Username: in.Register.Username, })  out.Order, err = u.purchase.Handle(tx, purchase.In{ UserID:    out.User.ID, ProductID: in.Purchase.ProductID, Quantity:  in.Purchase.Quantity, })  return out, nil }<\/code><\/pre>\n<p>Register \u0438 Purchase \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u043c\u0438 \u0432 FastPurchase. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"go\">func (u *FastPurchase) Handle(in In) (out Out, err error) { tx, err := u.db.Beginx()  out.User, err = u.register.Handle(tx, register.In{ Username: in.Register.Username, })  out.Order, err = u.purchase.Handle(tx, purchase.In{ UserID:    out.User.ID, ProductID: in.Purchase.ProductID, Quantity:  in.Purchase.Quantity, })  err = tx.Commit() \/\/ tr.Rollback()  return out, nil }<\/code><\/pre>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0432 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043f\u043e\u043a\u0443\u043f\u043a\u0438 \u0442\u043e\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0435\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0414\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0438\u0445, \u0447\u0442\u043e\u0431\u044b \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0441 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u043c\u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e.\u00a0<\/p>\n<p>\u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e func (u *Register) Handle(in In) (user *domain.User, err error) {     tr, err = u.db.Beginx()  user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(tr, user) err = u.queue.Publish(tr, domain.Registered{ID: user.ID})  err = tr.Commit() \/\/ tr.Rollback()  return user, nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) { hasExternalTx := true tr := domain.Tr(tx) if tr == nil { tr, err = u.db.Beginx() hasExternalTx = false }  defer func() { if !hasExternalTx &amp;&amp; err != nil { tx.Rollback() } }()  user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(tr, user); err != nil { err = u.queue.Publish(tr, domain.Registered{ID: user.ID})  if !hasExternalTx { return nil, tx.Commit() }  return user, nil }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c: \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438. \u041d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0435\u0449\u0451 \u0438 \u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<h3>\u0428\u0430\u0433 3 \u2014 \u0437\u0430\u043c\u044b\u043a\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:\u00a0<\/p>\n<pre><code class=\"go\">func (r *userRepo) FastOrder(fn func() (*User, *Order, error)) error {    tr, err := r.db.Begin()     user, order, err := fn() \/\/ \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f     err = r.Save(tr, user)    err = r.orderRepo.Save(tr, order)     err = tr.Commit() \/\/ \u0438\u043b\u0438 tr.Rollback()     return nil }<\/code><\/pre>\n<p>\u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0437\u043d\u0430\u0435\u0442 \u043e \u0437\u0430\u043a\u0430\u0437\u0430\u0445. \u041d\u0430\u043c \u044d\u0442\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e: \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u043f\u0440\u043e\u0434\u0443\u043a\u0442 \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442\u0441\u044f \u0438 \u0440\u0430\u0441\u0442\u0451\u0442, \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435. \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u0442\u043e\u0436\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0442\u0441\u044f \u0438 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u044e\u0442\u0441\u044f, \u0438 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443\u0437\u043d\u0430\u0451\u0442 \u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u044f\u0445.  \u0417\u043d\u0430\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c \u0438 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043e\u043d \u043f\u043e\u0442\u0435\u0440\u044f\u0435\u0442 \u0441\u0432\u043e\u044e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u2014 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.\u00a0<\/p>\n<p>\u0420\u0435\u0448\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u043e: \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c. \u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e WithTransaction:\u00a0<\/p>\n<pre><code class=\"go\">func WithTransaction(tx *sqlx.Tx, fn func(*sqlx.Tx) error) error {    hasExternalTx := true    if tx == nil {        tx, err = DB.Begin()        hasExternalTx = false    }     err := fn(tx) \/\/ \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f     if !hasExternalTx {        err = tx.Commit() \/\/ tx.Rollback()    }     return nil }<\/code><\/pre>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0430 \u0432\u0438\u0434\u043d\u0430 \u0435\u0433\u043e \u0441\u0443\u0442\u044c, \u0430 \u043d\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430:\u00a0<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) { hasExternalTx := true tr := domain.Tr(tx) if tr == nil { tr, err = u.db.Beginx() hasExternalTx = false }  defer func() { if !hasExternalTx &amp;&amp; err != nil { tx.Rollback() } }()  \/\/ \u0422\u0435\u043b\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f  if !hasExternalTx { return nil, tx.Commit() }  return user, nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) {     user, err = domain.NewUser(in.Username, in.Password)      err := WithTransaction(tx, func(tx *sqlx.Tx) error {     err = u.userRepo.Save(tx, user); err != nil {     err = u.queue.Publish(tx, domain.Registered{ID: user.ID})     })     return user, err }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438: \u043c\u044b \u0441\u043f\u0440\u044f\u0442\u0430\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043d\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0438\u0445 \u044f\u0432\u043d\u043e \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f.\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e1c\/bc0\/8fc\/e1cbc08fcdb9cf5ee48caa3eaf268a46.png\" width=\"1162\" height=\"494\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e1c\/bc0\/8fc\/e1cbc08fcdb9cf5ee48caa3eaf268a46.png\"\/><\/figure>\n<h3>\u0413\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 Go<\/h3>\n<p>\u0423 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043d\u043e \u044d\u0442\u043e \u043f\u043e\u043a\u0430 \u043d\u0435 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. \u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u0441 \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0440\u043e\u0432\u043d\u044f\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0434\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u2014 \u043a\u0430\u043a \u043e\u043d\u0438 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0442 \u043f\u0440\u043e\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e \u0432\u0441\u0435 \u0447\u0430\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u044d\u0442\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443:\u00a0<\/p>\n<ul>\n<li>\n<p>\u0432 Python (SQLAlchemy) \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u044f\u0432\u043d\u043e, \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043d\u043e \u043d\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f;<\/p>\n<\/li>\n<li>\n<p>\u0432 Python (Django) \u0438 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 PHP \u0438 JavaScript \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p>\u0432 C#, Java \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u043a \u043f\u043e\u0442\u043e\u043a\u0443.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u0440\u0435\u0434\u0438 \u044d\u0442\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u043e\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435: \u0432 Go \u0435\u0441\u0442\u044c Goroutine, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0438. \u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0443\u0436\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 Go:<\/p>\n<ol>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0447\u0442\u043e \u043c\u044b \u0438 \u0434\u0435\u043b\u0430\u0435\u043c. \u041d\u043e \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043a Goroutine \u043f\u043e id, \u043d\u043e \u044d\u0442\u043e \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 local-storage, \u043d\u043e \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u0432\u0435\u0440\u0441\u0438\u044f\u043c Go, \u043d\u0435 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043d\u0430\u043c.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043a\u0430\u043a context. \u0412\u043d\u0443\u0442\u0440\u0438 \u044d\u0442\u043e\u0442 \u043f\u0430\u043a\u0435\u0442 \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u043a\u0430\u043a \u0441\u043f\u0438\u0441\u043e\u043a, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0443 \u043d\u0435\u0433\u043e \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 O(N).\u00a0<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u0442, \u043d\u043e \u043c\u044b \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 context. \u042d\u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0432 Go \u0438 \u043d\u0435 \u043e\u043f\u0430\u0441\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u044f\u0437\u044b\u043a\u0430 \u043d\u0430 \u043d\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e. \u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 context, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<h3>\u041a\u0430\u043a \u0434\u043e\u0432\u0435\u0441\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043e \u0438\u0434\u0435\u0430\u043b\u0430<\/h3>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u044f\u0442\u044c \u043f\u043e\u0436\u0435\u043b\u0430\u043d\u0438\u0439 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e:\u00a0<\/p>\n<ol>\n<li>\n<p>\u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f,<\/p>\n<\/li>\n<li>\n<p>\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439,<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432,<\/p>\n<\/li>\n<li>\n<p>\u0441\u043a\u0440\u044b\u0442\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439,<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u0440\u0435\u0448\u0435\u043d\u0438\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c, \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0438\u0437 \u043d\u0438\u0445:\u00a0<\/p>\n<ol>\n<li>\n<p>\u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430: \u0444\u0443\u043d\u043a\u0446\u0438\u044f WithTransaction \u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 context.<\/p>\n<p>\u0420\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e WithTransaction \u0434\u043e \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439:<\/p>\n<pre><code class=\"go\">type Manager interface {    Do(context.Context, func(context.Context) error) error     DoWithSettings(      context.Context,      Settings,      func(context.Context) error,    ) error }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u043e\u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u041e\u0431\u0449\u0438\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u2014 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0431\u0430\u0437\u044b.<\/p>\n<pre><code class=\"go\">type Settings interface {    \/\/ \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438.    EnrichBy(external Settings) Setings    \/\/ \u041a\u043b\u044e\u0447 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.    CtxKey() CtxKey    \/\/ \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.    Propagation() Propagaion    \/\/ \u041e\u0442\u043c\u0435\u043d\u044f\u0435\u0442 \u043b\u0438 \u0432\u043d\u0435\u0448\u043d\u044f\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0432 \u043d\u0435\u0451.    Cancelable() bool    \/\/ \u0412\u0440\u0435\u043c\u044f \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.    TimeoutOrNil() *time.Duration }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0431\u0440\u0430\u0442\u044c \u0438\u0437 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b:\u00a0<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) { hasExternalTx := true tr := domain.Tr(tx) if tr == nil { tr, err = u.db.Beginx() hasExternalTx = false }  defer func() { if !hasExternalTx &amp;&amp; err != nil { tx.Rollback() } }()  \/\/ \u0422\u0435\u043b\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f  if !hasExternalTx { return nil, tx.Commit() }  return user, nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e func (u *Register) Handle(ctx context.Context, in In) (user *domain.User, err error) {     user, err = domain.NewUser(in.Username, in.Password)      err := u.trm.Do(ctx, func(ctx context.Context) error {     err = u.userRepo.Save(ctx, user); err != nil {     err = u.queue.Publish(ctx, domain.Registered{ID: user.ID})     })     return user, err }<\/code><\/pre>\n<p>\u041d\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043a\u043e\u0434\u0430 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435 \u0447\u0438\u0442\u0430\u0442\u044c.\u00a0<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c context \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0437\u0430\u043a\u0430\u0437\u0430:\u00a0<\/p>\n<pre><code class=\"go\">func (u *Purchase) Handle(ctx context.Context, in In) (order *domain.Order, err error) { order, err = domain.NewOrder(in.UserID, in.ProductID, in.Quantity)  err = u.trm.Do(ctx, func(ctx context.Context) error { err = u.orderRepo.Save(ctx, order) return u.queue.Publish(ctx, domain.Purchased{ID: order.ID}) })  return order, nil }<\/code><\/pre>\n<p>\u0418 \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u043f\u043e\u043a\u0443\u043f\u043a\u0443:<\/p>\n<pre><code class=\"go\">func (u *FastPurchase) Handle(ctx context.Context, in In) (out Out, err error) { err = u.trm.Do(ctx, func(ctx context.Context) error { out.User, err = u.register.Handle(ctx, register.In{ Username: in.Register.Username, Password: in.Register.Password, })  out.Order, err = u.purchase.Handle(ctx, purchase.In{ UserID:    out.User.ID, ProductID: in.Purchase.ProductID, Quantity:  in.Purchase.Quantity, })  return err })  return out, nil } <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u0439\u043c\u0451\u043c\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u043e\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u043c.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u0438\u043b\u0438 \u043a\u043b\u044e\u0447\u0443 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u041d\u0430\u043b\u0438\u0447\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"go\">type CtxManager interface {    Default(context.Context) Ir    ByKey(context.Context, CtxKey) Tr }  type Tr interface{    \/\/ *sqlx.DB    \/\/ *sqlx.Tx }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0422\u0435\u043f\u0435\u0440\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0449\u0435 \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u0432 \u043d\u0435\u0439 \u043c\u0435\u043d\u044c\u0448\u0435 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e func (r *userRepo) Save(tr domain.Tr, u *domain.User) error {     query := `INSERT INTO \"user\" ...;`          if tr != nil {         tr = r.db     }          rows, err := sqlx.NamedQuery(tr, query, r.toRow(u))          defer rows.Close()     if !rows.Next() {         return rows.Err()     }          err = rows.Scan(&amp;u.ID)          return err }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e func (r *userRepo) Save(ctx context.Context, u *User) error {     query := `INSERT INTO \"user\" ...;`      rows, err := sqlx.NamedQueryContext(       ctx, r.getter.DefaultTrOrDB(ctx, r.db), query, r.toRow(u))          defer rows.Close()     if !rows.Next() {         return rows.Err()     }          err = rows.Scan(&amp;u.ID)          return err }<\/code><\/pre>\n<p>\u0415\u0449\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043b\u0435\u0433\u043a\u043e \u043c\u0438\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u043e \u0441 \u043e\u0434\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044e \u043d\u0430\u0434 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Spring \u0432 Java \u0438 System Transaction \u0438\u0437 C#: <\/p>\n<pre><code class=\"go\">type Transaction interface {    IsActive() bool \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438    Commit(context.Context) error \/\/ \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f    Rollback(context.Context) error \/\/ \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f    Transaction() interface{} \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u044c\u043d\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e }  \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e type TrFactory func(Settings) Transaction  \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u0442 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \/\/ \u0434\u043b\u044f \u0411\u0414 \u0441 \u0438\u0445 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 type NestedTrFactory func(Setting) Transaction<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043d\u0430\u0443\u0447\u0438\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0435\u0439 \u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"go\">type CtxManager interface {    Default(context.Context) Transaction    ByKey(context.Context, CtxKey) Transaction }  type SQLtxManager interface {\/* *sql.DB *sql.TX *\/}  type SQLtxManager interface {   DefaultTrOrDB(context.Context, Tr) Tr   TrOrDB(context.Context, CtxKey, Tr) Tr }<\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u0432\u0441\u0435 \u043f\u043e\u0436\u0435\u043b\u0430\u043d\u0438\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bcc\/4ef\/a18\/bcc4efa18c5a4dab28ff62dce8b753a1.png\" width=\"1580\" height=\"820\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bcc\/4ef\/a18\/bcc4efa18c5a4dab28ff62dce8b753a1.png\"\/><\/figure>\n<h3>\u041a\u0430\u043a \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u00a0<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043d\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u043e\u0434\u0430, \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043d\u0430 sqlmock, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u0430\u0437\u0440\u044b\u0432 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u043a\u043e\u043b\u043e 3,5%.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7e6\/364\/398\/7e63643980c0bb4b8e07e4baadbe368b.png\" width=\"1418\" height=\"830\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7e6\/364\/398\/7e63643980c0bb4b8e07e4baadbe368b.png\"\/><\/figure>\n<p>\u041d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u0435\u043d\u044f \u0443\u0434\u0438\u0432\u0438\u043b\u043e, \u0447\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0442\u0435\u0441\u0442\u0430 \u043e\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043d\u0430 SQLite \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u043e\u0432\u044b\u0439 \u0442\u0435\u0441\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0441 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 18%. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e 5 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434.\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c02\/884\/dd9\/c02884dd90e66d74f7563b476503e460.png\" width=\"1358\" height=\"832\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c02\/884\/dd9\/c02884dd90e66d74f7563b476503e460.png\"\/><\/figure>\n<p>\u042f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0435\u0439\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0430\u0449\u0435: \u043a\u043e\u0433\u0434\u0430 \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043b\u0435\u0436\u0438\u0442 \u0432 \u0444\u0430\u0439\u043b\u0435. \u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0431\u044b\u043b\u043e. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0438\u0437\u0434\u0435\u0440\u0436\u043a\u0430\u043c\u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6b2\/a8e\/08c\/6b2a8e08c8107c617c3d857b3aa481e7.png\" width=\"1432\" height=\"820\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6b2\/a8e\/08c\/6b2a8e08c8107c617c3d857b3aa481e7.png\"\/><\/figure>\n<p>\u041e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e <a href=\"https:\/\/github.com\/avito-tech\/go-transaction-manager\/tree\/main\/internal\/benchmark\/with_or_without_trm\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a>.<\/p>\n<h3>\u041a\u0430\u043a\u0438\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u0435\u0441\u0442\u044c \u0443 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/h3>\n<p><strong>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 Go 1.13 \u0438 \u0432\u044b\u0448\u0435.<\/strong> \u041f\u0440\u0438 \u044d\u0442\u043e\u043c Go \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u043e\u0440\u043d\u044b\u043c\u0438 \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u043d\u0443\u0436\u043d\u0443\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e.\u00a0<\/p>\n<p><strong>\u041c\u0430\u043b\u043e \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u043e\u0432 \u0434\u043b\u044f \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u043e\u0432, \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 ORM.<\/strong> \u041f\u043e\u043a\u0430 \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e\u00a0database\/sql, jmoiron\/sqlx, mongo-go-driver, gorm, go-redis). \u041d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0430\u0434\u0430\u043f\u0442\u0435\u0440, \u043d\u0430 \u044d\u0442\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u043a\u043e\u043b\u043e 60 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430.<\/p>\n<p><strong>\u0421\u043d\u0438\u0436\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/strong> \u041d\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0435\u0441\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0440\u0430\u0437\u0440\u044b\u0432 \u0437\u0430\u043c\u0435\u0442\u0435\u043d \u0432 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0431\u0430\u0437\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0444\u0430\u0439\u043b\u0435 \u0438\u043b\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0437\u0434\u0435\u0440\u0436\u043a\u0438 \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0443 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0438 \u0441\u0435\u0442\u0435\u0432\u043e\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u043c \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u043c \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u043e\u0439.\u00a0<\/p>\n<p><strong>\u041d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 context.<\/strong> \u041f\u0440\u0430\u0432\u0434\u0430, \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Go, \u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0443\u0436\u0435 \u043d\u043e\u0441\u0438\u0442\u0435 \u0435\u0433\u043e \u0441 \u0441\u043e\u0431\u043e\u0439. \u042d\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043e\u0442\u043c\u0435\u043d\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441. \u0422\u0430\u043a\u0436\u0435 context \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041c\u043e\u0436\u043d\u043e \u0432\u043e\u0437\u0440\u0430\u0437\u0438\u0442\u044c, \u0447\u0442\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u2014 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0440\u044f\u0434 \u0441\u0442\u0430\u0442\u0435\u0439 (<a href=\"https:\/\/blog.gopheracademy.com\/advent-2016\/context-logging\/#potentially-good-patterns\">1<\/a>, <a href=\"https:\/\/www.calhoun.io\/pitfalls-of-context-values-and-how-to-avoid-or-mitigate-them\/#before-we-start-let-s-lay-out-some-ground-rules\">2<\/a>) \u0438\u0437 <a href=\"https:\/\/github.com\/golang\/go\/wiki\/ExperienceReports#context\">wiki<\/a> GoLang \u0438 <a href=\"https:\/\/pkg.go.dev\/context\">\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 context<\/a> \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p><strong>\u041d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0434\u043e\u043b\u0433\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/strong> \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 \u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0443, \u043e\u043d \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043f\u043e \u043d\u043e\u043c\u0435\u0440\u0443 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0430 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0438\u0437 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u041f\u043e\u0436\u0430\u043b\u0443\u0439, \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043c\u0438\u043d\u0443\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u0435\u0447\u0438\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 UnitOfWork, \u043d\u043e \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0442\u0435\u043c\u0430.\u00a0<\/p>\n<h3>\u041d\u0443\u0436\u0435\u043d \u043b\u0438 \u0432\u0430\u043c \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/h3>\n<p>\u0414\u043b\u044f \u043d\u0430\u0441 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d. \u0420\u0430\u043d\u044c\u0448\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0431\u044b\u043b\u0438 \u0436\u0451\u0441\u0442\u043a\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0435\u0451 \u0432 context. \u0422\u043e \u0435\u0441\u0442\u044c \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0430\u0437\u0443 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043a\u043e\u0434\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432. \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043a\u0440\u044b\u043b\u0438 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0434\u043e \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430.<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e type UserRepo interface {    GetByID(*sqlx.Tx, UserID) (*User, error)    Save(*sqlx.Tx, *User) error }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e  type UserRepo interface {    GetByID(context.Context, UserID) (*User, error)    Save(context.Context, *User) error }<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0441\u0442\u0430\u043b \u043f\u043e\u043d\u044f\u0442\u043d\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u0443\u0431\u0440\u0430\u043b\u0438 \u0438\u0437 \u043d\u0438\u0445 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.<\/p>\n<p>\u041d\u0443\u0436\u0435\u043d \u043b\u0438 \u0432\u0430\u043c \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435? \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432. \u042d\u0442\u043e \u0432\u0430\u0436\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0438\u0445 \u0445\u043e\u0442\u044f \u0431\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u0432\u0443\u0445, \u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u0440\u0430\u0441\u0442\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/552\/9d5\/d08\/5529d5d0827034816d334f77bbd22988.png\" width=\"1584\" height=\"876\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/552\/9d5\/d08\/5529d5d0827034816d334f77bbd22988.png\"\/><\/figure>\n<p>\u0421\u0430\u043c \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043a \u043a\u043e\u0434\u0443 \u0432\u0441\u0435\u0433\u043e 5 \u0441\u0442\u0440\u043e\u043a \u043d\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0438 \u0435\u0449\u0451 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u2014 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435. \u0411\u0435\u0437 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e 8 \u0441\u0442\u0440\u043e\u0447\u0435\u043a \u043a\u043e\u0434\u0430.<\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0435\u0449\u0451 \u044f \u0441\u043e\u0432\u0435\u0442\u0443\u044e \u0443\u0447\u0435\u0441\u0442\u044c \u043a\u043e\u0433\u043d\u0438\u0442\u0438\u0432\u043d\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043a\u043e\u0434 \u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432. \u0427\u0435\u043c \u043a\u043e\u0434\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u0435\u043c \u0432\u044b\u0448\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u041f\u0440\u0438 \u0442\u0430\u043a\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043b\u0443\u0447\u0448\u0435.\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/679\/105\/b01\/679105b01fc3788b73f87a756307c755.png\" width=\"2000\" height=\"1074\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/679\/105\/b01\/679105b01fc3788b73f87a756307c755.png\"\/><\/figure>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/github.com\/avito-tech\/go-transaction-manager\/\"><u>\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 Github<\/u><\/a>, \u043a\u0430\u043a \u0438 <a href=\"https:\/\/github.com\/avito-tech\/go-transaction-manager\/tree\/main\/internal\/example\/habr\">\u043f\u0440\u0438\u043c\u0435\u0440\u044b<\/a>. \u0418 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u2014 \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c, \u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u0432\u043d\u0435\u0434\u0440\u044f\u0442\u044c \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a.<\/p>\n<p><em>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f: <\/em><a href=\"https:\/\/habr.com\/ru\/companies\/avito\/articles\/710660\/\"><em>\u0423\u043b\u044c\u0442\u0438\u043c\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0433\u0430\u0439\u0434 \u043f\u043e HTTP. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u043e\u0442\u0432\u0435\u0442\u0430<\/em><\/a><\/p>\n<\/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\/727168\/\"> https:\/\/habr.com\/ru\/articles\/727168\/<\/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>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u042f \u0418\u043b\u044c\u044f \u0421\u0435\u0440\u0433\u0443\u043d\u0438\u043d, \u0432\u0435\u0431-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438\u0437 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0410\u0432\u0438\u0442\u043e. \u041c\u044b \u043f\u0438\u0448\u0435\u043c \u043d\u0430 Go \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0432\u044b\u043a\u0443\u043f\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432, \u043f\u0440\u043e MLP \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0436\u0435 \u043f\u0438\u0441\u0430\u043b\u0438 \u0432 <a href=\"https:\/\/habr.com\/ru\/companies\/avito\/articles\/700784\/\">\u0431\u043b\u043e\u0433\u0435<\/a>. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435\u043c \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442-\u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. <\/p>\n<p><strong>\u0412\u0430\u0436\u043d\u043e\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435: <\/strong>\u0432 Go \u043d\u0443\u0436\u043d\u043e \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u044f \u0441\u043a\u0440\u044b\u0432\u0430\u044e \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043b\u0435\u0433\u0447\u0435 \u0447\u0438\u0442\u0430\u043b\u0438\u0441\u044c.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/h3>\n<p>\u041f\u0430\u0442\u0442\u0435\u0440\u043d Repository \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d \u041c\u0430\u0440\u0442\u0438\u043d\u043e\u043c \u0424\u0430\u0443\u043b\u0435\u0440\u043e\u043c \u0432 \u043a\u043d\u0438\u0433\u0435 \u00ab\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439\u00bb. \u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u042d\u0440\u0438\u043a \u042d\u0432\u0430\u043d\u0441 \u0432 \u043a\u043d\u0438\u0433\u0435 \u00ab\u041f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (DDD). \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u00bb. <\/p>\n<p>Repository \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0430 \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u041a\u0430\u0436\u0434\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0434\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e. Repository \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b. <\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0443 \u043d\u0435\u0433\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438: \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>User \u043c\u043e\u0436\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432 \u0441\u0435\u0431\u044f \u043d\u0438\u043a\u043d\u0435\u0439\u043c, \u043f\u0430\u0440\u043e\u043b\u044c, \u0438\u043c\u044f, \u0444\u0430\u043c\u0438\u043b\u0438\u044e, \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439 \u0438 \u0442.\u0434. \u0412\u0441\u0435 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u0440\u0430\u0437\u043d\u043e\u043c \u0432\u0438\u0434\u0435. <\/p>\n<p>\u0418\u043c\u044f \u0438 \u0444\u0430\u043c\u0438\u043b\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u044b \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0435\u0441\u043b\u0438 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0434\u0438 \u0438 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u0432\u0438\u0434\u0435 json-\u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0418\u0445 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. Repository \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438, \u0430 \u043d\u0435 \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u044b:<\/p>\n<pre><code class=\"go\">\/\/ \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 func (r *userRepo) GetByID(id domain.UserID) (*domain.User, error) { query := `SELECT * FROM \"user\" WHERE id = ?;` uRow := userRow{}  err := r.db.Get(&amp;uRow, r.db.Rebind(query), id)  return r.toModel(uRow), nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 func (r *userRepo) Save(u *domain.User) error { query := `INSERT INTO \"user\" (username, password, notification) VALUES (:username, :password, :notification) ON CONFLICT (id)     DO UPDATE SET excluded.username     = :username,                   excluded.password     = :password,                   excluded.notification = :notification RETURNING id;`  rows, err := r.db.Query(query, r.toRow(u))  defer rows.Close() if !rows.Next() { return rows.Err() }  err = rows.Scan(&amp;u.ID)  return err }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0430\u0445  <code>toModel<\/code> \u0438 <code>toRow<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e.<\/p>\n<p>\u041c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u043b\u044e\u0431\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445 \u0438 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0430\u0445 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u043e\u0441\u0442\u0430\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c.\u00a0<\/p>\n<h3>\u0428\u0430\u0433 1 \u2014 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u00a0<\/h3>\n<p>\u041a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c:<\/p>\n<pre><code class=\"go\">func (u *Register) Handle(in In) (user *domain.User, err error) { user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(user)  return user, nil }<\/code><\/pre>\n<p>\u041c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"go\">func (u *Register) Handle(in In) (user *domain.User, err error) { user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(user)      \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414 err = u.queue.Publish(domain.Registered{ID: user.ID})  return user, nil }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c, \u0447\u0442\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0441\u0431\u043e\u0439, \u0438 \u0442\u043e\u0433\u0434\u0430 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0435 \u043e \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043d\u0435 \u0434\u043e\u0439\u0434\u0435\u0442 \u0434\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438 \u044f\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0435\u0451 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c:<\/p>\n<pre><code class=\"go\">func (u *Register) Handle(in In) (user *domain.User, err error) {     \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414     tr, err = u.db.Beginx()  user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(tr, user) err = u.queue.Publish(tr, domain.Registered{ID: user.ID})      \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414 err = tr.Commit() \/\/ tr.Rollback()  return user, nil }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0442\u0442\u0435\u0440\u043d transaction outbox, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0418 \u0442\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0437\u043d\u0430\u043b \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"go\">\/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 func (r *userRepo) Save(tr domain.Tr, u *domain.User) error { query := `INSERT INTO \"user\" ...;`      \/\/ \u041d\u041e\u0412\u042b\u0419 \u041a\u041e\u0414 if tr != nil { tr = r.db }  rows, err := sqlx.NamedQuery(tr, query, r.toRow(u))  defer rows.Close() if !rows.Next() { return rows.Err() }  err = rows.Scan(&amp;u.ID)  return err }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043d\u043e \u0432\u0437\u0430\u043c\u0435\u043d \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f:\u00a0<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0428\u0430\u0433 2 \u2014 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0437\u0430\u043a\u0430\u0437 \u0438 \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u0432 \u043e\u0434\u0438\u043d \u043a\u043b\u0438\u043a \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u00a0<\/h3>\n<p>\u0423 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043a\u0430\u0437. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0432\u0442\u043e\u0440\u043e\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0437\u0430\u043a\u0430\u0437\u0430 \u0438 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"go\">func (u *Purchase) Handle(tx *sqlx.Tx, in In) (order *domain.Order, err error) {     tr, err = u.db.Beginx();  order, err = domain.NewOrder(in.UserID, in.ProductID, in.Quantity)  err = u.orderRepo.Save(tr, order)  u.queue.Publish(tr, domain.Purchased{ID: order.ID})  err = tr.Commit() \/\/ tr.Rollback()  return order, nil }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0434\u0430\u0436 \u0434\u0430\u0434\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"go\">func (u *FastPurchase) Handle(in In) (out Out, err error) { out.User, err = u.register.Handle(tx, register.In{ Username: in.Register.Username, })  out.Order, err = u.purchase.Handle(tx, purchase.In{ UserID:    out.User.ID, ProductID: in.Purchase.ProductID, Quantity:  in.Purchase.Quantity, })  return out, nil }<\/code><\/pre>\n<p>Register \u0438 Purchase \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u043c\u0438 \u0432 FastPurchase. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"go\">func (u *FastPurchase) Handle(in In) (out Out, err error) { tx, err := u.db.Beginx()  out.User, err = u.register.Handle(tx, register.In{ Username: in.Register.Username, })  out.Order, err = u.purchase.Handle(tx, purchase.In{ UserID:    out.User.ID, ProductID: in.Purchase.ProductID, Quantity:  in.Purchase.Quantity, })  err = tx.Commit() \/\/ tr.Rollback()  return out, nil }<\/code><\/pre>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0432 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043f\u043e\u043a\u0443\u043f\u043a\u0438 \u0442\u043e\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0435\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0414\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0438\u0445, \u0447\u0442\u043e\u0431\u044b \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0441 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u043c\u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e.\u00a0<\/p>\n<p>\u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e func (u *Register) Handle(in In) (user *domain.User, err error) {     tr, err = u.db.Beginx()  user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(tr, user) err = u.queue.Publish(tr, domain.Registered{ID: user.ID})  err = tr.Commit() \/\/ tr.Rollback()  return user, nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) { hasExternalTx := true tr := domain.Tr(tx) if tr == nil { tr, err = u.db.Beginx() hasExternalTx = false }  defer func() { if !hasExternalTx &amp;&amp; err != nil { tx.Rollback() } }()  user, err = domain.NewUser(in.Username, in.Password)  err = u.userRepo.Save(tr, user); err != nil { err = u.queue.Publish(tr, domain.Registered{ID: user.ID})  if !hasExternalTx { return nil, tx.Commit() }  return user, nil }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c: \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438. \u041d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0435\u0449\u0451 \u0438 \u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<h3>\u0428\u0430\u0433 3 \u2014 \u0437\u0430\u043c\u044b\u043a\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:\u00a0<\/p>\n<pre><code class=\"go\">func (r *userRepo) FastOrder(fn func() (*User, *Order, error)) error {    tr, err := r.db.Begin()     user, order, err := fn() \/\/ \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f     err = r.Save(tr, user)    err = r.orderRepo.Save(tr, order)     err = tr.Commit() \/\/ \u0438\u043b\u0438 tr.Rollback()     return nil }<\/code><\/pre>\n<p>\u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0437\u043d\u0430\u0435\u0442 \u043e \u0437\u0430\u043a\u0430\u0437\u0430\u0445. \u041d\u0430\u043c \u044d\u0442\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e: \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u043f\u0440\u043e\u0434\u0443\u043a\u0442 \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442\u0441\u044f \u0438 \u0440\u0430\u0441\u0442\u0451\u0442, \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0438 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435. \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u0442\u043e\u0436\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0442\u0441\u044f \u0438 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u044e\u0442\u0441\u044f, \u0438 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443\u0437\u043d\u0430\u0451\u0442 \u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u044f\u0445.  \u0417\u043d\u0430\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c \u0438 \u0435\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043e\u043d \u043f\u043e\u0442\u0435\u0440\u044f\u0435\u0442 \u0441\u0432\u043e\u044e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u2014 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.\u00a0<\/p>\n<p>\u0420\u0435\u0448\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u043e: \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c. \u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e WithTransaction:\u00a0<\/p>\n<pre><code class=\"go\">func WithTransaction(tx *sqlx.Tx, fn func(*sqlx.Tx) error) error {    hasExternalTx := true    if tx == nil {        tx, err = DB.Begin()        hasExternalTx = false    }     err := fn(tx) \/\/ \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f     if !hasExternalTx {        err = tx.Commit() \/\/ tx.Rollback()    }     return nil }<\/code><\/pre>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0430 \u0432\u0438\u0434\u043d\u0430 \u0435\u0433\u043e \u0441\u0443\u0442\u044c, \u0430 \u043d\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430:\u00a0<\/p>\n<pre><code class=\"go\">\/\/ \u0411\u044b\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) { hasExternalTx := true tr := domain.Tr(tx) if tr == nil { tr, err = u.db.Beginx() hasExternalTx = false }  defer func() { if !hasExternalTx &amp;&amp; err != nil { tx.Rollback() } }()  \/\/ \u0422\u0435\u043b\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f  if !hasExternalTx { return nil, tx.Commit() }  return user, nil }<\/code><\/pre>\n<pre><code class=\"go\">\/\/ \u0421\u0442\u0430\u043b\u043e func (u *Register) Handle(tx *sqlx.Tx, in In) (user *domain.User, err error) {     user, err = domain.NewUser(in.Username, in.Password)      err := WithTransaction(tx, func(tx *sqlx.Tx) error {     err = u.userRepo.Save(tx, user); err != nil {     err = u.queue.Publish(tx, domain.Registered{ID: user.ID})     })     return user, err }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438: \u043c\u044b \u0441\u043f\u0440\u044f\u0442\u0430\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043d\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0438\u0445 \u044f\u0432\u043d\u043e \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f.\u00a0<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0413\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 Go<\/h3>\n<p>\u0423 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043d\u043e \u044d\u0442\u043e \u043f\u043e\u043a\u0430 \u043d\u0435 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. \u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u0441 \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0440\u043e\u0432\u043d\u044f\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0434\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u2014 \u043a\u0430\u043a \u043e\u043d\u0438 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0442 \u043f\u0440\u043e\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e \u0432\u0441\u0435 \u0447\u0430\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u044d\u0442\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443:\u00a0<\/p>\n<ul>\n<li>\n<p>\u0432 Python (SQLAlchemy) \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u044f\u0432\u043d\u043e, \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043d\u043e \u043d\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f;<\/p>\n<\/li>\n<li>\n<p>\u0432 Python (Django) \u0438 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 PHP \u0438 JavaScript \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p>\u0432 C#, Java \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u043a \u043f\u043e\u0442\u043e\u043a\u0443.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u0440\u0435\u0434\u0438 \u044d\u0442\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u043e\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435: \u0432 Go \u0435\u0441\u0442\u044c<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-397265","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/397265","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=397265"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/397265\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}