pg/cmd/pg-pubsub-demo/main.go

82 lines
1.6 KiB
Go

package main
import (
"context"
"database/sql"
"flag"
"fmt"
"io"
"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 {
if _, err := pg.ExecContext(ctx, `
`); err != nil {
return err
}
pub := func(topic string, a any) error {
return io.EOF
}
sub := func(topic, group string) (int, any, error) {
return 0, nil, io.EOF
}
commit := func(topic, group string, offset int) error {
return io.EOF
}
go with.Every(ctx, time.Second, func() {
for i := 0; i < 2; i++ {
topic := fmt.Sprintf("topic_%d", i)
if err := pub(topic, 1); err != nil {
log.Printf("failed pub: %v", err)
} else {
log.Printf("pubbed")
}
}
})
with.Every(ctx, time.Second, func() {
if err := func() error {
for i := 0; i < 2; i++ {
topic := fmt.Sprintf("topic_%d", i)
for j := 0; j < 2; j++ {
group := fmt.Sprintf("group_%d", i)
if offset, v, err := sub(topic, group); err != nil {
return err
} else if err := commit(topic, group, offset); err != nil {
return err
} else {
log.Printf("subbed/committed offset %d: %v", offset, v)
}
}
}
return nil
}(); err != nil {
log.Printf("failed subs: %v", err)
}
})
return ctx.Err()
})
}