This commit is contained in:
Pavel 2023-01-13 13:28:05 +03:00
commit d1db595637
12 changed files with 711 additions and 0 deletions

25
.gitignore vendored Normal file
View file

@ -0,0 +1,25 @@
# ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
main
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
vendor/
# Go workspace file
go.work
.idea
.vscode

9
LICENSE Normal file
View file

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023 Neur0toxine
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

14
README.md Normal file
View file

@ -0,0 +1,14 @@
# vk webhook flooder mock
Test app with routes for:
- MG message receiving
- VK users.get method
- custom api that floods vk transport with requests
a lot of stuff is hardcoded, even more is coded poorly because I didn't have enough time.
read code to understand what it does. there is not a lot of it.
fetch deps with `go mod tidy && go mod vendor`
build with `go build -o main .`
enjoy! ~

21
dto/dto.go Normal file
View file

@ -0,0 +1,21 @@
package dto
import "time"
// MessagesResponse message event response.
type MessagesResponse struct {
MessageID int `json:"message_id,omitempty"`
Time time.Time `json:"time,omitempty"`
}
type VKRequest struct {
AccessToken string `form:"access_token,omitempty"`
V string `form:"v,omitempty"`
}
type UsersGetRequest struct {
VKRequest
Lang int `form:"lang"`
UserIDs []string `form:"user_ids"`
Fields string `form:"fields"`
}

44
go.mod Normal file
View file

@ -0,0 +1,44 @@
module vk-webhook-flooder-mock
go 1.19
require (
github.com/JGLTechnologies/gin-rate-limit v1.5.2
github.com/SevereCloud/vksdk/v2 v2.15.0
github.com/allegro/bigcache/v3 v3.1.0
github.com/gin-gonic/gin v1.8.2
github.com/retailcrm/mg-transport-api-client-go v1.2.4
go.uber.org/atomic v1.10.0
)
require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.8 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

137
go.sum Normal file
View file

@ -0,0 +1,137 @@
github.com/JGLTechnologies/gin-rate-limit v1.5.2 h1:zPMvOBhiIE6rozLFnLHes5EhCzkqCwILhTANuSfYKiw=
github.com/JGLTechnologies/gin-rate-limit v1.5.2/go.mod h1:XGo66/RNHkTgCPdaaFyFtTVnm2G45FDrvT5OFjCTnQ0=
github.com/SevereCloud/vksdk/v2 v2.15.0 h1:ywyJvuJzN1sD5+GVcYendwNTpK3R/iBZOlOhulyI9ZQ=
github.com/SevereCloud/vksdk/v2 v2.15.0/go.mod h1:0Q20DuofWA78Vdy6aPjZAM6ep1UR6uVEf/fCqdmBYaY=
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.2 h1:UzKToD9/PoFj/V4rvlKqTRKnQYyz8Sc1MJlv4JHPtvY=
github.com/gin-gonic/gin v1.8.2/go.mod h1:qw5AYuDrzRTnhvusDsrov+fDIxp9Dleuu12h8nfB398=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.15.8 h1:JahtItbkWjf2jzm/T+qgMxkP9EMHsqEUA6vCMGmXvhA=
github.com/klauspost/compress v1.15.8/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/retailcrm/mg-transport-api-client-go v1.2.4 h1:onDJI/StNyZjW1rsRBsMrKumKU4HXhF7lJMsG1t8QZw=
github.com/retailcrm/mg-transport-api-client-go v1.2.4/go.mod h1:gDe/tj7t3Hr/uwIFSBVgGAmP85PoLajVl1A+skBo1Ro=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

116
handler/app.go Normal file
View file

