-
Notifications
You must be signed in to change notification settings - Fork 0
Increment14 #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Increment14 #31
Conversation
id, err := h.fetchID(c, link) | ||
uniqueErr := false | ||
id, err := h.fetchID(c, user, link) | ||
if errors.Is(err, h.Storage.ErrDupKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы предложил упростить эту логику, оставив
if err != nil && !errors.Is(err, h.Storage.ErrDupKey) {
return fmt.Errorf("fetch id: %w", err)
}
l := &Link{...}
if errors.Is(err, h.Storage.ErrDupKey) {
return c.JSON(http.StatusConflict, l)
}
return c.JSON(http.StatusCreated, l)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Потому что обнуление ошибки может привести к неожиданным последствиям, если логика будет меняться/дорабатываться
_, err = db.Exec("INSERT INTO shortener VALUES ($1, $2, $3, FALSE)", user, id, link) | ||
if err != nil { | ||
if err, ok := err.(*pq.Error); ok { | ||
if err.Code == "23505" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Стоит вынести в константу с говорящим названием
s.Pairs[user][id] = link | ||
|
||
if s.DBCredentials != "" { | ||
db, err := sqlx.Connect("postgres", s.DBCredentials) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я думаю, что нет смысла каждый раз подключаться к базе, можно держать объект db в структуре Storage
for _, linkToDelete := range l { | ||
for savedLink := range list { | ||
if savedLink == linkToDelete { | ||
go h.Storage.UpdateURL(user, linkToDelete, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Предлагаю воспользоваться советом из учебника и потренироваться в написании FanIn.
Для этого можно запустить отдельную функцию в фоновой горутине, которая принимает канал со ссылками, читает из него пачку ссылок и удаляет их одним запросом.
Здесь же в DeleteURL вместо запуска дополнительной горутины можно просто положить ссылку в этот канал.
Такой подход имеет два достоинства:
- снизится число отдельных запросов к базе данных
- снизится число единовременно запущенных горутин (сейчас число горутин = число единовременных запросов на удаление * число ссылок в рамках одного запроса, а будет - число единовременных запросов на удаление + 1)
No description provided.