Skip to content

Commit

Permalink
configurable concurrency limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekonan committed Dec 31, 2021
1 parent e536fc2 commit 0c6e174
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func newApp() *app {
KucoinApiURL: "https://openapi-v2.kucoin.com",
},
ProxyConfig: proxy.Config{
Port: "8080",
Bindaddr: "0.0.0.0",
Port: "8080",
Bindaddr: "0.0.0.0",
ConcurrencyLimit: fasthttp.DefaultConcurrency,
},
}
}
Expand Down
7 changes: 5 additions & 2 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package proxy
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/valyala/fasthttp"
)

type Config struct {
Port string `help:"listen port"`
Bindaddr string `help:"bindable address"`
Port string `help:"listen port"`
Bindaddr string `help:"bindable address"`
ConcurrencyLimit int `help:"server concurrency limit"`
}

func (c Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Port, is.Port),
validation.Field(&c.Bindaddr, is.IPv4),
validation.Field(&c.ConcurrencyLimit, validation.Min(fasthttp.DefaultConcurrency)),
)
}
19 changes: 11 additions & 8 deletions proxy/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Routable interface {
Name() string
}

func New(config *Config, routable Routable) *Router {
func New(config *Config, routable Routable) *Server {
router := routing.New()

for _, route := range routable.Routes() {
Expand All @@ -35,18 +35,21 @@ func New(config *Config, routable Routable) *Router {
router.To(route.Method, path, route.Handler)
}

return &Router{
router: router,
return &Server{
server: &fasthttp.Server{
Handler: router.HandleRequest,
Concurrency: config.ConcurrencyLimit,
},
config: config,
}
}

type Router struct {
type Server struct {
config *Config
router *routing.Router
server *fasthttp.Server
}

func (r *Router) Serve() {
logrus.Infof("starting proxy server on :%s port...", r.config.Port)
panic(fasthttp.ListenAndServe(fmt.Sprintf("%s:%s", r.config.Bindaddr, r.config.Port), r.router.HandleRequest))
func (s *Server) Serve() {
logrus.Infof("starting proxy server on :%s port...", s.config.Port)
logrus.Fatal(s.server.ListenAndServe(fmt.Sprintf("%s:%s", s.config.Bindaddr, s.config.Port)))
}

0 comments on commit 0c6e174

Please sign in to comment.