Skip to content

Commit

Permalink
Add debug logging to connpool
Browse files Browse the repository at this point in the history
  • Loading branch information
Bradley Kemp committed Jun 22, 2019
1 parent 8dfd742 commit 32d34b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion grpc-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type server struct {
func New(configurators ...Configurator) (*server, error) {
logger := logrus.New()
s := &server{
connPool: internal.NewConnPool(),
connPool: internal.NewConnPool(logger),
logger: logger,
}
s.serverOptions = []grpc.ServerOption{
Expand Down
3 changes: 2 additions & 1 deletion grpc-replay/grpc-replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bradleyjkemp/grpc-tools/internal/codec"
"github.com/bradleyjkemp/grpc-tools/internal/marker"
_ "github.com/bradleyjkemp/grpc-tools/internal/versionflag"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -98,7 +99,7 @@ RPC:
return nil
}

var cachedConns = internal.NewConnPool()
var cachedConns = internal.NewConnPool(logrus.New())

func getConnection(md metadata.MD) (*grpc.ClientConn, error) {
// if no destination override set then auto-detect from the metadata
Expand Down
8 changes: 7 additions & 1 deletion internal/connPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package internal
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"sync"
)

type ConnPool struct {
sync.Mutex
conns map[string]*grpc.ClientConn
logger logrus.FieldLogger
}

func NewConnPool() *ConnPool {
func NewConnPool(logger logrus.FieldLogger) *ConnPool {
return &ConnPool{
conns: map[string]*grpc.ClientConn{},
logger: logger.WithField("", "connpool"),
}
}

Expand All @@ -34,11 +37,14 @@ func (c *ConnPool) addConn(destination string, conn *grpc.ClientConn) {
func (c *ConnPool) GetClientConn(ctx context.Context, destination string, dialOptions ...grpc.DialOption) (*grpc.ClientConn, error) {
conn, ok := c.getConn(destination)
if ok {
c.logger.Debugf("Returning cached connection to %s", destination)
return conn, nil
}

c.logger.Debugf("Dialing new connection to %s", destination)
conn, err := grpc.DialContext(ctx, destination, dialOptions...)
if err != nil {
c.logger.WithError(err).Debugf("Failed dialing to %s", destination)
return nil, fmt.Errorf("failed dialing %s: %v", destination, err)
}

Expand Down

0 comments on commit 32d34b4

Please sign in to comment.