@ -0,0 +1,116 @@
package handler
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
)
var (
vkToken = os.Getenv("VK_TOKEN")
bodyTemplate = `{"group_id":183208397,"type":"message_new","event_id":"7dff3c1982dfbd9e6011948ab9b5da352d903c94","v":"5.131","object":{"message":{"date":%d,"from_id":%d,"id":%d,"out":0,"attachments":[],"conversation_message_id":%d,"fwd_messages":[],"important":false,"is_hidden":false,"peer_id":199640727,"random_id":0,"text":"Test message #%d"},"client_info":{"button_actions":["text","vkpay","open_app","location","open_link","callback","intent_subscribe","intent_unsubscribe"],"keyboard":true,"inline_keyboard":true,"carousel":true,"lang_id":0}}}`
)
func init() {
}
func FloodHandler(c *gin.Context) {
countStr := c.Query("count")
if countStr == "" {
c.String(http.StatusBadRequest, "Please specify the amount of requests in count query parameter")
return
}
count, err := strconv.Atoi(countStr)
if err != nil || count <= 0 {
c.String(http.StatusBadRequest, "Invalid count parameter value")
return
}
var sb strings.Builder
sb.Grow(100 * count)
for i := 0; i < count; i++ {
now, st, err := performRequest()
if err != nil {
sb.WriteString(fmt.Sprintf("%d: cannot perform request: %s\n", now, err))
continue
}
sb.WriteString(fmt.Sprintf("%d: received a response with code: %d\n", now, st))
}
c.String(http.StatusOK, sb.String())
}
func ConcurrentFloodHandler(c *gin.Context) {
workersStr := c.Query("workers")
if workersStr == "" {
c.String(http.StatusBadRequest, "Please specify the amount of workers in workers query parameter")
return
}
workers, err := strconv.Atoi(workersStr)
if err != nil || workers <= 0 {
c.String(http.StatusBadRequest, "Invalid workers parameter value")
return
}
countStr := c.Query("count")
if countStr == "" {
c.String(http.StatusBadRequest, "Please specify the amount of requests per worker in the count query parameter")
return
}
count, err := strconv.Atoi(countStr)
if err != nil || workers <= 0 {
c.String(http.StatusBadRequest, "Invalid count parameter value")
return
}
var (
wg sync.WaitGroup
sb strings.Builder
sbLock sync.Mutex
)
sb.Grow(100 * count * workers)
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
var workerSb strings.Builder
workerSb.Grow(100 * count)
for l := 0; l < count; l++ {
now, st, err := performRequest()
if err != nil {
workerSb.WriteString(fmt.Sprintf("%d: cannot perform request: %s\n", now, err))
continue
}
workerSb.WriteString(fmt.Sprintf("%d: received a response with code: %d\n", now, st))
}
defer sbLock.Unlock()
sbLock.Lock()
sb.WriteString(workerSb.String())
wg.Done()
}()
}
wg.Wait()
c.String(http.StatusOK, sb.String())
}
func performRequest() (int64, int, error) {
now := time.Now().UnixNano()
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://127.0.0.1:8000/vk/%s", vkToken), strings.NewReader(fmt.Sprintf(bodyTemplate, now, now, now, now, now)))
if err != nil {
return now, 0, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return now, 0, err
}
resp.Body.Close()
return now, resp.StatusCode, nil
}

34
handler/mg.go Normal file
View file

@ -0,0 +1,34 @@
package handler
import (
"log"
"net/http"
"strings"
"sync/atomic"
"time"
"github.com/gin-gonic/gin"
v1 "github.com/retailcrm/mg-transport-api-client-go/v1"
"vk-webhook-flooder-mock/dto"
)
var totalUserless uint32 = 0
func NewMessageHandler(c *gin.Context) {
var snd v1.SendData
if err := c.ShouldBindJSON(&snd); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errors": []string{err.Error()}})
return
}
if strings.HasPrefix(snd.Customer.Firstname, "User #") {
total := atomic.AddUint32(&totalUserless, 1)
log.Printf("MG NewMessageHandler: [%d] Beware! Message external_id=%s has placeholder user.",
total, snd.Message.ExternalID)
}
c.JSON(http.StatusOK, dto.MessagesResponse{
MessageID: int(time.Now().UnixNano()),
Time: time.Now(),
})
}

