Skip to content

Commit

Permalink
Add WithBlock DialOption
Browse files Browse the repository at this point in the history
  • Loading branch information
jkongie committed Jun 22, 2021
1 parent e991e9e commit e3541aa
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
ctx := peer.NewCallContext(context.Background(), pubKey)
res, err := c.Gnip(ctx, &pb.GnipRequest{Body: "Gnip"})
```
* Add a `WithBlock` DialOption which blocks the caller of Dial until the underlying connection is up.

# 0.1.1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ While the client's are connected, kill the server and see the client's enter a b

- [ ] Improve Tests
- [ ] Return a response status
- [ ] Add a Blocking DialOption
- [x] Add a Blocking DialOption
13 changes: 12 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ type ClientConn struct {
service *serviceInfo
}

// Dial creates a client connection to the given target.
// Dial creates a client connection to the given target. By default, it's
// a non-blocking dial (the function won't wait for connections to be
// established, and connecting happens in the background). To make it a blocking
// dial, use WithBlock() dial option.
func Dial(target string, opts ...DialOption) (*ClientConn, error) {
cc := &ClientConn{
ctx: context.Background(),
Expand All @@ -74,6 +77,14 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) {
addrConn.connect()
cc.conn = addrConn

if cc.dopts.block {
for {
if addrConn.state == connectivity.Ready {
break
}
}
}

return cc, nil
}

Expand Down
10 changes: 10 additions & 0 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type dialOptions struct {
copts transport.ConnectOptions
bs backoff.Strategy
block bool
}

// DialOption configures how we set up the connection.
Expand Down Expand Up @@ -54,6 +55,15 @@ func WithTransportCreds(privKey ed25519.PrivateKey, serverPubKey ed25519.PublicK
})
}

// WithBlock returns a DialOption which makes caller of Dial blocks until the
// underlying connection is up. Without this, Dial returns immediately and
// connecting the server happens in background.
func WithBlock() DialOption {
return newFuncDialOption(func(o *dialOptions) {
o.block = true
})
}

func defaultDialOptions() dialOptions {
return dialOptions{
copts: transport.ConnectOptions{},
Expand Down
5 changes: 4 additions & 1 deletion examples/simple/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func main() {
privKey := keys.FromHex(keys.Clients[cidx].PrivKey)
serverPubKey := keys.FromHex(keys.ServerPubKey)

conn, err := wsrpc.Dial("127.0.0.1:1338", wsrpc.WithTransportCreds(privKey, serverPubKey))
conn, err := wsrpc.Dial("127.0.0.1:1338",
wsrpc.WithTransportCreds(privKey, serverPubKey),
wsrpc.WithBlock(),
)
if err != nil {
log.Fatalln(err)
}
Expand Down
59 changes: 59 additions & 0 deletions wsrpclog/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package wsrpclog

import "log"

// Logger defines a simple logger interface. We may want to consider improving
// logging capabilities.
type Logger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}

var (
Default Logger
)

func init() {
Default = &logger{}
}

func SetVerboseLogger() {
Default = &logger{Verbose: true}
}

type logger struct {
Verbose bool
}

var _ Logger = (*logger)(nil)

func (l *logger) Print(v ...interface{}) {
if l.Verbose {
log.Println(v...)
}
}

func (l *logger) Printf(format string, v ...interface{}) {
if l.Verbose {
log.Printf(format, v...)
}
}

func (l *logger) Println(v ...interface{}) {
if l.Verbose {
log.Println(v...)
}
}

func Print(v ...interface{}) {
Default.Print(v...)
}

func Printf(format string, v ...interface{}) {
Default.Printf(format, v...)
}

func Println(v ...interface{}) {
Default.Println(v...)
}

0 comments on commit e3541aa

Please sign in to comment.