Compare commits

...

2 Commits

Author SHA1 Message Date
Bel LaPointe 18d8d9af77 println connected 2025-12-09 15:52:38 -07:00
Bel LaPointe 8740f890da go template 2025-12-09 15:49:34 -07:00
6 changed files with 86 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/cmd/pg-lo-demo/pg-lo-demo
/cmd/pg-pulse/pg-pulse
/cmd/pg-walspam/pg-walspam
/cmd/pg-template

45
cmd/.template/main.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"context"
"database/sql"
"flag"
"log"
"os"
"time"
"pg/src/with"
_ "github.com/lib/pq"
)
func main() {
if err := with.Context(run); err != nil {
panic(err)
}
}
func run(ctx context.Context) error {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
c := fs.String("c", "postgresql://pulsegres:pulsegres@localhost:15432", "conn string")
if err := fs.Parse(os.Args[1:]); err != nil {
panic(err)
}
return with.PSQL(ctx, *c, func(pg *sql.DB) error {
go with.Every(ctx, 5*time.Second, func() {
row := pg.QueryRowContext(ctx, `SELECT 1`)
var n int
if err := row.Err(); err != nil {
log.Println("query err:", err)
} else if err := row.Scan(&n); err != nil {
log.Println("scan err:", err)
} else {
log.Println("ping...")
}
})
<-ctx.Done()
return nil
})
}

3
cmd/generate.go Normal file
View File

@ -0,0 +1,3 @@
package main
//go:generate sh -c "if ! [ -d ./pg-template ]; then cp -r ./.template ./pg-template; fi"

1
cmd/pg-queue/README.md Normal file
View File

@ -0,0 +1 @@
https://topicpartition.io/blog/postgres-pubsub-queue-benchmarks

31
cmd/pg-queue/main.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"context"
"database/sql"
"flag"
"os"
"pg/src/with"
_ "github.com/lib/pq"
)
func main() {
if err := with.Context(run); err != nil {
panic(err)
}
}
func run(ctx context.Context) error {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
c := fs.String("c", "postgresql://pulsegres:pulsegres@localhost:15432", "conn string")
if err := fs.Parse(os.Args[1:]); err != nil {
panic(err)
}
return with.PSQL(ctx, *c, func(pg *sql.DB) error {
<-ctx.Done()
return nil
})
}

View File

@ -42,10 +42,14 @@ func PSQL(ctx context.Context, conn string, foo func(db *sql.DB) error) error {
if ok {
return
}
case <-time.After(time.Second * 5):
}
select {
case <-ctx.Done():
case <-time.After(time.Second):
}
}
}()
log.Println("connected")
return foo(pg)
}