51
handler/vk.go Normal file
View file

@ -0,0 +1,51 @@
package handler
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"time"
"github.com/SevereCloud/vksdk/v2/api"
"github.com/SevereCloud/vksdk/v2/object"
"github.com/gin-gonic/gin"
"vk-webhook-flooder-mock/dto"
)
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
func UsersGetHandler(c *gin.Context) {
// Simulate real network latency + VK API latency.
time.Sleep(time.Millisecond * time.Duration(rnd.Intn(150)+200))
var req dto.UsersGetRequest
if err := c.ShouldBind(&req); err != nil {
log.Printf("invalid users.get request: %s", err)
c.String(http.StatusBadRequest, "Invalid request data")
return
}
val := api.UsersGetResponse{}
for _, id := range req.UserIDs {
uID, _ := strconv.Atoi(id)
val = append(val, object.UsersUser{
ID: uID,
FirstName: "Mocked",
LastName: fmt.Sprintf("Profile #%s", id),
})
}
data, err := json.Marshal(val)
if err != nil {
c.Error(err)
return
}
var res api.Response
res.Response = data
c.JSON(http.StatusOK, res)
}

36
main.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
"vk-webhook-flooder-mock/handler"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "teststore" {
f := newFetcher("token")
go func() {
for i := 0; i < 1000; i++ {
f.Enqueue(int64(i))
time.Sleep(time.Millisecond)
}
}()
fmt.Printf("starting")
for {
fmt.Printf("\033[2K\roffload: %d, output: %d, result: %d", f.offload.Len(), len(f.output), f.result.Len())
time.Sleep(time.Millisecond)
}
}
gin.SetMode(gin.ReleaseMode)
g := gin.New()
g.Use(gin.Recovery())
g.GET("/flood", handler.FloodHandler)
g.GET("/concurrentflood", handler.ConcurrentFloodHandler)
g.POST("/method/users.get", vkRateLimit(), handler.UsersGetHandler)
g.POST("/api/transport/v1/messages", mgRateLimit(), handler.NewMessageHandler)
g.Run(":8090")
}

47
middleware.go Normal file
View file

@ -0,0 +1,47 @@
package main
import (
"log"
"net/http"
"sync/atomic"
"time"
ratelimit "github.com/JGLTechnologies/gin-rate-limit"
"github.com/gin-gonic/gin"
)
func mgRateLimit() gin.HandlerFunc {
var totalLimited uint32
store := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
Rate: time.Second,
Limit: 50, // TODO: This must be changed to the 1000rps.
})
return ratelimit.RateLimiter(store, &ratelimit.Options{
ErrorHandler: func(c *gin.Context, info ratelimit.Info) {
cur := atomic.AddUint32(&totalLimited, 1)
log.Printf("%s: [%d] rate limited > %t, will reset at %s\n", c.ClientIP(), cur, info.RateLimited, info.ResetTime)
c.AbortWithStatus(http.StatusTooManyRequests)
},
KeyFunc: keyFunc,
})
}
func vkRateLimit() gin.HandlerFunc {
var totalLimited uint32
store := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
Rate: time.Second,
Limit: 20,
})
return ratelimit.RateLimiter(store, &ratelimit.Options{
ErrorHandler: func(c *gin.Context, info ratelimit.Info) {
cur := atomic.AddUint32(&totalLimited, 1)
log.Printf("%s: [%d] VK request rate limited > %t, will reset at %s\n", c.ClientIP(), cur, info.RateLimited, info.ResetTime)
c.AbortWithStatus(http.StatusTooManyRequests)
},
KeyFunc: keyFunc,
})
}
func keyFunc(c *gin.Context) string {
return c.ClientIP()
}

177
teststore.go Normal file
View file

