Skip to content

Commit

Permalink
Merge pull request #999 from Fenny/master
Browse files Browse the repository at this point in the history
📦 update fasthttp and storage interface
  • Loading branch information
Fenny authored Nov 6, 2020
2 parents 0d25f8c + ea1fed9 commit 6c165f9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 41 deletions.
20 changes: 14 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ type Map map[string]interface{}
// Storage interface that is implemented by storage providers for different
// middleware packages like cache, limiter, session and csrf
type Storage interface {
// Get value by key, nil is returned if either does not exist
// Get retrieves the value for the given key.
// If no value is not found it returns ErrNotExit error
Get(key string) ([]byte, error)

// Set key value. `exp` will be zero for no expiration.
Set(key string, val []byte, exp time.Duration) error
// Set stores the given value for the given key along with a
// time-to-live expiration value, 0 means live for ever
// The key must not be "" and the empty values are ignored.
Set(key string, val []byte, ttl time.Duration) error

// Delete key
// Delete deletes the stored value for the given key.
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
Delete(key string) error

// Clear storage
Clear() error
// Reset the storage
Reset() error

// Close the storage
Close() error
}

// ErrorHandler defines a function that will process all errors
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/gofiber/fiber/v2
go 1.14

require (
github.com/valyala/fasthttp v1.16.0
github.com/valyala/fasthttp v1.17.0
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1
)
21 changes: 9 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.2 h1:MiK62aErc3gIiVEtyzKfeOHgW7atJb5g/KNX5m3c2nQ=
github.com/klauspost/compress v1.11.2/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ=
github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA=
github.com/valyala/fasthttp v1.17.0 h1:P8/koH4aSnJ4xbd0cUUFEGQs3jQqIxoDDyRQrUiAkqg=
github.com/valyala/fasthttp v1.17.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1 h1:a/mKvvZr9Jcc8oKfcmgzyp7OwF73JPWsQLvH1z2Kxck=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
6 changes: 5 additions & 1 deletion middleware/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (s testStore) Set(id string, val []byte, _ time.Duration) error {
return nil
}

func (s testStore) Clear() error {
func (s testStore) Reset() error {
s.stmap = map[string][]byte{}
return nil
}
Expand All @@ -344,3 +344,7 @@ func (s testStore) Delete(id string) error {
s.mutex.Unlock()
return nil
}

func (s testStore) Close() error {
return nil
}
6 changes: 5 additions & 1 deletion middleware/limiter/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,14 @@ func (s testStore) Set(id string, val []byte, _ time.Duration) error {
return nil
}

func (s testStore) Clear() error {
func (s testStore) Reset() error {
return nil
}

func (s testStore) Delete(id string) error {
return nil
}

func (s testStore) Close() error {
return nil
}
24 changes: 4 additions & 20 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proxy
import (
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -47,7 +46,7 @@ func Test_Proxy(t *testing.T) {
utils.AssertEqual(t, nil, target.Listen(":3001"))
}()

time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)

resp, err := target.Test(httptest.NewRequest("GET", "/", nil), 2000)
utils.AssertEqual(t, nil, err)
Expand All @@ -64,21 +63,6 @@ func Test_Proxy(t *testing.T) {
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
}

// go test -run Test_Proxy_Do_With_Error
func Test_Proxy_Do_With_Error(t *testing.T) {
app := fiber.New()

app.Use(Balancer(Config{Servers: []string{"127.0.0.1:90000"}}))

resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)

b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(b), "127.0.0.1:90000"))
}

func Test_Proxy_Forward(t *testing.T) {
app := fiber.New()

Expand All @@ -89,7 +73,7 @@ func Test_Proxy_Forward(t *testing.T) {
go func() {
utils.AssertEqual(t, nil, target.Listen(":50001"))
}()
time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)

app.Use(Forward("http://127.0.0.1:50001"))

Expand All @@ -110,7 +94,7 @@ func Test_Proxy_Modify_Response(t *testing.T) {
go func() {
utils.AssertEqual(t, nil, target.Listen(":50002"))
}()
time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)

app := fiber.New()
app.Use(Balancer(Config{
Expand Down Expand Up @@ -139,7 +123,7 @@ func Test_Proxy_Modify_Request(t *testing.T) {
go func() {
utils.AssertEqual(t, nil, target.Listen(":50003"))
}()
time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)

app := fiber.New()
app.Use(Balancer(Config{
Expand Down

0 comments on commit 6c165f9

Please sign in to comment.