├── .github └── workflows │ └── go.yml ├── .gitignore ├── LICENSE ├── README.md ├── docs └── overview.jpg ├── example ├── cancel │ └── main.go ├── cyclically │ └── main.go ├── delay │ └── main.go ├── elapse │ └── main.go ├── start │ └── main.go └── stop │ └── main.go ├── go.mod ├── misc.go ├── pkg ├── linker │ └── doublinker.go └── scheduler │ ├── LICENSE │ ├── README.md │ ├── circularlink.go │ ├── circularlink_test.go │ ├── example │ └── main.go │ ├── scheduler.go │ ├── strategy.go │ └── strategy_test.go ├── slot.go ├── test ├── bench │ └── tolerance.go └── profile │ └── profile.go ├── tick.go ├── timer.go ├── timer_test.go ├── timingwheel.go ├── timingwheel_test.go └── wheel.go /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Go 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Set up Go 17 | uses: actions/setup-go@v2 18 | with: 19 | go-version: 1.17 20 | 21 | - name: Build 22 | run: go build -v ./... 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ---> Go 2 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 3 | *.o 4 | *.a 5 | *.so 6 | 7 | # Folders 8 | _obj 9 | _test 10 | 11 | # Architecture specific extensions/prefixes 12 | *.[568vq] 13 | [568vq].out 14 | 15 | *.cgo1.go 16 | *.cgo2.c 17 | _cgo_defun.c 18 | _cgo_gotypes.go 19 | _cgo_export.* 20 | 21 | _testmain.go 22 | 23 | *.exe 24 | *.test 25 | *.prof 26 | .DS_Store 27 | .*.swp 28 | 29 | # ---> JetBrains 30 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 31 | 32 | *.iml 33 | 34 | ## Directory-based project format: 35 | .idea/ 36 | # if you remove the above rule, at least ignore the following: 37 | 38 | # User-specific stuff: 39 | # .idea/workspace.xml 40 | # .idea/tasks.xml 41 | # .idea/dictionaries 42 | 43 | # Sensitive or high-churn files: 44 | # .idea/dataSources.ids 45 | # .idea/dataSources.xml 46 | # .idea/sqlDataSources.xml 47 | # .idea/dynamic.xml 48 | # .idea/uiDesigner.xml 49 | 50 | # Gradle: 51 | # .idea/gradle.xml 52 | # .idea/libraries 53 | 54 | # Mongo Explorer plugin: 55 | # .idea/mongoSettings.xml 56 | 57 | ## File-based project format: 58 | *.ipr 59 | *.iws 60 | 61 | ## Plugin-specific files: 62 | 63 | # IntelliJ 64 | /out/ 65 | 66 | # mpeltonen/sbt-idea plugin 67 | .idea_modules/ 68 | 69 | # JIRA plugin 70 | atlassian-ide-plugin.xml 71 | 72 | # Crashlytics plugin (for Android Studio and IntelliJ) 73 | com_crashlytics_export_strings.xml 74 | crashlytics.properties 75 | crashlytics-build.properties 76 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GO-TIMER 2 | 3 | [![Go](https://github.com/singchia/go-xtables/actions/workflows/go.yml/badge.svg)](https://github.com/singchia/go-xtables/actions/workflows/go.yml) 4 | [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 5 | [![Go Report Card](https://goreportcard.com/badge/github.com/singchia/go-timer/v2)](https://goreportcard.com/report/github.com/singchia/go-timer/v2) 6 | 7 | ## Overview 8 | 9 | A high performance timer with minimal goroutines. 10 | 11 | ### How it works 12 | 13 | ![](docs/overview.jpg) 14 | 15 | ### Features 16 | 17 | * One goroutine runs all 18 | * Goroutine safe 19 | * Clean and simple, no third-party deps at all 20 | * High performance with timing-wheels algorithm 21 | * Minimal resources use 22 | * Managed data and handler 23 | * Customizing channel 24 | * Well tested 25 | 26 | ## Usage 27 | 28 | ### Quick Start 29 | 30 | ```golang 31 | package main 32 | 33 | import ( 34 | "log" 35 | "time" 36 | 37 | timer "github.com/singchia/go-timer/v2" 38 | ) 39 | 40 | func main() { 41 | t1 := time.Now() 42 | // new timer 43 | t := timer.NewTimer() 44 | // add a tick in 1s 45 | tick := t.Add(time.Second) 46 | // wait for it 47 | <-tick.C() 48 | // tick fired as time is up, calculate and print the elapse 49 | log.Printf("time elapsed: %fs\n", time.Now().Sub(t1).Seconds()) 50 | } 51 | ``` 52 | 53 | ### Async handler 54 | 55 | ```golang 56 | package main 57 | 58 | import ( 59 | "log" 60 | "sync" 61 | "time" 62 | 63 | timer "github.com/singchia/go-timer/v2" 64 | ) 65 | 66 | func main() { 67 | // we need a wait group since using async handler 68 | wg := sync.WaitGroup{} 69 | wg.Add(1) 70 | // new timer 71 | t := timer.NewTimer() 72 | // add a tick in 1s with current time and a async handler 73 | t.Add(time.Second, timer.WithData(time.Now()), timer.WithHandler(func(event *timer.Event) { 74 | defer wg.Done() 75 | // tick fired as time is up, calculate and print the elapse 76 | log.Printf("time elapsed: %fs\n", time.Now().Sub(event.Data.(time.Time)).Seconds()) 77 | })) 78 | 79 | wg.Wait() 80 | } 81 | ``` 82 | 83 | ### With cyclically 84 | 85 | ```golang 86 | package main 87 | 88 | import ( 89 | "log" 90 | "time" 91 | 92 | timer "github.com/singchia/go-timer/v2" 93 | ) 94 | 95 | func main() { 96 | t1 := time.Now() 97 | // new timer 98 | t := timer.NewTimer() 99 | // add cyclical tick in 1s 100 | tick := t.Add(time.Second, timer.WithCyclically()) 101 | for { 102 | // wait for it cyclically 103 | <-tick.C() 104 | t2 := time.Now() 105 | // calculate and print the elapse 106 | log.Printf("time elapsed: %fs\n", t2.Sub(t1).Seconds()) 107 | t1 = t2 108 | } 109 | } 110 | ``` 111 | 112 | ## License 113 | 114 | © Austin Zhai, 2015-2025 115 | 116 | Released under the [Apache License 2.0](https://github.com/singchia/go-timer/blob/master/LICENSE) -------------------------------------------------------------------------------- /docs/overview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singchia/go-timer/825cc388cecb4925e0ba54037cb041422e0335b7/docs/overview.jpg -------------------------------------------------------------------------------- /example/cancel/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "math/rand" 6 | "net/http" 7 | _ "net/http/pprof" 8 | "os" 9 | "sync/atomic" 10 | "time" 11 | 12 | "github.com/singchia/go-timer/v2" 13 | ) 14 | 15 | func main() { 16 | server := &http.Server{ 17 | Addr: ":6060", 18 | Handler: nil, 19 | } 20 | go server.ListenAndServe() 21 | n := 100000 22 | tw := timer.NewTimer(timer.WithTimeInterval(time.Millisecond)) 23 | fired := int32(0) 24 | for i := 0; i < n; i++ { 25 | second := time.Duration(rand.Intn(10)+1) * time.Second 26 | tick := tw.Add(second, timer.WithData(time.Now()), timer.WithHandler(func(event *timer.Event) { 27 | atomic.AddInt32(&fired, 1) 28 | })) 29 | err := tick.Cancel() 30 | if err != nil { 31 | log.Fatal(err) 32 | return 33 | } 34 | } 35 | 36 | sigs := make(chan os.Signal, 1) 37 | tick := tw.Add(time.Second, timer.WithCyclically()) 38 | for { 39 | select { 40 | case <-sigs: 41 | goto END 42 | case <-tick.C(): 43 | log.Println(n, fired) 44 | } 45 | } 46 | END: 47 | log.Println(n, fired) 48 | } 49 | -------------------------------------------------------------------------------- /example/cyclically/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | timer "github.com/singchia/go-timer/v2" 8 | ) 9 | 10 | func main() { 11 | t1 := time.Now() 12 | 13 | t := timer.NewTimer() 14 | tick := t.Add(time.Second, timer.WithCyclically()) 15 | for { 16 | <-tick.C() 17 | t2 := time.Now() 18 | log.Printf("time elapsed: %fs\n", t2.Sub(t1).Seconds()) 19 | t1 = t2 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /example/delay/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "math/rand" 6 | "net/http" 7 | _ "net/http/pprof" 8 | "os" 9 | "os/signal" 10 | "sync/atomic" 11 | "syscall" 12 | "time" 13 | 14 | timer "github.com/singchia/go-timer/v2" 15 | ) 16 | 17 | func main() { 18 | server := &http.Server{ 19 | Addr: ":6060", 20 | Handler: nil, 21 | } 22 | go server.ListenAndServe() 23 | n := 100000 24 | delay := 14 * time.Second 25 | tw := timer.NewTimer() 26 | fired := int32(0) 27 | for i := 0; i < n; i++ { 28 | second := time.Duration(rand.Intn(10)+1) * time.Second 29 | tick := tw.Add(second, timer.WithData(time.Now()), timer.WithHandler(func(event *timer.Event) { 30 | elapse := time.Since(event.Data.(time.Time).Add(second)) 31 | if elapse < delay-time.Second || elapse > delay+time.Second { 32 | log.Println(elapse.Seconds()) 33 | } 34 | atomic.AddInt32(&fired, 1) 35 | })) 36 | err := tick.Delay(delay) 37 | if err != nil { 38 | log.Fatal(err) 39 | return 40 | } 41 | } 42 | 43 | sigs := make(chan os.Signal, 1) 44 | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) 45 | tick := tw.Add(time.Second, timer.WithCyclically()) 46 | for { 47 | select { 48 | case <-sigs: 49 | goto END 50 | case <-tick.C(): 51 | log.Println(n, fired) 52 | } 53 | } 54 | END: 55 | log.Println(n, fired) 56 | } 57 | -------------------------------------------------------------------------------- /example/elapse/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "sync" 6 | "time" 7 | 8 | timer "github.com/singchia/go-timer/v2" 9 | ) 10 | 11 | func main() { 12 | wg := sync.WaitGroup{} 13 | wg.Add(1) 14 | 15 | t := timer.NewTimer() 16 | t.Add(time.Second, timer.WithData(time.Now()), timer.WithHandler(func(event *timer.Event) { 17 | defer wg.Done() 18 | log.Printf("time elapsed: %fs\n", time.Now().Sub(event.Data.(time.Time)).Seconds()) 19 | })) 20 | 21 | wg.Wait() 22 | } 23 | -------------------------------------------------------------------------------- /example/start/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | timer "github.com/singchia/go-timer/v2" 8 | ) 9 | 10 | func main() { 11 | t1 := time.Now() 12 | 13 | t := timer.NewTimer() 14 | tick := t.Add(time.Second) 15 | <-tick.C() 16 | 17 | log.Printf("time elapsed: %fs\n", time.Now().Sub(t1).Seconds()) 18 | } 19 | -------------------------------------------------------------------------------- /example/stop/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "math/rand" 7 | "net/http" 8 | _ "net/http/pprof" 9 | "os" 10 | "os/signal" 11 | "sync/atomic" 12 | "syscall" 13 | "time" 14 | 15 | timer "github.com/singchia/go-timer/v2" 16 | ) 17 | 18 | func main() { 19 | server := &http.Server{ 20 | Addr: ":6060", 21 | Handler: nil, 22 | } 23 | go server.ListenAndServe() 24 | n := 100000 25 | tw := timer.NewTimer() 26 | fired := int32(0) 27 | for i := 0; i < n; i++ { 28 | second := time.Duration(rand.Intn(10)+1) * time.Second 29 | tick := tw.Add(second, timer.WithData(time.Now()), timer.WithHandler(func(event *timer.Event) { 30 | atomic.AddInt32(&fired, 1) 31 | })) 32 | if tick == nil { 33 | fmt.Println("tick nil") 34 | } 35 | } 36 | tw.Close() 37 | 38 | sigs := make(chan os.Signal, 1) 39 | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) 40 | tick := time.NewTicker(time.Second) 41 | for { 42 | select { 43 | case <-sigs: 44 | goto END 45 | case <-tick.C: 46 | log.Println(n, fired) 47 | } 48 | } 49 | END: 50 | log.Println(n, fired) 51 | } 52 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/singchia/go-timer/v2 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /misc.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import "encoding/json" 4 | 5 | type ws struct { 6 | Wheels map[int]interface{} `json:"wheels"` 7 | } 8 | 9 | type w struct { 10 | Slots map[int]interface{} `json:"slots"` 11 | } 12 | 13 | type s struct { 14 | Ticks map[int]interface{} `json:"ticks"` 15 | } 16 | 17 | //for debugging 18 | func (t *timingwheel) Topology() ([]byte, error) { 19 | ws := &ws{ 20 | Wheels: map[int]interface{}{}, 21 | } 22 | for i, wheel := range t.wheels { 23 | w := &w{ 24 | Slots: map[int]interface{}{}, 25 | } 26 | for j, slot := range wheel.slots { 27 | s := &s{ 28 | Ticks: map[int]interface{}{}, 29 | } 30 | k := 0 31 | slot.foreach(func(data interface{}) error { 32 | t := data.(*tick) 33 | s.Ticks[k] = t.duration 34 | k++ 35 | return nil 36 | }) 37 | w.Slots[j] = s 38 | } 39 | ws.Wheels[i] = w 40 | } 41 | return json.Marshal(ws) 42 | } 43 | -------------------------------------------------------------------------------- /pkg/linker/doublinker.go: -------------------------------------------------------------------------------- 1 | package linker 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | type HasEqual interface { 8 | Equal(src interface{}) bool 9 | } 10 | 11 | // we don't want generate a int64 to index the concrete node 12 | // pointer is the best index 13 | type DoubID *doubnode 14 | 15 | type Doublinker struct { 16 | head *doubnode 17 | tail *doubnode 18 | length int64 19 | } 20 | 21 | func NewDoublinker() *Doublinker { 22 | return &Doublinker{ 23 | head: nil, 24 | tail: nil, 25 | length: 0, 26 | } 27 | } 28 | 29 | func (d *Doublinker) Length() int64 { 30 | return d.length 31 | } 32 | 33 | // when node is deleted 34 | type doubnode struct { 35 | data interface{} 36 | next *doubnode 37 | prev *doubnode 38 | } 39 | 40 | // append a node at tail 41 | func (d *Doublinker) Add(data interface{}) DoubID { 42 | node := &doubnode{data: data, next: nil, prev: nil} 43 | 44 | if d.length == 0 { 45 | d.head, d.tail = node, node 46 | d.length++ 47 | return node 48 | } 49 | d.tail.next = node 50 | node.prev = d.tail 51 | d.tail = node 52 | d.length++ 53 | return node 54 | } 55 | 56 | func (d *Doublinker) UniqueAdd(data interface{}) (error, DoubID) { 57 | for itor := d.head; itor != nil; itor = itor.next { 58 | dst, ok := itor.data.(HasEqual) 59 | if ok && dst.Equal(data) { 60 | return errors.New("alrealy exists"), nil 61 | } 62 | } 63 | node := &doubnode{data: data, next: nil, prev: nil} 64 | 65 | if d.length == 0 { 66 | d.head, d.tail = node, node 67 | d.length++ 68 | return nil, node 69 | } 70 | d.tail.next = node 71 | node.prev = d.tail 72 | d.tail = node 73 | d.length++ 74 | return nil, node 75 | 76 | } 77 | 78 | func (d *Doublinker) UniqueDelete(data interface{}) error { 79 | if data == nil { 80 | return errors.New("data empty") 81 | } 82 | 83 | if d.length == 0 { 84 | return errors.New("empty doublinker") 85 | } 86 | 87 | dst, ok := d.head.data.(HasEqual) 88 | if d.length == 1 && ok && dst.Equal(data) { 89 | d.head, d.tail = nil, nil 90 | d.length-- 91 | return nil 92 | } 93 | if ok && dst.Equal(data) { 94 | d.head = d.head.next 95 | d.head.prev = nil 96 | d.length-- 97 | return nil 98 | } 99 | 100 | dst, ok = d.tail.data.(HasEqual) 101 | if ok && dst.Equal(data) { 102 | d.tail = d.tail.prev 103 | d.tail.next = nil 104 | d.length-- 105 | return nil 106 | } 107 | //not first and last 108 | for itor := d.head; itor != nil; itor = itor.next { 109 | dst, ok := itor.data.(HasEqual) 110 | if ok && dst.Equal(data) { 111 | itor.prev.next = itor.next 112 | itor.next.prev = itor.prev 113 | d.length-- 114 | return nil 115 | } 116 | } 117 | return errors.New("not found") 118 | } 119 | 120 | func (d *Doublinker) UniqueRetrieve(data interface{}) (error, interface{}) { 121 | for itor := d.head; itor != nil; itor = itor.next { 122 | dst, ok := itor.data.(HasEqual) 123 | if ok && dst.Equal(data) { 124 | return nil, itor.data 125 | } 126 | } 127 | return errors.New("not found"), nil 128 | } 129 | 130 | func (d *Doublinker) Delete(id DoubID) error { 131 | 132 | if id == nil { 133 | return errors.New("id empty") 134 | } 135 | 136 | if d.length == 0 { 137 | return errors.New("linker empty") 138 | } 139 | 140 | node := (*doubnode)(id) 141 | 142 | if d.length == 1 && d.head == node { 143 | d.head, d.tail = nil, nil 144 | d.length-- 145 | return nil 146 | } 147 | if d.head == node { 148 | d.head = d.head.next 149 | d.head.prev = nil 150 | d.length-- 151 | return nil 152 | } 153 | if d.tail == node { 154 | d.tail = d.tail.prev 155 | d.tail.next = nil 156 | d.length-- 157 | return nil 158 | } 159 | if node.prev == nil || node.next == nil { 160 | return errors.New("isolated node") 161 | } 162 | node.prev.next = node.next 163 | node.next.prev = node.prev 164 | d.length-- 165 | return nil 166 | } 167 | 168 | func (d *Doublinker) Update(id DoubID, data interface{}) error { 169 | if id == nil { 170 | return errors.New("id empty") 171 | } 172 | node := (*doubnode)(id) 173 | node.data = data 174 | return nil 175 | } 176 | 177 | func (d *Doublinker) Retrieve(id DoubID) interface{} { 178 | node := (*doubnode)(id) 179 | return node.data 180 | } 181 | 182 | func (d *Doublinker) RetrieveFree(id DoubID) interface{} { 183 | node := (*doubnode)(id) 184 | return node.data 185 | } 186 | 187 | // move to another doublinker 188 | func (d *Doublinker) UniqueMove(data interface{}, dst *Doublinker) error { 189 | if data == nil || dst == nil { 190 | return errors.New("data or dst doublinker empty") 191 | } 192 | var node *doubnode 193 | 194 | for itor := d.head; itor != nil; itor = itor.next { 195 | dst, ok := itor.data.(HasEqual) 196 | if ok && dst.Equal(data) { 197 | node = itor 198 | break 199 | } 200 | } 201 | 202 | if d.length == 1 && d.head == node { 203 | d.head, d.tail = nil, nil 204 | d.length-- 205 | 206 | } else if d.head == node { 207 | d.head = node.next 208 | d.head.prev = nil 209 | d.length-- 210 | 211 | } else if d.tail == node { 212 | d.tail = d.tail.prev 213 | d.tail.next = nil 214 | d.length-- 215 | 216 | } else { 217 | node.prev.next = node.next 218 | node.next.prev = node.prev 219 | d.length-- 220 | } 221 | 222 | node.prev = nil 223 | node.next = nil 224 | 225 | return dst.Take(node) 226 | } 227 | 228 | // move to another doublinker 229 | func (d *Doublinker) Move(id DoubID, dst *Doublinker) error { 230 | if id == nil || dst == nil { 231 | return errors.New("id or dst doublinker empty") 232 | } 233 | node := (*doubnode)(id) 234 | 235 | if d.length == 1 && d.head == node { 236 | d.head, d.tail = nil, nil 237 | d.length-- 238 | 239 | } else if d.head == node { 240 | d.head = node.next 241 | d.head.prev = nil 242 | d.length-- 243 | 244 | } else if d.tail == node { 245 | d.tail = d.tail.prev 246 | d.tail.next = nil 247 | d.length-- 248 | 249 | } else { 250 | node.prev.next = node.next 251 | node.next.prev = node.prev 252 | d.length-- 253 | } 254 | 255 | return dst.Take(node) 256 | } 257 | 258 | func (d *Doublinker) Take(node *doubnode) error { 259 | if node == nil { 260 | return errors.New("node empty") 261 | } 262 | 263 | if d.length == 0 { 264 | d.head, d.tail = node, node 265 | d.length++ 266 | return nil 267 | } 268 | d.tail.next = node 269 | node.prev = d.tail 270 | d.tail = node 271 | d.length++ 272 | return nil 273 | 274 | } 275 | 276 | type ForeachDoubNodeFunc func(id DoubID) error 277 | 278 | func (d *Doublinker) ForeachNode(f ForeachDoubNodeFunc) error { 279 | for itor := d.head; itor != nil; itor = itor.next { 280 | err := f(DoubID(itor)) 281 | if err != nil { 282 | return err 283 | } 284 | } 285 | return nil 286 | } 287 | 288 | type ForeachFunc func(data interface{}) error 289 | 290 | func (d *Doublinker) Foreach(f ForeachFunc) error { 291 | for itor := d.head; itor != nil; itor = itor.next { 292 | err := f(itor.data) 293 | if err != nil { 294 | return err 295 | } 296 | } 297 | return nil 298 | } 299 | -------------------------------------------------------------------------------- /pkg/scheduler/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 SingChia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pkg/scheduler/README.md: -------------------------------------------------------------------------------- 1 | # go-scheduler 2 | A simple scheduler for goroutines, **go-scheduler** helps to manage goroutines, only needs to set three optional quotas: 3 | 4 | * the maximum count of goroutines 5 | * the maximum count of processed requests per interval 6 | * the maximum value of rate (processed requests per interval/ incoming requests per interval) 7 | 8 | Actually **go-sheduler** only adjust count of goroutines to satisfy those quotas if set, the default strategy works like gradienter, if runtime statistics don't match any quotas, **go-sheduler** starts to work. 9 | Since scheduler manage goroutines to handle user's **_Request_** which contains **_Data_** and **_Handler_**, the scheduler simple call **_Request.Handler(Request.Data)_**. 10 | **note:** three optional quotas are only undercontrolled in **go-scheduler** 11 | 12 | ## How-to-use 13 | **go-scheduler** supplies several easy-understand and easy-integrate interfaces, Let's see a easy sample. 14 | ``` golang 15 | import ( 16 | "fmt" 17 | "sync/atomic" 18 | "time" 19 | scheduler "github.com/singchia/go-scheduler" 20 | ) 21 | 22 | func main() { 23 | sch := scheduler.NewScheduler() 24 | 25 | sch.SetMaxGoroutines(5000) 26 | sch.StartSchedule() 27 | 28 | var val int64 29 | for i := 0; i < 10*10000*10000; i++ { 30 | sch.PublishRequest(&scheduler.Request{Data: val, Handler: SchedulerHandler}) 31 | atomic.AddInt64(&val, 1) 32 | } 33 | time.Sleep(time.Second * 5) 34 | fmt.Printf("maxValue: %d\n", maxValue) 35 | sch.Close() 36 | } 37 | 38 | var maxValue int64 = 0 39 | 40 | func SchedulerHandler(data interface{}) { 41 | val, ok := data.(int64) 42 | if ok { 43 | if val > maxValue { 44 | maxValue = val 45 | } 46 | } 47 | } 48 | ``` 49 | It's not a good sample in production environment, but it does illustrate the usage of **go-scheduler**. After **_SetMaxGoroutines(5000)_**, the max count of scheduler's goroutines shouldn't go beyond the range **_5000_**, use **_StartSchedule_** to start the scheduler, publish the **_Request_** into the scheduler by using **_PublishRequest_**, then scheduler will handle the request undercontrol. 50 | 51 | ## Installation 52 | If you don't have the Go development environment installed, visit the [Getting Started](https://golang.org/doc/install) document and follow the instructions. Once you're ready, execute the following command: 53 | ``` 54 | go get -u github.com/singchia/go-scheduler 55 | ``` 56 | 57 | ## Interfaces 58 | #### _Scheduler.Interval_ 59 | This should be set before call **_StartSchedule_** and bigger than **_500us_**, if not set or less than **_500us_**, default 200ms. 60 | 61 | #### _Scheduler.SetMaxGoroutines(int64)_ 62 | This limits the max count of goroutines in **go-scheduler**, can be set at any time. 63 | 64 | #### _Scheduler.SetMaxProcessedReqs(int64)_ 65 | This limits the max processed requests per interval, can be set at any time. 66 | 67 | #### _Scheduler.SetMaxRate(float64)_ 68 | The rate is the value of processed requests / incoming requests, bigger means you want a faster speed to handle requests, can be set at any time. 69 | 70 | #### _Scheduler.SetDefaultHandler(scheduler.Handler)_ 71 | If you want set a default handler when **_scheduler.Request.Handler_** not given, can be set at any time. 72 | 73 | #### _Scheduler.SetMonitor(scheduler.Monitor)_ 74 | You can use this to monitor incoming requests, processed requests, shift(changing of goroutines), count of goroutines this interval, can be set at any time. 75 | 76 | #### _Scheduler.SetStrategy(scheduler.Strategy)_ 77 | **_scheduler.Strategy_** is the key deciding how to shift(update) the count of goroutines, you can replace it as your own strategy. 78 | 79 | ## Strategy 80 | #### _scheduler.Gradienter_ 81 | Defaultly **go-scheduler** uses _Gradienter_ as strategy, it behaves like: 82 | ``` 83 | if incoming requests == 0 then shrink 20% 84 | if any quotas > max quotas then shrink the count of goroutines 85 | if quotas == 1 then shrink directly to MaxGoroutines 86 | else shrink 20% 87 | if all quotas < max quotas then expand randFloat * incomingReqs / (incomingReqs + maxCountGoroutines) * (maxCountGoroutines - currentCountGoroutines) 88 | ``` 89 | 90 | #### other strategies 91 | In scheduler file, a circularLink.go exists, I was trying to look for next goroutines-updating by using history status, but temporarily no idea came up, if you have some idea welcome to contact me. 92 | -------------------------------------------------------------------------------- /pkg/scheduler/circularlink.go: -------------------------------------------------------------------------------- 1 | package scheduler 2 | 3 | import "sync" 4 | 5 | type CircularList struct { 6 | head *CircularNode 7 | tail *CircularNode 8 | length int 9 | cur *CircularNode 10 | mutex sync.RWMutex 11 | } 12 | 13 | type CircularNode struct { 14 | data interface{} 15 | next *CircularNode 16 | } 17 | 18 | type ForEachFunc func(node *CircularNode) error 19 | 20 | func NewCircularList() *CircularList { 21 | return &CircularList{head: nil, tail: nil, length: 0, cur: nil} 22 | } 23 | 24 | func (c *CircularList) ForEach(f ForEachFunc) bool { 25 | c.mutex.RLock() 26 | defer c.mutex.RUnlock() 27 | 28 | itor := c.head 29 | for i := 0; i < c.length; i++ { 30 | err := f(itor) 31 | if err != nil { 32 | return false 33 | } 34 | itor = itor.next 35 | } 36 | return true 37 | } 38 | 39 | func (c *CircularList) generateNode(data interface{}) *CircularNode { 40 | return &CircularNode{data: data, next: nil} 41 | } 42 | 43 | func (c *CircularList) AppendNode(data interface{}) *CircularNode { 44 | node := c.generateNode(data) 45 | c.mutex.Lock() 46 | defer c.mutex.Unlock() 47 | 48 | if c.length == 0 { 49 | //first time to append node 50 | c.head = node 51 | c.tail = node 52 | c.tail.next = c.head 53 | c.cur = node 54 | c.length++ 55 | return node 56 | } 57 | 58 | c.tail.next = node 59 | c.tail = c.tail.next 60 | c.tail.next = c.head 61 | c.length++ 62 | return node 63 | } 64 | 65 | func (c *CircularList) DeleteNode(node *CircularNode) bool { 66 | c.mutex.Lock() 67 | defer c.mutex.Unlock() 68 | 69 | var nextItor *CircularNode 70 | itor := c.tail 71 | for i := 0; i < c.length; i++ { 72 | nextItor = itor.next 73 | if nextItor == node { 74 | //if the node is current pointed node, then move forward 75 | //if c.length == 0, don't need to specify corresponding action, 76 | //it will be repointed at AppendNode 77 | if c.cur == node { 78 | c.cur = c.cur.next 79 | } 80 | //if the node is tail, tail should move backward 81 | if c.tail == node { 82 | c.tail = itor 83 | } 84 | itor.next = nextItor.next 85 | c.length-- 86 | return true 87 | } 88 | itor = itor.next 89 | } 90 | return false 91 | } 92 | 93 | func (c *CircularList) GetCurNode() *CircularNode { 94 | c.mutex.RLock() 95 | defer c.mutex.RUnlock() 96 | if c.length == 0 { 97 | return nil 98 | } 99 | node := *c.cur 100 | return &node 101 | } 102 | 103 | func (c *CircularList) GetCurNodeWithNoCopied() *CircularNode { 104 | c.mutex.RLock() 105 | defer c.mutex.RUnlock() 106 | if c.length == 0 { 107 | return nil 108 | } 109 | return c.cur 110 | } 111 | 112 | func (c *CircularList) RightShiftCurPointer() bool { 113 | c.mutex.Lock() 114 | defer c.mutex.Unlock() 115 | if c.length == 0 { 116 | return false 117 | } 118 | c.cur = c.cur.next 119 | return true 120 | } 121 | 122 | func (c *CircularList) RightShiftCurPointerAndUpdate(data interface{}) bool { 123 | c.mutex.Lock() 124 | defer c.mutex.Unlock() 125 | if c.length == 0 { 126 | return false 127 | } 128 | c.cur = c.cur.next 129 | c.cur.data = data 130 | return true 131 | } 132 | 133 | func (c *CircularList) RightShiftCurPointerToCertainNode(dstNode *CircularNode) bool { 134 | c.mutex.Lock() 135 | defer c.mutex.Unlock() 136 | 137 | itor := c.head 138 | for i := 0; i < c.length; i++ { 139 | if itor == dstNode { 140 | c.cur = dstNode 141 | return true 142 | } 143 | itor = itor.next 144 | } 145 | return false 146 | } 147 | -------------------------------------------------------------------------------- /pkg/scheduler/circularlink_test.go: -------------------------------------------------------------------------------- 1 | package scheduler 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func ForEachFunction(node *CircularNode) error { 9 | fmt.Printf("%p, %v\n", node, node.data) 10 | return nil 11 | } 12 | 13 | func Test_AppendNode(t *testing.T) { 14 | circularList := NewCircularList() 15 | 16 | circularList.ForEach(ForEachFunction) 17 | fmt.Printf("**********************\n") 18 | 19 | node := circularList.AppendNode(1) 20 | if node == nil { 21 | t.Error("empty") 22 | return 23 | } 24 | circularList.ForEach(ForEachFunction) 25 | fmt.Printf("**********************\n") 26 | 27 | node = circularList.AppendNode(2) 28 | if node == nil { 29 | t.Error("empty") 30 | return 31 | } 32 | circularList.ForEach(ForEachFunction) 33 | fmt.Printf("**********************\n") 34 | 35 | node = circularList.AppendNode(3) 36 | if node == nil { 37 | t.Error("empty") 38 | return 39 | } 40 | circularList.ForEach(ForEachFunction) 41 | fmt.Printf("**********************\n") 42 | } 43 | 44 | func Test_DeleteNode_Empty(t *testing.T) { 45 | circularList := NewCircularList() 46 | ok := circularList.DeleteNode(&CircularNode{}) 47 | if !ok { 48 | t.Log("node not exists") 49 | return 50 | } 51 | t.Error("delete empty node abnormal") 52 | } 53 | 54 | func Test_DeleteNode_Unempty(t *testing.T) { 55 | circularList := NewCircularList() 56 | 57 | circularList.ForEach(ForEachFunction) 58 | fmt.Printf("**********************\n") 59 | 60 | //add node1 61 | node1 := circularList.AppendNode(1) 62 | if node1 == nil { 63 | t.Error("empty") 64 | return 65 | } 66 | circularList.ForEach(ForEachFunction) 67 | fmt.Printf("**********************\n") 68 | 69 | //add node2 70 | node2 := circularList.AppendNode(2) 71 | if node2 == nil { 72 | t.Error("empty") 73 | return 74 | } 75 | circularList.ForEach(ForEachFunction) 76 | fmt.Printf("**********************\n") 77 | 78 | //delete node2 79 | ok := circularList.DeleteNode(node2) 80 | if !ok { 81 | t.Error("node not exists") 82 | return 83 | } 84 | circularList.ForEach(ForEachFunction) 85 | fmt.Printf("**********************\n") 86 | 87 | //add node3 88 | node3 := circularList.AppendNode(3) 89 | if node3 == nil { 90 | t.Error("empty") 91 | return 92 | } 93 | circularList.ForEach(ForEachFunction) 94 | fmt.Printf("**********************\n") 95 | 96 | //delete node3 97 | ok = circularList.DeleteNode(node3) 98 | if !ok { 99 | t.Error("node not exists") 100 | return 101 | } 102 | circularList.ForEach(ForEachFunction) 103 | fmt.Printf("**********************\n") 104 | 105 | //delete a unexist node 106 | ok = circularList.DeleteNode(&CircularNode{}) 107 | if ok { 108 | t.Error("node should not exists") 109 | return 110 | } 111 | circularList.ForEach(ForEachFunction) 112 | fmt.Printf("**********************\n") 113 | 114 | //delete node1 115 | ok = circularList.DeleteNode(node1) 116 | if !ok { 117 | t.Error("node not exists") 118 | return 119 | } 120 | circularList.ForEach(ForEachFunction) 121 | fmt.Printf("**********************\n") 122 | } 123 | 124 | func Test_GetCurNode(t *testing.T) { 125 | circularList := NewCircularList() 126 | //get current node when list empty 127 | node := circularList.GetCurNodeWithNoCopied() 128 | if node != nil { 129 | t.Error("cur node abnormal exists") 130 | return 131 | } 132 | circularList.ForEach(ForEachFunction) 133 | fmt.Printf("**********************\n") 134 | 135 | //add node1 136 | node1 := circularList.AppendNode(1) 137 | if node1 == nil { 138 | t.Error("empty") 139 | return 140 | } 141 | circularList.ForEach(ForEachFunction) 142 | 143 | //get current node 144 | node = circularList.GetCurNodeWithNoCopied() 145 | if node == nil { 146 | t.Error("cur node abnormal exists") 147 | return 148 | } 149 | fmt.Printf("current node: %p, %v\n", node, node.data) 150 | fmt.Printf("**********************\n") 151 | 152 | //add node2 153 | node2 := circularList.AppendNode(2) 154 | if node2 == nil { 155 | t.Error("empty") 156 | return 157 | } 158 | circularList.ForEach(ForEachFunction) 159 | //get current node 160 | node = circularList.GetCurNodeWithNoCopied() 161 | if node == nil { 162 | t.Error("cur node not exists") 163 | return 164 | } 165 | fmt.Printf("current node: %p, %v\n", node, node.data) 166 | fmt.Printf("**********************\n") 167 | 168 | //delete node2 169 | ok := circularList.DeleteNode(node2) 170 | if !ok { 171 | t.Error("node not exists") 172 | return 173 | } 174 | circularList.ForEach(ForEachFunction) 175 | 176 | //get current node 177 | node = circularList.GetCurNodeWithNoCopied() 178 | if node == nil { 179 | t.Error("cur node not exists") 180 | return 181 | } 182 | fmt.Printf("current node: %p, %v\n", node, node.data) 183 | fmt.Printf("**********************\n") 184 | 185 | //delete node1 186 | ok = circularList.DeleteNode(node1) 187 | if !ok { 188 | t.Error("node not exists") 189 | return 190 | } 191 | circularList.ForEach(ForEachFunction) 192 | 193 | //get current node 194 | node = circularList.GetCurNode() 195 | if node != nil { 196 | t.Error("cur node abnormal exists") 197 | fmt.Printf("current node: %p, %v", node, node.data) 198 | return 199 | } 200 | fmt.Printf("**********************\n") 201 | } 202 | 203 | func Test_RightShiftCurPointer(t *testing.T) { 204 | circularList := NewCircularList() 205 | 206 | //add node1 207 | node1 := circularList.AppendNode(1) 208 | if node1 == nil { 209 | t.Error("empty") 210 | return 211 | } 212 | circularList.ForEach(ForEachFunction) 213 | fmt.Printf("**********************\n") 214 | 215 | //get current node 216 | node := circularList.GetCurNodeWithNoCopied() 217 | if node == nil { 218 | t.Error("cur node abnormal exists") 219 | return 220 | } 221 | fmt.Printf("current node: %p, %v\n", node, node.data) 222 | fmt.Printf("**********************\n") 223 | 224 | //add node2 225 | node2 := circularList.AppendNode(2) 226 | if node2 == nil { 227 | t.Error("empty") 228 | return 229 | } 230 | circularList.ForEach(ForEachFunction) 231 | //get current node 232 | node = circularList.GetCurNodeWithNoCopied() 233 | if node == nil { 234 | t.Error("cur node not exists") 235 | return 236 | } 237 | fmt.Printf("current node: %p, %v\n", node, node.data) 238 | fmt.Printf("**********************\n") 239 | 240 | //right shift the cur pointer 241 | ok := circularList.RightShiftCurPointer() 242 | if !ok { 243 | t.Error("rigth shift error") 244 | return 245 | } 246 | //get current node 247 | node = circularList.GetCurNodeWithNoCopied() 248 | if node == nil { 249 | t.Error("cur node not exists") 250 | return 251 | } 252 | fmt.Printf("current node: %p, %v\n", node, node.data) 253 | fmt.Printf("**********************\n") 254 | 255 | //right shift the cur pointer 256 | ok = circularList.RightShiftCurPointer() 257 | if !ok { 258 | t.Error("rigth shift error") 259 | return 260 | } 261 | //get current node 262 | node = circularList.GetCurNodeWithNoCopied() 263 | if node == nil { 264 | t.Error("cur node not exists") 265 | return 266 | } 267 | fmt.Printf("current node: %p, %v\n", node, node.data) 268 | fmt.Printf("**********************\n") 269 | } 270 | 271 | func Test_RightShiftCurPointerAndUpdate(t *testing.T) { 272 | circularList := NewCircularList() 273 | 274 | //add node1 275 | node1 := circularList.AppendNode(1) 276 | if node1 == nil { 277 | t.Error("empty") 278 | return 279 | } 280 | circularList.ForEach(ForEachFunction) 281 | fmt.Printf("**********************\n") 282 | 283 | //add node2 284 | node2 := circularList.AppendNode(2) 285 | if node2 == nil { 286 | t.Error("empty") 287 | return 288 | } 289 | circularList.ForEach(ForEachFunction) 290 | fmt.Printf("**********************\n") 291 | 292 | //right shift the cur pointer and update the data 293 | ok := circularList.RightShiftCurPointerAndUpdate(3) 294 | if !ok { 295 | t.Error("rigth shift error") 296 | return 297 | } 298 | //get current node 299 | node := circularList.GetCurNodeWithNoCopied() 300 | if node == nil { 301 | t.Error("cur node not exists") 302 | return 303 | } 304 | fmt.Printf("current node: %p, %v\n", node, node.data) 305 | fmt.Printf("**********************\n") 306 | } 307 | 308 | func Test_RightShiftCurPointerToCertainNode(t *testing.T) { 309 | circularList := NewCircularList() 310 | 311 | //add node1 312 | node1 := circularList.AppendNode(1) 313 | if node1 == nil { 314 | t.Error("empty") 315 | return 316 | } 317 | circularList.ForEach(ForEachFunction) 318 | fmt.Printf("**********************\n") 319 | 320 | //add node2 321 | node2 := circularList.AppendNode(2) 322 | if node2 == nil { 323 | t.Error("empty") 324 | return 325 | } 326 | circularList.ForEach(ForEachFunction) 327 | fmt.Printf("**********************\n") 328 | 329 | //add node3 330 | node3 := circularList.AppendNode(3) 331 | if node3 == nil { 332 | t.Error("empty") 333 | return 334 | } 335 | circularList.ForEach(ForEachFunction) 336 | fmt.Printf("**********************\n") 337 | 338 | //right shift the cur pointer 339 | ok := circularList.RightShiftCurPointerToCertainNode(node3) 340 | if !ok { 341 | t.Error("rigth shift error") 342 | return 343 | } 344 | //get current node 345 | node := circularList.GetCurNodeWithNoCopied() 346 | if node == nil { 347 | t.Error("cur node not exists") 348 | return 349 | } 350 | fmt.Printf("current node: %p, %v\n", node, node.data) 351 | fmt.Printf("**********************\n") 352 | } 353 | -------------------------------------------------------------------------------- /pkg/scheduler/example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | _ "net/http/pprof" 7 | "os" 8 | "os/signal" 9 | "runtime" 10 | "sync/atomic" 11 | "syscall" 12 | "time" 13 | 14 | "github.com/singchia/go-timer/v2/pkg/scheduler" 15 | ) 16 | 17 | func main() { 18 | 19 | go func() { 20 | http.ListenAndServe("0.0.0.0:6060", nil) 21 | }() 22 | 23 | sch := scheduler.NewScheduler() 24 | sch.Interval = time.Millisecond * 100 25 | 26 | sch.SetMonitor(SchedulerMonitor) 27 | sch.SetMaxRate(0.95) 28 | sch.SetMaxGoroutines(5000) 29 | sch.StartSchedule() 30 | 31 | var val int64 32 | for i := 0; i < 100*10000*10; i++ { 33 | sch.PublishRequest(&scheduler.Request{Data: val, Handler: SchedulerHandler}) 34 | atomic.AddInt64(&val, 1) 35 | } 36 | time.Sleep(time.Second * 10) 37 | fmt.Printf("maxValue: %d\n", maxValue) 38 | 39 | sch.Close() 40 | 41 | sigs := make(chan os.Signal, 1) 42 | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) 43 | 44 | done := make(chan bool, 1) 45 | go func() { 46 | <-sigs 47 | done <- true 48 | }() 49 | <-done 50 | 51 | fmt.Println(runtime.NumGoroutine()) 52 | } 53 | 54 | var maxValue int64 = 0 55 | 56 | func SchedulerHandler(data interface{}) { 57 | val, ok := data.(int64) 58 | if ok { 59 | if val > maxValue { 60 | maxValue = val 61 | } 62 | } 63 | } 64 | 65 | func SchedulerMonitor(incomingReqsDiff, processedReqsDiff, diff, currentGotoutines int64) { 66 | fmt.Printf("%d, %d, %d, %d\n", incomingReqsDiff, processedReqsDiff, diff, currentGotoutines) 67 | } 68 | -------------------------------------------------------------------------------- /pkg/scheduler/scheduler.go: -------------------------------------------------------------------------------- 1 | package scheduler 2 | 3 | import ( 4 | "sync" 5 | "sync/atomic" 6 | "time" 7 | ) 8 | 9 | type Scheduler struct { 10 | //the interval of goroutines number changing, min 200ms, default 1s 11 | //should be set before Schedule called 12 | Interval time.Duration 13 | 14 | incomingChan chan *Request 15 | 16 | //gorouties close chan 17 | closeChan chan struct{} 18 | 19 | //for shutting down whole scheduler 20 | allCloseChan chan struct{} 21 | 22 | //count of whole incoming requests 23 | countIncomingReqs int64 24 | 25 | //count of whole incoming requests until (now - interval) 26 | countIncomingReqsL int64 27 | 28 | //count of whole processed requests 29 | countProcessedReqs int64 30 | 31 | //count of whole processed requests until (now - interval) 32 | countProcessedReqsL int64 33 | 34 | //number of goroutines 35 | numActives int64 36 | 37 | defaultHandler Handler 38 | monitor Monitor 39 | strategy Strategy 40 | 41 | //runtimeLock locks strategy, defaultHandler, any changes refer to them locked 42 | runtimeLock sync.RWMutex 43 | } 44 | 45 | type Monitor func(incomingReqsLastInterval, processedReqsLastInterval, shift, numActives int64) 46 | type Handler func(data interface{}) 47 | 48 | type Request struct { 49 | Data interface{} 50 | Handler Handler 51 | } 52 | 53 | func NewScheduler() *Scheduler { 54 | var initialNum int64 = 1 55 | scheduler := &Scheduler{ 56 | Interval: time.Millisecond * 200, 57 | strategy: NewGradienter(), 58 | countIncomingReqs: 0, 59 | countIncomingReqsL: 0, 60 | countProcessedReqs: 0, 61 | countProcessedReqsL: 0, 62 | numActives: 0, 63 | incomingChan: make(chan *Request, 1024), 64 | closeChan: make(chan struct{}, 1024), 65 | allCloseChan: make(chan struct{})} 66 | scheduler.expandGoRoutines(initialNum) 67 | return scheduler 68 | } 69 | 70 | func (s *Scheduler) SetDefaultHandler(handler Handler) { 71 | s.runtimeLock.Lock() 72 | defer s.runtimeLock.Unlock() 73 | s.defaultHandler = handler 74 | } 75 | 76 | func (s *Scheduler) SetMonitor(monitor Monitor) { 77 | s.runtimeLock.Lock() 78 | defer s.runtimeLock.Unlock() 79 | s.monitor = monitor 80 | } 81 | 82 | func (s *Scheduler) SetStrategy(strategy Strategy) { 83 | s.runtimeLock.Lock() 84 | defer s.runtimeLock.Unlock() 85 | s.strategy = strategy 86 | } 87 | 88 | // max number of active goroutines, -1 means not limited 89 | // default -1, can be set at any runtime 90 | func (s *Scheduler) SetMaxGoroutines(maxCountGoroutines int64) { 91 | s.runtimeLock.Lock() 92 | defer s.runtimeLock.Unlock() 93 | s.strategy.SetMaxActives(maxCountGoroutines) 94 | } 95 | 96 | // max number of request processed per second, -1 means not limited 97 | // default -1, can be set at any runtime 98 | func (s *Scheduler) SetMaxProcessedReqs(maxProcessedReqs int64) { 99 | s.runtimeLock.Lock() 100 | defer s.runtimeLock.Unlock() 101 | s.strategy.SetMaxProcessedReqs(maxProcessedReqs) 102 | } 103 | 104 | // max rate of (incoming requests)/(processed requests), should between 0 and 1 105 | // default 1 106 | func (s *Scheduler) SetMaxRate(rate float64) { 107 | s.runtimeLock.Lock() 108 | defer s.runtimeLock.Unlock() 109 | s.strategy.SetMaxRate(rate) 110 | } 111 | 112 | func (s *Scheduler) StartSchedule() { 113 | go s.control() 114 | } 115 | 116 | func (s *Scheduler) control() { 117 | if s.Interval < time.Microsecond*500 { 118 | s.Interval = time.Millisecond * 200 119 | } 120 | 121 | ticker := time.NewTicker(s.Interval) 122 | defer ticker.Stop() 123 | for { 124 | select { 125 | case _, ok := <-ticker.C: 126 | if !ok { 127 | return 128 | } 129 | numActives := atomic.LoadInt64(&s.numActives) 130 | incomingReqsDiff := s.countIncomingReqs - s.countIncomingReqsL 131 | processedReqsDiff := s.countProcessedReqs - s.countProcessedReqsL 132 | s.countIncomingReqsL = s.countIncomingReqs 133 | s.countProcessedReqsL = s.countProcessedReqs 134 | 135 | s.runtimeLock.RLock() 136 | shift := s.strategy.ExpandOrShrink(incomingReqsDiff, processedReqsDiff, numActives) 137 | s.runtimeLock.RUnlock() 138 | 139 | if shift < 0 && numActives == 1 { 140 | // at least reserve 1 to handle request 141 | if s.monitor != nil { 142 | s.monitor(incomingReqsDiff, processedReqsDiff, shift, numActives) 143 | } 144 | continue 145 | } else { 146 | if s.monitor != nil { 147 | s.monitor(incomingReqsDiff, processedReqsDiff, shift, numActives+shift) 148 | } 149 | } 150 | if shift > 0 { 151 | s.expandGoRoutines(shift) 152 | } else { 153 | s.shrinkGoRoutines(-shift) 154 | } 155 | case <-s.allCloseChan: 156 | return 157 | } 158 | } 159 | } 160 | 161 | func (s *Scheduler) shrinkGoRoutines(num int64) { 162 | var i int64 163 | for i = 0; i < num; i++ { 164 | s.closeChan <- struct{}{} 165 | } 166 | } 167 | 168 | func (s *Scheduler) expandGoRoutines(num int64) { 169 | var i int64 170 | for i = 0; i < num; i++ { 171 | go func() { 172 | atomic.AddInt64(&s.numActives, 1) 173 | for { 174 | select { 175 | case r, ok := <-s.incomingChan: 176 | if !ok { 177 | atomic.AddInt64(&s.numActives, -1) 178 | return 179 | } 180 | if r.Handler == nil && s.defaultHandler != nil { 181 | s.runtimeLock.RLock() 182 | s.defaultHandler(r.Data) 183 | s.runtimeLock.RUnlock() 184 | 185 | } else if r.Handler != nil { 186 | r.Handler(r.Data) 187 | } 188 | atomic.AddInt64(&s.countProcessedReqs, 1) 189 | case <-s.closeChan: 190 | atomic.AddInt64(&s.numActives, -1) 191 | return 192 | } 193 | } 194 | }() 195 | } 196 | } 197 | 198 | func (s *Scheduler) PublishRequest(req *Request) { 199 | s.incomingChan <- req 200 | atomic.AddInt64(&s.countIncomingReqs, 1) 201 | } 202 | 203 | func (s *Scheduler) Close() { 204 | close(s.incomingChan) 205 | close(s.allCloseChan) 206 | } 207 | -------------------------------------------------------------------------------- /pkg/scheduler/strategy.go: -------------------------------------------------------------------------------- 1 | package scheduler 2 | 3 | import ( 4 | "math" 5 | "math/rand" 6 | ) 7 | 8 | type Strategy interface { 9 | //SetMaxQuota(maxProcessedReqs int, maxRate float64, maxActives int) 10 | SetMaxActives(maxActives int64) 11 | SetMaxProcessedReqs(maxProcessedReqs int64) 12 | SetMaxRate(maxRate float64) 13 | 14 | ExpandOrShrink(incomingRegsItv int64, processedReqsItv int64, numActives int64) int64 15 | } 16 | 17 | type Gradienter struct { 18 | quotaList *CircularList 19 | 20 | maxActives int64 21 | maxProcessedReqs int64 22 | maxRate float64 23 | } 24 | 25 | const ( 26 | NoNeedUpdating = iota 27 | NumActivesNeedsExpansion 28 | NumActivesNeedsShrinking 29 | MaxProcessedReqsNeedsExpansion 30 | MaxProcessedReqsNeedsShrinking 31 | MaxRateNeedsExpansion 32 | MaxRateNeedsShrinking 33 | ) 34 | 35 | const ( 36 | MaxDefaultGoRoutines int64 = 500 * 10000 37 | MaxDefaultProcessedReqs int64 = int64(^uint(0) >> 1) 38 | MaxDefaultRate float64 = 1000 39 | ) 40 | 41 | func NewGradienter() *Gradienter { 42 | return &Gradienter{ 43 | //quotaList: NewCircularList(), 44 | maxActives: MaxDefaultGoRoutines, 45 | maxProcessedReqs: MaxDefaultProcessedReqs, 46 | maxRate: MaxDefaultRate, 47 | } 48 | } 49 | 50 | //already locked at sheduler 51 | func (g *Gradienter) SetMaxActives(maxActives int64) { 52 | if maxActives == -1 { 53 | g.maxActives = MaxDefaultGoRoutines 54 | return 55 | } 56 | g.maxActives = maxActives 57 | } 58 | 59 | func (g *Gradienter) SetMaxProcessedReqs(maxProcessedReqs int64) { 60 | if maxProcessedReqs == -1 { 61 | g.maxProcessedReqs = MaxDefaultProcessedReqs 62 | return 63 | } 64 | g.maxProcessedReqs = maxProcessedReqs 65 | } 66 | 67 | func (g *Gradienter) SetMaxRate(maxRate float64) { 68 | if maxRate >= 0 && maxRate <= MaxDefaultRate { 69 | g.maxRate = maxRate 70 | return 71 | } 72 | g.maxRate = MaxDefaultRate 73 | } 74 | 75 | func (g *Gradienter) expand(ir int64, pr int64, numActives int64) int64 { 76 | weight := float64(ir) / (float64(ir) + float64(g.maxActives)) 77 | reminder := float64(g.maxActives - numActives) 78 | randfloat := rand.Float64() 79 | uncasted := randfloat * reminder * weight 80 | casted := int64(uncasted) 81 | return casted 82 | } 83 | 84 | func (g *Gradienter) ExpandOrShrink(ir int64, pr int64, numActives int64) (diff int64) { 85 | flag := g.needToUpdate(ir, pr, numActives) 86 | 87 | switch flag { 88 | case NoNeedUpdating: 89 | return 0 90 | 91 | case NumActivesNeedsExpansion: 92 | return g.expand(ir, pr, numActives) 93 | 94 | case NumActivesNeedsShrinking: 95 | //directly shrink to maxActives 96 | return g.maxActives - numActives 97 | 98 | case MaxProcessedReqsNeedsExpansion: 99 | //map the processedReqs expanding section to goroutines expanding section 100 | //the goroutines expanding section should be: [0, maxActives - numActives] 101 | //the processedReqs expanding section shoulde be: [0, maxProcessedReqs - pr] 102 | //so the shrinking number should be mapped as: number / (maxProcessedReqs - pr) / (maxActives - numActives) 103 | //any error exists please point out 104 | return g.expand(ir, pr, numActives) 105 | 106 | case MaxProcessedReqsNeedsShrinking: 107 | //shrink 20%, should've add some weight like others 108 | shrinks := float64(numActives) * 0.2 * -1 109 | return int64(math.Floor(shrinks)) 110 | 111 | case MaxRateNeedsExpansion: 112 | return g.expand(ir, pr, numActives) 113 | 114 | case MaxRateNeedsShrinking: 115 | //shrink 20% 116 | shrinks := float64(numActives) * 0.2 * -1 117 | return int64(math.Floor(shrinks)) 118 | } 119 | return 0 120 | } 121 | 122 | // 123 | //no updating first, then shrinking, expansion last 124 | func (g *Gradienter) needToUpdate(ir int64, pr int64, numActives int64) int { 125 | flag := NoNeedUpdating 126 | if float64(g.maxActives) > float64(numActives)*(1+0.1) { 127 | flag = NumActivesNeedsExpansion 128 | } else if float64(g.maxActives) < float64(numActives)*(1-0.1) { 129 | flag = NumActivesNeedsShrinking 130 | } 131 | 132 | if float64(g.maxProcessedReqs) > float64(pr)*(1+0.1) && flag == NoNeedUpdating { 133 | flag = MaxProcessedReqsNeedsExpansion 134 | } else if float64(g.maxProcessedReqs) < float64(pr)*(1-0.1) && (flag == NoNeedUpdating || flag == NumActivesNeedsExpansion) { 135 | flag = MaxProcessedReqsNeedsShrinking 136 | } 137 | 138 | if ir == 0 && numActives > 0 { 139 | //means no incoming requests, needs to shrink 140 | flag = MaxRateNeedsShrinking 141 | return flag 142 | } 143 | 144 | if g.maxRate > float64(pr)/float64(ir)*(1+0.05) && flag == NoNeedUpdating { 145 | //over speed, need to shrink 146 | flag = MaxRateNeedsExpansion 147 | } else if g.maxRate < float64(pr)/float64(ir)*(1-0.05) && (flag == NoNeedUpdating || flag == NumActivesNeedsExpansion || flag == MaxProcessedReqsNeedsExpansion) { 148 | flag = MaxRateNeedsShrinking 149 | } 150 | return flag 151 | } 152 | -------------------------------------------------------------------------------- /pkg/scheduler/strategy_test.go: -------------------------------------------------------------------------------- 1 | package scheduler 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_ExpandOrShrink_NotLimited(t *testing.T) { 9 | tables := [][]int64{} 10 | row1 := []int64{0, 0} 11 | row2 := []int64{100, 80} 12 | row3 := []int64{200, 140} 13 | row4 := []int64{1000, 1000} 14 | row5 := []int64{2000, 1998} 15 | row6 := []int64{10000, 5000} 16 | row7 := []int64{200000, 190000} 17 | row8 := []int64{10000, 80000} 18 | row9 := []int64{200000, 80000} 19 | row10 := []int64{1000, 800} 20 | row11 := []int64{100, 100} 21 | row12 := []int64{0, 0} 22 | row13 := []int64{0, 0} 23 | row14 := []int64{0, 0} 24 | row15 := []int64{0, 0} 25 | row16 := []int64{0, 0} 26 | row17 := []int64{0, 0} 27 | row18 := []int64{0, 0} 28 | row19 := []int64{0, 0} 29 | row20 := []int64{0, 0} 30 | 31 | tables = append(tables, row1) 32 | tables = append(tables, row2) 33 | tables = append(tables, row3) 34 | tables = append(tables, row4) 35 | tables = append(tables, row5) 36 | tables = append(tables, row6) 37 | tables = append(tables, row7) 38 | tables = append(tables, row8) 39 | tables = append(tables, row9) 40 | tables = append(tables, row10) 41 | tables = append(tables, row11) 42 | tables = append(tables, row12) 43 | tables = append(tables, row13) 44 | tables = append(tables, row14) 45 | tables = append(tables, row15) 46 | tables = append(tables, row16) 47 | tables = append(tables, row17) 48 | tables = append(tables, row18) 49 | tables = append(tables, row19) 50 | tables = append(tables, row20) 51 | 52 | gradienter := NewGradienter() 53 | var numActives int64 54 | var diff int64 55 | numActives = 100 56 | 57 | for i := 0; i < 20; i++ { 58 | diff = gradienter.ExpandOrShrink(tables[i][0], tables[i][1], numActives) 59 | numActives = numActives + diff 60 | fmt.Printf("%d, %d, %d, %d\n", tables[i][0], tables[i][1], diff, numActives) 61 | } 62 | } 63 | 64 | func Test_ExpandOrShrink_NumActivesLimited(t *testing.T) { 65 | tables := [][]int64{} 66 | row1 := []int64{0, 0} 67 | row2 := []int64{100, 80} 68 | row3 := []int64{200, 140} 69 | row4 := []int64{1000, 1000} 70 | row5 := []int64{2000, 1998} 71 | row6 := []int64{10000, 5000} 72 | row7 := []int64{200000, 190000} 73 | row8 := []int64{10000, 80000} 74 | row9 := []int64{200000, 80000} 75 | row10 := []int64{1000, 800} 76 | row11 := []int64{100, 100} 77 | row12 := []int64{0, 0} 78 | row13 := []int64{0, 0} 79 | row14 := []int64{0, 0} 80 | row15 := []int64{0, 0} 81 | row16 := []int64{0, 0} 82 | row17 := []int64{0, 0} 83 | row18 := []int64{0, 0} 84 | row19 := []int64{0, 0} 85 | row20 := []int64{0, 0} 86 | 87 | tables = append(tables, row1) 88 | tables = append(tables, row2) 89 | tables = append(tables, row3) 90 | tables = append(tables, row4) 91 | tables = append(tables, row5) 92 | tables = append(tables, row6) 93 | tables = append(tables, row7) 94 | tables = append(tables, row8) 95 | tables = append(tables, row9) 96 | tables = append(tables, row10) 97 | tables = append(tables, row11) 98 | tables = append(tables, row12) 99 | tables = append(tables, row13) 100 | tables = append(tables, row14) 101 | tables = append(tables, row15) 102 | tables = append(tables, row16) 103 | tables = append(tables, row17) 104 | tables = append(tables, row18) 105 | tables = append(tables, row19) 106 | tables = append(tables, row20) 107 | 108 | gradienter := NewGradienter() 109 | gradienter.SetMaxActives(5000) 110 | var numActives int64 111 | var diff int64 112 | numActives = 100 113 | 114 | for i := 0; i < 20; i++ { 115 | diff = gradienter.ExpandOrShrink(tables[i][0], tables[i][1], numActives) 116 | numActives = numActives + diff 117 | fmt.Printf("%d, %d, %d, %d\n", tables[i][0], tables[i][1], diff, numActives) 118 | } 119 | } 120 | 121 | func Test_ExpandOrShrink_MaxRateLimited(t *testing.T) { 122 | tables := [][]int64{} 123 | row1 := []int64{0, 0} 124 | row2 := []int64{100, 80} 125 | row3 := []int64{200, 140} 126 | row4 := []int64{1000, 1000} 127 | row5 := []int64{2000, 1998} 128 | row6 := []int64{10000, 5000} 129 | row7 := []int64{200000, 190000} 130 | row8 := []int64{10000, 80000} 131 | row9 := []int64{200000, 80000} 132 | row10 := []int64{1000, 800} 133 | row11 := []int64{100, 100} 134 | row12 := []int64{0, 0} 135 | row13 := []int64{0, 0} 136 | row14 := []int64{0, 0} 137 | row15 := []int64{0, 0} 138 | row16 := []int64{0, 0} 139 | row17 := []int64{0, 0} 140 | row18 := []int64{0, 0} 141 | row19 := []int64{0, 0} 142 | row20 := []int64{0, 0} 143 | 144 | tables = append(tables, row1) 145 | tables = append(tables, row2) 146 | tables = append(tables, row3) 147 | tables = append(tables, row4) 148 | tables = append(tables, row5) 149 | tables = append(tables, row6) 150 | tables = append(tables, row7) 151 | tables = append(tables, row8) 152 | tables = append(tables, row9) 153 | tables = append(tables, row10) 154 | tables = append(tables, row11) 155 | tables = append(tables, row12) 156 | tables = append(tables, row13) 157 | tables = append(tables, row14) 158 | tables = append(tables, row15) 159 | tables = append(tables, row16) 160 | tables = append(tables, row17) 161 | tables = append(tables, row18) 162 | tables = append(tables, row19) 163 | tables = append(tables, row20) 164 | 165 | gradienter := NewGradienter() 166 | gradienter.SetMaxRate(0.5) 167 | var numActives int64 168 | var diff int64 169 | numActives = 100 170 | 171 | for i := 0; i < 20; i++ { 172 | diff = gradienter.ExpandOrShrink(tables[i][0], tables[i][1], numActives) 173 | numActives = numActives + diff 174 | fmt.Printf("%d, %d, %d, %d\n", tables[i][0], tables[i][1], diff, numActives) 175 | } 176 | } 177 | 178 | func Test_ExpandOrShrink_MaxProcessedReqs(t *testing.T) { 179 | tables := [][]int64{} 180 | row1 := []int64{0, 0} 181 | row2 := []int64{100, 80} 182 | row3 := []int64{200, 140} 183 | row4 := []int64{1000, 1000} 184 | row5 := []int64{2000, 1998} 185 | row6 := []int64{10000, 5000} 186 | row7 := []int64{200000, 190000} 187 | row8 := []int64{10000, 80000} 188 | row9 := []int64{200000, 80000} 189 | row10 := []int64{1000, 800} 190 | row11 := []int64{100, 100} 191 | row12 := []int64{0, 0} 192 | row13 := []int64{0, 0} 193 | row14 := []int64{0, 0} 194 | row15 := []int64{0, 0} 195 | row16 := []int64{0, 0} 196 | row17 := []int64{0, 0} 197 | row18 := []int64{0, 0} 198 | row19 := []int64{0, 0} 199 | row20 := []int64{0, 0} 200 | 201 | tables = append(tables, row1) 202 | tables = append(tables, row2) 203 | tables = append(tables, row3) 204 | tables = append(tables, row4) 205 | tables = append(tables, row5) 206 | tables = append(tables, row6) 207 | tables = append(tables, row7) 208 | tables = append(tables, row8) 209 | tables = append(tables, row9) 210 | tables = append(tables, row10) 211 | tables = append(tables, row11) 212 | tables = append(tables, row12) 213 | tables = append(tables, row13) 214 | tables = append(tables, row14) 215 | tables = append(tables, row15) 216 | tables = append(tables, row16) 217 | tables = append(tables, row17) 218 | tables = append(tables, row18) 219 | tables = append(tables, row19) 220 | tables = append(tables, row20) 221 | 222 | gradienter := NewGradienter() 223 | gradienter.SetMaxProcessedReqs(5000) 224 | var numActives int64 225 | var diff int64 226 | numActives = 100 227 | 228 | for i := 0; i < 20; i++ { 229 | diff = gradienter.ExpandOrShrink(tables[i][0], tables[i][1], numActives) 230 | numActives = numActives + diff 231 | fmt.Printf("%d, %d, %d, %d\n", tables[i][0], tables[i][1], diff, numActives) 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /slot.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import ( 4 | "github.com/singchia/go-timer/v2/pkg/linker" 5 | ) 6 | 7 | type slot struct { 8 | dlinker *linker.Doublinker 9 | w *wheel 10 | } 11 | 12 | func newSlot(w *wheel) *slot { 13 | return &slot{w: w, dlinker: linker.NewDoublinker()} 14 | } 15 | 16 | func (s *slot) length() int64 { 17 | return s.dlinker.Length() 18 | } 19 | 20 | func (s *slot) add(tick *tick) *tick { 21 | doubID := s.dlinker.Add(tick) 22 | tick.id = doubID 23 | tick.s = s 24 | return tick 25 | } 26 | 27 | func (s *slot) delete(tick *tick) { 28 | s.dlinker.Delete(tick.id) 29 | } 30 | 31 | func (s *slot) update(tick *tick, data interface{}) { 32 | tick.data = data 33 | } 34 | 35 | func (s *slot) remove() *linker.Doublinker { 36 | temp := s.dlinker 37 | s.dlinker = linker.NewDoublinker() 38 | return temp 39 | } 40 | 41 | func (s *slot) close() *linker.Doublinker { 42 | if s == nil { 43 | return nil 44 | } 45 | s.w = nil 46 | temp := s.dlinker 47 | s.dlinker = nil 48 | return temp 49 | } 50 | 51 | func (s *slot) foreach(handler linker.ForeachFunc) error { 52 | if s == nil { 53 | return nil 54 | } 55 | return s.dlinker.Foreach(handler) 56 | } 57 | -------------------------------------------------------------------------------- /test/bench/tolerance.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "math" 7 | "reflect" 8 | "sync" 9 | "sync/atomic" 10 | "time" 11 | 12 | gotime "github.com/singchia/go-timer/v2" 13 | ) 14 | 15 | var points int 16 | var grain int64 17 | var ratio int 18 | var start int 19 | 20 | func main() { 21 | flag.IntVar(&start, "s", 1, "start") 22 | flag.IntVar(&points, "p", 10, "points") 23 | flag.IntVar(&ratio, "r", 5, "ratio of tick") // 系数 24 | flag.Int64Var(&grain, "g", 1000, "grain of tick") 25 | flag.Parse() 26 | 27 | index := 0 28 | for index < points { 29 | count := start * int(math.Pow(float64(ratio), float64(index))) 30 | tolerance := goTime(count) 31 | fmt.Println(count, tolerance) 32 | index++ 33 | } 34 | 35 | index = 0 36 | for index < points { 37 | count := start * int(math.Pow(float64(ratio), float64(index))) 38 | tolerance := buildinTime(count) 39 | fmt.Println(count, tolerance) 40 | index++ 41 | } 42 | } 43 | 44 | func buildinTime(count int) (tolerance int64) { 45 | var wait sync.WaitGroup 46 | for i := 0; i < count; i++ { 47 | wait.Add(1) 48 | t := time.Now() 49 | timer := time.NewTimer(time.Duration(grain)) 50 | go func(t time.Time, timer *time.Timer) { 51 | defer wait.Done() 52 | <-timer.C 53 | thisToler := int64(math.Abs(float64(time.Since(t) - time.Duration(grain)))) 54 | atomic.AddInt64(&tolerance, thisToler) 55 | }(t, timer) 56 | } 57 | wait.Wait() 58 | return tolerance / int64(count) 59 | } 60 | 61 | func goTime(count int) (tolerance int64) { 62 | var wait sync.WaitGroup 63 | tw := gotime.NewTimer(gotime.WithTimeInterval(time.Microsecond)) 64 | for i := 0; i < count; i++ { 65 | wait.Add(1) 66 | t := time.Now() 67 | tw.Add(time.Duration(grain), gotime.WithData(t), gotime.WithHandler(func(event *gotime.Event) { 68 | defer wait.Done() 69 | t = event.Data.(time.Time) 70 | thisToler := int64(math.Abs(float64(time.Since(t) - time.Duration(grain)))) 71 | atomic.AddInt64(&tolerance, thisToler) 72 | })) 73 | } 74 | wait.Wait() 75 | tw.Close() 76 | return tolerance / int64(count) 77 | } 78 | 79 | func buildinReflectTime(count int) (tolerance int64) { 80 | var wait sync.WaitGroup 81 | cases := []reflect.SelectCase{} 82 | mu := new(sync.RWMutex) 83 | for i := 0; i < count; i++ { 84 | mu.Lock() 85 | wait.Add(1) 86 | timer := time.NewTimer(time.Duration(grain * 1000)) 87 | cases = append(cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(timer.C)}) 88 | mu.Unlock() 89 | } 90 | 91 | go func() { 92 | for { 93 | mu.RLock() 94 | i, value, ok := reflect.Select(cases) 95 | mu.RUnlock() 96 | if !ok { 97 | mu.Lock() 98 | cases = append(cases[:i], cases[i+1:]...) 99 | mu.Unlock() 100 | } else { 101 | fmt.Printf("%v\n", value) 102 | wait.Done() 103 | } 104 | } 105 | }() 106 | wait.Wait() 107 | return tolerance / int64(count) 108 | } 109 | -------------------------------------------------------------------------------- /test/profile/profile.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | _ "net/http/pprof" 6 | 7 | gotime "github.com/singchia/go-timer/v2" 8 | ) 9 | 10 | func main() { 11 | 12 | tw := gotime.NewTimer() 13 | ch := make(chan struct{}, 1024) 14 | 15 | http.HandleFunc("/bench", func(w http.ResponseWriter, req *http.Request) { 16 | tw.Add(1, gotime.WithData(w), gotime.WithHandler(func(event *gotime.Event) { 17 | rw := event.Data.(http.ResponseWriter) 18 | rw.WriteHeader(http.StatusFound) 19 | ch <- struct{}{} 20 | })) 21 | <-ch 22 | }) 23 | 24 | http.ListenAndServe(":6060", nil) 25 | } 26 | -------------------------------------------------------------------------------- /tick.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import ( 4 | "sync/atomic" 5 | "time" 6 | 7 | "github.com/singchia/go-timer/v2/pkg/linker" 8 | ) 9 | 10 | type tickOption struct { 11 | data interface{} 12 | ch chan *Event 13 | chOutside bool 14 | handler func(*Event) 15 | cyclically bool 16 | } 17 | 18 | type status int 19 | 20 | const ( 21 | statusAdd = iota 22 | statusWait 23 | statusFire 24 | statusCanceled 25 | ) 26 | 27 | // the real shit 28 | type tick struct { 29 | // user interface 30 | *tickOption 31 | 32 | // location 33 | id linker.DoubID 34 | s *slot 35 | tw *timingwheel 36 | ipw []uint 37 | 38 | // meta 39 | duration time.Duration 40 | delay time.Duration 41 | insertTime time.Time 42 | 43 | // status 44 | fired int64 45 | status status 46 | } 47 | 48 | // TODO revision 49 | func (t *tick) Reset(data interface{}) error { 50 | t.tw.mtx.RLock() 51 | defer t.tw.mtx.RUnlock() 52 | if t.tw.twStatus != twStatusStarted { 53 | return ErrTimerNotStarted 54 | } 55 | 56 | ch := make(chan *operationRet) 57 | t.tw.operations <- &operation{ 58 | tick: t, 59 | operType: operReset, 60 | retCh: ch, 61 | data: data, 62 | } 63 | ret, ok := <-ch 64 | if !ok { 65 | return ErrOperationForceClosed 66 | } 67 | return ret.err 68 | } 69 | 70 | // Don't cancel the tick in timeout handler, it may cause the block when buffer is fulfilled. 71 | // The cancel operation may not work when the tick is about to time up. 72 | func (t *tick) Cancel() error { 73 | t.tw.mtx.RLock() 74 | defer t.tw.mtx.RUnlock() 75 | if t.tw.twStatus != twStatusStarted { 76 | return ErrTimerNotStarted 77 | } 78 | 79 | ch := make(chan *operationRet) 80 | t.tw.operations <- &operation{ 81 | tick: t, 82 | operType: operCancel, 83 | retCh: ch, 84 | } 85 | ret, ok := <-ch 86 | if !ok { 87 | return ErrOperationForceClosed 88 | } 89 | return ret.err 90 | } 91 | 92 | // TODO revision 93 | func (t *tick) Delay(d time.Duration) error { 94 | if t.cyclically { 95 | return ErrDelayOnCyclically 96 | } 97 | t.tw.mtx.RLock() 98 | defer t.tw.mtx.RUnlock() 99 | if t.tw.twStatus != twStatusStarted { 100 | return ErrTimerNotStarted 101 | } 102 | ch := make(chan *operationRet) 103 | t.tw.operations <- &operation{ 104 | tick: t, 105 | operType: operDelay, 106 | delay: d, 107 | retCh: ch, 108 | } 109 | ret, ok := <-ch 110 | if !ok { 111 | return ErrOperationForceClosed 112 | } 113 | return ret.err 114 | } 115 | 116 | func (t *tick) Fired() int64 { 117 | return atomic.LoadInt64(&t.fired) 118 | } 119 | 120 | func (t *tick) C() <-chan *Event { 121 | return t.ch 122 | } 123 | 124 | func (t *tick) InsertTime() time.Time { 125 | return t.insertTime 126 | } 127 | 128 | func (t *tick) Duration() time.Duration { 129 | return t.duration 130 | } 131 | -------------------------------------------------------------------------------- /timer.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import "time" 4 | 5 | type TimerOption func(*timerOption) 6 | 7 | func WithTimeInterval(interval time.Duration) TimerOption { 8 | return func(to *timerOption) { 9 | to.interval = interval 10 | } 11 | } 12 | 13 | func WithOperationBufferSize(n int) TimerOption { 14 | return func(to *timerOption) { 15 | to.operationBufferSize = n 16 | } 17 | } 18 | 19 | type TickOption func(*tickOption) 20 | 21 | func WithData(data interface{}) TickOption { 22 | return func(to *tickOption) { 23 | to.data = data 24 | } 25 | } 26 | 27 | func WithCyclically() TickOption { 28 | return func(to *tickOption) { 29 | to.cyclically = true 30 | } 31 | } 32 | 33 | func WithChan(C chan *Event) TickOption { 34 | return func(to *tickOption) { 35 | to.ch = C 36 | to.chOutside = true 37 | } 38 | } 39 | 40 | func WithHandler(handler func(*Event)) TickOption { 41 | return func(to *tickOption) { 42 | to.handler = handler 43 | } 44 | } 45 | 46 | type Timer interface { 47 | Add(d time.Duration, opts ...TickOption) Tick 48 | 49 | // Close to close timer, 50 | // all ticks set would be discarded. 51 | Close() 52 | 53 | // Pause the timer, 54 | // all ticks won't continue after Timer.Movenon(). 55 | Pause() 56 | 57 | // Continue the paused timer. 58 | Moveon() 59 | } 60 | 61 | // Tick that set in Timer can be required from Timer.Add() 62 | type Tick interface { 63 | //To reset the data set at Timer.Time() 64 | Reset(data interface{}) error 65 | 66 | //To cancel the tick 67 | Cancel() error 68 | 69 | //Delay the tick 70 | Delay(d time.Duration) error 71 | 72 | //To get the channel called at Timer.Time(), 73 | //you will get the same channel if set, if not and handler is nil, 74 | //then a new created channel will be returned. 75 | C() <-chan *Event 76 | 77 | // Insert time 78 | InsertTime() time.Time 79 | 80 | // The tick duration original set 81 | Duration() time.Duration 82 | 83 | // Fired count 84 | Fired() int64 85 | } 86 | 87 | type Event struct { 88 | Duration time.Duration 89 | InsertTIme time.Time 90 | Data interface{} 91 | Error error 92 | } 93 | 94 | // Entry 95 | func NewTimer(opts ...TimerOption) Timer { 96 | return newTimingwheel(opts...) 97 | } 98 | -------------------------------------------------------------------------------- /timer_test.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import ( 4 | "runtime" 5 | "sync" 6 | "testing" 7 | "time" 8 | ) 9 | 10 | func TestGoTimer(t *testing.T) { 11 | tw := NewTimer() 12 | 13 | tick := tw.Add(time.Second) 14 | <-tick.C() 15 | t.Logf("%v %v", tick.InsertTime(), time.Now()) 16 | } 17 | 18 | func BenchmarkBuildinTimer(b *testing.B) { 19 | var memStats runtime.MemStats 20 | runtime.ReadMemStats(&memStats) 21 | startAllocs := memStats.Mallocs 22 | b.ResetTimer() 23 | b.ReportAllocs() 24 | wg := sync.WaitGroup{} 25 | wg.Add(b.N) 26 | for i := 0; i < b.N; i++ { 27 | timer := time.NewTimer(time.Second) 28 | go func() { 29 | defer wg.Done() 30 | <-timer.C 31 | }() 32 | } 33 | wg.Wait() 34 | runtime.ReadMemStats(&memStats) 35 | endAllocs := memStats.Mallocs 36 | b.ReportMetric(float64(endAllocs-startAllocs), "allocs") 37 | b.ReportMetric(float64(memStats.TotalAlloc)/1024, "allocs_kb") 38 | b.ReportMetric(float64(memStats.Sys)/1024/1024, "sys_mb") 39 | b.ReportMetric(float64(memStats.HeapAlloc)/1024, "heap_alloc_kb") 40 | b.ReportMetric(float64(memStats.HeapSys)/1024/1024, "heap_sys_mb") 41 | b.ReportMetric(float64(memStats.StackSys)/1024/1024, "stack_sys_mb") 42 | b.ReportMetric(float64(runtime.NumCPU()), "num_cpu") 43 | } 44 | 45 | func BenchmarkGoTimer(b *testing.B) { 46 | var memStats runtime.MemStats 47 | runtime.ReadMemStats(&memStats) 48 | startAllocs := memStats.Mallocs 49 | b.ResetTimer() 50 | b.ReportAllocs() 51 | wg := sync.WaitGroup{} 52 | wg.Add(b.N) 53 | 54 | tw := NewTimer() 55 | for i := 0; i < b.N; i++ { 56 | tw.Add(time.Second, WithHandler(func(event *Event) { 57 | wg.Done() 58 | })) 59 | } 60 | wg.Wait() 61 | 62 | runtime.ReadMemStats(&memStats) 63 | endAllocs := memStats.Mallocs 64 | b.ReportMetric(float64(endAllocs-startAllocs), "allocs") 65 | b.ReportMetric(float64(memStats.TotalAlloc)/1024, "allocs_kb") 66 | b.ReportMetric(float64(memStats.Sys)/1024/1024, "sys_mb") 67 | b.ReportMetric(float64(memStats.HeapAlloc)/1024, "heap_alloc_kb") 68 | b.ReportMetric(float64(memStats.HeapSys)/1024/1024, "heap_sys_mb") 69 | b.ReportMetric(float64(memStats.StackSys)/1024/1024, "stack_sys_mb") 70 | b.ReportMetric(float64(runtime.NumCPU()), "num_cpu") 71 | } 72 | -------------------------------------------------------------------------------- /timingwheel.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Austin Zhai 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as 6 | * published by the Free Software Foundation; either version 2 of 7 | * the License, or (at your option) any later version. 8 | */ 9 | package timer 10 | 11 | import ( 12 | "errors" 13 | "sync" 14 | "sync/atomic" 15 | "time" 16 | 17 | "github.com/singchia/go-timer/v2/pkg/scheduler" 18 | ) 19 | 20 | const ( 21 | defaultTickInterval time.Duration = 10 * time.Millisecond 22 | defaultTicks uint64 = 1024 * 1024 * 1024 * 1024 23 | defaultSlots uint = 256 24 | ) 25 | 26 | var ( 27 | ErrDurationOutOfRange = errors.New("duration out of range") 28 | ErrTimerNotStarted = errors.New("timer not started") 29 | ErrTimerForceClosed = errors.New("timer force closed") 30 | ErrOperationForceClosed = errors.New("operation force closed") 31 | ErrDelayOnCyclically = errors.New("cannot delay on cyclically tick") 32 | ErrCancelOnNonWait = errors.New("cannot cancel on firing or fired tick") 33 | ) 34 | 35 | const ( 36 | twStatusInit = iota 37 | twStatusStarted 38 | twStatusPaused 39 | twStatusStoped 40 | ) 41 | 42 | // operation type and return 43 | type operType int 44 | 45 | const ( 46 | operAdd operType = iota 47 | operCancel 48 | operDelay 49 | operReset 50 | ) 51 | 52 | type operation struct { 53 | tick *tick 54 | operType operType 55 | retCh chan *operationRet 56 | // for delay 57 | delay time.Duration 58 | // for reset 59 | data interface{} 60 | } 61 | 62 | type operRetType int 63 | 64 | const ( 65 | operOK operRetType = iota 66 | operErr 67 | ) 68 | 69 | type operationRet struct { 70 | operRetType operRetType 71 | err error 72 | } 73 | 74 | // timer options 75 | type timerOption struct { 76 | interval time.Duration 77 | operationBufferSize int 78 | } 79 | 80 | type timingwheel struct { 81 | *timerOption 82 | 83 | mtx sync.RWMutex 84 | twStatus int 85 | 86 | wheels []*wheel 87 | max uint64 88 | sch *scheduler.Scheduler 89 | operations chan *operation 90 | 91 | pause, quit chan struct{} 92 | } 93 | 94 | func newTimingwheel(opts ...TimerOption) *timingwheel { 95 | max, length := calcuWheels(defaultTicks) 96 | tw := &timingwheel{ 97 | twStatus: twStatusInit, 98 | sch: scheduler.NewScheduler(), 99 | timerOption: &timerOption{ 100 | interval: defaultTickInterval, 101 | operationBufferSize: 128, 102 | }, 103 | pause: make(chan struct{}), 104 | quit: make(chan struct{}), 105 | } 106 | for _, opt := range opts { 107 | opt(tw.timerOption) 108 | } 109 | tw.operations = make(chan *operation, tw.operationBufferSize) 110 | tw.setWheels(max, length) 111 | tw.sch.SetMaxGoroutines(1000) 112 | tw.sch.StartSchedule() 113 | tw.Start() 114 | return tw 115 | } 116 | 117 | func (tw *timingwheel) setWheels(max uint64, length int) { 118 | tw.wheels = make([]*wheel, 0, length) 119 | tw.max = max 120 | for i := uint(0); i < uint(length); i++ { 121 | tw.wheels = append(tw.wheels, newWheel(defaultSlots, i)) 122 | } 123 | } 124 | 125 | func (tw *timingwheel) Add(d time.Duration, opts ...TickOption) Tick { 126 | tick := &tick{ 127 | duration: d, 128 | tw: tw, 129 | insertTime: time.Now(), 130 | tickOption: &tickOption{}, 131 | status: statusAdd, 132 | } 133 | for _, opt := range opts { 134 | opt(tick.tickOption) 135 | } 136 | if tick.ch == nil && tick.handler == nil { 137 | tick.ch = make(chan *Event, 1) 138 | } 139 | tw.mtx.RLock() 140 | defer tw.mtx.RUnlock() 141 | 142 | if tw.twStatus != twStatusStarted { 143 | tw.handleError(tick, ErrTimerNotStarted) 144 | return tick 145 | } 146 | tw.operations <- &operation{ 147 | tick: tick, 148 | operType: operAdd, 149 | } 150 | return tick 151 | } 152 | 153 | func (tw *timingwheel) Start() { 154 | tw.mtx.Lock() 155 | defer tw.mtx.Unlock() 156 | if tw.twStatus != twStatusInit && tw.twStatus != twStatusPaused { 157 | return 158 | } 159 | tw.twStatus = twStatusStarted 160 | go tw.drive() 161 | return 162 | } 163 | 164 | func (tw *timingwheel) Pause() { 165 | tw.mtx.Lock() 166 | defer tw.mtx.Unlock() 167 | if tw.twStatus != twStatusStarted { 168 | return 169 | } 170 | tw.twStatus = twStatusPaused 171 | tw.pause <- struct{}{} 172 | return 173 | } 174 | 175 | func (tw *timingwheel) Moveon() { 176 | tw.mtx.Lock() 177 | defer tw.mtx.Unlock() 178 | if tw.twStatus != twStatusPaused { 179 | return 180 | } 181 | tw.twStatus = twStatusStarted 182 | go tw.drive() 183 | } 184 | 185 | func (tw *timingwheel) Close() { 186 | tw.mtx.Lock() 187 | defer tw.mtx.Unlock() 188 | if tw.twStatus != twStatusStarted { 189 | return 190 | } 191 | close(tw.quit) 192 | tw.twStatus = twStatusStoped 193 | } 194 | 195 | func (tw *timingwheel) drive() { 196 | driver := time.NewTicker(tw.interval) 197 | defer driver.Stop() 198 | 199 | for { 200 | select { 201 | case <-driver.C: 202 | // fire all ready tick 203 | for _, wheel := range tw.wheels { 204 | linker := wheel.incN(1) 205 | if linker != nil && linker.Length() > 0 { 206 | linker.Foreach(tw.iterate) 207 | } 208 | if wheel.cur != 0 { 209 | break 210 | } 211 | } 212 | case operation, ok := <-tw.operations: 213 | if !ok { 214 | continue 215 | } 216 | switch operation.operType { 217 | case operAdd: 218 | // the specific tick's add operation must be before the del or delay operation 219 | // so we don't care the status cause it must be statusAdd 220 | ipw := tw.indexesPerWheel(operation.tick.duration) 221 | operation.tick.ipw = ipw 222 | tw.wheels[len(ipw)-1].add(ipw[len(ipw)-1], operation.tick) 223 | operation.tick.status = statusWait 224 | 225 | case operCancel: 226 | // only in wait status can delete the tick 227 | if operation.tick.status != statusWait { 228 | operation.retCh <- &operationRet{ 229 | operRetType: operErr, 230 | err: ErrCancelOnNonWait, 231 | } 232 | continue 233 | } 234 | operation.tick.s.delete(operation.tick) 235 | operation.retCh <- &operationRet{ 236 | operRetType: operOK, 237 | } 238 | operation.tick.status = statusCanceled 239 | 240 | case operDelay: 241 | if operation.tick.status != statusWait { 242 | operation.retCh <- &operationRet{ 243 | operRetType: operErr, 244 | err: ErrCancelOnNonWait, 245 | } 246 | continue 247 | } 248 | operation.tick.s.delete(operation.tick) 249 | ipw := tw.indexesPerWheelBased(operation.delay, operation.tick.ipw) 250 | operation.tick.ipw = ipw 251 | operation.tick.delay = operation.delay 252 | tw.wheels[len(ipw)-1].add(ipw[len(ipw)-1], operation.tick) 253 | operation.retCh <- &operationRet{ 254 | operRetType: operOK, 255 | } 256 | 257 | case operReset: 258 | if operation.tick.status != statusWait { 259 | operation.retCh <- &operationRet{ 260 | operRetType: operErr, 261 | err: ErrCancelOnNonWait, 262 | } 263 | continue 264 | } 265 | operation.tick.data = operation.data 266 | operation.retCh <- &operationRet{ 267 | operRetType: operOK, 268 | } 269 | } 270 | case <-tw.pause: 271 | return 272 | case <-tw.quit: 273 | close(tw.operations) 274 | for operation := range tw.operations { 275 | switch operation.operType { 276 | case operCancel, operDelay, operReset: 277 | operation.retCh <- &operationRet{ 278 | operRetType: operErr, 279 | err: ErrTimerForceClosed, 280 | } 281 | case operAdd: 282 | tw.handleError(operation.tick, ErrTimerForceClosed) 283 | continue 284 | } 285 | } 286 | for _, wheel := range tw.wheels { 287 | for _, slot := range wheel.slots { 288 | slot.foreach(tw.forceClose) 289 | } 290 | } 291 | // accelerate gc 292 | tw.sch.Close() 293 | tw.wheels = nil 294 | tw.operations = nil 295 | tw.sch = nil 296 | tw.quit = nil 297 | tw.pause = nil 298 | return 299 | } 300 | } 301 | } 302 | 303 | func (tw *timingwheel) iterate(data interface{}) error { 304 | tk, _ := data.(*tick) 305 | if tk.status == statusCanceled { 306 | return nil 307 | } 308 | position := tk.s.w.position 309 | for position > 0 { 310 | position-- 311 | if tk.ipw[position] > 0 { 312 | tw.wheels[position].add(tk.ipw[position], tk) 313 | return nil 314 | } 315 | } 316 | tk.status = statusFire 317 | if !tk.cyclically { 318 | tw.sch.PublishRequest(&scheduler.Request{Data: tk, Handler: tw.handleNormal}) 319 | return nil 320 | } 321 | // copy the tick, since the data might be Reset. 322 | tickCopy := &tick{ 323 | tickOption: &tickOption{ 324 | data: tk.data, 325 | ch: tk.ch, 326 | handler: tk.handler, 327 | }, 328 | insertTime: tk.insertTime, 329 | duration: tk.duration, 330 | fired: tk.fired, 331 | } 332 | tw.sch.PublishRequest(&scheduler.Request{Data: tickCopy, Handler: tw.handleNormal}) 333 | 334 | ipw := tw.indexesPerWheelBased(tk.duration, tk.ipw) 335 | tk.ipw = ipw 336 | tw.wheels[len(ipw)-1].add(ipw[len(ipw)-1], tk) 337 | tk.status = statusWait 338 | return nil 339 | } 340 | 341 | func (tw *timingwheel) forceClose(data interface{}) error { 342 | tick, _ := data.(*tick) 343 | if tick.status == statusCanceled { 344 | return nil 345 | } 346 | tick.status = statusFire 347 | tw.sch.PublishRequest(&scheduler.Request{Data: tick, Handler: tw.handleForceClosed}) 348 | return nil 349 | } 350 | 351 | func (tw *timingwheel) handleForceClosed(data interface{}) { 352 | tw.handleError(data, ErrTimerForceClosed) 353 | } 354 | 355 | func (tw *timingwheel) handleError(data interface{}, err error) { 356 | tk, _ := data.(*tick) 357 | if tk.status == statusCanceled { 358 | return 359 | } 360 | event := &Event{ 361 | Duration: tk.duration, 362 | InsertTIme: tk.insertTime, 363 | Data: tk.data, 364 | Error: err, 365 | } 366 | if tk.ch == nil { 367 | tk.handler(event) 368 | } else { 369 | tk.ch <- event 370 | if !tk.chOutside { 371 | close(tk.ch) 372 | } 373 | } 374 | } 375 | 376 | func (tw *timingwheel) handleNormal(data interface{}) { 377 | tk, _ := data.(*tick) 378 | if tk.status == statusCanceled { 379 | return 380 | } 381 | atomic.AddInt64(&tk.fired, 1) 382 | event := &Event{ 383 | Duration: tk.duration, 384 | InsertTIme: tk.insertTime, 385 | Data: tk.data, 386 | Error: nil, 387 | } 388 | if tk.ch == nil { 389 | tk.handler(event) 390 | } else { 391 | tk.ch <- event 392 | } 393 | } 394 | 395 | func (tw *timingwheel) indexesPerWheel(d time.Duration) []uint { 396 | var ipw []uint 397 | var reminder uint64 398 | var quotient = uint64((d + tw.interval - 1) / tw.interval) 399 | for i, wheel := range tw.wheels { 400 | if quotient == 0 { 401 | break 402 | } 403 | quotient += uint64(tw.wheels[i].cur) 404 | reminder = quotient % uint64(wheel.numSlots) 405 | quotient = quotient / uint64(wheel.numSlots) 406 | ipw = append(ipw, uint(reminder)) 407 | } 408 | return ipw 409 | } 410 | 411 | func (tw *timingwheel) indexesPerWheelBased(d time.Duration, base []uint) []uint { 412 | var ipw []uint 413 | var reminder uint64 414 | var quotient = uint64((d + tw.interval - 1) / tw.interval) 415 | for i, wheel := range tw.wheels { 416 | if quotient == 0 { 417 | break 418 | } 419 | if len(base) <= i { 420 | quotient += uint64(tw.wheels[i].cur) 421 | } else { 422 | quotient += uint64(base[i]) 423 | } 424 | 425 | reminder = quotient % uint64(wheel.numSlots) 426 | quotient = quotient / uint64(wheel.numSlots) 427 | ipw = append(ipw, uint(reminder)) 428 | } 429 | return ipw 430 | } 431 | 432 | func calcuWheels(num uint64) (uint64, int) { 433 | count := uint64(defaultSlots) 434 | length := 1 435 | for { 436 | if count < num { 437 | count *= uint64(defaultSlots) 438 | length += 1 439 | continue 440 | } 441 | break 442 | } 443 | return count, length 444 | } 445 | -------------------------------------------------------------------------------- /timingwheel_test.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | ) 7 | 8 | func Test_indexesPerWheel(t *testing.T) { 9 | tw := &timingwheel{ 10 | timerOption: &timerOption{ 11 | interval: time.Millisecond, 12 | }, 13 | } 14 | ipw := tw.indexesPerWheel(1023) 15 | t.Log(ipw) 16 | return 17 | } 18 | 19 | func Test_calcuWheels(t *testing.T) { 20 | max, length := calcuWheels(defaultTicks) 21 | t.Log(max, length) 22 | } 23 | -------------------------------------------------------------------------------- /wheel.go: -------------------------------------------------------------------------------- 1 | package timer 2 | 3 | import ( 4 | "github.com/singchia/go-timer/v2/pkg/linker" 5 | ) 6 | 7 | type wheel struct { 8 | slots []*slot 9 | cur uint 10 | numSlots uint 11 | //position in whole timer 12 | position uint 13 | } 14 | 15 | func newWheel(numSlots uint, position uint) *wheel { 16 | slots := make([]*slot, numSlots, numSlots) 17 | wil := &wheel{cur: 0, numSlots: numSlots, position: position} 18 | wil.slots = slots 19 | return wil 20 | } 21 | 22 | func (w *wheel) add(n uint, tick *tick) *tick { 23 | if w.slots[n] == nil { 24 | w.slots[n] = newSlot(w) 25 | } 26 | return w.slots[n].add(tick) 27 | } 28 | 29 | // increace n on cur 30 | func (w *wheel) incN(n uint) *linker.Doublinker { 31 | w.cur += n 32 | if w.cur >= w.numSlots { 33 | w.cur -= w.numSlots 34 | } 35 | if w.slots[w.cur] == nil || w.slots[w.cur].length() == 0 { 36 | return nil 37 | } 38 | return w.slots[w.cur].remove() 39 | } 40 | --------------------------------------------------------------------------------