Sobes.tech

Data Analyst

// we have a method to get product information, this method is called very often // are there any problems with this code and how to fix it? PostgreSQL database

Principal
264

// We have a method for retrieving product information, this method is called very often // are there any problems in this code and how to fix them? PostgreSQL database func (r *ProductRepository) GetProductDetails(ctx context.Context, productIDs []int) []Product { products := make([]Product, 0, len(productIDs)) for _, id := range productIDs { go func(id int) { var p Product query := "SELECT name, price, description FROM products WHERE id = $1" err := r.db.QueryRowContext(ctx, query, id).Scan(&p.Name, &p.Price, &p.Desc) if err != nil { r.logger.Error("error get product", "err", err) return } products = append(products, p) }(id) } return products }

Principal
163

// When updating an order, we need to send order data to several services (third-party) // the number of services is growing (could be thousands+) // we wrote the code, initially everything was fine, but over time our service started consuming a lot of resources // We need to optimize this code to work efficiently with a large number of services func (s *orderService) SendOrder(ctx context.Context, hosts []string, order Order) { for i := 0; i < len(hosts); i++ { go func() { // Imagine this is a long network call response, err := s.httpClient.Send(ctx, hosts[i], order) if err != nil { s.logger.Error(ctx, "failed to send", err) return } s.logger.Info(ctx, "success", response) }() } }

Principal
151

Is an UPDATE transaction itself? Is an explicit transaction needed for a single atomic UPDATE?

Principal
142

What will happen if thousands of connections are launched simultaneously?

Principal
124

func (r *OrderRepository) GetActiveOrderIDs(ctx context.Context, offset, limit int) ([]uint32, error) { var ( res []uint32 args []interface{} ) query := `SELECT id FROM "order" WHERE is_terminated = false LIMIT $1 OFFSET $2;` args = append(args, limit, offset) rows, err := r.slave.Query(ctx, query, args...) if err != nil { return nil, err } for rows.Next() { var orderID uint32 if err = rows.Scan(&orderID); err != nil { return nil, err } res = append(res, orderID) } return res, nil }

Principal
123

// We have a table with a product that has a view counter // table goods: // ID | cnt_view // ----------- // 1 | 100 // 2 users started viewing the product with id = 1 simultaneously // and we have two parallel transactions in the database // what will be the result? postgress func IncrementView(ctx context.Context, db *sql.DB, id int64) error { tx, err := db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() var cnt int err = tx.QueryRowContext(ctx, "SELECT cnt_view FROM goods WHERE id = $1", id, ).Scan(&cnt) if err != nil { return err } cnt++ _, err = tx.ExecContext(ctx, "UPDATE goods SET cnt_view = $1 WHERE id = $2", cnt, id, ) if err != nil { return err } return tx.Commit() }

Principal
121

// When updating an order, we need to send order data to several (possibly thousands) third-party services // We wrote the code, everything was fine at first, but over time our service started consuming a lot of resources func (s *orderService) SendOrder(ctx context.Context, hosts []string, order Order) { for i := 0; i < len(hosts); i++ { go func() { // Imagine this is a long network call response, err := s.httpClient.Send(ctx, hosts[i], order) if err != nil { s.logger.Error(ctx, "failed to send", err) return } s.logger.Info(ctx, "success", response) }() } }

Principal
96