From c936044526eff70d73fbefa3c53963d4cfb2a5c0 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期二, 16 五月 2023 16:58:40 +0800 Subject: [PATCH] for go get --- cmd/make.sh | 0 pool.go | 0 README.md | 54 ++++++++++ cmd/TST/ctest/ctest.cpp | 0 /dev/null | 218 ------------------------------------------- producer.go | 0 consumer.go | 0 pointer.go | 0 cmd/clib/libnsqclient.h | 0 cmd/main.go | 0 conn.go | 0 channel.go | 0 cmd/TST/test/test.go | 0 13 files changed, 52 insertions(+), 220 deletions(-) diff --git a/README.md b/README.md index ed54d46..432d86d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,54 @@ -## nsqclient +# NSQPool -nsq client +NSQPool is a thread safe connection pool for nsq producer. It can be used to +manage and reuse nsq producer connection. + +## Install and Usage + +Install the package with: + +```bash +github.com/qgymje/nsqpool +``` + +Import it with: + +```go +import ( + "github.com/qgymje/nsqpool" + nsq "github.com/nsqio/go-nsq" +) +``` + +and use `pool` as the package name inside the code. + +## Example + +```go +// create a factory() to be used with channel based pool +factory := func() (*nsq.Producer, error) { + config := nsq.NewConfig() + return nsq.NewProducer(":4150", config) +} + +nsqPool, err := pool.NewChannelPool(5, 30, factory) + +producer, err := nsqPool.Get() + +producer.Publish("topic", "some data") +// do something with producer and put it back to the pool by closing the connection +// (this doesn't close the underlying connection instead it's putting it back +// to the pool). +producer.Close() + +// close pool any time you want, this closes all the connections inside a pool +nsqPool.Close() + +// currently available connections in the pool +current := nsqPool.Len() +``` + +## License + +The MIT License (MIT) - see LICENSE for more details diff --git a/nsqclient/channel.go b/channel.go similarity index 100% rename from nsqclient/channel.go rename to channel.go diff --git a/TST/ctest/ctest.cpp b/cmd/TST/ctest/ctest.cpp similarity index 100% rename from TST/ctest/ctest.cpp rename to cmd/TST/ctest/ctest.cpp diff --git a/TST/test/test.go b/cmd/TST/test/test.go similarity index 100% rename from TST/test/test.go rename to cmd/TST/test/test.go diff --git a/clib/libnsqclient.h b/cmd/clib/libnsqclient.h similarity index 100% rename from clib/libnsqclient.h rename to cmd/clib/libnsqclient.h diff --git a/main.go b/cmd/main.go similarity index 100% rename from main.go rename to cmd/main.go diff --git a/make.sh b/cmd/make.sh similarity index 100% rename from make.sh rename to cmd/make.sh diff --git a/nsqclient/conn.go b/conn.go similarity index 100% rename from nsqclient/conn.go rename to conn.go diff --git a/nsqclient/consumer.go b/consumer.go similarity index 100% rename from nsqclient/consumer.go rename to consumer.go diff --git a/go.mod b/go.mod deleted file mode 100644 index a5e43e0..0000000 --- a/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module nsqclient - -go 1.14 - -require github.com/nsqio/go-nsq v1.1.0 diff --git a/go.sum b/go.sum deleted file mode 100644 index 457d1d9..0000000 --- a/go.sum +++ /dev/null @@ -1,4 +0,0 @@ -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/nsqio/go-nsq v1.1.0 h1:PQg+xxiUjA7V+TLdXw7nVrJ5Jbl3sN86EhGCQj4+FYE= -github.com/nsqio/go-nsq v1.1.0/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY= diff --git a/gonsqcli b/gonsqcli deleted file mode 100755 index 01eb96b..0000000 --- a/gonsqcli +++ /dev/null Binary files differ diff --git a/nsqclient/LICENSE b/nsqclient/LICENSE deleted file mode 100644 index 25fdaf6..0000000 --- a/nsqclient/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2013 Fatih Arslan - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/nsqclient/README.md b/nsqclient/README.md deleted file mode 100644 index 432d86d..0000000 --- a/nsqclient/README.md +++ /dev/null @@ -1,54 +0,0 @@ -# NSQPool - -NSQPool is a thread safe connection pool for nsq producer. It can be used to -manage and reuse nsq producer connection. - - -## Install and Usage - -Install the package with: - -```bash -github.com/qgymje/nsqpool -``` - -Import it with: - -```go -import ( - "github.com/qgymje/nsqpool" - nsq "github.com/nsqio/go-nsq" -) -``` - -and use `pool` as the package name inside the code. - -## Example - -```go -// create a factory() to be used with channel based pool -factory := func() (*nsq.Producer, error) { - config := nsq.NewConfig() - return nsq.NewProducer(":4150", config) -} - -nsqPool, err := pool.NewChannelPool(5, 30, factory) - -producer, err := nsqPool.Get() - -producer.Publish("topic", "some data") -// do something with producer and put it back to the pool by closing the connection -// (this doesn't close the underlying connection instead it's putting it back -// to the pool). -producer.Close() - -// close pool any time you want, this closes all the connections inside a pool -nsqPool.Close() - -// currently available connections in the pool -current := nsqPool.Len() -``` - -## License - -The MIT License (MIT) - see LICENSE for more details diff --git a/nsqclient/channel_test.go b/nsqclient/channel_test.go deleted file mode 100644 index 725db9b..0000000 --- a/nsqclient/channel_test.go +++ /dev/null @@ -1,218 +0,0 @@ -package nsqclient - -import ( - "math/rand" - "sync" - "testing" - "time" - - "github.com/nsqio/go-nsq" -) - -var ( - InitialCap = 2 - MaximumCap = 10 - factory = func() (*nsq.Producer, error) { - config := nsq.NewConfig() - return nsq.NewProducer(":4160", config) - } -) - -func newChannelPool() (Pool, error) { - return NewChannelPool(InitialCap, MaximumCap, factory) -} - -func TestNew(t *testing.T) { - _, err := newChannelPool() - if err != nil { - t.Errorf("New error: %s", err) - } -} - -func TestPool_Get(t *testing.T) { - p, _ := newChannelPool() - defer p.Close() - - _, err := p.Get() - if err != nil { - t.Errorf("Get error: %s", err) - } - - // after one get, current capacity should be lowered by one. - if p.Len() != (InitialCap - 1) { - t.Errorf("Get error. Expecting %d, got %d", - (InitialCap - 1), p.Len()) - } - - // get them all - var wg sync.WaitGroup - for i := 0; i < (InitialCap - 1); i++ { - wg.Add(1) - go func() { - defer wg.Done() - _, err := p.Get() - if err != nil { - t.Errorf("Get error: %s", err) - } - }() - } - wg.Wait() - - if p.Len() != 0 { - t.Errorf("Get error. Expecting %d, got %d", - (InitialCap - 1), p.Len()) - } - - _, err = p.Get() - if err != nil { - t.Errorf("Get error: %s", err) - } -} - -func TestPool_Put(t *testing.T) { - p, err := NewChannelPool(0, 30, factory) - if err != nil { - t.Fatal(err) - } - defer p.Close() - - // get/create from the pool - conns := make([]*PoolConn, MaximumCap) - for i := 0; i < MaximumCap; i++ { - conn, _ := p.Get() - conns[i] = conn - } - - // now put them all back - for _, conn := range conns { - conn.Close() - } - - if p.Len() != MaximumCap { - t.Errorf("Put error len. Expecting %d, got %d", - 1, p.Len()) - } - - conn, _ := p.Get() - p.Close() // close pool - - conn.Close() // try to put into a full pool - if p.Len() != 0 { - t.Errorf("Put error. Closed pool shouldn't allow to put connections.") - } -} - -func TestPool_PutUnusableConn(t *testing.T) { - p, _ := newChannelPool() - defer p.Close() - - // ensure pool is not empty - conn, _ := p.Get() - conn.Close() - - poolSize := p.Len() - conn, _ = p.Get() - conn.Close() - if p.Len() != poolSize { - t.Errorf("Pool size is expected to be equal to initial size") - } - - conn, _ = p.Get() - conn.MarkUnusable() - - conn.Close() - if p.Len() != poolSize-1 { - t.Errorf("Pool size is expected to be initial_size - 1 [%d:%d]", p.Len(), poolSize-1) - } -} - -func TestPool_UsedCapacity(t *testing.T) { - p, _ := newChannelPool() - defer p.Close() - - if p.Len() != InitialCap { - t.Errorf("InitialCap error. Expecting %d, got %d", - InitialCap, p.Len()) - } -} - -func TestPool_Close(t *testing.T) { - p, _ := newChannelPool() - - // now close it and test all cases we are expecting. - p.Close() - - c := p.(*channelPool) - - if c.conns != nil { - t.Errorf("Close error, conns channel should be nil") - } - - if c.factory != nil { - t.Errorf("Close error, factory should be nil") - } - - _, err := p.Get() - if err == nil { - t.Errorf("Close error, get conn should return an error") - } - - if p.Len() != 0 { - t.Errorf("Close error used capacity. Expecting 0, got %d", p.Len()) - } -} - -func TestPoolConcurrent(t *testing.T) { - p, _ := newChannelPool() - pipe := make(chan *PoolConn, 0) - - go func() { - p.Close() - }() - - for i := 0; i < MaximumCap; i++ { - go func() { - conn, _ := p.Get() - - pipe <- conn - }() - - go func() { - conn := <-pipe - if conn == nil { - return - } - conn.Close() - }() - } -} - -func TestPoolConcurrent2(t *testing.T) { - p, _ := NewChannelPool(0, 30, factory) - - var wg sync.WaitGroup - - go func() { - for i := 0; i < 10; i++ { - wg.Add(1) - go func(i int) { - conn, _ := p.Get() - time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) - conn.Close() - wg.Done() - }(i) - } - }() - - for i := 0; i < 10; i++ { - wg.Add(1) - go func(i int) { - conn, _ := p.Get() - time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) - conn.Close() - wg.Done() - }(i) - } - - wg.Wait() -} diff --git a/nsqclient/pointer.go b/pointer.go similarity index 100% rename from nsqclient/pointer.go rename to pointer.go diff --git a/nsqclient/pool.go b/pool.go similarity index 100% rename from nsqclient/pool.go rename to pool.go diff --git a/nsqclient/producer.go b/producer.go similarity index 100% rename from nsqclient/producer.go rename to producer.go -- Gitblit v1.8.0