@ -0,0 +1,177 @@
package main
import (
"context"
"encoding/json"
"errors"
"math/rand"
"runtime"
"strconv"
"time"
"github.com/SevereCloud/vksdk/v2/object"
"github.com/allegro/bigcache/v3"
"go.uber.org/atomic"
)
type UsersUser struct {
ID int64
}
var UserUpdateInterval = 24
const (
maxUsersPerChannel = 50
fetcherInterval = time.Millisecond
fetcherBatchingInterval = time.Millisecond * 20
)
type Fetcher interface {
Enqueue(userID int64)
WaitFor(userID int64, sleepBetweenAttempts time.Duration, attemptsCount int) UsersUser
}
type fetcher struct {
rnd *rand.Rand
input chan int64
offload *bigcache.BigCache
output chan []int64
result *bigcache.BigCache
stop atomic.Bool
token string
}
func newFetcher(token string) *fetcher {
input, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(time.Minute*10))
output, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(time.Minute))
uf := &fetcher{
rnd: rand.New(rand.NewSource(time.Now().UnixNano())),
input: make(chan int64, maxUsersPerChannel),
output: make(chan []int64),
offload: input,
result: output,
token: token,
}
runtime.SetFinalizer(uf, usersFetcherDestructor)
go uf.consumeOffload()
go uf.assembleBatches()
go uf.doWork()
return uf
}
func usersFetcherDestructor(uf *fetcher) {
uf.stop.Store(true)
}
func (u *fetcher) Enqueue(userID int64) {
if u.stop.Load() {
return
}
if userID == 0 {
return
}
if len(u.input) < 50 {
u.input <- userID
return
}
_ = u.offload.Set(strconv.FormatInt(userID, 10), []byte{})
}
func (u *fetcher) WaitFor(userID int64, sleepBetweenAttempts time.Duration, attemptsCount int) object.UsersUser {
attempt := 0
uid := strconv.FormatInt(userID, 10)
retryFetching:
val, err := u.result.Get(uid)
if err != nil {
if errors.Is(err, bigcache.ErrEntryNotFound) {
if attempt == attemptsCount {
return object.UsersUser{}
}
time.Sleep(sleepBetweenAttempts)
attempt++
goto retryFetching
}
return object.UsersUser{}
}
var user object.UsersUser
if err := json.Unmarshal(val, &user); err != nil {
return object.UsersUser{}
}
return user
}
func (u *fetcher) consumeOffload() {
for {
if u.stop.Load() {
break
}
it := u.offload.Iterator()
for it.SetNext() {
entry, err := it.Value()
if err != nil {
break
}
uid, _ := strconv.ParseInt(entry.Key(), 10, 64)
if uid == 0 {
u.offload.Delete(entry.Key())
continue
}
u.input <- uid
u.offload.Delete(entry.Key())
time.Sleep(fetcherInterval)
}
}
}
func (u *fetcher) assembleBatches() {
cnt := 0
items := make([]int64, 50)
for {
if u.stop.Load() {
break
}
select {
case item := <-u.input:
items[cnt] = item
cnt++
if cnt == 50 {
cnt = 0
u.output <- items
items = make([]int64, 50)
continue
}
case <-time.After(fetcherBatchingInterval):
if len(items) > 0 && items[0] != 0 {
cnt = 0
u.output <- items
items = make([]int64, 50)
}
}
}
}
func (u *fetcher) doWork() {
defer func() {
if r := recover(); r != nil {
time.Sleep(fetcherInterval)
go u.doWork()
}
}()
for batch := range u.output {
time.Sleep(time.Millisecond * time.Duration(u.rnd.Intn(150)+200))
for _, user := range batch {
data, err := json.Marshal(UsersUser{ID: user})
if err != nil {
continue
}
_ = u.result.Set(strconv.FormatInt(user, 10), data)
}
}
close(u.output)
close(u.input)
}