From fad7946b58308e4898be57d54a1d4787ad30fe47 Mon Sep 17 00:00:00 2001 From: Ryan Field Date: Sun, 4 Feb 2024 12:48:03 +0100 Subject: [PATCH] Fix premature closing of the store client --- example/docker-compose.yml | 18 +++++++++++++++++ example/goflow-example-postgres.go | 32 ++++++++++++++++++++++++++++++ goflow.go | 7 +++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 example/docker-compose.yml create mode 100644 example/goflow-example-postgres.go diff --git a/example/docker-compose.yml b/example/docker-compose.yml new file mode 100644 index 0000000..4d03372 --- /dev/null +++ b/example/docker-compose.yml @@ -0,0 +1,18 @@ +# for testing goflow-example-postgres.go +version: '3.1' + +services: + + db: + image: postgres + restart: always + environment: + POSTGRES_PASSWORD: example + ports: + - 5432:5432 + + adminer: + image: adminer + restart: always + ports: + - 8080:8080 diff --git a/example/goflow-example-postgres.go b/example/goflow-example-postgres.go new file mode 100644 index 0000000..864ed3c --- /dev/null +++ b/example/goflow-example-postgres.go @@ -0,0 +1,32 @@ +package main + +//import ( +// "github.com/fieldryand/goflow/v2" +// "github.com/philippgille/gokv/encoding" +// "github.com/philippgille/gokv/postgresql" +//) +// +//func main() { +// storeOptions := postgresql.Options{ +// ConnectionURL: "postgres://postgres:example@0.0.0.0:5432/postgres?sslmode=disable", +// TableName: "Item", +// MaxOpenConnections: 100, +// Codec: encoding.JSON, +// } +// +// client, err := postgresql.NewClient(storeOptions) +// if err != nil { +// panic(err) +// } +// defer client.Close() +// +// options := goflow.Options{ +// Store: client, +// Streaming: true, +// ShowExamples: true, +// WithSeconds: true, +// } +// gf := goflow.New(options) +// gf.Use(goflow.DefaultLogger()) +// gf.Run(":8181") +//} diff --git a/goflow.go b/goflow.go index f78a26d..13f8e2b 100644 --- a/goflow.go +++ b/goflow.go @@ -33,11 +33,14 @@ type Options struct { // New returns a Goflow engine. func New(opts Options) *Goflow { - var c *cron.Cron + + // Add a default store if necessary if opts.Store == nil { opts.Store = gomap.NewStore(gomap.DefaultOptions) } - defer opts.Store.Close() + + // Add the cron schedule + var c *cron.Cron if opts.WithSeconds { c = cron.New(cron.WithSeconds()) } else {