Sobes.tech

package main func main() { c := make(chan int, 1) for range 3 { select { default: println(1) case <-c: println(2) case c <- 1: println(3) } } }

263

```go func fail() { panic("Fail!") } func main() { fmt.Println("Start!") var wg sync.WaitGroup wg.Add(1) go func() { defer func() { if e := recover(); e != nil { fmt.Println(e) } }() fail() wg.Done() }() wg.Wait() fmt.Println("Finish!") } ```

221

Why did you choose Elasticsearch? What other options were there? Did you choose something or was ELK already there?

209

You say that you used Elasticsearch to create a system for quick transaction searches. What is it, how many transactions does it handle, and what are the benefits?

184

func fetch(ctx context.Context, u User) (string, error) { // do something over the network time.Sleep(time.Millisecond * 10) // simulate delay return u.Name, nil //return "", errors.New("some error") } func Do(ctx context.Context, users []User) (map[string]int64, error) { var mu sync.Mutex var wg sync.WaitGroup ch := make(chan error, len(users)) names := make(map[string]int64, 0) ctxWithCancel, cancel := context.WithCancel(ctx) wg.Add(len(users)) for _, u := range users { go func() { defer wg.Done() select { case name, err := fetch(ctx, u): if err != nil { ch <- err return } mu.Lock() names[name] = names[name] + 1 mu.Unlock() case <-ctxWithCancel.Err: } }() }

180

In the end, which solutions did you choose from? ClickHouse, ELK, and what was the third one?

178

After implementing Elasticsearch, was there any product feedback from users or technical support?

169

package main import ( "fmt" ) func main() { c := make(chan string, 1) go fmt.Println(<-c) c <- "Hello World!" fmt.Println("Exit!") } fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan receive]: main.main() /tmp/sandbox[phone]/prog.go:10 +0x3a Program exited: status 2.

158

package main func main() { c := make(chan int) for range 3 { select { default: println(1) case <-c: println(2) case c <- 1: println(3) } } }

157

package main import ( "fmt" ) func main() { c := make(chan string) go fmt.Println(<-c) c <- "Hello World!" fmt.Println("Exit!") }

156

What do services have to do with it, if we are talking about fault tolerance and have reduced it to replicas?

149

package main import ( "fmt" ) func main() { c := make(chan string) go fmt.Println(<-c) c <- "Hello World!" fmt.Println("Exit!") } fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan receive]: main.main() /tmp/sandbox[phone]/prog.go:10 +0x3a Program exited: status 2.

146

```go var defaultTxOptions = &sql.TxOptions{} const defaultCurrency = "RUB" func CreateUserWithDefaultCurrency(ctx context.Context, db *sql.DB, email, passwordHash string) error { tx, err := db.BeginTx(ctx, defaultTxOptions) if err != nil { return err } defer func() { if err != nil{ tx.Rollback() } }() _, err = tx.Exec("INSERT INTO users (email, password) VALUES (?, ?)", email, passwordHash) if err != nil { return err } rows, err := tx.Query("SELECT id FROM users WHERE email = ?", email) if err != nil { return err } defer rows.Close() id := 0 for rows.Next() { err = rows.Scan(&id) if err != nil { return err } } _, err = tx.Exec("INSERT INTO balances (user_id, currency, current_balance) VALUES (?, ?, 0)", id, defaultCurrency) if err != nil { return err } return tx.Commit() } ```

145

func Do(ctx context.Context, users []User) (map[string]int64, error) { var mu sync.Mutex var wg sync.WaitGroup ch := make(chan error, len(users)) names := make(map[string]int64, 0) ctxWithCancel, cancel := context.WithCancel(ctx) wg.Add(len(users)) for _, u := range users { go func() { defer wg.Done() select { case name, err := fetch(ctx, u): if err != nil { ch <- err return } mu.Lock() names[name] = names[name] + 1 mu.Unlock() case <-ctxWithCancel.Err: } }() } return names, nil }

135
/2