shad-go/lectures/07-sql/hasql/open.go

38 lines
698 B
Go
Raw Normal View History

2021-04-08 11:32:08 +00:00
package hasql
import (
2022-03-15 20:19:41 +00:00
"context"
"database/sql"
2021-04-08 11:32:08 +00:00
"log"
2022-03-15 20:19:41 +00:00
"time"
2021-04-08 11:32:08 +00:00
_ "github.com/jackc/pgx/v5/stdlib"
2022-02-17 10:09:14 +00:00
"golang.yandex/hasql"
"golang.yandex/hasql/checkers"
2021-04-08 11:32:08 +00:00
)
func Open() {
dbFoo, _ := sql.Open("pgx", "host=foo")
dbBar, _ := sql.Open("pgx", "host=bar")
cluster, err := hasql.NewCluster(
2021-04-08 11:37:21 +00:00
[]hasql.Node{hasql.NewNode("foo", dbFoo), hasql.NewNode("bar", dbBar)},
2021-04-08 11:32:08 +00:00
checkers.PostgreSQL,
)
if err != nil {
log.Fatal(err)
}
node := cluster.Primary()
if err == nil {
log.Fatal(err)
}
2022-03-15 20:19:41 +00:00
log.Println("Node address", node.Addr())
2021-04-08 11:32:08 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err = node.DB().PingContext(ctx); err != nil {
log.Fatal(err)
}
}