-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ibuildthecloud/pause-resume
- Loading branch information
Showing
8 changed files
with
278 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package remotedialer | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type backPressure struct { | ||
cond sync.Cond | ||
c *connection | ||
paused bool | ||
} | ||
|
||
func newBackPressure(c *connection) *backPressure { | ||
return &backPressure{ | ||
cond: sync.Cond{ | ||
L: &sync.Mutex{}, | ||
}, | ||
c: c, | ||
paused: false, | ||
} | ||
} | ||
|
||
func (b *backPressure) OnPause() { | ||
b.cond.L.Lock() | ||
defer b.cond.L.Unlock() | ||
|
||
b.paused = true | ||
b.cond.Broadcast() | ||
} | ||
|
||
func (b *backPressure) OnResume() { | ||
b.cond.L.Lock() | ||
defer b.cond.L.Unlock() | ||
|
||
b.paused = false | ||
b.cond.Broadcast() | ||
} | ||
|
||
func (b *backPressure) Pause() error { | ||
b.cond.L.Lock() | ||
defer b.cond.L.Unlock() | ||
if b.paused { | ||
return nil | ||
} | ||
if _, err := b.c.Pause(); err != nil { | ||
return err | ||
} | ||
b.paused = true | ||
return nil | ||
} | ||
|
||
func (b *backPressure) Resume() error { | ||
b.cond.L.Lock() | ||
defer b.cond.L.Unlock() | ||
if !b.paused { | ||
return nil | ||
} | ||
if _, err := b.c.Resume(); err != nil { | ||
return err | ||
} | ||
b.paused = false | ||
return nil | ||
} | ||
|
||
func (b *backPressure) Wait() { | ||
b.cond.L.Lock() | ||
defer b.cond.L.Unlock() | ||
|
||
for b.paused { | ||
b.cond.Wait() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package remotedialer | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExceedBuffer(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
producerAddress, err := newTestProducer(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
serverAddress, server, err := newTestServer(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := newTestClient(ctx, "ws://"+serverAddress); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(ctx context.Context, proto, address string) (net.Conn, error) { | ||
return server.Dialer("client")(ctx, proto, address) | ||
}, | ||
}, | ||
} | ||
|
||
producerURL := "http://" + producerAddress | ||
|
||
resp, err := client.Get(producerURL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
resp2, err := client.Get(producerURL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp2.Body.Close() | ||
|
||
resp2Body, err := ioutil.ReadAll(resp2.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
respBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, 4096*4096, len(resp2Body)) | ||
assert.Equal(t, 4096*4096, len(respBody)) | ||
} | ||
|
||
func newTestServer(ctx context.Context) (string, *Server, error) { | ||
auth := func(req *http.Request) (clientKey string, authed bool, err error) { | ||
return "client", true, nil | ||
} | ||
|
||
server := New(auth, DefaultErrorWriter) | ||
address, err := newServer(ctx, server) | ||
return address, server, err | ||
} | ||
|
||
func newTestClient(ctx context.Context, url string) error { | ||
result := make(chan error, 2) | ||
go func() { | ||
err := ConnectToProxy(ctx, url, nil, func(proto, address string) bool { | ||
return true | ||
}, nil, func(ctx context.Context, session *Session) error { | ||
result <- nil | ||
return nil | ||
}) | ||
result <- err | ||
}() | ||
return <-result | ||
} | ||
|
||
func newServer(ctx context.Context, handler http.Handler) (string, error) { | ||
server := http.Server{ | ||
BaseContext: func(_ net.Listener) context.Context { | ||
return ctx | ||
}, | ||
Handler: handler, | ||
} | ||
listener, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
return "", err | ||
} | ||
go func() { | ||
<-ctx.Done() | ||
listener.Close() | ||
server.Shutdown(context.Background()) | ||
}() | ||
go server.Serve(listener) | ||
return listener.Addr().String(), nil | ||
} | ||
|
||
func newTestProducer(ctx context.Context) (string, error) { | ||
buffer := make([]byte, 4096) | ||
return newServer(ctx, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { | ||
for i := 0; i < 4096; i++ { | ||
if _, err := resp.Write(buffer); err != nil { | ||
panic(err) | ||
} | ||
} | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.