From 1154f7126aea15eeca60bfc38ac7cdcd3cf4973d Mon Sep 17 00:00:00 2001 From: stream1080 <77672403+stream1080@users.noreply.github.com> Date: Sat, 30 Sep 2023 16:48:20 +0800 Subject: [PATCH 1/2] feat: add go commons pool v2 --- go.mod | 2 ++ go.sum | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 go.sum diff --git a/go.mod b/go.mod index a145f74..0ae9a4b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/stream1080/godis go 1.18 + +require github.com/jolestar/go-commons-pool/v2 v2.1.2 // indirect \ No newline at end of file diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5bfcee1 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= +github.com/jolestar/go-commons-pool/v2 v2.1.2 h1:E+XGo58F23t7HtZiC/W6jzO2Ux2IccSH/yx4nD+J1CM= +github.com/jolestar/go-commons-pool/v2 v2.1.2/go.mod h1:r4NYccrkS5UqP1YQI1COyTZ9UjPJAAGTUxzcsK1kqhY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 0dfb9cf7dc9f807775ed6b504bd6bacd8dec9be3 Mon Sep 17 00:00:00 2001 From: stream1080 <77672403+stream1080@users.noreply.github.com> Date: Sat, 30 Sep 2023 16:48:41 +0800 Subject: [PATCH 2/2] feat: add connection factory --- cluster/client_pool.go | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 cluster/client_pool.go diff --git a/cluster/client_pool.go b/cluster/client_pool.go new file mode 100644 index 0000000..171731f --- /dev/null +++ b/cluster/client_pool.go @@ -0,0 +1,49 @@ +package cluster + +import ( + "context" + "errors" + + "github.com/stream1080/godis/resp/client" + + pool "github.com/jolestar/go-commons-pool/v2" +) + +type ConnFactory struct { + Peer string +} + +func (f *ConnFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) { + + c, err := client.MakeClient(f.Peer) + if err != nil { + return nil, err + } + + c.Start() + + return pool.NewPooledObject(c), nil +} + +func (f *ConnFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error { + c, ok := object.Object.(*client.Client) + if !ok { + return errors.New("type not match") + } + + c.Close() + return nil + +} + +func (f *ConnFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool { + return true +} + +func (f *ConnFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error { + return nil +} + +func (f *ConnFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error { + return nil +}