Skip to content

Commit

Permalink
add new capability to limit accepted connections
Browse files Browse the repository at this point in the history
add internal package which includes json package (gin-gonic#1504)

chore: update vendor version (gin-gonic#1520)

chore: update issue_implate (gin-gonic#1524)

docs: add fnproject to gin's user list (gin-gonic#1505)

example for RunLimited

example for RunLimited

add new capability to limit accepted connections

Add golang 1.11.x testing (gin-gonic#1514)

* Add golang 1.11.x testing

* remove the latest golang testing

See the issue: gin-gonic#1510

Update README.md (gin-gonic#1509)

change  `ShouldBindXML` to `ShouldBindJSON`

add internal package which includes json package (gin-gonic#1504)

chore: update vendor version (gin-gonic#1520)

chore: update issue_implate (gin-gonic#1524)

docs: add fnproject to gin's user list (gin-gonic#1505)

added new capability to limit the accepted requests

example for RunLimited

example for RunLimited
  • Loading branch information
Jim Lambert committed Sep 11, 2018
1 parent 0da5b0c commit abb9d47
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.

- go version:
- gin version (or commit ref):
- git version:
- operating system:

## Description
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go:
- 1.8.x
- 1.9.x
- 1.10.x
- master
- 1.11.x

git:
depth: 10
Expand Down
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
- [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)
- [Try to bind body into different structs](#try-to-bind-body-into-different-structs)
- [http2 server push](#http2-server-push)
- [Support limiting the number of accepted requests using netutil.LimitListener](#RunLimited)
- [Testing](#testing)
- [Users](#users)

Expand Down Expand Up @@ -557,7 +558,7 @@ func main() {
// Example for binding JSON ({"user": "manu", "password": "123"})
router.POST("/loginJSON", func(c *gin.Context) {
var json Login
if err := c.ShouldBindXML(&json); err != nil {
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
Expand Down Expand Up @@ -1835,6 +1836,29 @@ func main() {
}
```

### RunLimited

Limit the number of accepted requests via [netutils.LimitListener](https://godoc.org/golang.org/x/net/netutil)


[embedmd]:# (examples/run-limited/main.go go)
```go
package main

import "github.com/gin-gonic/gin"

const maxConnections = 10
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.RunLimited(maxConnections, ":80") // listen and serve on 0.0.0.0:8080
}
```

## Testing

The `net/http/httptest` package is preferable way for HTTP testing.
Expand Down Expand Up @@ -1885,5 +1909,6 @@ func TestPingRoute(t *testing.T) {

Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.

* [drone](https://github.com/drone/drone): Drone is a Continuous Delivery platform built on Docker, written in Go
* [drone](https://github.com/drone/drone): Drone is a Continuous Delivery platform built on Docker, written in Go.
* [gorush](https://github.com/appleboy/gorush): A push notification server written in Go.
* [fnproject](https://github.com/fnproject/fn): The container native, cloud agnostic serverless platform.
2 changes: 1 addition & 1 deletion binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"io"
"net/http"

"github.com/gin-gonic/gin/json"
"github.com/gin-gonic/gin/internal/json"
)

// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"reflect"

"github.com/gin-gonic/gin/json"
"github.com/gin-gonic/gin/internal/json"
)

type ErrorType uint64
Expand Down
2 changes: 1 addition & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"testing"

"github.com/gin-gonic/gin/json"
"github.com/gin-gonic/gin/internal/json"
"github.com/stretchr/testify/assert"
)

Expand Down
14 changes: 14 additions & 0 deletions examples/run-limited/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "github.com/gin-gonic/gin"

const maxConnections = 10
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.RunLimited(maxConnections, ":80") // listen and serve on 0.0.0.0:8080
}
38 changes: 38 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"net/http"
"os"
"sync"
"time"

"github.com/gin-gonic/gin/render"
"golang.org/x/net/netutil"
)

const (
Expand Down Expand Up @@ -288,6 +290,42 @@ func (engine *Engine) Run(addr ...string) (err error) {
return
}

// RunLimited - use netuil.LimitListener to limit the number of inbound accepts
func (engine *GalapagosEngine) RunLimited(limit int, addr ...string) (err error) {
defer func() { debugPrintError(err) }()

address := resolveAddress(addr)
debugPrint("Listening and serving HTTP on %s\n", address)

// err = http.ListenAndServe(address, engine)

srv := &http.Server{Addr: address, Handler: engine}
address = srv.Addr
if address == "" {
address = ":http"
}
ln, err := net.Listen("tcp", address)
if err != nil {
return err
}
lnLimited := netutil.LimitListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, limit)
return srv.Serve(lnLimited)
}

type tcpKeepAliveListener struct {
*net.TCPListener
}

func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}

// RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
// Note: this method will block the calling goroutine indefinitely unless an error happens.
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"html/template"
"net/http"

"github.com/gin-gonic/gin/json"
"github.com/gin-gonic/gin/internal/json"
)

type JSON struct {
Expand Down
2 changes: 1 addition & 1 deletion render/json_17.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package render
import (
"net/http"

"github.com/gin-gonic/gin/json"
"github.com/gin-gonic/gin/internal/json"
)

type PureJSON struct {
Expand Down
28 changes: 2 additions & 26 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"path": "github.com/json-iterator/go",
"revision": "1624edc4454b8682399def8740d46db5e4362ba4",
"revisionTime": "2018-08-06T06:07:27Z",
"version": "1.1.5",
"versionExact": "1.1.5"
"version": "v1.1",
"versionExact": "v1.1.5"
},
{
"checksumSHA1": "y/A5iuvwjytQE2CqVuphQRXR2nI=",
Expand All @@ -38,24 +38,6 @@
"version": "v0.0.3",
"versionExact": "v0.0.3"
},
{
"checksumSHA1": "ZTcgWKWHsrX0RXYVXn5Xeb8Q0go=",
"path": "github.com/modern-go/concurrent",
"revision": "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94",
"revisionTime": "2018-03-06T01:26:44Z"
},
{
"checksumSHA1": "qvH48wzTIV3QKSDqI0dLFtVjaDI=",
"path": "github.com/modern-go/reflect2",
"revision": "94122c33edd36123c84d5368cfb2b69df93a0ec8",
"revisionTime": "2018-07-18T01:23:57Z"
},
{
"checksumSHA1": "LuFv4/jlrmFNnDb/5SCSEPAM9vU=",
"path": "github.com/pmezard/go-difflib/difflib",
"revision": "792786c7400a136282c1664665ae0a8db921c6c2",
"revisionTime": "2016-01-10T10:55:54Z"
},
{
"checksumSHA1": "c6pbpF7eowwO59phRTpF8cQ80Z0=",
"path": "github.com/stretchr/testify/assert",
Expand All @@ -79,12 +61,6 @@
"revision": "d4c55e66d8c3a2f3382d264b08e3e3454a66355a",
"revisionTime": "2016-10-18T08:54:36Z"
},
{
"checksumSHA1": "7Gocawl8bm27cpAILtuf21xvVD8=",
"path": "golang.org/x/sys/unix",
"revision": "1c9583448a9c3aa0f9a6a5241bf73c0bd8aafded",
"revisionTime": "2018-08-15T07:37:39Z"
},
{
"checksumSHA1": "P/k5ZGf0lEBgpKgkwy++F7K1PSg=",
"path": "gopkg.in/go-playground/validator.v8",
Expand Down

0 comments on commit abb9d47

Please sign in to comment.