├── .gitignore ├── .travis.yml ├── BENCHMARK.md ├── CODE_OF_CONDUCT.md ├── Gopkg.lock ├── Gopkg.toml ├── LICENSE ├── Makefile ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── application.go ├── application_test.go ├── balancer.go ├── balancer_random.go ├── balancer_random_test.go ├── balancer_rr.go ├── balancer_rr_test.go ├── balancer_wrr.go ├── balancer_wrr_test.go ├── breaker.go ├── breaker_test.go ├── coarse_time.go ├── coarse_time_test.go ├── config.go ├── config_test.go ├── graceful.go ├── graceful_test.go ├── main.go ├── nocopy.go ├── proxy.go ├── proxy_test.go ├── radix_tree.go ├── radix_tree_test.go ├── timeline.go ├── timeline_test.go └── workflow.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | 16 | # binarys 17 | guard 18 | guard.linux_386 19 | guard.linux_amd64 20 | guard.macos_amd64 21 | guard.windows_386.exe 22 | guard.windows_amd64.exe 23 | 24 | # vendor 25 | vendor/ 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.8.x 4 | - 1.9.x 5 | - 1.10.x 6 | 7 | before_install: 8 | - go get -t -v ./... 9 | 10 | script: 11 | - go test -race -coverprofile=coverage.txt -covermode=atomic 12 | 13 | after_success: 14 | - bash <(curl -s https://codecov.io/bash) 15 | -------------------------------------------------------------------------------- /BENCHMARK.md: -------------------------------------------------------------------------------- 1 | # Benchmark 2 | 3 | CPU & RAM: 4 | 5 | ``` 6 | $ lscpu 7 | Architecture: x86_64 8 | CPU op-mode(s): 32-bit, 64-bit 9 | Byte Order: Little Endian 10 | CPU(s): 4 11 | On-line CPU(s) list: 0-3 12 | Thread(s) per core: 2 13 | Core(s) per socket: 2 14 | Socket(s): 1 15 | NUMA node(s): 1 16 | Vendor ID: GenuineIntel 17 | CPU family: 6 18 | Model: 58 19 | Model name: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz 20 | Stepping: 9 21 | CPU MHz: 1214.294 22 | CPU max MHz: 3100.0000 23 | CPU min MHz: 1200.0000 24 | BogoMIPS: 4988.41 25 | Virtualization: VT-x 26 | L1d cache: 32K 27 | L1i cache: 32K 28 | L2 cache: 256K 29 | L3 cache: 3072K 30 | NUMA node0 CPU(s): 0-3 31 | Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm epb retpoline kaiser tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms xsaveopt dtherm ida arat pln pts 32 | 33 | $ free -h 34 | total used free shared buff/cache available 35 | Mem: 11G 536M 9.0G 11M 2.2G 10G 36 | Swap: 9.6G 0B 9.6G 37 | ``` 38 | 39 | ## systemd service files & nginx conf 40 | 41 | - nginx that service pages 42 | 43 | ``` 44 | $ cat /usr/lib/systemd/system/nginx.service 45 | [Unit] 46 | Description=A high performance web server and a reverse proxy server 47 | After=network.target network-online.target nss-lookup.target 48 | 49 | [Service] 50 | Type=forking 51 | PIDFile=/run/nginx.pid 52 | PrivateDevices=yes 53 | SyslogLevel=err 54 | User=root 55 | 56 | LimitNOFILE=4096000 57 | ExecStart=/usr/bin/nginx -g 'pid /run/nginx.pid; error_log stderr;' 58 | ExecReload=/usr/bin/nginx -s reload 59 | KillSignal=SIGQUIT 60 | KillMode=mixed 61 | 62 | [Install] 63 | WantedBy=multi-user.target 64 | 65 | $ cat /etc/nginx/nginx.conf 66 | worker_processes 2; 67 | worker_rlimit_nofile 204800; 68 | 69 | error_log /var/log/nginx/error.log; 70 | 71 | events { 72 | worker_connections 10240; 73 | use epoll; 74 | multi_accept on; 75 | } 76 | 77 | http { 78 | include mime.types; 79 | default_type application/text-plain; 80 | 81 | access_log /var/log/nginx/access.log; 82 | 83 | sendfile on; 84 | 85 | keepalive_timeout 65; 86 | 87 | gzip on; 88 | 89 | server { 90 | listen 80; 91 | server_name localhost; 92 | 93 | location / { 94 | root /usr/share/nginx/html; 95 | index index.html; 96 | } 97 | 98 | location /hello { 99 | return 200 'hello world'; 100 | } 101 | } 102 | } 103 | ``` 104 | 105 | - nginx as proxy 106 | 107 | ``` 108 | $ cat /etc/systemd/system/nginx_proxy.service 109 | [Unit] 110 | Description=A high performance web server and a reverse proxy server 111 | After=network.target network-online.target nss-lookup.target 112 | 113 | [Service] 114 | Type=forking 115 | PIDFile=/tmp/nginx.pid 116 | PrivateDevices=yes 117 | SyslogLevel=err 118 | 119 | LimitNOFILE=4096000 120 | ExecStart=/usr/bin/nginx -g 'pid /tmp/nginx.pid;' -c /tmp/nginx_proxy.conf 121 | ExecReload=/usr/bin/nginx -s reload 122 | KillSignal=SIGQUIT 123 | KillMode=mixed 124 | 125 | [Install] 126 | WantedBy=multi-user.target 127 | 128 | $ cat /tmp/nginx_proxy.conf 129 | worker_processes 2; 130 | worker_rlimit_nofile 204800; 131 | 132 | error_log /var/log/nginx/error.log; 133 | 134 | events { 135 | worker_connections 10240; 136 | use epoll; 137 | multi_accept on; 138 | } 139 | 140 | http { 141 | include /etc/nginx/mime.types; 142 | default_type application/text-plain; 143 | 144 | access_log /var/log/nginx/access.log; 145 | 146 | sendfile on; 147 | 148 | keepalive_timeout 65; 149 | 150 | gzip on; 151 | 152 | server { 153 | listen 9999; 154 | server_name localhost; 155 | 156 | location / { 157 | proxy_pass http://127.0.0.1:80/; 158 | } 159 | } 160 | } 161 | ``` 162 | 163 | - guard 164 | 165 | ``` 166 | $ cat /etc/systemd/system/guard.service 167 | [Unit] 168 | Description=Guard Circuit Breaker 169 | After=network.target 170 | 171 | [Service] 172 | Type=simple 173 | User=root 174 | Environment=GOMAXPROCS=2 175 | LimitNOFILE=204800 176 | WorkingDirectory=/usr/local/bin/ 177 | ExecStart=/usr/local/bin/guard 178 | Restart=on-abort 179 | 180 | [Install] 181 | WantedBy=multi-user.target 182 | 183 | $ cat ~/backends.json 184 | { 185 | "name": "www.example.com", 186 | "backends": ["127.0.0.1:80", "127.0.0.1:80", "127.0.0.1:80"], 187 | "weights": [5, 1, 1], 188 | "ratio": 0.3, 189 | "paths": ["/", "/doc"], 190 | "methods": ["GET", "GET"] 191 | } 192 | ``` 193 | 194 | ## Start! 195 | 196 | - make sure all things goes well 197 | 198 | ```bash 199 | $ curl -X GET --header "Host: www.example.com" -I http://127.0.0.1:80/ 200 | HTTP/1.1 200 OK 201 | Server: nginx/1.12.2 202 | Date: Fri, 26 Jan 2018 02:02:56 GMT 203 | Content-Type: text/html 204 | Content-Length: 612 205 | Last-Modified: Wed, 22 Nov 2017 19:48:42 GMT 206 | Connection: keep-alive 207 | ETag: "5a15d49a-264" 208 | Accept-Ranges: bytes 209 | 210 | $ curl -X GET --header "Host: www.example.com" -I http://127.0.0.1:9999/ 211 | HTTP/1.1 200 OK 212 | Server: nginx/1.12.2 213 | Date: Fri, 26 Jan 2018 02:03:03 GMT 214 | Content-Type: text/html 215 | Content-Length: 612 216 | Connection: keep-alive 217 | Last-Modified: Wed, 22 Nov 2017 19:48:42 GMT 218 | ETag: "5a15d49a-264" 219 | Accept-Ranges: bytes 220 | 221 | $ curl -X GET --header "Host: www.example.com" -I http://127.0.0.1:23456/ 222 | HTTP/1.1 200 OK 223 | Server: nginx/1.12.2 224 | Date: Fri, 26 Jan 2018 02:03:07 GMT 225 | Content-Type: text/html 226 | Content-Length: 612 227 | Last-Modified: Wed, 22 Nov 2017 19:48:42 GMT 228 | Etag: "5a15d49a-264" 229 | Accept-Ranges: bytes 230 | ``` 231 | 232 | - test nginx server itself 233 | 234 | ``` 235 | $ wrk --latency -H "Host: www.example.com" -c 2048 -d 30 -t 2 http://127.0.0.1:80 236 | Running 30s test @ http://127.0.0.1:80 237 | 2 threads and 2048 connections 238 | Thread Stats Avg Stdev Max +/- Stdev 239 | Latency 30.71ms 15.74ms 274.65ms 73.86% 240 | Req/Sec 16.34k 2.45k 22.59k 77.59% 241 | Latency Distribution 242 | 50% 30.73ms 243 | 75% 35.13ms 244 | 90% 57.45ms 245 | 99% 65.53ms 246 | 973250 requests in 30.09s, 788.89MB read 247 | Socket errors: connect 1029, read 0, write 0, timeout 0 248 | Requests/sec: 32343.50 249 | Transfer/sec: 26.22MB 250 | ``` 251 | 252 | - test nginx proxy 253 | 254 | ``` 255 | $ wrk --latency -H "Host: www.example.com" -c 2048 -d 30 -t 2 http://127.0.0.1:9999 256 | Running 30s test @ http://127.0.0.1:9999 257 | 2 threads and 2048 connections 258 | Thread Stats Avg Stdev Max +/- Stdev 259 | Latency 90.05ms 94.04ms 1.23s 97.98% 260 | Req/Sec 6.04k 1.99k 9.98k 56.38% 261 | Latency Distribution 262 | 50% 81.12ms 263 | 75% 134.20ms 264 | 90% 147.84ms 265 | 99% 527.96ms 266 | 358981 requests in 30.04s, 290.98MB read 267 | Socket errors: connect 1029, read 0, write 0, timeout 110 268 | Requests/sec: 11951.02 269 | Transfer/sec: 9.69MB 270 | ``` 271 | 272 | - test guard 273 | 274 | ``` 275 | $ wrk --latency -H "Host: www.example.com" -c 2048 -d 30 -t 2 http://127.0.0.1:23456 276 | Running 30s test @ http://127.0.0.1:23456 277 | 2 threads and 2048 connections 278 | Thread Stats Avg Stdev Max +/- Stdev 279 | Latency 43.38ms 21.79ms 1.10s 91.83% 280 | Req/Sec 11.83k 2.45k 16.43k 63.70% 281 | Latency Distribution 282 | 50% 42.37ms 283 | 75% 48.88ms 284 | 90% 58.47ms 285 | 99% 77.49ms 286 | 704433 requests in 30.07s, 554.91MB read 287 | Socket errors: connect 1029, read 0, write 0, timeout 0 288 | Requests/sec: 23425.98 289 | Transfer/sec: 18.45MB 290 | ``` 291 | 292 | ## FAQ 293 | 294 | - why not set CPU affinity? 295 | 296 | for fair. guard does set this. but you can set CPU affinity by using `taskset` for guard. your benchmark is welcome! 297 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at gansteed@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /Gopkg.lock: -------------------------------------------------------------------------------- 1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. 2 | 3 | 4 | [[projects]] 5 | name = "github.com/klauspost/compress" 6 | packages = [ 7 | "flate", 8 | "gzip", 9 | "zlib" 10 | ] 11 | revision = "6c8db69c4b49dd4df1fff66996cf556176d0b9bf" 12 | version = "v1.2.1" 13 | 14 | [[projects]] 15 | name = "github.com/klauspost/cpuid" 16 | packages = ["."] 17 | revision = "ae7887de9fa5d2db4eaa8174a7eff2c1ac00f2da" 18 | version = "v1.1" 19 | 20 | [[projects]] 21 | name = "github.com/klauspost/crc32" 22 | packages = ["."] 23 | revision = "cb6bfca970f6908083f26f39a79009d608efd5cd" 24 | version = "v1.1" 25 | 26 | [[projects]] 27 | name = "github.com/valyala/fasthttp" 28 | packages = [ 29 | ".", 30 | "fasthttputil" 31 | ] 32 | revision = "d42167fd04f636e20b005e9934159e95454233c7" 33 | version = "v20160617" 34 | 35 | [solve-meta] 36 | analyzer-name = "dep" 37 | analyzer-version = 1 38 | inputs-digest = "1d40b9e8f162f2a20856011650ccd49e327060aca676f0126dde30c99de39ed8" 39 | solver-name = "gps-cdcl" 40 | solver-version = 1 41 | -------------------------------------------------------------------------------- /Gopkg.toml: -------------------------------------------------------------------------------- 1 | # Gopkg.toml example 2 | # 3 | # Refer to https://golang.github.io/dep/docs/Gopkg.toml.html 4 | # for detailed Gopkg.toml documentation. 5 | # 6 | # required = ["github.com/user/thing/cmd/thing"] 7 | # ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] 8 | # 9 | # [[constraint]] 10 | # name = "github.com/user/project" 11 | # version = "1.0.0" 12 | # 13 | # [[constraint]] 14 | # name = "github.com/user/project2" 15 | # branch = "dev" 16 | # source = "github.com/myfork/project2" 17 | # 18 | # [[override]] 19 | # name = "github.com/x/y" 20 | # version = "2.4.0" 21 | # 22 | # [prune] 23 | # non-go = false 24 | # go-tests = true 25 | # unused-packages = true 26 | 27 | 28 | [[constraint]] 29 | name = "github.com/valyala/fasthttp" 30 | version = "20160617.0.0" 31 | 32 | [prune] 33 | go-tests = true 34 | unused-packages = true 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The httprouter part obey the original user's LICENSE, which can be found 2 | in here: https://github.com/julienschmidt/httprouter/blob/master/LICENSE 3 | 4 | The rest of code(all except httprouter part) is published at GPLv3 as below. 5 | 6 | 7 | GNU GENERAL PUBLIC LICENSE 8 | Version 3, 29 June 2007 9 | 10 | Copyright (C) 2007 Free Software Foundation, Inc. 11 | Everyone is permitted to copy and distribute verbatim copies 12 | of this license document, but changing it is not allowed. 13 | 14 | Preamble 15 | 16 | The GNU General Public License is a free, copyleft license for 17 | software and other kinds of works. 18 | 19 | The licenses for most software and other practical works are designed 20 | to take away your freedom to share and change the works. By contrast, 21 | the GNU General Public License is intended to guarantee your freedom to 22 | share and change all versions of a program--to make sure it remains free 23 | software for all its users. We, the Free Software Foundation, use the 24 | GNU General Public License for most of our software; it applies also to 25 | any other work released this way by its authors. You can apply it to 26 | your programs, too. 27 | 28 | When we speak of free software, we are referring to freedom, not 29 | price. Our General Public Licenses are designed to make sure that you 30 | have the freedom to distribute copies of free software (and charge for 31 | them if you wish), that you receive source code or can get it if you 32 | want it, that you can change the software or use pieces of it in new 33 | free programs, and that you know you can do these things. 34 | 35 | To protect your rights, we need to prevent others from denying you 36 | these rights or asking you to surrender the rights. Therefore, you have 37 | certain responsibilities if you distribute copies of the software, or if 38 | you modify it: responsibilities to respect the freedom of others. 39 | 40 | For example, if you distribute copies of such a program, whether 41 | gratis or for a fee, you must pass on to the recipients the same 42 | freedoms that you received. You must make sure that they, too, receive 43 | or can get the source code. And you must show them these terms so they 44 | know their rights. 45 | 46 | Developers that use the GNU GPL protect your rights with two steps: 47 | (1) assert copyright on the software, and (2) offer you this License 48 | giving you legal permission to copy, distribute and/or modify it. 49 | 50 | For the developers' and authors' protection, the GPL clearly explains 51 | that there is no warranty for this free software. For both users' and 52 | authors' sake, the GPL requires that modified versions be marked as 53 | changed, so that their problems will not be attributed erroneously to 54 | authors of previous versions. 55 | 56 | Some devices are designed to deny users access to install or run 57 | modified versions of the software inside them, although the manufacturer 58 | can do so. This is fundamentally incompatible with the aim of 59 | protecting users' freedom to change the software. The systematic 60 | pattern of such abuse occurs in the area of products for individuals to 61 | use, which is precisely where it is most unacceptable. Therefore, we 62 | have designed this version of the GPL to prohibit the practice for those 63 | products. If such problems arise substantially in other domains, we 64 | stand ready to extend this provision to those domains in future versions 65 | of the GPL, as needed to protect the freedom of users. 66 | 67 | Finally, every program is threatened constantly by software patents. 68 | States should not allow patents to restrict development and use of 69 | software on general-purpose computers, but in those that do, we wish to 70 | avoid the special danger that patents applied to a free program could 71 | make it effectively proprietary. To prevent this, the GPL assures that 72 | patents cannot be used to render the program non-free. 73 | 74 | The precise terms and conditions for copying, distribution and 75 | modification follow. 76 | 77 | TERMS AND CONDITIONS 78 | 79 | 0. Definitions. 80 | 81 | "This License" refers to version 3 of the GNU General Public License. 82 | 83 | "Copyright" also means copyright-like laws that apply to other kinds of 84 | works, such as semiconductor masks. 85 | 86 | "The Program" refers to any copyrightable work licensed under this 87 | License. Each licensee is addressed as "you". "Licensees" and 88 | "recipients" may be individuals or organizations. 89 | 90 | To "modify" a work means to copy from or adapt all or part of the work 91 | in a fashion requiring copyright permission, other than the making of an 92 | exact copy. The resulting work is called a "modified version" of the 93 | earlier work or a work "based on" the earlier work. 94 | 95 | A "covered work" means either the unmodified Program or a work based 96 | on the Program. 97 | 98 | To "propagate" a work means to do anything with it that, without 99 | permission, would make you directly or secondarily liable for 100 | infringement under applicable copyright law, except executing it on a 101 | computer or modifying a private copy. Propagation includes copying, 102 | distribution (with or without modification), making available to the 103 | public, and in some countries other activities as well. 104 | 105 | To "convey" a work means any kind of propagation that enables other 106 | parties to make or receive copies. Mere interaction with a user through 107 | a computer network, with no transfer of a copy, is not conveying. 108 | 109 | An interactive user interface displays "Appropriate Legal Notices" 110 | to the extent that it includes a convenient and prominently visible 111 | feature that (1) displays an appropriate copyright notice, and (2) 112 | tells the user that there is no warranty for the work (except to the 113 | extent that warranties are provided), that licensees may convey the 114 | work under this License, and how to view a copy of this License. If 115 | the interface presents a list of user commands or options, such as a 116 | menu, a prominent item in the list meets this criterion. 117 | 118 | 1. Source Code. 119 | 120 | The "source code" for a work means the preferred form of the work 121 | for making modifications to it. "Object code" means any non-source 122 | form of a work. 123 | 124 | A "Standard Interface" means an interface that either is an official 125 | standard defined by a recognized standards body, or, in the case of 126 | interfaces specified for a particular programming language, one that 127 | is widely used among developers working in that language. 128 | 129 | The "System Libraries" of an executable work include anything, other 130 | than the work as a whole, that (a) is included in the normal form of 131 | packaging a Major Component, but which is not part of that Major 132 | Component, and (b) serves only to enable use of the work with that 133 | Major Component, or to implement a Standard Interface for which an 134 | implementation is available to the public in source code form. A 135 | "Major Component", in this context, means a major essential component 136 | (kernel, window system, and so on) of the specific operating system 137 | (if any) on which the executable work runs, or a compiler used to 138 | produce the work, or an object code interpreter used to run it. 139 | 140 | The "Corresponding Source" for a work in object code form means all 141 | the source code needed to generate, install, and (for an executable 142 | work) run the object code and to modify the work, including scripts to 143 | control those activities. However, it does not include the work's 144 | System Libraries, or general-purpose tools or generally available free 145 | programs which are used unmodified in performing those activities but 146 | which are not part of the work. For example, Corresponding Source 147 | includes interface definition files associated with source files for 148 | the work, and the source code for shared libraries and dynamically 149 | linked subprograms that the work is specifically designed to require, 150 | such as by intimate data communication or control flow between those 151 | subprograms and other parts of the work. 152 | 153 | The Corresponding Source need not include anything that users 154 | can regenerate automatically from other parts of the Corresponding 155 | Source. 156 | 157 | The Corresponding Source for a work in source code form is that 158 | same work. 159 | 160 | 2. Basic Permissions. 161 | 162 | All rights granted under this License are granted for the term of 163 | copyright on the Program, and are irrevocable provided the stated 164 | conditions are met. This License explicitly affirms your unlimited 165 | permission to run the unmodified Program. The output from running a 166 | covered work is covered by this License only if the output, given its 167 | content, constitutes a covered work. This License acknowledges your 168 | rights of fair use or other equivalent, as provided by copyright law. 169 | 170 | You may make, run and propagate covered works that you do not 171 | convey, without conditions so long as your license otherwise remains 172 | in force. You may convey covered works to others for the sole purpose 173 | of having them make modifications exclusively for you, or provide you 174 | with facilities for running those works, provided that you comply with 175 | the terms of this License in conveying all material for which you do 176 | not control copyright. Those thus making or running the covered works 177 | for you must do so exclusively on your behalf, under your direction 178 | and control, on terms that prohibit them from making any copies of 179 | your copyrighted material outside their relationship with you. 180 | 181 | Conveying under any other circumstances is permitted solely under 182 | the conditions stated below. Sublicensing is not allowed; section 10 183 | makes it unnecessary. 184 | 185 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 186 | 187 | No covered work shall be deemed part of an effective technological 188 | measure under any applicable law fulfilling obligations under article 189 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 190 | similar laws prohibiting or restricting circumvention of such 191 | measures. 192 | 193 | When you convey a covered work, you waive any legal power to forbid 194 | circumvention of technological measures to the extent such circumvention 195 | is effected by exercising rights under this License with respect to 196 | the covered work, and you disclaim any intention to limit operation or 197 | modification of the work as a means of enforcing, against the work's 198 | users, your or third parties' legal rights to forbid circumvention of 199 | technological measures. 200 | 201 | 4. Conveying Verbatim Copies. 202 | 203 | You may convey verbatim copies of the Program's source code as you 204 | receive it, in any medium, provided that you conspicuously and 205 | appropriately publish on each copy an appropriate copyright notice; 206 | keep intact all notices stating that this License and any 207 | non-permissive terms added in accord with section 7 apply to the code; 208 | keep intact all notices of the absence of any warranty; and give all 209 | recipients a copy of this License along with the Program. 210 | 211 | You may charge any price or no price for each copy that you convey, 212 | and you may offer support or warranty protection for a fee. 213 | 214 | 5. Conveying Modified Source Versions. 215 | 216 | You may convey a work based on the Program, or the modifications to 217 | produce it from the Program, in the form of source code under the 218 | terms of section 4, provided that you also meet all of these conditions: 219 | 220 | a) The work must carry prominent notices stating that you modified 221 | it, and giving a relevant date. 222 | 223 | b) The work must carry prominent notices stating that it is 224 | released under this License and any conditions added under section 225 | 7. This requirement modifies the requirement in section 4 to 226 | "keep intact all notices". 227 | 228 | c) You must license the entire work, as a whole, under this 229 | License to anyone who comes into possession of a copy. This 230 | License will therefore apply, along with any applicable section 7 231 | additional terms, to the whole of the work, and all its parts, 232 | regardless of how they are packaged. This License gives no 233 | permission to license the work in any other way, but it does not 234 | invalidate such permission if you have separately received it. 235 | 236 | d) If the work has interactive user interfaces, each must display 237 | Appropriate Legal Notices; however, if the Program has interactive 238 | interfaces that do not display Appropriate Legal Notices, your 239 | work need not make them do so. 240 | 241 | A compilation of a covered work with other separate and independent 242 | works, which are not by their nature extensions of the covered work, 243 | and which are not combined with it such as to form a larger program, 244 | in or on a volume of a storage or distribution medium, is called an 245 | "aggregate" if the compilation and its resulting copyright are not 246 | used to limit the access or legal rights of the compilation's users 247 | beyond what the individual works permit. Inclusion of a covered work 248 | in an aggregate does not cause this License to apply to the other 249 | parts of the aggregate. 250 | 251 | 6. Conveying Non-Source Forms. 252 | 253 | You may convey a covered work in object code form under the terms 254 | of sections 4 and 5, provided that you also convey the 255 | machine-readable Corresponding Source under the terms of this License, 256 | in one of these ways: 257 | 258 | a) Convey the object code in, or embodied in, a physical product 259 | (including a physical distribution medium), accompanied by the 260 | Corresponding Source fixed on a durable physical medium 261 | customarily used for software interchange. 262 | 263 | b) Convey the object code in, or embodied in, a physical product 264 | (including a physical distribution medium), accompanied by a 265 | written offer, valid for at least three years and valid for as 266 | long as you offer spare parts or customer support for that product 267 | model, to give anyone who possesses the object code either (1) a 268 | copy of the Corresponding Source for all the software in the 269 | product that is covered by this License, on a durable physical 270 | medium customarily used for software interchange, for a price no 271 | more than your reasonable cost of physically performing this 272 | conveying of source, or (2) access to copy the 273 | Corresponding Source from a network server at no charge. 274 | 275 | c) Convey individual copies of the object code with a copy of the 276 | written offer to provide the Corresponding Source. This 277 | alternative is allowed only occasionally and noncommercially, and 278 | only if you received the object code with such an offer, in accord 279 | with subsection 6b. 280 | 281 | d) Convey the object code by offering access from a designated 282 | place (gratis or for a charge), and offer equivalent access to the 283 | Corresponding Source in the same way through the same place at no 284 | further charge. You need not require recipients to copy the 285 | Corresponding Source along with the object code. If the place to 286 | copy the object code is a network server, the Corresponding Source 287 | may be on a different server (operated by you or a third party) 288 | that supports equivalent copying facilities, provided you maintain 289 | clear directions next to the object code saying where to find the 290 | Corresponding Source. Regardless of what server hosts the 291 | Corresponding Source, you remain obligated to ensure that it is 292 | available for as long as needed to satisfy these requirements. 293 | 294 | e) Convey the object code using peer-to-peer transmission, provided 295 | you inform other peers where the object code and Corresponding 296 | Source of the work are being offered to the general public at no 297 | charge under subsection 6d. 298 | 299 | A separable portion of the object code, whose source code is excluded 300 | from the Corresponding Source as a System Library, need not be 301 | included in conveying the object code work. 302 | 303 | A "User Product" is either (1) a "consumer product", which means any 304 | tangible personal property which is normally used for personal, family, 305 | or household purposes, or (2) anything designed or sold for incorporation 306 | into a dwelling. In determining whether a product is a consumer product, 307 | doubtful cases shall be resolved in favor of coverage. For a particular 308 | product received by a particular user, "normally used" refers to a 309 | typical or common use of that class of product, regardless of the status 310 | of the particular user or of the way in which the particular user 311 | actually uses, or expects or is expected to use, the product. A product 312 | is a consumer product regardless of whether the product has substantial 313 | commercial, industrial or non-consumer uses, unless such uses represent 314 | the only significant mode of use of the product. 315 | 316 | "Installation Information" for a User Product means any methods, 317 | procedures, authorization keys, or other information required to install 318 | and execute modified versions of a covered work in that User Product from 319 | a modified version of its Corresponding Source. The information must 320 | suffice to ensure that the continued functioning of the modified object 321 | code is in no case prevented or interfered with solely because 322 | modification has been made. 323 | 324 | If you convey an object code work under this section in, or with, or 325 | specifically for use in, a User Product, and the conveying occurs as 326 | part of a transaction in which the right of possession and use of the 327 | User Product is transferred to the recipient in perpetuity or for a 328 | fixed term (regardless of how the transaction is characterized), the 329 | Corresponding Source conveyed under this section must be accompanied 330 | by the Installation Information. But this requirement does not apply 331 | if neither you nor any third party retains the ability to install 332 | modified object code on the User Product (for example, the work has 333 | been installed in ROM). 334 | 335 | The requirement to provide Installation Information does not include a 336 | requirement to continue to provide support service, warranty, or updates 337 | for a work that has been modified or installed by the recipient, or for 338 | the User Product in which it has been modified or installed. Access to a 339 | network may be denied when the modification itself materially and 340 | adversely affects the operation of the network or violates the rules and 341 | protocols for communication across the network. 342 | 343 | Corresponding Source conveyed, and Installation Information provided, 344 | in accord with this section must be in a format that is publicly 345 | documented (and with an implementation available to the public in 346 | source code form), and must require no special password or key for 347 | unpacking, reading or copying. 348 | 349 | 7. Additional Terms. 350 | 351 | "Additional permissions" are terms that supplement the terms of this 352 | License by making exceptions from one or more of its conditions. 353 | Additional permissions that are applicable to the entire Program shall 354 | be treated as though they were included in this License, to the extent 355 | that they are valid under applicable law. If additional permissions 356 | apply only to part of the Program, that part may be used separately 357 | under those permissions, but the entire Program remains governed by 358 | this License without regard to the additional permissions. 359 | 360 | When you convey a copy of a covered work, you may at your option 361 | remove any additional permissions from that copy, or from any part of 362 | it. (Additional permissions may be written to require their own 363 | removal in certain cases when you modify the work.) You may place 364 | additional permissions on material, added by you to a covered work, 365 | for which you have or can give appropriate copyright permission. 366 | 367 | Notwithstanding any other provision of this License, for material you 368 | add to a covered work, you may (if authorized by the copyright holders of 369 | that material) supplement the terms of this License with terms: 370 | 371 | a) Disclaiming warranty or limiting liability differently from the 372 | terms of sections 15 and 16 of this License; or 373 | 374 | b) Requiring preservation of specified reasonable legal notices or 375 | author attributions in that material or in the Appropriate Legal 376 | Notices displayed by works containing it; or 377 | 378 | c) Prohibiting misrepresentation of the origin of that material, or 379 | requiring that modified versions of such material be marked in 380 | reasonable ways as different from the original version; or 381 | 382 | d) Limiting the use for publicity purposes of names of licensors or 383 | authors of the material; or 384 | 385 | e) Declining to grant rights under trademark law for use of some 386 | trade names, trademarks, or service marks; or 387 | 388 | f) Requiring indemnification of licensors and authors of that 389 | material by anyone who conveys the material (or modified versions of 390 | it) with contractual assumptions of liability to the recipient, for 391 | any liability that these contractual assumptions directly impose on 392 | those licensors and authors. 393 | 394 | All other non-permissive additional terms are considered "further 395 | restrictions" within the meaning of section 10. If the Program as you 396 | received it, or any part of it, contains a notice stating that it is 397 | governed by this License along with a term that is a further 398 | restriction, you may remove that term. If a license document contains 399 | a further restriction but permits relicensing or conveying under this 400 | License, you may add to a covered work material governed by the terms 401 | of that license document, provided that the further restriction does 402 | not survive such relicensing or conveying. 403 | 404 | If you add terms to a covered work in accord with this section, you 405 | must place, in the relevant source files, a statement of the 406 | additional terms that apply to those files, or a notice indicating 407 | where to find the applicable terms. 408 | 409 | Additional terms, permissive or non-permissive, may be stated in the 410 | form of a separately written license, or stated as exceptions; 411 | the above requirements apply either way. 412 | 413 | 8. Termination. 414 | 415 | You may not propagate or modify a covered work except as expressly 416 | provided under this License. Any attempt otherwise to propagate or 417 | modify it is void, and will automatically terminate your rights under 418 | this License (including any patent licenses granted under the third 419 | paragraph of section 11). 420 | 421 | However, if you cease all violation of this License, then your 422 | license from a particular copyright holder is reinstated (a) 423 | provisionally, unless and until the copyright holder explicitly and 424 | finally terminates your license, and (b) permanently, if the copyright 425 | holder fails to notify you of the violation by some reasonable means 426 | prior to 60 days after the cessation. 427 | 428 | Moreover, your license from a particular copyright holder is 429 | reinstated permanently if the copyright holder notifies you of the 430 | violation by some reasonable means, this is the first time you have 431 | received notice of violation of this License (for any work) from that 432 | copyright holder, and you cure the violation prior to 30 days after 433 | your receipt of the notice. 434 | 435 | Termination of your rights under this section does not terminate the 436 | licenses of parties who have received copies or rights from you under 437 | this License. If your rights have been terminated and not permanently 438 | reinstated, you do not qualify to receive new licenses for the same 439 | material under section 10. 440 | 441 | 9. Acceptance Not Required for Having Copies. 442 | 443 | You are not required to accept this License in order to receive or 444 | run a copy of the Program. Ancillary propagation of a covered work 445 | occurring solely as a consequence of using peer-to-peer transmission 446 | to receive a copy likewise does not require acceptance. However, 447 | nothing other than this License grants you permission to propagate or 448 | modify any covered work. These actions infringe copyright if you do 449 | not accept this License. Therefore, by modifying or propagating a 450 | covered work, you indicate your acceptance of this License to do so. 451 | 452 | 10. Automatic Licensing of Downstream Recipients. 453 | 454 | Each time you convey a covered work, the recipient automatically 455 | receives a license from the original licensors, to run, modify and 456 | propagate that work, subject to this License. You are not responsible 457 | for enforcing compliance by third parties with this License. 458 | 459 | An "entity transaction" is a transaction transferring control of an 460 | organization, or substantially all assets of one, or subdividing an 461 | organization, or merging organizations. If propagation of a covered 462 | work results from an entity transaction, each party to that 463 | transaction who receives a copy of the work also receives whatever 464 | licenses to the work the party's predecessor in interest had or could 465 | give under the previous paragraph, plus a right to possession of the 466 | Corresponding Source of the work from the predecessor in interest, if 467 | the predecessor has it or can get it with reasonable efforts. 468 | 469 | You may not impose any further restrictions on the exercise of the 470 | rights granted or affirmed under this License. For example, you may 471 | not impose a license fee, royalty, or other charge for exercise of 472 | rights granted under this License, and you may not initiate litigation 473 | (including a cross-claim or counterclaim in a lawsuit) alleging that 474 | any patent claim is infringed by making, using, selling, offering for 475 | sale, or importing the Program or any portion of it. 476 | 477 | 11. Patents. 478 | 479 | A "contributor" is a copyright holder who authorizes use under this 480 | License of the Program or a work on which the Program is based. The 481 | work thus licensed is called the contributor's "contributor version". 482 | 483 | A contributor's "essential patent claims" are all patent claims 484 | owned or controlled by the contributor, whether already acquired or 485 | hereafter acquired, that would be infringed by some manner, permitted 486 | by this License, of making, using, or selling its contributor version, 487 | but do not include claims that would be infringed only as a 488 | consequence of further modification of the contributor version. For 489 | purposes of this definition, "control" includes the right to grant 490 | patent sublicenses in a manner consistent with the requirements of 491 | this License. 492 | 493 | Each contributor grants you a non-exclusive, worldwide, royalty-free 494 | patent license under the contributor's essential patent claims, to 495 | make, use, sell, offer for sale, import and otherwise run, modify and 496 | propagate the contents of its contributor version. 497 | 498 | In the following three paragraphs, a "patent license" is any express 499 | agreement or commitment, however denominated, not to enforce a patent 500 | (such as an express permission to practice a patent or covenant not to 501 | sue for patent infringement). To "grant" such a patent license to a 502 | party means to make such an agreement or commitment not to enforce a 503 | patent against the party. 504 | 505 | If you convey a covered work, knowingly relying on a patent license, 506 | and the Corresponding Source of the work is not available for anyone 507 | to copy, free of charge and under the terms of this License, through a 508 | publicly available network server or other readily accessible means, 509 | then you must either (1) cause the Corresponding Source to be so 510 | available, or (2) arrange to deprive yourself of the benefit of the 511 | patent license for this particular work, or (3) arrange, in a manner 512 | consistent with the requirements of this License, to extend the patent 513 | license to downstream recipients. "Knowingly relying" means you have 514 | actual knowledge that, but for the patent license, your conveying the 515 | covered work in a country, or your recipient's use of the covered work 516 | in a country, would infringe one or more identifiable patents in that 517 | country that you have reason to believe are valid. 518 | 519 | If, pursuant to or in connection with a single transaction or 520 | arrangement, you convey, or propagate by procuring conveyance of, a 521 | covered work, and grant a patent license to some of the parties 522 | receiving the covered work authorizing them to use, propagate, modify 523 | or convey a specific copy of the covered work, then the patent license 524 | you grant is automatically extended to all recipients of the covered 525 | work and works based on it. 526 | 527 | A patent license is "discriminatory" if it does not include within 528 | the scope of its coverage, prohibits the exercise of, or is 529 | conditioned on the non-exercise of one or more of the rights that are 530 | specifically granted under this License. You may not convey a covered 531 | work if you are a party to an arrangement with a third party that is 532 | in the business of distributing software, under which you make payment 533 | to the third party based on the extent of your activity of conveying 534 | the work, and under which the third party grants, to any of the 535 | parties who would receive the covered work from you, a discriminatory 536 | patent license (a) in connection with copies of the covered work 537 | conveyed by you (or copies made from those copies), or (b) primarily 538 | for and in connection with specific products or compilations that 539 | contain the covered work, unless you entered into that arrangement, 540 | or that patent license was granted, prior to 28 March 2007. 541 | 542 | Nothing in this License shall be construed as excluding or limiting 543 | any implied license or other defenses to infringement that may 544 | otherwise be available to you under applicable patent law. 545 | 546 | 12. No Surrender of Others' Freedom. 547 | 548 | If conditions are imposed on you (whether by court order, agreement or 549 | otherwise) that contradict the conditions of this License, they do not 550 | excuse you from the conditions of this License. If you cannot convey a 551 | covered work so as to satisfy simultaneously your obligations under this 552 | License and any other pertinent obligations, then as a consequence you may 553 | not convey it at all. For example, if you agree to terms that obligate you 554 | to collect a royalty for further conveying from those to whom you convey 555 | the Program, the only way you could satisfy both those terms and this 556 | License would be to refrain entirely from conveying the Program. 557 | 558 | 13. Use with the GNU Affero General Public License. 559 | 560 | Notwithstanding any other provision of this License, you have 561 | permission to link or combine any covered work with a work licensed 562 | under version 3 of the GNU Affero General Public License into a single 563 | combined work, and to convey the resulting work. The terms of this 564 | License will continue to apply to the part which is the covered work, 565 | but the special requirements of the GNU Affero General Public License, 566 | section 13, concerning interaction through a network will apply to the 567 | combination as such. 568 | 569 | 14. Revised Versions of this License. 570 | 571 | The Free Software Foundation may publish revised and/or new versions of 572 | the GNU General Public License from time to time. Such new versions will 573 | be similar in spirit to the present version, but may differ in detail to 574 | address new problems or concerns. 575 | 576 | Each version is given a distinguishing version number. If the 577 | Program specifies that a certain numbered version of the GNU General 578 | Public License "or any later version" applies to it, you have the 579 | option of following the terms and conditions either of that numbered 580 | version or of any later version published by the Free Software 581 | Foundation. If the Program does not specify a version number of the 582 | GNU General Public License, you may choose any version ever published 583 | by the Free Software Foundation. 584 | 585 | If the Program specifies that a proxy can decide which future 586 | versions of the GNU General Public License can be used, that proxy's 587 | public statement of acceptance of a version permanently authorizes you 588 | to choose that version for the Program. 589 | 590 | Later license versions may give you additional or different 591 | permissions. However, no additional obligations are imposed on any 592 | author or copyright holder as a result of your choosing to follow a 593 | later version. 594 | 595 | 15. Disclaimer of Warranty. 596 | 597 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 598 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 599 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 600 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 601 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 602 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 603 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 604 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 605 | 606 | 16. Limitation of Liability. 607 | 608 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 609 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 610 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 611 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 612 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 613 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 614 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 615 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 616 | SUCH DAMAGES. 617 | 618 | 17. Interpretation of Sections 15 and 16. 619 | 620 | If the disclaimer of warranty and limitation of liability provided 621 | above cannot be given local legal effect according to their terms, 622 | reviewing courts shall apply local law that most closely approximates 623 | an absolute waiver of all civil liability in connection with the 624 | Program, unless a warranty or assumption of liability accompanies a 625 | copy of the Program in return for a fee. 626 | 627 | END OF TERMS AND CONDITIONS 628 | 629 | How to Apply These Terms to Your New Programs 630 | 631 | If you develop a new program, and you want it to be of the greatest 632 | possible use to the public, the best way to achieve this is to make it 633 | free software which everyone can redistribute and change under these terms. 634 | 635 | To do so, attach the following notices to the program. It is safest 636 | to attach them to the start of each source file to most effectively 637 | state the exclusion of warranty; and each file should have at least 638 | the "copyright" line and a pointer to where the full notice is found. 639 | 640 | 641 | Copyright (C) 642 | 643 | This program is free software: you can redistribute it and/or modify 644 | it under the terms of the GNU General Public License as published by 645 | the Free Software Foundation, either version 3 of the License, or 646 | (at your option) any later version. 647 | 648 | This program is distributed in the hope that it will be useful, 649 | but WITHOUT ANY WARRANTY; without even the implied warranty of 650 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 651 | GNU General Public License for more details. 652 | 653 | You should have received a copy of the GNU General Public License 654 | along with this program. If not, see . 655 | 656 | Also add information on how to contact you by electronic and paper mail. 657 | 658 | If the program does terminal interaction, make it output a short 659 | notice like this when it starts in an interactive mode: 660 | 661 | Copyright (C) 662 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 663 | This is free software, and you are welcome to redistribute it 664 | under certain conditions; type `show c' for details. 665 | 666 | The hypothetical commands `show w' and `show c' should show the appropriate 667 | parts of the General Public License. Of course, your program's commands 668 | might be different; for a GUI interface, you would use an "about box". 669 | 670 | You should also get your employer (if you work as a programmer) or school, 671 | if any, to sign a "copyright disclaimer" for the program, if necessary. 672 | For more information on this, and how to apply and follow the GNU GPL, see 673 | . 674 | 675 | The GNU General Public License does not permit incorporating your program 676 | into proprietary programs. If your program is a subroutine library, you 677 | may consider it more useful to permit linking proprietary applications with 678 | the library. If this is what you want to do, use the GNU Lesser General 679 | Public License instead of this License. But first, please read 680 | . 681 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build clean fmt vet test bench cover profiling 2 | 3 | COVERPROFILE= 4 | DEBUG= 5 | ARGS=-args -configPath=/tmp/guard.test.json -configAddr=":34567" -proxyAddr=":45678" 6 | 7 | build: clean fmt vet test 8 | go build 9 | 10 | clean: 11 | rm -f guard 12 | 13 | fmt: 14 | go fmt ./... 15 | 16 | vet: 17 | go vet -v . 18 | 19 | test: 20 | go test -cover $(COVERPROFILE) -race $(DEBUG) $(ARGS) 21 | 22 | bench: 23 | go test -bench=. -benchmem $(ARGS) 24 | 25 | cover: 26 | $(eval COVERPROFILE += -coverprofile=coverage.out) 27 | go test -cover $(COVERPROFILE) -race $(ARGS) $(DEBUG) 28 | go tool cover -html=coverage.out 29 | rm -f coverage.out 30 | 31 | profiling: 32 | go test -bench=. -cpuprofile cpu.out -memprofile mem.out $(ARGS) 33 | 34 | release: clean fmt vet test 35 | GOOS=linux GOARCH=amd64 go build -o guard.linux_amd64 36 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please discribe what have done by this pull request in short: 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Guard 2 | 3 | [![Build Status](https://travis-ci.org/jiajunhuang/guard.svg?branch=master)](https://travis-ci.org/jiajunhuang/guard) 4 | [![codecov](https://codecov.io/gh/jiajunhuang/guard/branch/master/graph/badge.svg)](https://codecov.io/gh/jiajunhuang/guard) 5 | ![GoReport](https://goreportcard.com/badge/github.com/jiajunhuang/guard) 6 | 7 | guard is a generic high performance circuit breaker & proxy written in Go. It has four major components: 8 | 9 | - radix tree & response status ring: which stores registered URLs 10 | - load balancer: which distributes requests(algorithms: randomized distribute, round robin, weighted round robin) 11 | - circuit breaker: which makes sure your backend services will not be broken down by a large quantity of requests 12 | - proxy server: it's based on fasthttp 13 | 14 | ## Workflow 15 | 16 | ![workflow diagram](./workflow.png) 17 | 18 | ## Benchmark 19 | 20 | I've made a simple benchmark on my laptop(i5-3210M CPU @ 2.50GHz with 4 cores): 21 | 22 | ```bash 23 | $ wrk --latency -H "Host: www.example.com" -c 2048 -d 30 -t 2 http://127.0.0.1:9999 # Nginx with 4 workers 24 | Running 30s test @ http://127.0.0.1:9999 25 | 2 threads and 2048 connections 26 | Thread Stats Avg Stdev Max +/- Stdev 27 | Latency 99.62ms 158.44ms 1.36s 95.41% 28 | Req/Sec 6.43k 2.43k 11.36k 53.85% 29 | Latency Distribution 30 | 50% 58.34ms 31 | 75% 153.81ms 32 | 90% 165.62ms 33 | 99% 965.10ms 34 | 383670 requests in 30.09s, 311.00MB read 35 | Socket errors: connect 1029, read 0, write 0, timeout 114 36 | Requests/sec: 12749.73 37 | Transfer/sec: 10.33MB 38 | 39 | $ wrk --latency -H "Host: www.example.com" -c 2048 -d 30 -t 2 http://127.0.0.1:23456 # guard with GOMAXPROCS=4 40 | Running 30s test @ http://127.0.0.1:23456 41 | 2 threads and 2048 connections 42 | Thread Stats Avg Stdev Max +/- Stdev 43 | Latency 45.78ms 34.36ms 1.12s 89.09% 44 | Req/Sec 11.35k 1.18k 14.19k 73.20% 45 | Latency Distribution 46 | 50% 41.87ms 47 | 75% 56.55ms 48 | 90% 74.52ms 49 | 99% 111.06ms 50 | 676392 requests in 30.07s, 532.82MB read 51 | Socket errors: connect 1029, read 0, write 0, timeout 0 52 | Requests/sec: 22494.06 53 | Transfer/sec: 17.72MB 54 | ``` 55 | 56 | For now, guard's proxy performance is about ~~55% of Nginx~~ **1.76x** faster than Nginx, 57 | and **I'm still working on it! Don't worry, it will become better and better!** 58 | 59 | By the way, ~~thanks the [suggestion](https://github.com/jiajunhuang/guard/issues/15) 60 | from [@dongzerun](https://github.com/dongzerun), by configure the `GOGC` in environment, 61 | guard's proxy performance is about 70% of Nginx.~~ guard does not allocate much memory now, 62 | `GOGC` does not make a change, but still say thanks to [@dongzerun](https://github.com/dongzerun)! 63 | 64 | ## TODO 65 | 66 | moved to https://github.com/jiajunhuang/guard/projects/1 67 | 68 | ## Set it up 69 | 70 | 1. build it using `go get -u`: 71 | 72 | ```bash 73 | $ go get -u github.com/jiajunhuang/guard 74 | ``` 75 | 76 | 2. start it 77 | 78 | ```bash 79 | $ guard 80 | ``` 81 | 82 | 3. now you need to register an application by send a POST request to `http://127.0.0.1:12345/app` with json like this: 83 | 84 | ```json 85 | { 86 | "name": "www.example.com", 87 | "backends": ["127.0.0.1:80", "127.0.0.1:80", "127.0.0.1:80"], 88 | "weights": [5, 1, 1], 89 | "ratio": 0.3, 90 | "paths": ["/"], 91 | "methods": ["GET"], 92 | "fallback_type": "text", 93 | "fallback_content": "hello world" 94 | } 95 | ``` 96 | 97 | I'm doing it like this: 98 | 99 | ```bash 100 | $ http POST :12345/app < backends.json 101 | HTTP/1.1 200 OK 102 | Content-Length: 8 103 | Content-Type: text/plain; charset=utf-8 104 | Date: Sun, 21 Jan 2018 08:51:16 GMT 105 | 106 | success! 107 | ``` 108 | 109 | 4. and now, it works! whoops! try it: 110 | 111 | ```bash 112 | $ http :23456 'Host: www.example.com' 113 | HTTP/1.1 200 OK 114 | ... 115 | ``` 116 | 117 | 5. by the way, you can inspect your configuration by visit http://127.0.0.1:12345/ : 118 | 119 | ```bash 120 | $ http :12345 121 | HTTP/1.1 200 OK 122 | Content-Length: 235 123 | Content-Type: application/json 124 | Date: Fri, 26 Jan 2018 14:11:02 GMT 125 | 126 | { 127 | "apps": { 128 | "www.example.com": { 129 | "backends": [ 130 | "127.0.0.1:80", 131 | "127.0.0.1:80", 132 | "127.0.0.1:80" 133 | ], 134 | "disable_tsr": false, 135 | "load_balance_method": "rr", 136 | "methods": [ 137 | "GET", 138 | "GET" 139 | ], 140 | "name": "www.example.com", 141 | "paths": [ 142 | "/", 143 | "/doc" 144 | ], 145 | "ratio": 0.3, 146 | "weights": [ 147 | 5, 148 | 1, 149 | 1 150 | ] 151 | } 152 | } 153 | } 154 | ``` 155 | 156 | ## Changelogs 157 | 158 | - 2018-01-25: rewrite proxy from `net/http` to `fasthttp`, it's super fast now! 159 | - 2018-01-24: rewrite radix tree & statistics module, it's lock-free now! 160 | - 2018-01-22: add randomized distribute, naive round robin algorithms 161 | - 2018-01-21: rewrite status statistics module 162 | - 2018-01-20: guard works! 163 | - 2018-01-19: first commit 164 | 165 | ## Thanks 166 | 167 | - [Golang](https://golang.org) 168 | - [fasthttp](https://github.com/valyala/fasthttp) 169 | - [httprouter](https://github.com/jiajunhuang/httprouter) 170 | -------------------------------------------------------------------------------- /application.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/valyala/fasthttp" 7 | ) 8 | 9 | // Application is an abstraction of radix-tree, timeline, balancer, and configurations... 10 | type Application struct { 11 | // redirect if tsr is true? 12 | TSRRedirect bool 13 | 14 | balancer Balancer 15 | root *node 16 | fallbackType string 17 | FallbackContent []byte 18 | } 19 | 20 | // NewApp return a brand new Application 21 | func NewApp(b Balancer, tsr bool) *Application { 22 | return &Application{tsr, b, &node{}, "", []byte("")} 23 | } 24 | 25 | func convertMethod(methods ...string) HTTPMethod { 26 | httpMethods := NONE 27 | 28 | if len(methods) == 0 { 29 | log.Panicf("at least one method is required") 30 | } 31 | 32 | for _, m := range methods { 33 | switch m { 34 | case "GET": 35 | httpMethods |= GET 36 | case "POST": 37 | httpMethods |= POST 38 | case "PUT": 39 | httpMethods |= PUT 40 | case "DELETE": 41 | httpMethods |= DELETE 42 | case "HEAD": 43 | httpMethods |= HEAD 44 | case "OPTIONS": 45 | httpMethods |= OPTIONS 46 | case "CONNECT": 47 | httpMethods |= CONNECT 48 | case "TRACE": 49 | httpMethods |= TRACE 50 | case "PATCH": 51 | httpMethods |= PATCH 52 | default: 53 | log.Panicf("bad http method: %s", m) 54 | } 55 | } 56 | 57 | return httpMethods 58 | } 59 | 60 | // AddRoute add a route to itself 61 | func (a *Application) AddRoute(path string, methods ...string) { 62 | a.root.addRoute([]byte(path), convertMethod(methods...)) 63 | } 64 | 65 | func (a *Application) ServeHTTP(ctx *fasthttp.RequestCtx) { 66 | if a.root == nil { 67 | log.Panic("application should bind a URL-tree") 68 | } 69 | if a.balancer == nil { 70 | log.Panic("application should bind a load balancer") 71 | } 72 | 73 | path := ctx.Path() 74 | n, tsr, found := a.root.byPath(path) 75 | 76 | // redirect? 77 | if tsr && a.TSRRedirect { 78 | code := fasthttp.StatusMovedPermanently 79 | if string(ctx.Method()) != "GET" { 80 | code = fasthttp.StatusTemporaryRedirect 81 | } 82 | 83 | var redirectTo []byte 84 | if len(path) > 1 && path[len(path)-1] == '/' { 85 | redirectTo = path[:len(path)-1] 86 | } else { 87 | redirectTo = append(path, '/') 88 | } 89 | log.Printf("redirect to %s", redirectTo) 90 | ctx.RedirectBytes(redirectTo, code) 91 | return 92 | } 93 | 94 | // not found 95 | if !found { 96 | ctx.NotFound() 97 | return 98 | } 99 | 100 | // method allowed? 101 | if !n.hasMethod(convertMethod(string(ctx.Method()))) { 102 | ctx.SetStatusCode(fasthttp.StatusMethodNotAllowed) 103 | return 104 | } 105 | 106 | // circuit breaker is open? 107 | _, _, _, _, ratio := n.query() 108 | if ratio > 0.3 { 109 | // fallback 110 | log.Printf("too many requests, ratio is %f", ratio) 111 | switch a.fallbackType { 112 | case fallbackJSON: 113 | ctx.SetContentType("application/json") 114 | case fallbackHTML, fallbackHTMLFile: 115 | ctx.SetContentType("text/html") 116 | case fallbackTEXT: 117 | ctx.SetContentType("text/plain") 118 | default: 119 | ctx.SetContentType("text/plain") 120 | } 121 | ctx.SetStatusCode(fasthttp.StatusTooManyRequests) 122 | ctx.Write(a.FallbackContent) 123 | 124 | return 125 | } 126 | 127 | // proxy! and then feedback the result 128 | n.incr(Proxy(a.balancer, ctx)) 129 | } 130 | -------------------------------------------------------------------------------- /application_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/valyala/fasthttp" 7 | "github.com/valyala/fasthttp/fasthttputil" 8 | ) 9 | 10 | func TestAddRouteWorks(t *testing.T) { 11 | a := NewApp(NewRdm(), true) 12 | 13 | a.AddRoute("/user/:name", "GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "CONNECT", "TRACE", "PATCH") 14 | } 15 | 16 | func TestAddRouteBadMethod(t *testing.T) { 17 | defer shouldPanic() 18 | 19 | a := NewApp(NewRdm(), true) 20 | 21 | a.AddRoute("/user/:name", "BABY") 22 | } 23 | 24 | func TestAddRouteNoMethod(t *testing.T) { 25 | defer shouldPanic() 26 | 27 | a := NewApp(NewRdm(), true) 28 | 29 | a.AddRoute("/user/:name") 30 | } 31 | 32 | func TestApplicationNilTree(t *testing.T) { 33 | defer shouldPanic() 34 | 35 | a := NewApp(NewRdm(), true) 36 | a.root = nil 37 | 38 | ctx := &fasthttp.RequestCtx{} 39 | ctx.Request.SetRequestURI("/") 40 | 41 | a.ServeHTTP(ctx) 42 | } 43 | 44 | func TestApplicationNilBalancer(t *testing.T) { 45 | defer shouldPanic() 46 | 47 | a := NewApp(nil, true) 48 | 49 | ctx := &fasthttp.RequestCtx{} 50 | ctx.Request.SetRequestURI("/") 51 | 52 | a.ServeHTTP(ctx) 53 | } 54 | 55 | func TestApplicationRedirect(t *testing.T) { 56 | ln := fasthttputil.NewInmemoryListener() 57 | defer ln.Close() 58 | 59 | go fasthttp.Serve(ln, fakeHandler) 60 | 61 | setFakeBackend(ln.Addr().String(), 1) 62 | 63 | fb := fakeBalancer{} 64 | a := NewApp(fb, true) 65 | a.AddRoute("/user/jhon", "POST") 66 | a.AddRoute("/user/jhon/card/", "POST") 67 | 68 | //redirect 69 | ctx := &fasthttp.RequestCtx{} 70 | ctx.Request.SetRequestURI("/user/jhon/") 71 | ctx.Request.Header.SetMethod("POST") 72 | a.ServeHTTP(ctx) 73 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusTemporaryRedirect { 74 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusTemporaryRedirect, code) 75 | } 76 | 77 | // redirect 78 | ctx = &fasthttp.RequestCtx{} 79 | ctx.Request.SetRequestURI("/user/jhon/card") 80 | ctx.Request.Header.SetMethod("POST") 81 | a.ServeHTTP(ctx) 82 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusTemporaryRedirect { 83 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusTemporaryRedirect, code) 84 | } 85 | 86 | // not found 87 | ctx = &fasthttp.RequestCtx{} 88 | ctx.Request.SetRequestURI("/user/what/card") 89 | ctx.Request.Header.SetMethod("POST") 90 | a.ServeHTTP(ctx) 91 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusNotFound { 92 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusNotFound, code) 93 | } 94 | 95 | // method not allowed 96 | ctx = &fasthttp.RequestCtx{} 97 | ctx.Request.SetRequestURI("/user/jhon") 98 | ctx.Request.Header.SetMethod("GET") 99 | a.ServeHTTP(ctx) 100 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusMethodNotAllowed { 101 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusMethodNotAllowed, code) 102 | } 103 | } 104 | 105 | func TestApplicationCircuit(t *testing.T) { 106 | ln := fasthttputil.NewInmemoryListener() 107 | defer ln.Close() 108 | 109 | go fasthttp.Serve(ln, fakeHandler) 110 | 111 | setFakeBackend(ln.Addr().String(), 1) 112 | 113 | fb := fakeBalancer{} 114 | a := NewApp(fb, true) 115 | a.AddRoute("/user/jhon", "POST") 116 | 117 | //redirect 118 | ctx := &fasthttp.RequestCtx{} 119 | ctx.Request.SetRequestURI("/user/jhon/") 120 | ctx.Request.Header.SetMethod("POST") 121 | a.ServeHTTP(ctx) 122 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusTemporaryRedirect { 123 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusTemporaryRedirect, code) 124 | } 125 | 126 | // circuit is not on 127 | ctx = &fasthttp.RequestCtx{} 128 | ctx.Request.SetRequestURI("/user/jhon") 129 | ctx.Request.Header.SetMethod("POST") 130 | a.ServeHTTP(ctx) // do not check what `Proxy` returns 131 | 132 | // circuit is on 133 | for i := 0; i < 100; i++ { 134 | a.root.incr(fasthttp.StatusBadGateway) 135 | } 136 | a.ServeHTTP(ctx) 137 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusTooManyRequests { 138 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusTooManyRequests, code) 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /balancer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/valyala/fasthttp" 5 | ) 6 | 7 | // load balancer: return which backend should we proxy to 8 | const ( 9 | LBMWRR = "wrr" 10 | LBMRR = "rr" 11 | LBMRandom = "random" 12 | ) 13 | 14 | // Backend is the backend server, usually a app server like: gunicorn+flask 15 | type Backend struct { 16 | Weight int 17 | URL string // cache the result 18 | client *fasthttp.HostClient 19 | } 20 | 21 | // NewBackend return a new backend 22 | func NewBackend(url string, weight int) Backend { 23 | return Backend{ 24 | weight, url, 25 | &fasthttp.HostClient{Addr: url, MaxConns: fasthttp.DefaultMaxConnsPerHost * 4}, 26 | } 27 | } 28 | 29 | // Balancer should have a method `Select`, which return the backend we should 30 | // proxy. 31 | type Balancer interface { 32 | Select() (*Backend, bool) 33 | } 34 | -------------------------------------------------------------------------------- /balancer_random.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | // Rdm is struct for random balance algorithm 8 | type Rdm struct { 9 | upstream []Backend 10 | } 11 | 12 | // NewRdm return a brand new random balancer 13 | func NewRdm(backends ...Backend) *Rdm { 14 | return &Rdm{backends} 15 | } 16 | 17 | // Select return a backend randomly 18 | func (r *Rdm) Select() (*Backend, bool) { 19 | length := len(r.upstream) 20 | if length == 0 { 21 | return nil, false 22 | } else if length == 1 { 23 | return &r.upstream[0], true 24 | } 25 | 26 | return &(r.upstream[rand.Int()%length]), true 27 | } 28 | -------------------------------------------------------------------------------- /balancer_random_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestRandomBalancer(t *testing.T) { 8 | b1 := NewBackend("192.168.1.1:80", 5) 9 | b2 := NewBackend("192.168.1.2:80", 1) 10 | b3 := NewBackend("192.168.1.3:80", 1) 11 | 12 | // no backends 13 | balancer := NewRdm() 14 | _, found := balancer.Select() 15 | if found { 16 | t.Error("no backend should found!") 17 | } 18 | 19 | // one backend 20 | balancer = NewRdm(b1) 21 | _, found = balancer.Select() 22 | if !found { 23 | t.Error("one backend should found!") 24 | } 25 | 26 | balancer = NewRdm(b1, b2, b3) 27 | _, found = balancer.Select() 28 | if !found { 29 | t.Error("one backend should found!") 30 | } 31 | } 32 | 33 | func BenchmarkRdmSelect(b *testing.B) { 34 | b1 := NewBackend("192.168.1.1:80", 5) 35 | b2 := NewBackend("192.168.1.2:80", 1) 36 | b3 := NewBackend("192.168.1.3:80", 1) 37 | 38 | // no backends 39 | balancer := NewRdm(b1, b2, b3) 40 | 41 | for i := 0; i < b.N; i++ { 42 | balancer.Select() 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /balancer_rr.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sync/atomic" 5 | ) 6 | 7 | // RR is struct for naive round robin balance algorithm 8 | type RR struct { 9 | upstream []Backend 10 | index uint64 11 | } 12 | 13 | // NewRR return a brand new naive round robin balancer 14 | func NewRR(backends ...Backend) *RR { 15 | return &RR{backends, 0} 16 | } 17 | 18 | // Select return a backend randomly 19 | func (r *RR) Select() (b *Backend, found bool) { 20 | length := uint64(len(r.upstream)) 21 | if length == 0 { 22 | return nil, false 23 | } else if length == 1 { 24 | return &r.upstream[0], true 25 | } 26 | 27 | // TODO: shuold we check for overflow? 28 | return &(r.upstream[atomic.AddUint64(&r.index, 1)%length]), true 29 | } 30 | -------------------------------------------------------------------------------- /balancer_rr_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestRRBalancer(t *testing.T) { 8 | b1 := NewBackend("192.168.1.1:80", 5) 9 | b2 := NewBackend("192.168.1.2:80", 1) 10 | b3 := NewBackend("192.168.1.3:80", 1) 11 | 12 | // no backends 13 | balancer := NewRR() 14 | _, found := balancer.Select() 15 | if found { 16 | t.Error("no backend should found!") 17 | } 18 | 19 | // one backends 20 | balancer = NewRR(b1) 21 | _, found = balancer.Select() 22 | if !found { 23 | t.Error("one backend should found!") 24 | } 25 | 26 | balancer = NewRR(b1, b2, b3) 27 | _, found = balancer.Select() 28 | if !found { 29 | t.Error("one backend should found!") 30 | } 31 | } 32 | 33 | func BenchmarkRRSelect(b *testing.B) { 34 | b1 := NewBackend("192.168.1.1:80", 5) 35 | b2 := NewBackend("192.168.1.2:80", 1) 36 | b3 := NewBackend("192.168.1.3:80", 1) 37 | 38 | // no backends 39 | balancer := NewRR(b1, b2, b3) 40 | 41 | for i := 0; i < b.N; i++ { 42 | balancer.Select() 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /balancer_wrr.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | // WRR is weighted round robin algorithm, it's borrowed from Nginx: 8 | // https://github.com/nginx/nginx/commit/52327e0627f49dbda1e8db695e63a4b0af4448b1 9 | type WRR struct { 10 | lock sync.Mutex 11 | 12 | upstream []Backend 13 | totalWeight int 14 | weights []int 15 | } 16 | 17 | // NewWRR return a instance with initialized weights & totalWeight 18 | func NewWRR(backends ...Backend) *WRR { 19 | totalWeight := 0 20 | weights := make([]int, len(backends)) 21 | 22 | for _, b := range backends { 23 | totalWeight += b.Weight 24 | } 25 | 26 | return &WRR{upstream: backends, totalWeight: totalWeight, weights: weights} 27 | } 28 | 29 | // Select return the backend we should proxy 30 | // for example, weights of [5, 1, 1] should generate sequence of index: 31 | // [1, 1, 2, 1, 3, 1, 1] 32 | func (w *WRR) Select() (b *Backend, found bool) { 33 | length := uint64(len(w.upstream)) 34 | if length == 0 { 35 | return nil, false 36 | } else if length == 1 { 37 | return &w.upstream[0], true 38 | } 39 | 40 | w.lock.Lock() 41 | 42 | totalWeight := w.totalWeight 43 | upstream := w.upstream 44 | weights := w.weights 45 | biggest := -1 46 | biggestWeight := 0 47 | 48 | for i := range weights { 49 | weights[i] += upstream[i].Weight 50 | 51 | if weights[i] > biggestWeight { 52 | biggestWeight = weights[i] 53 | biggest = i 54 | } 55 | } 56 | 57 | if biggest >= 0 && biggest < len(weights) { 58 | weights[biggest] -= totalWeight 59 | 60 | // defer is too slow... 61 | w.lock.Unlock() 62 | 63 | return &w.upstream[biggest], true 64 | } 65 | 66 | // defer is too slow... 67 | w.lock.Unlock() 68 | 69 | return nil, false 70 | } 71 | -------------------------------------------------------------------------------- /balancer_wrr_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func TestWRRFound(t *testing.T) { 9 | // weights of [5, 1, 1] should generate sequence of index: [1, 1, 2, 1, 3, 1, 1] 10 | h1 := "192.168.1.1:80" 11 | h2 := "192.168.1.2:80" 12 | h3 := "192.168.1.3:80" 13 | b1 := NewBackend(h1, 5) 14 | b2 := NewBackend(h2, 1) 15 | b3 := NewBackend(h3, 1) 16 | 17 | wrr := NewWRR(b1) 18 | _, found := wrr.Select() 19 | if !found { 20 | t.Errorf("one backend should found") 21 | } 22 | 23 | wrr = NewWRR(b1, b2, b3) 24 | 25 | var r *Backend 26 | var f bool 27 | type expectResult struct { 28 | found bool 29 | host string 30 | shouldBe []int 31 | } 32 | expectResultList := []expectResult{ 33 | expectResult{true, h1, []int{-2, 1, 1}}, 34 | expectResult{true, h1, []int{-4, 2, 2}}, 35 | expectResult{true, h2, []int{1, -4, 3}}, 36 | expectResult{true, h1, []int{-1, -3, 4}}, 37 | expectResult{true, h3, []int{4, -2, -2}}, 38 | expectResult{true, h1, []int{2, -1, -1}}, 39 | expectResult{true, h1, []int{0, 0, 0}}, 40 | } 41 | 42 | for i, e := range expectResultList { 43 | r, f = wrr.Select() 44 | if f != e.found || r.URL != e.host { 45 | t.Errorf("the %dth select should found the %s, but got: %+v, %t", i, e.host, r.URL, f) 46 | } 47 | wrr.lock.Lock() 48 | if !reflect.DeepEqual(wrr.weights, e.shouldBe) { 49 | t.Errorf("wrr's weights should be %+v, but got: %+v", e.shouldBe, wrr.weights) 50 | } 51 | wrr.lock.Unlock() 52 | } 53 | } 54 | 55 | func TestWRRNotFound(t *testing.T) { 56 | h1 := "192.168.1.1:80" 57 | h2 := "192.168.1.2:80" 58 | h3 := "192.168.1.3:80" 59 | b1 := NewBackend(h1, 0) 60 | b2 := NewBackend(h2, 0) 61 | b3 := NewBackend(h3, 0) 62 | 63 | // no backends 64 | wrr := NewWRR() 65 | _, found := wrr.Select() 66 | if found { 67 | t.Errorf("no backend should found") 68 | } 69 | 70 | // backends with weight 0 71 | wrr = NewWRR(b1, b2, b3) 72 | 73 | var r *Backend 74 | var f bool 75 | type expectResult struct { 76 | found bool 77 | backend *Backend 78 | shouldBe []int 79 | } 80 | expectResultList := []expectResult{ 81 | expectResult{false, nil, []int{0, 0, 0}}, 82 | expectResult{false, nil, []int{0, 0, 0}}, 83 | expectResult{false, nil, []int{0, 0, 0}}, 84 | expectResult{false, nil, []int{0, 0, 0}}, 85 | expectResult{false, nil, []int{0, 0, 0}}, 86 | expectResult{false, nil, []int{0, 0, 0}}, 87 | expectResult{false, nil, []int{0, 0, 0}}, 88 | } 89 | 90 | for i, e := range expectResultList { 91 | r, f = wrr.Select() 92 | if f != e.found || r != e.backend { 93 | t.Errorf("the %dth select should found the %+v, but got: %+v, %t", i, e.backend, r.URL, f) 94 | } 95 | wrr.lock.Lock() 96 | if !reflect.DeepEqual(wrr.weights, e.shouldBe) { 97 | t.Errorf("wrr's weights should be %+v, but got: %+v", e.shouldBe, wrr.weights) 98 | } 99 | wrr.lock.Unlock() 100 | } 101 | } 102 | 103 | func BenchmarkWRRSelect(b *testing.B) { 104 | h1 := "192.168.1.1:80" 105 | h2 := "192.168.1.2:80" 106 | h3 := "192.168.1.3:80" 107 | b1 := NewBackend(h1, 5) 108 | b2 := NewBackend(h2, 1) 109 | b3 := NewBackend(h3, 1) 110 | 111 | wrr := NewWRR(b1, b2, b3) 112 | 113 | for i := 0; i < b.N; i++ { 114 | wrr.Select() 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /breaker.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/valyala/fasthttp" 5 | ) 6 | 7 | /* 8 | circuit breaker, response for handle requests, decide reject it or not, record response 9 | status. 10 | */ 11 | 12 | // Breaker is circuit breaker, it's a collection of Application 13 | type Breaker struct { 14 | apps map[string]*Application 15 | } 16 | 17 | // NewBreaker return a brand new circuit breaker, with nothing in mapper 18 | func NewBreaker() *Breaker { 19 | return &Breaker{ 20 | make(map[string]*Application), 21 | } 22 | } 23 | 24 | func (b *Breaker) ServeHTTP(ctx *fasthttp.RequestCtx) { 25 | appName := string(ctx.Host()) 26 | var app *Application 27 | var exist bool 28 | if app, exist = b.apps[appName]; !exist { 29 | ctx.WriteString("app " + appName + " not exist") 30 | ctx.SetStatusCode(fasthttp.StatusNotFound) 31 | return 32 | } 33 | 34 | app.ServeHTTP(ctx) 35 | } 36 | -------------------------------------------------------------------------------- /breaker_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/valyala/fasthttp" 7 | "github.com/valyala/fasthttp/fasthttputil" 8 | ) 9 | 10 | func TestBreakerServeHTTP(t *testing.T) { 11 | ln := fasthttputil.NewInmemoryListener() 12 | defer ln.Close() 13 | 14 | go fasthttp.Serve(ln, fakeHandler) 15 | 16 | setFakeBackend(ln.Addr().String(), 0) 17 | 18 | fb := fakeBalancer{} 19 | a := NewApp(fb, true) 20 | a.AddRoute("/", "GET") 21 | 22 | breaker := NewBreaker() 23 | appName := "www.example.com" 24 | 25 | // app not found 26 | ctx := &fasthttp.RequestCtx{} 27 | ctx.Request.SetRequestURI("/") 28 | ctx.Request.SetHost(appName) 29 | breaker.ServeHTTP(ctx) 30 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusNotFound { 31 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusNotFound, code) 32 | } 33 | 34 | // app found, but circuit is open, so return 403 forbidden 35 | ctx = &fasthttp.RequestCtx{} 36 | ctx.Request.SetRequestURI("/") 37 | ctx.Request.SetHost(appName) 38 | breaker.apps[appName] = a 39 | breaker.ServeHTTP(ctx) 40 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusForbidden { 41 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusForbidden, code) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /coarse_time.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sync/atomic" 5 | "time" 6 | ) 7 | 8 | // CoarseTimeNow returns the current time truncated to the nearest second. 9 | // 10 | // This is a faster alternative to time.Now(). 11 | func CoarseTimeNow() time.Time { 12 | tp := coarseTime.Load().(*time.Time) 13 | return *tp 14 | } 15 | 16 | func init() { 17 | t := time.Now().Truncate(time.Second) 18 | coarseTime.Store(&t) 19 | go func() { 20 | for { 21 | time.Sleep(time.Second) 22 | t := time.Now().Truncate(time.Second) 23 | coarseTime.Store(&t) 24 | } 25 | }() 26 | } 27 | 28 | var coarseTime atomic.Value 29 | -------------------------------------------------------------------------------- /coarse_time_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sync/atomic" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func BenchmarkCoarseTimeNow(b *testing.B) { 10 | var zeroTimeCount uint64 11 | b.RunParallel(func(pb *testing.PB) { 12 | for pb.Next() { 13 | t := CoarseTimeNow() 14 | if t.IsZero() { 15 | atomic.AddUint64(&zeroTimeCount, 1) 16 | } 17 | } 18 | }) 19 | if zeroTimeCount > 0 { 20 | b.Fatalf("zeroTimeCount must be zero") 21 | } 22 | } 23 | 24 | func BenchmarkTimeNow(b *testing.B) { 25 | var zeroTimeCount uint64 26 | b.RunParallel(func(pb *testing.PB) { 27 | for pb.Next() { 28 | t := time.Now() 29 | if t.IsZero() { 30 | atomic.AddUint64(&zeroTimeCount, 1) 31 | } 32 | } 33 | }) 34 | if zeroTimeCount > 0 { 35 | b.Fatalf("zeroTimeCount must be zero") 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "io/ioutil" 7 | "log" 8 | "net/http" 9 | "os" 10 | "strings" 11 | ) 12 | 13 | const ( 14 | fallbackTEXT = "text" 15 | fallbackJSON = "json" 16 | fallbackHTML = "html" 17 | fallbackHTMLFile = "html_file" 18 | ) 19 | 20 | var ( 21 | errNameEmpty = errors.New("name is required") 22 | errBackendWeightNotMatch = errors.New("backend and weight does not match") 23 | errPathMethodNotMatch = errors.New("path and method does not match") 24 | errBadLoadBalanceAlgorithm = errors.New("bad load balance algorithm, only wrr, rr, random are support now") 25 | errBadFallbackType = errors.New("bad fallback type") 26 | 27 | configSync = make(chan appConfig) 28 | ) 29 | 30 | type appConfig struct { 31 | Name string `json:"name"` 32 | Backends []string `json:"backends"` // e.g. ["192.168.1.1:80", "192.168.1.2:80", "192.168.1.3:1080"] 33 | Weights []int `json:"weights"` // e.g. [5, 1, 1] 34 | Ratio float64 `json:"ratio"` 35 | DisableTSR bool `json:"disable_tsr"` 36 | LoadBalanceMethod string `json:"load_balance_method"` // wrr, rr, random 37 | Paths []string `json:"paths"` 38 | Methods []string `json:"methods"` 39 | FallbackType string `json:"fallback_type"` 40 | FallbackContent string `json:"fallback_content"` 41 | } 42 | 43 | func checkAppConfig(a *appConfig) error { 44 | if a.Name == "" { 45 | return errNameEmpty 46 | } 47 | 48 | if len(a.Backends) != len(a.Weights) { 49 | return errBackendWeightNotMatch 50 | } 51 | 52 | if len(a.Paths) != len(a.Methods) { 53 | return errPathMethodNotMatch 54 | } 55 | 56 | if a.LoadBalanceMethod == "" { 57 | a.LoadBalanceMethod = "rr" 58 | log.Printf("by default, app %s are using %s as load balance algorithm", a.Name, a.LoadBalanceMethod) 59 | } 60 | 61 | switch a.FallbackType { 62 | case "", fallbackTEXT: 63 | a.FallbackType = fallbackTEXT 64 | if len(a.FallbackContent) == 0 { 65 | a.FallbackContent = "too many requests" 66 | } 67 | case fallbackJSON, fallbackHTML: 68 | 69 | case fallbackHTMLFile: 70 | html, err := ioutil.ReadFile(a.FallbackContent) 71 | if err != nil { 72 | return err 73 | } 74 | a.FallbackType = fallbackHTML 75 | a.FallbackContent = string(html) 76 | default: 77 | return errBadFallbackType 78 | } 79 | 80 | switch a.LoadBalanceMethod { 81 | case LBMWRR, LBMRR, LBMRandom: 82 | return nil 83 | default: 84 | return errBadLoadBalanceAlgorithm 85 | } 86 | } 87 | 88 | func getBalancer(loadBalanceMethod string, backends ...Backend) Balancer { 89 | switch loadBalanceMethod { 90 | case LBMWRR: 91 | return NewWRR(backends...) 92 | case LBMRR: 93 | return NewRR(backends...) 94 | case LBMRandom: 95 | return NewRdm(backends...) 96 | default: 97 | log.Panicf("bad load balance algorithm: %s", loadBalanceMethod) 98 | return nil // never here 99 | } 100 | } 101 | 102 | func getAPP(config *appConfig) *Application { 103 | backends := []Backend{} 104 | for i, url := range config.Backends { 105 | backends = append(backends, NewBackend(url, config.Weights[i])) 106 | } 107 | 108 | balancer := getBalancer(config.LoadBalanceMethod, backends...) 109 | 110 | app := NewApp(balancer, !config.DisableTSR) 111 | 112 | for i, path := range config.Paths { 113 | app.AddRoute(path, strings.ToUpper(config.Methods[i])) 114 | } 115 | 116 | app.fallbackType = config.FallbackType 117 | app.FallbackContent = []byte(config.FallbackContent) 118 | 119 | return app 120 | } 121 | 122 | func appHandler(w http.ResponseWriter, r *http.Request) { 123 | if r.Method != "POST" { 124 | w.WriteHeader(http.StatusMethodNotAllowed) 125 | return 126 | } 127 | 128 | defer r.Body.Close() 129 | var config appConfig 130 | 131 | decoder := json.NewDecoder(r.Body) 132 | if err := decoder.Decode(&config); err != nil { 133 | w.WriteHeader(http.StatusBadRequest) 134 | w.Write([]byte("bad configuration: " + err.Error())) 135 | return 136 | } 137 | 138 | if err := checkAppConfig(&config); err != nil { 139 | w.WriteHeader(http.StatusBadRequest) 140 | w.Write([]byte("bad configuration: " + err.Error())) 141 | return 142 | } 143 | 144 | // replace breaker's map, FIXME: here may raise data race... 145 | breaker.apps[config.Name] = getAPP(&config) 146 | 147 | go func() { configSync <- config }() 148 | w.Write([]byte("success!")) 149 | } 150 | 151 | type breakerConfig struct { 152 | APPs map[string]appConfig `json:"apps"` 153 | } 154 | 155 | func readFromFile(path string) ([]byte, error) { 156 | f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644) 157 | if err != nil { 158 | return nil, err 159 | } 160 | defer f.Close() 161 | 162 | return ioutil.ReadAll(f) 163 | } 164 | 165 | func configKeeper() { 166 | // first try to load config 167 | b := breakerConfig{make(map[string](appConfig))} 168 | 169 | fileBytes, err := readFromFile(*configPath) 170 | if err != nil { 171 | log.Panicf("failed to use config file %s: %s", *configPath, err) 172 | } 173 | if err := json.Unmarshal(fileBytes, &b); err == nil { 174 | log.Printf("loading config from config file") 175 | for k, v := range b.APPs { 176 | breaker.apps[k] = getAPP(&v) 177 | } 178 | } else { 179 | log.Printf("failed to unmarshal config file %s because %s", *configPath, err) 180 | } 181 | 182 | // listen channel for sync 183 | for config := range configSync { 184 | f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644) 185 | if err != nil { 186 | log.Panicf("failed to open config file: %s", err) 187 | } 188 | 189 | if err := checkAppConfig(&config); err != nil { 190 | log.Printf("receive a bad config: %+v, ignore it", config) 191 | continue 192 | } 193 | 194 | if err = json.Unmarshal(fileBytes, &b); err != nil && len(fileBytes) > 0 { 195 | log.Printf("failed to unmarshal config file %s because %s", *configPath, err) 196 | continue 197 | } 198 | b.APPs[config.Name] = config 199 | 200 | f.Truncate(0) 201 | f.Seek(0, 0) 202 | jsonBytes, err := json.Marshal(b) 203 | if err != nil { 204 | log.Printf("failed to marshal configuration, err is: %s", err) 205 | continue 206 | } 207 | _, err = f.Write(jsonBytes) 208 | if err != nil { 209 | log.Printf("failed to sync configuration to backup file %s because: %s", *configPath, err) 210 | continue 211 | } 212 | 213 | f.Close() 214 | log.Printf("sync configuration to backup file %s succeed", *configPath) 215 | } 216 | 217 | log.Printf("stop sync config file") 218 | } 219 | 220 | func configIndexHandler(w http.ResponseWriter, r *http.Request) { 221 | if fileBytes, err := readFromFile(*configPath); err != nil { 222 | log.Printf("failed to read from %s: %s", *configPath, err) 223 | w.WriteHeader(http.StatusInternalServerError) 224 | } else { 225 | w.Header().Set("Content-Type", "application/json") 226 | w.Write(fileBytes) 227 | } 228 | } 229 | 230 | func configManager() { 231 | go configKeeper() 232 | http.HandleFunc("/app", appHandler) 233 | http.HandleFunc("/", configIndexHandler) 234 | log.Fatal(http.ListenAndServe(*configAddr, nil)) 235 | } 236 | -------------------------------------------------------------------------------- /config_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "net/http" 6 | "net/http/httptest" 7 | "os" 8 | "testing" 9 | ) 10 | 11 | var ( 12 | badJSON = `"name":"www.example.com","backends":["127.0.0.1:80","127.0.0.1:80","127.0.0.1:80"],"weights":[5,1],"ratio":0.3,"paths":["/"],"methods":["GET"]}` 13 | badConfig = `{"name":"www.example.com","backends":["127.0.0.1:80","127.0.0.1:80","127.0.0.1:80"],"weights":[5,1],"ratio":0.3,"paths":["/"],"methods":["GET"]}` 14 | goodConfig = `{"name":"www.example.com","backends":["127.0.0.1:80","127.0.0.1:80","127.0.0.1:80"],"weights":[5,1,1],"ratio":0.3,"paths":["/"],"methods":["GET"]}` 15 | ) 16 | 17 | func TestUpdateConfig(t *testing.T) { 18 | fakeServer := httptest.NewServer( 19 | http.HandlerFunc(appHandler), 20 | ) 21 | defer fakeServer.Close() 22 | 23 | url := fakeServer.URL + "/app" 24 | 25 | // try get 26 | resp, err := http.Get(url) 27 | if err != nil || resp.StatusCode != http.StatusMethodNotAllowed { 28 | t.Errorf("should return 405, but got: %d", resp.StatusCode) 29 | } 30 | 31 | // post bad json 32 | resp, err = http.Post(url, "application/json", bytes.NewBuffer([]byte(badJSON))) 33 | if err != nil || resp.StatusCode != http.StatusBadRequest { 34 | t.Errorf("should return 400, but got: %d", resp.StatusCode) 35 | } 36 | 37 | // post bad config 38 | resp, err = http.Post(url, "application/json", bytes.NewBuffer([]byte(badConfig))) 39 | if err != nil || resp.StatusCode != http.StatusBadRequest { 40 | t.Errorf("should return 400, but got: %d", resp.StatusCode) 41 | } 42 | 43 | // post bad config 44 | resp, err = http.Post(url, "application/json", bytes.NewBuffer([]byte(goodConfig))) 45 | if err != nil || resp.StatusCode != http.StatusOK { 46 | t.Errorf("should return 200, but got: %d", resp.StatusCode) 47 | } 48 | } 49 | 50 | func TestCheckAPPConfig(t *testing.T) { 51 | config := &appConfig{} 52 | 53 | // not not exist 54 | if err := checkAppConfig(config); err == nil { 55 | t.Errorf("should return error but not") 56 | } 57 | 58 | config.Name = "www.example.com" 59 | // len(backends) != len(weights) 60 | config.Backends = []string{"192.168.1.1:80"} 61 | if err := checkAppConfig(config); err == nil { 62 | t.Errorf("should return error but not") 63 | } 64 | 65 | // len(path) != len(methods) 66 | config.Weights = []int{1} 67 | config.Paths = []string{"/"} 68 | if err := checkAppConfig(config); err == nil { 69 | t.Errorf("should return error but not") 70 | } 71 | config.Methods = []string{"get"} 72 | 73 | // load balancer algorithm not exist 74 | config.LoadBalanceMethod = "what" 75 | if err := checkAppConfig(config); err == nil { 76 | t.Errorf("should return error but not") 77 | } 78 | 79 | // check fallback 80 | config.Name = "www.example.com" 81 | config.Backends = []string{"192.168.1.1:80"} 82 | config.Weights = []int{1} 83 | config.Ratio = 0.3 84 | config.DisableTSR = false 85 | config.LoadBalanceMethod = LBMWRR 86 | config.Paths = []string{"/"} 87 | config.Methods = []string{"get"} 88 | 89 | // "" or text 90 | config.FallbackType = "" 91 | content := "too many requests" 92 | if err := checkAppConfig(config); err != nil { 93 | t.Errorf("should not return error but got: %s", err) 94 | } 95 | if config.FallbackType != fallbackTEXT || config.FallbackContent != content { 96 | t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent) 97 | } 98 | config.FallbackType = fallbackTEXT 99 | if err := checkAppConfig(config); err != nil { 100 | t.Errorf("should not return error but got: %s", err) 101 | } 102 | if config.FallbackType != fallbackTEXT || config.FallbackContent != content { 103 | t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent) 104 | } 105 | 106 | // "json" 107 | config.FallbackType = fallbackJSON 108 | content = "{}" 109 | config.FallbackContent = content 110 | if err := checkAppConfig(config); err != nil { 111 | t.Errorf("should not return error but got: %s", err) 112 | } 113 | if config.FallbackType != fallbackJSON || config.FallbackContent != content { 114 | t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent) 115 | } 116 | 117 | // "html" 118 | config.FallbackType = fallbackHTML 119 | content = "" 120 | config.FallbackContent = content 121 | if err := checkAppConfig(config); err != nil { 122 | t.Errorf("should not return error but got: %s", err) 123 | } 124 | if config.FallbackType != fallbackHTML || config.FallbackContent != content { 125 | t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent) 126 | } 127 | 128 | // "html_file" 129 | // file not exist 130 | filePath := "/tmp/test_guard_html_file_path.html" 131 | os.Remove(filePath) 132 | config.FallbackType = fallbackHTMLFile 133 | config.FallbackContent = filePath 134 | if err := checkAppConfig(config); err == nil { 135 | t.Errorf("should return error but not") 136 | } 137 | 138 | // file exists 139 | f, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0644) 140 | if err != nil { 141 | t.Errorf("failed to open %s: %s", filePath, err) 142 | } 143 | defer f.Close() 144 | content = "" 145 | f.Write([]byte(content)) 146 | if err := checkAppConfig(config); err != nil { 147 | t.Errorf("should not return error, but got: %s", err) 148 | } 149 | if config.FallbackType != fallbackHTML || config.FallbackContent != content { 150 | t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent) 151 | } 152 | 153 | // bad type 154 | config.FallbackType = "what" 155 | if err := checkAppConfig(config); err == nil { 156 | t.Errorf("should return error but not") 157 | } 158 | } 159 | 160 | func TestGetBalancer(t *testing.T) { 161 | getBalancer(LBMWRR) 162 | getBalancer(LBMRR) 163 | getBalancer(LBMRandom) 164 | 165 | defer shouldPanic() 166 | getBalancer("what") 167 | } 168 | 169 | func TestReadFromFile(t *testing.T) { 170 | os.Remove(*configPath) 171 | defer os.Remove(*configPath) 172 | 173 | f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644) 174 | if err != nil { 175 | t.Errorf("failed to open config file: %s", err) 176 | } 177 | defer f.Close() 178 | content := []byte("hello world") 179 | f.Write(content) 180 | 181 | readContent, _ := readFromFile(*configPath) 182 | if !bytes.Equal(readContent, content) { 183 | t.Errorf("read from file return bad content: %s", string(readContent)) 184 | } 185 | } 186 | 187 | func TestConfigKeeper(t *testing.T) { 188 | os.Remove(*configPath) 189 | defer os.Remove(*configPath) 190 | 191 | config := appConfig{ 192 | "www.example.com", 193 | []string{"192.168.1.1:80"}, 194 | []int{1}, 195 | 0.3, 196 | false, 197 | LBMWRR, 198 | []string{"/"}, 199 | []string{"GET"}, 200 | "", 201 | "too many requests", 202 | } 203 | 204 | go configKeeper() 205 | 206 | configSync <- config 207 | } 208 | 209 | func TestConfigKeeperBadJSON(t *testing.T) { 210 | os.Remove(*configPath) 211 | defer os.Remove(*configPath) 212 | f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644) 213 | if err != nil { 214 | t.Errorf("failed to open config file: %s", err) 215 | } 216 | defer f.Close() 217 | content := []byte("hello world") 218 | f.Write(content) 219 | 220 | go configKeeper() 221 | } 222 | 223 | func TestConfigKeeperGoodJSON(t *testing.T) { 224 | os.Remove(*configPath) 225 | //defer os.Remove(*configPath) 226 | f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644) 227 | if err != nil { 228 | t.Errorf("failed to open config file: %s", err) 229 | } 230 | f.Truncate(0) 231 | f.Seek(0, 0) 232 | f.Write([]byte(goodConfig)) 233 | f.Close() 234 | 235 | go configKeeper() 236 | 237 | close(configSync) 238 | } 239 | 240 | func TestConfigIndex(t *testing.T) { 241 | fakeServer := httptest.NewServer( 242 | http.HandlerFunc(configIndexHandler), 243 | ) 244 | defer fakeServer.Close() 245 | url := fakeServer.URL + "/" 246 | 247 | // test 200 248 | f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644) 249 | if err != nil { 250 | t.Errorf("failed to open config file: %s", err) 251 | } 252 | f.Truncate(0) 253 | f.Seek(0, 0) 254 | f.Write([]byte(goodConfig)) 255 | f.Close() 256 | 257 | resp, err := http.Get(url) 258 | if err != nil { 259 | t.Errorf("failed to get config index page") 260 | } 261 | if resp.StatusCode != http.StatusOK { 262 | t.Errorf("should got 200, but: %d", resp.StatusCode) 263 | } 264 | } 265 | -------------------------------------------------------------------------------- /graceful.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "sync/atomic" 7 | "time" 8 | ) 9 | 10 | // see https://github.com/valyala/fasthttp/issues/66 11 | 12 | type GracefulListener struct { 13 | // inner listener 14 | ln net.Listener 15 | 16 | // maximum wait time for graceful shutdown 17 | maxWaitTime time.Duration 18 | 19 | // this channel is closed during graceful shutdown on zero open connections. 20 | done chan struct{} 21 | 22 | // the number of open connections 23 | connsCount uint64 24 | 25 | // becomes non-zero when graceful shutdown starts 26 | shutdown uint64 27 | } 28 | 29 | // NewGracefulListener wraps the given listener into 'graceful shutdown' listener. 30 | func newGracefulListener(ln net.Listener, maxWaitTime time.Duration) net.Listener { 31 | return &GracefulListener{ 32 | ln: ln, 33 | maxWaitTime: maxWaitTime, 34 | done: make(chan struct{}), 35 | } 36 | } 37 | 38 | func (ln *GracefulListener) Accept() (net.Conn, error) { 39 | c, err := ln.ln.Accept() 40 | 41 | if err != nil { 42 | return nil, err 43 | } 44 | 45 | atomic.AddUint64(&ln.connsCount, 1) 46 | 47 | return &gracefulConn{ 48 | Conn: c, 49 | ln: ln, 50 | }, nil 51 | } 52 | 53 | func (ln *GracefulListener) Addr() net.Addr { 54 | return ln.ln.Addr() 55 | } 56 | 57 | // Close closes the inner listener and waits until all the pending open connections 58 | // are closed before returning. 59 | func (ln *GracefulListener) Close() error { 60 | err := ln.ln.Close() 61 | 62 | if err != nil { 63 | return nil 64 | } 65 | 66 | return ln.waitForZeroConns() 67 | } 68 | 69 | func (ln *GracefulListener) waitForZeroConns() error { 70 | atomic.AddUint64(&ln.shutdown, 1) 71 | 72 | if atomic.LoadUint64(&ln.connsCount) == 0 { 73 | close(ln.done) 74 | return nil 75 | } 76 | 77 | select { 78 | case <-ln.done: 79 | return nil 80 | case <-time.After(ln.maxWaitTime): 81 | return fmt.Errorf("cannot complete graceful shutdown in %s", ln.maxWaitTime) 82 | } 83 | } 84 | 85 | func (ln *GracefulListener) closeConn() { 86 | connsCount := atomic.AddUint64(&ln.connsCount, ^uint64(0)) 87 | 88 | if atomic.LoadUint64(&ln.shutdown) != 0 && connsCount == 0 { 89 | close(ln.done) 90 | } 91 | } 92 | 93 | type gracefulConn struct { 94 | net.Conn 95 | ln *GracefulListener 96 | } 97 | 98 | func (c *gracefulConn) Close() error { 99 | err := c.Conn.Close() 100 | 101 | if err != nil { 102 | return err 103 | } 104 | 105 | c.ln.closeConn() 106 | 107 | return nil 108 | } 109 | -------------------------------------------------------------------------------- /graceful_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | "net/http" 6 | "testing" 7 | "time" 8 | ) 9 | 10 | func getGracefulListener(t *testing.T) net.Listener { 11 | ln, err := net.Listen("tcp", ":45678") 12 | if err != nil { 13 | t.Logf("error while listen at %s", err) 14 | } 15 | return newGracefulListener(ln, time.Second*10) 16 | } 17 | 18 | func TestGracefulListener(t *testing.T) { 19 | gln := getGracefulListener(t) 20 | gln.Close() 21 | 22 | gln.Close() 23 | } 24 | 25 | func TestGracefulListenerAddr(t *testing.T) { 26 | gln := getGracefulListener(t) 27 | defer gln.Close() 28 | 29 | gln.Addr() 30 | } 31 | 32 | func TestGracefulListen(t *testing.T) { 33 | gln := getGracefulListener(t) 34 | defer gln.Close() 35 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) 36 | go http.Serve(gln, handler) 37 | time.Sleep(time.Second) 38 | } 39 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | "net" 7 | "os" 8 | "os/signal" 9 | "time" 10 | 11 | "github.com/valyala/fasthttp" 12 | ) 13 | 14 | // guard is a high performance circuit breaker written in Go. 15 | 16 | var ( 17 | proxyAddr = flag.String("proxyAddr", ":23456", "proxy server listen at") 18 | configAddr = flag.String("configAddr", ":12345", "config server listen at") 19 | configPath = flag.String("configPath", "/tmp/guard.json", "configuration sync path") 20 | 21 | breaker = NewBreaker() 22 | ) 23 | 24 | func main() { 25 | flag.Parse() 26 | log.Printf("running with pid: %d\n", os.Getpid()) 27 | 28 | // config manager 29 | go configManager() 30 | 31 | // proxy listener 32 | ln, err := net.Listen("tcp", *proxyAddr) 33 | if err != nil { 34 | log.Fatalf("error while listen at %s: %s", *proxyAddr, err) 35 | } 36 | gln := newGracefulListener(ln, time.Second*10) 37 | 38 | // singal handler 39 | c := make(chan os.Signal, 1) 40 | signal.Notify(c, os.Interrupt) 41 | go func() { 42 | for { 43 | <-c 44 | log.Printf("graceful shutdown...") 45 | gln.Close() 46 | } 47 | }() 48 | 49 | // proxy server 50 | if err := fasthttp.Serve(gln, breaker.ServeHTTP); err != nil { 51 | log.Fatalf("error in fasthttp server: %s", err) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /nocopy.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // Embed this type into a struct, which mustn't be copied, 4 | // so `go vet` gives a warning if this struct is copied. 5 | // 6 | // See https://github.com/golang/go/issues/8005#issuecomment-190753527 for details. 7 | type noCopy struct{} 8 | 9 | func (*noCopy) Lock() {} 10 | -------------------------------------------------------------------------------- /proxy.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | proxy server 5 | */ 6 | 7 | import ( 8 | "log" 9 | 10 | "github.com/valyala/fasthttp" 11 | ) 12 | 13 | // Proxy use fasthttp: https://github.com/valyala/fasthttp/issues/64 14 | func Proxy(balancer Balancer, ctx *fasthttp.RequestCtx) int { 15 | backend, found := balancer.Select() 16 | if !found { 17 | ctx.SetStatusCode(fasthttp.StatusForbidden) 18 | return fasthttp.StatusForbidden 19 | } 20 | 21 | client := backend.client 22 | req := &ctx.Request 23 | resp := &ctx.Response 24 | 25 | // prepare 26 | req.Header.Del("Connection") 27 | 28 | // proxy 29 | if err := client.Do(req, resp); err != nil { 30 | log.Printf("failed to proxy: %s", err) 31 | return fasthttp.StatusBadGateway 32 | } 33 | 34 | // after 35 | resp.Header.Del("Connection") 36 | 37 | return resp.StatusCode() 38 | } 39 | -------------------------------------------------------------------------------- /proxy_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "net/url" 7 | "testing" 8 | 9 | "github.com/valyala/fasthttp" 10 | "github.com/valyala/fasthttp/fasthttputil" 11 | ) 12 | 13 | type fakeBalancer struct{} 14 | 15 | var fakeBackend = Backend{} 16 | 17 | func setFakeBackend(url string, weight int) { 18 | fakeBackend.Weight = weight 19 | fakeBackend.URL = url 20 | fakeBackend.client = &fasthttp.HostClient{Addr: url, MaxConns: fasthttp.DefaultMaxConnsPerHost} 21 | } 22 | 23 | func (b fakeBalancer) Select() (*Backend, bool) { 24 | if fakeBackend.Weight == 0 { 25 | return nil, false 26 | } 27 | return &fakeBackend, true 28 | } 29 | 30 | func fakeHandler(ctx *fasthttp.RequestCtx) { 31 | ctx.WriteString("hoho!") 32 | } 33 | 34 | func TestProxyBackendNotFound(t *testing.T) { 35 | ln := fasthttputil.NewInmemoryListener() 36 | defer ln.Close() 37 | 38 | go fasthttp.Serve(ln, fakeHandler) 39 | 40 | setFakeBackend(ln.Addr().String(), 0) 41 | 42 | fb := fakeBalancer{} 43 | 44 | ctx := &fasthttp.RequestCtx{} 45 | ctx.Request.SetRequestURI("/") 46 | Proxy(fb, ctx) 47 | 48 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusForbidden { 49 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusForbidden, code) 50 | } 51 | } 52 | 53 | func TestProxyWorks(t *testing.T) { 54 | fakeServer := httptest.NewServer( 55 | http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), 56 | ) 57 | defer fakeServer.Close() 58 | 59 | u, _ := url.ParseRequestURI(fakeServer.URL) 60 | 61 | setFakeBackend(u.Host, 1) 62 | 63 | fb := fakeBalancer{} 64 | 65 | ctx := &fasthttp.RequestCtx{} 66 | // RequestURI is used first if it's not empty: https://github.com/valyala/fasthttp/issues/114 67 | ctx.Request.SetRequestURI("http://" + u.Host + "/") 68 | Proxy(fb, ctx) 69 | 70 | if code := ctx.Response.StatusCode(); code != fasthttp.StatusOK { 71 | t.Errorf("response code should be %d but got: %d", fasthttp.StatusOK, code) 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /radix_tree.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "log" 6 | ) 7 | 8 | // radix tree, combine with timeline 9 | 10 | // +-----------+ 11 | // | root, / | here is radix tree 12 | // +-----------+ 13 | // / \ 14 | // +--------+ +----------+ 15 | // | ... | | user | 16 | // +--------+ +----------+ 17 | // | 18 | //----------------------------------------------------------------------------- 19 | // | below is ring of status 20 | // +---------+ 21 | // | status1 | 22 | // +---------+ 23 | // / \ 24 | // +---------+ +---------+ 25 | // | status2 | | status3 | 26 | // +---------+ +---------+ 27 | // \ / 28 | // +---------+ 29 | // | status4 | 30 | // +---------+ 31 | // 32 | // it's a radix tree, all the leaf has a ring of status. 33 | // each time there is a request, we try to find find it through the radix tree, 34 | // and then calculate the ratio, decide step down or not. 35 | 36 | // ring of Status is for counting http status code. 37 | 38 | type nodeType uint8 39 | 40 | const ( 41 | static nodeType = iota // default, static string 42 | root // root node 43 | param // like `:name` in `/user/:name/hello` 44 | catchAll // like `*filepath` in `/share/*filepath` 45 | ) 46 | 47 | // HTTPMethod is HTTP method 48 | type HTTPMethod uint16 49 | 50 | // HTTP Methods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods 51 | const ( 52 | NONE HTTPMethod = 0 // means no method had set 53 | GET HTTPMethod = 1 << iota 54 | POST 55 | PUT 56 | DELETE 57 | HEAD 58 | OPTIONS 59 | CONNECT 60 | TRACE 61 | PATCH 62 | ) 63 | 64 | // radix tree is "read only" after constructed. which means, it's not read only 65 | // but I assume it is... 66 | type node struct { 67 | noCopy noCopy 68 | path []byte // common prefix of childs 69 | nType nodeType // node type, static, root, param, or catchAll 70 | // supported HTTP methods, for decide raise a `405 Method Not Allowd` or not, 71 | // if a method is support, the correspoding bit is set 72 | methods HTTPMethod 73 | wildChild bool // child type is param, or catchAll 74 | indices []byte // first letter of childs, it's index for binary search. 75 | children []*node // childrens 76 | isLeaf bool // if it's a leaf 77 | status *Status // if it's a leaf, it should have a ring of `Status` struct 78 | } 79 | 80 | func min(a, b int) int { 81 | if a <= b { 82 | return a 83 | } 84 | 85 | return b 86 | } 87 | 88 | func (n *node) setMethods(methods ...HTTPMethod) { 89 | for _, method := range methods { 90 | // set corresponding bit 91 | n.methods |= method 92 | } 93 | } 94 | 95 | func (n *node) hasMethod(method HTTPMethod) bool { 96 | return method == (method & n.methods) 97 | } 98 | 99 | // addRoute adds a node with given path, handle all the resource with it. 100 | // if it's a leaf, it should have a ring of `Status`. 101 | func (n *node) addRoute(path []byte, methods ...HTTPMethod) { 102 | fullPath := path 103 | 104 | /* tree is empty */ 105 | if len(n.path) == 0 && len(n.children) == 0 { 106 | n.nType = root 107 | // reset these properties 108 | n.isLeaf = false 109 | n.methods = NONE 110 | n.status = nil 111 | 112 | // insert 113 | n.insertChild(path, fullPath, methods...) 114 | return 115 | } 116 | 117 | /* tree is not empty */ 118 | walk: 119 | for { 120 | maxLen := min(len(path), len(n.path)) 121 | 122 | // find max common prefix 123 | i := 0 124 | for i < maxLen && path[i] == n.path[i] { 125 | i++ 126 | } 127 | 128 | // if max common prefix is shorter than n.path, split n 129 | if i < len(n.path) { 130 | child := node{ 131 | path: n.path[i:], 132 | nType: static, 133 | methods: n.methods, 134 | wildChild: n.wildChild, 135 | indices: n.indices, 136 | children: n.children, 137 | isLeaf: n.isLeaf, 138 | status: n.status, 139 | } 140 | 141 | n.methods = NONE 142 | n.isLeaf = false 143 | n.status = nil 144 | n.children = []*node{&child} 145 | n.indices = []byte{n.path[i]} 146 | n.path = path[:i] 147 | n.wildChild = false 148 | } 149 | 150 | // path is shorter or equal than n.path, so quit 151 | if i == len(path) { 152 | n.setMethods(methods...) 153 | return 154 | } 155 | 156 | // path is longer than n.path, so insert it! 157 | path = path[i:] 158 | 159 | // only one wildChild is permit 160 | if n.wildChild { 161 | n = n.children[0] 162 | 163 | // check if wildcard matchs. 164 | // for example, if n.path is `:name`, path should be `:name` or `:name/` 165 | // only thses two cases are permit, panic if not 166 | lenNode := len(n.path) 167 | lenPath := len(path) 168 | if lenPath >= lenNode && bytes.Equal(n.path, path[:lenNode]) && 169 | // Check for longer wildcard, e.g. :name and :names 170 | (lenNode >= lenPath || path[lenNode] == '/') { 171 | continue walk 172 | } else { 173 | log.Panicf("%s in %s conflict with node %s", path, fullPath, n.path) 174 | 175 | } 176 | } 177 | 178 | c := path[0] 179 | 180 | // check if n is slash after param, e.g. path is `/jhon`, n.path is `:name`, and n.children is `/` 181 | if n.nType == param && c == '/' && len(n.children) == 1 { 182 | n = n.children[0] 183 | continue walk 184 | } 185 | 186 | // check if a child with next path bytes exists 187 | // TODO: use a binary search to search index. but for now, we just loop over it, because for the most cases 188 | // children will not be too much 189 | for i := 0; i < len(n.indices); i++ { 190 | if c == n.indices[i] { 191 | n = n.children[i] 192 | continue walk 193 | } 194 | } 195 | 196 | // insert it! 197 | if c != ':' && c != '*' { 198 | n.indices = append(n.indices, c) 199 | child := &node{} 200 | n.children = append(n.children, child) 201 | n = child 202 | } 203 | n.insertChild(path, fullPath, methods...) 204 | } 205 | } 206 | 207 | func (n *node) insertChild(path []byte, fullPath []byte, methods ...HTTPMethod) { 208 | var offset int // bytes in the path have already handled 209 | var numParams uint8 210 | var maxLen = len(path) 211 | 212 | for i := 0; i < len(path); i++ { 213 | if path[i] == ':' || path[i] == '*' { 214 | numParams++ 215 | } 216 | } 217 | 218 | var i = 0 219 | var c byte 220 | for ; numParams > 0; numParams-- { 221 | // first step, find the first wildcard(beginning with ':' or '*') of the current path 222 | for i = offset; i < len(path); i++ { 223 | c = path[i] 224 | if c == ':' || c == '*' { 225 | break 226 | } 227 | } 228 | 229 | // second step, find wildcard name, wildcard name cannot contain ':' and '*' 230 | // stops when meet '/' or the end 231 | end := i + 1 232 | for end < maxLen && path[end] != '/' { 233 | switch path[end] { 234 | case ':', '*': 235 | log.Panicf("wildcards ':' or '*' are not allowed in param names: %s in %s", path, fullPath) 236 | default: 237 | end++ 238 | } 239 | } 240 | 241 | // node whose type is param or catchAll are conflict, check it 242 | if len(n.children) > 0 { 243 | log.Panicf("wildcard route %s conflict with existing children in path %s", path[i:end], fullPath) 244 | } 245 | 246 | // check if the wildcard has a name 247 | if end-i < 2 { 248 | log.Panicf("wildcards must be named with a non-empty name in path %s", fullPath) 249 | } 250 | 251 | if c == ':' { // param 252 | // split path at the beginning of the wildcard 253 | if i > 0 { 254 | n.path = path[offset:i] 255 | offset = i 256 | } 257 | 258 | child := &node{nType: param} 259 | n.children = []*node{child} 260 | n.wildChild = true 261 | n = child 262 | 263 | // if the path doesn't end with the wildcard, then there will be another non-wildcard subpath 264 | // starting with '/' 265 | if end < maxLen { 266 | n.path = path[offset:end] 267 | offset = end 268 | 269 | child := &node{} 270 | n.children = []*node{child} 271 | n = child 272 | } 273 | } else { //catchAll 274 | if end != maxLen || numParams > 1 { 275 | log.Panicf("catchAll routers are only allowed once at the end of the path: %s", fullPath) 276 | } 277 | 278 | if path[i-1] != '/' { 279 | log.Panicf("no / before catchAll in path %s", fullPath) 280 | } 281 | 282 | // this node holding path 'xxx/' 283 | n.path = path[offset:i] 284 | n.wildChild = true 285 | 286 | // child node holding the variable, '*xxxx' 287 | child := &node{path: path[i:], nType: catchAll, isLeaf: true, status: StatusRing()} 288 | child.setMethods(methods...) 289 | n.children = []*node{child} 290 | 291 | // all done 292 | return 293 | } 294 | } 295 | 296 | // insert the remaining part of path 297 | n.path = path[offset:] 298 | n.setMethods(methods...) 299 | n.isLeaf = true 300 | n.status = StatusRing() 301 | } 302 | 303 | // byPath return a node with the given path 304 | func (n *node) byPath(path []byte) (nd *node, tsr bool, found bool) { 305 | walk: 306 | for { 307 | if len(path) > len(n.path) { 308 | if bytes.Equal(path[:len(n.path)], n.path) { 309 | path = path[len(n.path):] 310 | // if this node does not have a wildcard(param or catchAll) child, we can just look up 311 | // the next child node and continue to walk down the tree 312 | if !n.wildChild { 313 | c := path[0] 314 | 315 | for i := 0; i < len(n.indices); i++ { 316 | if c == n.indices[i] { 317 | n = n.children[i] 318 | continue walk 319 | } 320 | } 321 | 322 | // nothing found 323 | // we can recommend to redirect to the same URL without a trailing slash if a leaf 324 | // exists for that path 325 | tsr = (bytes.Equal(path, []byte("/")) && n.isLeaf) 326 | return nil, tsr, false 327 | } 328 | 329 | // handle wildcard child 330 | n = n.children[0] 331 | switch n.nType { 332 | case param: 333 | end := 0 334 | for end < len(path) && path[end] != '/' { 335 | end++ 336 | } 337 | 338 | // we need to go deeper, because we've not visit all bytes in path 339 | if end < len(path) { 340 | if len(n.children) > 0 { 341 | path = path[end:] 342 | n = n.children[0] 343 | continue walk 344 | } 345 | 346 | // oh, no, we can't go deeper 347 | // if URL is `/user/:name/`, redirect it to `/user/:name` 348 | tsr = (len(path) == end+1 && path[len(path)-1] == '/') 349 | return nil, tsr, false 350 | } 351 | 352 | // else, n is the node we want if it's a leaf 353 | if n.isLeaf { 354 | return n, false, true 355 | } 356 | 357 | tsr = len(n.children) == 1 && n.children[0].isLeaf && bytes.Equal(n.children[0].path, []byte("/")) 358 | return nil, tsr, false 359 | case catchAll: 360 | return n, false, true 361 | default: 362 | log.Panicf("invalid node type: %+v", n) 363 | } 364 | } 365 | } else if bytes.Equal(path, n.path) { 366 | if n.isLeaf { 367 | return n, false, true 368 | } 369 | 370 | // it seems that the case in below(comment) will never hapeen... 371 | //if path == "/" && n.wildChild && n.nType != root { 372 | //return nil, true, false 373 | //} 374 | 375 | // nothing found, check if a child with this path + a trailing slash exists 376 | for i := 0; i < len(n.indices); i++ { 377 | if n.indices[i] == '/' { 378 | n = n.children[i] 379 | tsr = len(n.path) == 1 && n.isLeaf 380 | } 381 | 382 | return nil, tsr, false 383 | } 384 | } 385 | 386 | // nothing found, e.g. URL is `/user/jhon/card/`, but request `/user/jhon/card` 387 | tsr = (bytes.Equal(path, []byte("/"))) || 388 | (len(n.path) == len(path)+1 && n.path[len(path)] == '/' && 389 | bytes.Equal(path, n.path[:len(n.path)-1]) && n.isLeaf) 390 | return nil, tsr, false 391 | } 392 | } 393 | -------------------------------------------------------------------------------- /radix_tree_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | type nodeExpceted struct { 8 | path string 9 | nType nodeType 10 | methods HTTPMethod 11 | wildChild bool 12 | indicesIsEmpty bool 13 | childrenNum int 14 | isLeaf bool 15 | statusIsNil bool 16 | } 17 | 18 | func checkNodeValid(t *testing.T, n *node, e nodeExpceted) { 19 | if n == nil { 20 | t.Fatalf("checkNodeValid: both node and expect should not be nil") 21 | } 22 | 23 | if string(n.path) != e.path { 24 | t.Errorf("n.path should be %s, but n is: %+v", e.path, n) 25 | } 26 | 27 | if n.nType != e.nType { 28 | t.Errorf("n.nType should be %d, but n is: %+v", e.nType, n) 29 | } 30 | 31 | if n.methods != e.methods { 32 | t.Errorf("n.methods should be %x, but n is: %+v", e.methods, n) 33 | } 34 | 35 | if n.wildChild != e.wildChild { 36 | t.Errorf("n.wildChild should be %t, but n is: %+v", e.wildChild, n) 37 | } 38 | 39 | if !(len(n.indices) > 0 && !e.indicesIsEmpty || len(n.indices) == 0 && e.indicesIsEmpty) { 40 | t.Errorf("n.indices should be empty? %t, but n is: %+v", e.indicesIsEmpty, n) 41 | } 42 | 43 | if len(n.children) != e.childrenNum { 44 | t.Errorf("n should have %d childrens, but n is: %+v", e.childrenNum, n) 45 | } 46 | 47 | if n.isLeaf != e.isLeaf { 48 | t.Errorf("n.leaf should be %t, but n is: %+v", e.isLeaf, n) 49 | } 50 | 51 | if !(n.status == nil && e.statusIsNil || n.status != nil && !e.statusIsNil) { 52 | 53 | t.Errorf("n.status should be nil? %t, but n is: %+v", e.statusIsNil, n) 54 | } 55 | } 56 | 57 | func TestMin(t *testing.T) { 58 | if 1 != min(1, 2) { 59 | t.Error("min(1, 2) should return 1") 60 | } 61 | 62 | if 1 != min(2, 1) { 63 | t.Error("min(2, 1) should return 1") 64 | } 65 | } 66 | 67 | func TestSetMethods(t *testing.T) { 68 | n := &node{} 69 | 70 | if n.methods != NONE { 71 | t.Errorf("n.methods should be NONE, but got: %x", n.methods) 72 | } 73 | 74 | methods := []HTTPMethod{GET, POST, PUT, DELETE, HEAD, OPTIONS, CONNECT, TRACE, PATCH} 75 | for _, m := range methods { 76 | n.setMethods(m) 77 | 78 | if !n.hasMethod(m) { 79 | t.Errorf("n should have HTTP method %x, but got: %x", m, n.methods) 80 | } 81 | } 82 | } 83 | 84 | func TestInsertLeaf(t *testing.T) { 85 | n := &node{} 86 | 87 | n.insertChild([]byte("this"), []byte("/use/this")) 88 | 89 | checkNodeValid( 90 | t, n, 91 | nodeExpceted{"this", static, NONE, false, true, 0, true, false}, 92 | ) 93 | 94 | n.insertChild([]byte("this"), []byte("/use/this"), GET) 95 | 96 | if !n.hasMethod(GET) { 97 | t.Error("n should have HTTP method `GET` been set, but not") 98 | } 99 | } 100 | 101 | func TestInsertChild(t *testing.T) { 102 | n := &node{} 103 | 104 | n.insertChild([]byte("/:name"), []byte("/user/:name"), GET) 105 | 106 | checkNodeValid( 107 | t, n, 108 | nodeExpceted{"/", static, NONE, true, true, 1, false, true}, 109 | ) 110 | 111 | // check it's child, then 112 | n = n.children[0] 113 | 114 | checkNodeValid( 115 | t, n, 116 | nodeExpceted{":name", param, GET, false, true, 0, true, false}, 117 | ) 118 | } 119 | 120 | func shouldPanic() { 121 | if err := recover(); err == nil { 122 | panic("should panic but not") 123 | } 124 | } 125 | 126 | func TestInsertBadParamDualWildchard(t *testing.T) { 127 | defer shouldPanic() 128 | 129 | n := &node{} 130 | 131 | n.insertChild([]byte("/:name:this"), []byte("/user/:name:this/there")) 132 | } 133 | 134 | func TestInsertBadParamNoParamName(t *testing.T) { 135 | defer shouldPanic() 136 | 137 | n := &node{} 138 | 139 | n.insertChild([]byte("/:"), []byte("/user/:/there")) 140 | } 141 | 142 | func TestInsertBadParamConflict(t *testing.T) { 143 | defer shouldPanic() 144 | 145 | n := &node{} 146 | 147 | n.insertChild([]byte("/:name"), []byte("/user/:name/there")) 148 | n.insertChild([]byte("/:name"), []byte("/user/:name/there")) 149 | } 150 | 151 | func TestInsertDualParam(t *testing.T) { 152 | n := &node{} 153 | 154 | n.insertChild([]byte("/:name/:card"), []byte("/user/:name/:card"), POST) 155 | 156 | slash1 := n 157 | name := n.children[0] 158 | slash2 := name.children[0] 159 | card := slash2.children[0] 160 | 161 | checkNodeValid( 162 | t, slash1, 163 | nodeExpceted{"/", static, NONE, true, true, 1, false, true}, 164 | ) 165 | checkNodeValid( 166 | t, name, 167 | nodeExpceted{":name", param, NONE, false, true, 1, false, true}, 168 | ) 169 | checkNodeValid( 170 | t, slash2, 171 | nodeExpceted{"/", static, NONE, true, true, 1, false, true}, 172 | ) 173 | checkNodeValid( 174 | t, card, 175 | nodeExpceted{":card", param, POST, false, true, 0, true, false}, 176 | ) 177 | } 178 | 179 | func TestInsertChildCatchAll(t *testing.T) { 180 | n := &node{} 181 | 182 | n.insertChild([]byte("/user/*name"), []byte("/user/*name"), POST) 183 | 184 | // first, check n itself 185 | checkNodeValid( 186 | t, n, 187 | nodeExpceted{"/user/", static, NONE, true, true, 1, false, true}, 188 | ) 189 | 190 | // last, the child 191 | n = n.children[0] 192 | checkNodeValid( 193 | t, n, 194 | nodeExpceted{"*name", catchAll, POST, false, true, 0, true, false}, 195 | ) 196 | } 197 | 198 | func TestInsertCatchAllMultiTimes(t *testing.T) { 199 | defer shouldPanic() 200 | 201 | n := &node{} 202 | n.insertChild([]byte("/*name/:haha"), []byte("/*name/:haha")) 203 | } 204 | 205 | func TestInsertCatchAllNoSlash(t *testing.T) { 206 | defer shouldPanic() 207 | 208 | n := &node{} 209 | n.insertChild([]byte("/user*name"), []byte("/user*name")) 210 | } 211 | 212 | func TestAddRoute(t *testing.T) { 213 | n := &node{} 214 | 215 | n.addRoute([]byte("/user/hello"), GET, POST) 216 | checkNodeValid( 217 | t, n, 218 | nodeExpceted{"/user/hello", root, GET | POST, false, true, 0, true, false}, 219 | ) 220 | 221 | n.addRoute([]byte("/user/world"), DELETE) 222 | checkNodeValid( 223 | t, n, 224 | nodeExpceted{"/user/", root, NONE, false, false, 2, false, true}, 225 | ) 226 | 227 | hello := n.children[0] 228 | world := n.children[1] 229 | if string(hello.path) == "world" { 230 | hello, world = world, hello 231 | } 232 | 233 | checkNodeValid( 234 | t, hello, 235 | nodeExpceted{"hello", static, GET | POST, false, true, 0, true, false}, 236 | ) 237 | checkNodeValid( 238 | t, world, 239 | nodeExpceted{"world", static, DELETE, false, true, 0, true, false}, 240 | ) 241 | } 242 | 243 | func TestAddRouteWildChild(t *testing.T) { 244 | n := &node{} 245 | 246 | n.addRoute([]byte("/user/:name/hello"), GET) 247 | checkNodeValid( 248 | t, n, 249 | nodeExpceted{"/user/", root, NONE, true, true, 1, false, true}, 250 | ) 251 | 252 | name := n.children[0] 253 | checkNodeValid( 254 | t, name, 255 | nodeExpceted{":name", param, NONE, false, true, 1, false, true}, 256 | ) 257 | 258 | hello := name.children[0] 259 | checkNodeValid( 260 | t, hello, 261 | nodeExpceted{"/hello", static, GET, false, true, 0, true, false}, 262 | ) 263 | } 264 | 265 | func TestAddRouteDualWildChild(t *testing.T) { 266 | n := &node{} 267 | 268 | n.addRoute([]byte("/user/:name/hello"), GET) 269 | checkNodeValid( 270 | t, n, 271 | nodeExpceted{"/user/", root, NONE, true, true, 1, false, true}, 272 | ) 273 | 274 | n.addRoute([]byte("/user/:name/hello/:card"), GET) 275 | checkNodeValid( 276 | t, n, 277 | nodeExpceted{"/user/", root, NONE, true, true, 1, false, true}, 278 | ) 279 | 280 | name := n.children[0] 281 | checkNodeValid( 282 | t, name, 283 | nodeExpceted{":name", param, NONE, false, true, 1, false, true}, 284 | ) 285 | 286 | slashHello := name.children[0] 287 | checkNodeValid( 288 | t, slashHello, 289 | nodeExpceted{"/hello", static, GET, false, false, 1, true, false}, 290 | ) 291 | 292 | slash := slashHello.children[0] 293 | checkNodeValid( 294 | t, slash, 295 | nodeExpceted{"/", static, NONE, true, true, 1, false, true}, 296 | ) 297 | 298 | card := slash.children[0] 299 | checkNodeValid( 300 | t, card, 301 | nodeExpceted{":card", param, GET, false, true, 0, true, false}, 302 | ) 303 | } 304 | 305 | func TestAddRouteWildParamConflict(t *testing.T) { 306 | defer shouldPanic() 307 | 308 | n := &node{} 309 | n.addRoute([]byte("/user/:name/hello/world")) 310 | n.addRoute([]byte("/user/*whoever")) 311 | } 312 | 313 | func TestAddRouteMultiIndices(t *testing.T) { 314 | n := &node{} 315 | n.addRoute([]byte("/user/:name/hello/world")) 316 | n.addRoute([]byte("/use/this")) 317 | n.addRoute([]byte("/usea/this")) 318 | n.addRoute([]byte("/useb/that")) 319 | n.addRoute([]byte("/usea/that")) 320 | } 321 | 322 | func TestAddRouteSamePath(t *testing.T) { 323 | n := &node{} 324 | 325 | n.addRoute([]byte("/user/hello"), GET, POST) 326 | checkNodeValid( 327 | t, n, 328 | nodeExpceted{"/user/hello", root, GET | POST, false, true, 0, true, false}, 329 | ) 330 | 331 | n.addRoute([]byte("/user/hello"), DELETE) 332 | checkNodeValid( 333 | t, n, 334 | nodeExpceted{"/user/hello", root, GET | POST | DELETE, false, true, 0, true, false}, 335 | ) 336 | } 337 | 338 | type byPathExpected struct { 339 | node *node 340 | tsr bool 341 | found bool 342 | } 343 | 344 | func checkByPath(t *testing.T, n *node, tsr bool, found bool, e byPathExpected) { 345 | if n != e.node { 346 | t.Errorf("node should be %+v, but got: %+v", e.node, n) 347 | } 348 | 349 | if tsr != e.tsr { 350 | t.Errorf("tsr should be %t, but got: %t", e.tsr, tsr) 351 | } 352 | 353 | if found != e.found { 354 | t.Errorf("found should be %t, but got: %t", e.found, found) 355 | } 356 | } 357 | 358 | func TestByPath(t *testing.T) { 359 | n := &node{} 360 | n.addRoute([]byte("/user"), GET, DELETE) 361 | 362 | checkNodeValid( 363 | t, n, 364 | nodeExpceted{"/user", root, GET | DELETE, false, true, 0, true, false}, 365 | ) 366 | 367 | nd, tsr, found := n.byPath([]byte([]byte("/user"))) 368 | checkByPath(t, nd, tsr, found, byPathExpected{n, false, true}) 369 | 370 | nd, tsr, found = n.byPath([]byte([]byte("/user/"))) 371 | checkByPath(t, nd, tsr, found, byPathExpected{nil, true, false}) 372 | 373 | nd, tsr, found = n.byPath([]byte("/what???")) 374 | checkByPath(t, nd, tsr, found, byPathExpected{nil, false, false}) 375 | 376 | n = &node{} 377 | n.addRoute([]byte("/user/"), GET, DELETE) 378 | n.addRoute([]byte("/usera"), GET, DELETE) 379 | 380 | checkNodeValid( 381 | t, n, 382 | nodeExpceted{"/user", root, NONE, false, false, 2, false, true}, 383 | ) 384 | nd, tsr, found = n.byPath([]byte("/user")) 385 | checkByPath(t, nd, tsr, found, byPathExpected{nil, true, false}) 386 | } 387 | 388 | func TestByPathWithWildchild(t *testing.T) { 389 | n := &node{} 390 | n.addRoute([]byte("/user/:name/hello"), GET, DELETE) 391 | n.addRoute([]byte("/use/:this/that"), GET, DELETE) 392 | 393 | checkNodeValid( 394 | t, n, 395 | nodeExpceted{"/use", root, NONE, false, false, 2, false, true}, 396 | ) 397 | 398 | nd, tsr, found := n.byPath([]byte("/user/jhon")) 399 | checkByPath(t, nd, tsr, found, byPathExpected{nil, false, false}) 400 | 401 | nd, tsr, found = n.byPath([]byte("/user/jhon/hello/")) 402 | checkByPath(t, nd, tsr, found, byPathExpected{nil, true, false}) 403 | } 404 | 405 | func TestByPathParamAndCatchAll(t *testing.T) { 406 | n := &node{} 407 | n.addRoute([]byte("/user/:name"), GET, DELETE) 408 | 409 | nd, tsr, found := n.byPath([]byte("/user/jhon")) 410 | checkByPath(t, nd, tsr, found, byPathExpected{n.children[0], false, true}) 411 | nd, tsr, found = n.byPath([]byte("/user/jhon/")) 412 | checkByPath(t, nd, tsr, found, byPathExpected{nil, true, false}) 413 | 414 | n = &node{} 415 | n.addRoute([]byte("/user/*name"), GET, DELETE) 416 | 417 | nd, tsr, found = n.byPath([]byte("/user/jhon")) 418 | checkByPath(t, nd, tsr, found, byPathExpected{n.children[0], false, true}) 419 | } 420 | 421 | func TestByPathBadNode(t *testing.T) { 422 | defer shouldPanic() 423 | 424 | n := &node{} 425 | n.addRoute([]byte("/user/:name"), GET, DELETE) 426 | child := n.children[0] 427 | child.nType = static 428 | 429 | n.byPath([]byte("/user/jhon")) 430 | } 431 | 432 | // benchmark 433 | func BenchmarkByPath(b *testing.B) { 434 | n := &node{} 435 | n.addRoute([]byte("/user/hello/world/this/is/so/long")) 436 | 437 | for i := 0; i < b.N; i++ { 438 | n.byPath([]byte("/user/hello/world/this/is/so/long")) 439 | } 440 | } 441 | 442 | func BenchmarkByPathNotFound(b *testing.B) { 443 | n := &node{} 444 | n.addRoute([]byte("/user/hello/world/this/is/so/long")) 445 | 446 | for i := 0; i < b.N; i++ { 447 | n.byPath([]byte("/user/")) 448 | } 449 | } 450 | 451 | func BenchmarkByPathParam(b *testing.B) { 452 | n := &node{} 453 | n.addRoute([]byte("/user/hello/:world/this/is/so/long")) 454 | 455 | for i := 0; i < b.N; i++ { 456 | n.byPath([]byte("/user/hello/world/this/is/so/long")) 457 | } 458 | } 459 | 460 | func BenchmarkByPathCatchall(b *testing.B) { 461 | n := &node{} 462 | n.addRoute([]byte("/user/hello/*world")) 463 | 464 | for i := 0; i < b.N; i++ { 465 | n.byPath([]byte("/user/hello/world/this/is/so/long")) 466 | } 467 | } 468 | -------------------------------------------------------------------------------- /timeline.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | "sync/atomic" 7 | "unsafe" 8 | ) 9 | 10 | const ( 11 | statusStep int64 = 10 12 | maxStatusLen = 12 13 | ) 14 | 15 | // RightNow return status key 16 | func RightNow() int64 { 17 | t := CoarseTimeNow().Unix() 18 | return t - t%statusStep 19 | } 20 | 21 | // Status is for counting http status code 22 | // uint32 can be at most 4294967296, it's enough for proxy server, because this 23 | // means in the past second, you've received 4294967296 requests, 429496729/second. 24 | type Status struct { 25 | prev *Status 26 | next *Status 27 | key int64 // for now, key is time 28 | OK uint32 29 | TooManyRequests uint32 30 | InternalError uint32 31 | BadGateway uint32 32 | } 33 | 34 | // StatusRing return a ring of status 35 | func StatusRing() *Status { 36 | head := &Status{} 37 | cursor := head 38 | 39 | for i := 0; i < maxStatusLen-1; i++ { 40 | cursor.next = &Status{} 41 | cursor.next.prev = cursor 42 | cursor = cursor.next 43 | } 44 | 45 | head.prev = cursor 46 | cursor.next = head 47 | 48 | return head 49 | } 50 | 51 | // refreshStatus refresh the current status if it's outdate, and return the latest one 52 | func (n *node) refreshStatus(now int64) *Status { 53 | status := n.status 54 | if status.key != now { 55 | if atomic.CompareAndSwapPointer( 56 | // first, get address of n.status, means, address of `status field in n`, get it's address, 57 | // cast it to `*unsafe.Pointer` 58 | (*unsafe.Pointer)(unsafe.Pointer(&(n.status))), unsafe.Pointer(status), unsafe.Pointer(status.next), 59 | ) { 60 | // clean old data, though it may cause some dirty reads 61 | atomic.StoreInt64(&n.status.key, now) 62 | atomic.StoreUint32(&n.status.OK, 0) 63 | atomic.StoreUint32(&n.status.TooManyRequests, 0) 64 | atomic.StoreUint32(&n.status.InternalError, 0) 65 | atomic.StoreUint32(&n.status.BadGateway, 0) 66 | } 67 | } 68 | 69 | return n.status 70 | } 71 | 72 | // incr increase by 1 on the given genericURL and status code, return value after incr 73 | func (n *node) incr(code int) uint32 { 74 | if n.status == nil { 75 | log.Panicf("status of node %+v is nil", n) 76 | } 77 | 78 | status := n.refreshStatus(RightNow()) 79 | switch code { 80 | case http.StatusOK: 81 | return atomic.AddUint32(&status.OK, 1) 82 | case http.StatusTooManyRequests: 83 | return atomic.AddUint32(&status.TooManyRequests, 1) 84 | case http.StatusInternalServerError: 85 | return atomic.AddUint32(&status.InternalError, 1) 86 | case http.StatusBadGateway: 87 | return atomic.AddUint32(&status.BadGateway, 1) 88 | default: 89 | log.Printf("ignore status code %d", code) 90 | return 0 // just for go lint, code here should never been execute 91 | } 92 | } 93 | 94 | func (n *node) query() (uint32, uint32, uint32, uint32, float64) { 95 | if n.status == nil { 96 | log.Panicf("status of node %+v is nil", n) 97 | } 98 | 99 | status := n.refreshStatus(RightNow()) 100 | ok, too, internal, bad := status.OK, status.TooManyRequests, status.InternalError, status.BadGateway 101 | 102 | ratio := float64( 103 | too+internal+bad, 104 | ) / float64( 105 | 1+ok+too+internal+bad, 106 | ) 107 | 108 | return ok, too, internal, bad, ratio 109 | } 110 | -------------------------------------------------------------------------------- /timeline_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "testing" 6 | ) 7 | 8 | func TestStatusRing(t *testing.T) { 9 | ring := StatusRing() 10 | cursor := ring 11 | 12 | for i := 0; i < maxStatusLen; i++ { 13 | cursor = cursor.next 14 | } 15 | 16 | if cursor != ring { 17 | t.Errorf("after loop, cursor should equal to ring(%+v), but got: %+v", ring, cursor) 18 | } 19 | } 20 | 21 | func TestIncr(t *testing.T) { 22 | n := &node{} 23 | n.addRoute([]byte("/user/hello")) 24 | 25 | n.incr(http.StatusOK) 26 | n.incr(http.StatusTooManyRequests) 27 | n.incr(http.StatusInternalServerError) 28 | n.incr(http.StatusBadGateway) 29 | } 30 | 31 | func TestBadIncrStatusIsNil(t *testing.T) { 32 | defer shouldPanic() 33 | 34 | n := &node{} 35 | n.incr(http.StatusOK) 36 | } 37 | 38 | func TestQuery(t *testing.T) { 39 | n := &node{} 40 | n.addRoute([]byte("/user")) 41 | 42 | ok, too, internal, bad, _ := n.query() 43 | 44 | if ok != 0 { 45 | t.Errorf("ok should be 0, but it is %d", ok) 46 | } 47 | if too != 0 { 48 | t.Errorf("too should be 0, but it is %d", too) 49 | } 50 | if internal != 0 { 51 | t.Errorf("internal should be 0, but it is %d", internal) 52 | } 53 | if bad != 0 { 54 | t.Errorf("bad should be 0, but it is %d", bad) 55 | } 56 | 57 | for i := 0; i < 100; i++ { 58 | n.incr(http.StatusInternalServerError) 59 | n.incr(http.StatusBadGateway) 60 | n.incr(http.StatusTooManyRequests) 61 | } 62 | 63 | ok, too, internal, bad, ratio := n.query() 64 | 65 | if ok != 0 { 66 | t.Errorf("ok should be 0, but it is %d", ok) 67 | } 68 | if too != 100 { 69 | t.Errorf("too should be 100, but it is %d", too) 70 | } 71 | if internal != 100 { 72 | t.Errorf("internal should be 100, but it is %d", internal) 73 | } 74 | if bad != 100 { 75 | t.Errorf("bad should be 100, but it is %d", bad) 76 | } 77 | 78 | if ratio < 0.75 { 79 | t.Errorf("ratio should at least 0.75, but it is %f", ratio) 80 | } 81 | } 82 | 83 | func TestQueryNilStatus(t *testing.T) { 84 | defer shouldPanic() 85 | 86 | n := &node{} 87 | 88 | n.query() 89 | } 90 | 91 | func TestRefreshStatus(t *testing.T) { 92 | n := &node{} 93 | n.addRoute([]byte("/user/hello"), GET) 94 | 95 | status := n.status 96 | now := RightNow() 97 | n.status.key = now - 3*statusStep 98 | 99 | n.refreshStatus(now) 100 | if n.status == status { 101 | t.Errorf("n.status should refresh to %d, but it's %+v", now, n.status) 102 | } 103 | 104 | status = n.status 105 | if status.key != now { 106 | t.Errorf("brand new status's key should be %d, but status is: %+v, status.prev is: %+v, status.next is: %+v", now, status, status.prev, status.next) 107 | } 108 | if status.OK != 0 || status.TooManyRequests != 0 || status.InternalError != 0 || status.BadGateway != 0 { 109 | t.Errorf("brand new status's property should be reset, but it not: %+v", status) 110 | } 111 | } 112 | 113 | func TestRefreshStatusShouldNotRefresh(t *testing.T) { 114 | n := &node{} 115 | n.addRoute([]byte("/user/hello"), GET) 116 | 117 | now := RightNow() 118 | status := n.status 119 | status.key = now 120 | n.refreshStatus(now) 121 | if n.status != status { 122 | t.Errorf("n.status should not be refreshed, should be %p, but n is: %+v", status, n) 123 | } 124 | } 125 | 126 | // benchmark 127 | func BenchmarkIncr(b *testing.B) { 128 | n := &node{} 129 | n.addRoute([]byte("/user/hello")) 130 | 131 | for i := 0; i < b.N; i++ { 132 | n.incr(http.StatusBadGateway) 133 | } 134 | } 135 | 136 | func BenchmarkQuery(b *testing.B) { 137 | n := &node{} 138 | n.addRoute([]byte("/user/hello")) 139 | 140 | for i := 0; i < 100; i++ { 141 | n.incr(http.StatusBadGateway) 142 | } 143 | 144 | for i := 0; i < b.N; i++ { 145 | n.query() 146 | } 147 | } 148 | 149 | func BenchmarkRightNow(b *testing.B) { 150 | for i := 0; i < b.N; i++ { 151 | RightNow() 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /workflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiajunhuang/guard/2b4e189d57a967e8cf5ce421d9580113c34fa9c7/workflow.png --------------------------------------------------------------------------------