From 00cef92d2cfa9f4d842f32d6831b19e73acee9c9 Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Tue, 16 Apr 2024 22:13:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=20release=200.0.9=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/go.yml | 2 +- iox/json_reader_example_test.go | 36 ++++++++++++++++++++++++ list/skip_list_example_test.go | 31 +++++++++++++++++++++ queue/priority_queue_example_test.go | 33 ++++++++++++++++++++++ syncx/limit_pool.go | 8 ++++-- syncx/limit_pool_example_test.go | 38 ++++++++++++++++++++++++++ syncx/limit_pool_test.go | 16 +++++++---- syncx/segment_key_lock_example_test.go | 35 ++++++++++++++++++++++++ 8 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 iox/json_reader_example_test.go create mode 100644 list/skip_list_example_test.go create mode 100644 queue/priority_queue_example_test.go create mode 100644 syncx/limit_pool_example_test.go create mode 100644 syncx/segment_key_lock_example_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index cdefa5cb..026fc62b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -34,7 +34,7 @@ jobs: run: go build -v ./... - name: Test - run: go test -race -coverprofile=cover.out -v ./... + run: go test -race -coverprofile=cover.out ./... - name: Post Coverage uses: codecov/codecov-action@v2 \ No newline at end of file diff --git a/iox/json_reader_example_test.go b/iox/json_reader_example_test.go new file mode 100644 index 00000000..ed0dfb11 --- /dev/null +++ b/iox/json_reader_example_test.go @@ -0,0 +1,36 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package iox_test + +import ( + "fmt" + "net/http" + + "github.com/ecodeclub/ekit/iox" +) + +func ExampleNewJSONReader() { + val := iox.NewJSONReader(User{Name: "Tom"}) + _, err := http.NewRequest(http.MethodPost, "/hello", val) + if err != nil { + fmt.Println(err.Error()) + return + } + fmt.Println("OK") +} + +type User struct { + Name string `json:"name"` +} diff --git a/list/skip_list_example_test.go b/list/skip_list_example_test.go new file mode 100644 index 00000000..f5a45b14 --- /dev/null +++ b/list/skip_list_example_test.go @@ -0,0 +1,31 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package list_test + +import ( + "fmt" + + "github.com/ecodeclub/ekit" + "github.com/ecodeclub/ekit/internal/list" +) + +func ExampleNewSkipList() { + l := list.NewSkipList[int](ekit.ComparatorRealNumber[int]) + l.Insert(123) + val, _ := l.Get(0) + fmt.Println(val) + // Output: + // 123 +} diff --git a/queue/priority_queue_example_test.go b/queue/priority_queue_example_test.go new file mode 100644 index 00000000..f8bfb799 --- /dev/null +++ b/queue/priority_queue_example_test.go @@ -0,0 +1,33 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package queue_test + +import ( + "fmt" + + "github.com/ecodeclub/ekit" + "github.com/ecodeclub/ekit/internal/queue" +) + +func ExampleNewPriorityQueue() { + // 容量,并且队列里面放的是 int + pq := queue.NewPriorityQueue(10, ekit.ComparatorRealNumber[int]) + _ = pq.Enqueue(10) + _ = pq.Enqueue(9) + val, _ := pq.Dequeue() + fmt.Println(val) + // Output: + // 9 +} diff --git a/syncx/limit_pool.go b/syncx/limit_pool.go index b7427f74..2903d6db 100644 --- a/syncx/limit_pool.go +++ b/syncx/limit_pool.go @@ -37,13 +37,15 @@ func NewLimitPool[T any](maxTokens int, factory func() T) *LimitPool[T] { } // Get 取出一个元素 -func (l *LimitPool[T]) Get() T { +// 如果返回值是 true,则代表确实从 Pool 里面取出来了一个 +// 否则是新建了一个 +func (l *LimitPool[T]) Get() (T, bool) { if l.tokens.Add(-1) < 0 { l.tokens.Add(1) var zero T - return zero + return zero, false } - return l.pool.Get() + return l.pool.Get(), true } // Put 放回去一个元素 diff --git a/syncx/limit_pool_example_test.go b/syncx/limit_pool_example_test.go new file mode 100644 index 00000000..64db5606 --- /dev/null +++ b/syncx/limit_pool_example_test.go @@ -0,0 +1,38 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package syncx_test + +import ( + "fmt" + + "github.com/ecodeclub/ekit/syncx" +) + +func ExampleNewLimitPool() { + p := syncx.NewLimitPool(1, func() int { + return 123 + }) + val, ok := p.Get() + fmt.Println("第一次", val, ok) + val, ok = p.Get() + fmt.Println("第二次", val, ok) + p.Put(123) + val, ok = p.Get() + fmt.Println("第三次", val, ok) + // Output: + // 第一次 123 true + // 第二次 0 false + // 第三次 123 true +} diff --git a/syncx/limit_pool_test.go b/syncx/limit_pool_test.go index aad405c8..430b0197 100644 --- a/syncx/limit_pool_test.go +++ b/syncx/limit_pool_test.go @@ -42,8 +42,8 @@ func TestLimitPool(t *testing.T) { go func() { defer wg.Done() - buf := pool.Get() - + buf, ok := pool.Get() + assert.True(t, ok) assert.NotZero(t, buf) assert.Equal(t, string(expectedVal), string(buf)) @@ -55,14 +55,20 @@ func TestLimitPool(t *testing.T) { close(bufChan) // 超过最大申请次数返回零值 - assert.Zero(t, pool.Get()) + val, ok := pool.Get() + assert.True(t, ok) + assert.Zero(t, val) // 归还一个 pool.Put(<-bufChan) // 再次申请仍可以拿到非零值缓冲区 - assert.NotZero(t, string(expectedVal), string(pool.Get())) + val, ok = pool.Get() + assert.True(t, ok) + assert.NotZero(t, string(expectedVal), string(val)) // 超过最大申请次数返回零值 - assert.Zero(t, pool.Get()) + val, ok = pool.Get() + assert.False(t, ok) + assert.Zero(t, val) } diff --git a/syncx/segment_key_lock_example_test.go b/syncx/segment_key_lock_example_test.go new file mode 100644 index 00000000..eb5c9871 --- /dev/null +++ b/syncx/segment_key_lock_example_test.go @@ -0,0 +1,35 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package syncx_test + +import ( + "fmt" + + "github.com/ecodeclub/ekit/syncx" +) + +func ExampleNewSegmentKeysLock() { + // 参数就是分多少段,你也可以理解为总共有多少锁 + // 锁越多,并发竞争越低,但是消耗内存; + // 锁越少,并发竞争越高,但是内存消耗少; + lock := syncx.NewSegmentKeysLock(100) + // 对应的还有 TryLock + // RLock 和 RUnlock + lock.Lock("key1") + defer lock.Unlock("key1") + fmt.Println("OK") + // Output: + // OK +}