-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
252 additions
and
68 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,69 @@ | ||
package payer | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/xmtp/xmtpd/pkg/registry" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
/* | ||
* | ||
The ClientManager contains a mapping of nodeIDs to gRPC client connections. | ||
These client connections are safe to be shared and re-used and will automatically attempt | ||
to reconnect if the underlying socket connection is lost. | ||
* | ||
*/ | ||
type ClientManager struct { | ||
log *zap.Logger | ||
connections map[uint32]*grpc.ClientConn | ||
connectionsMutex sync.RWMutex | ||
nodeRegistry registry.NodeRegistry | ||
} | ||
|
||
func NewClientManager(log *zap.Logger, nodeRegistry registry.NodeRegistry) *ClientManager { | ||
return &ClientManager{ | ||
log: log, | ||
nodeRegistry: nodeRegistry, | ||
connections: make(map[uint32]*grpc.ClientConn), | ||
} | ||
} | ||
|
||
func (c *ClientManager) GetClient(nodeID uint32) (*grpc.ClientConn, error) { | ||
c.connectionsMutex.RLock() | ||
existing, ok := c.connections[nodeID] | ||
c.connectionsMutex.RUnlock() | ||
if ok { | ||
return existing, nil | ||
} | ||
|
||
c.connectionsMutex.Lock() | ||
defer c.connectionsMutex.Unlock() | ||
|
||
conn, err := c.newClientConnection(nodeID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Store the connection | ||
c.connections[nodeID] = conn | ||
|
||
return conn, nil | ||
} | ||
|
||
func (c *ClientManager) newClientConnection( | ||
nodeID uint32, | ||
) (*grpc.ClientConn, error) { | ||
c.log.Info("connecting to node", zap.Uint32("nodeID", nodeID)) | ||
node, err := c.nodeRegistry.GetNode(nodeID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conn, err := node.BuildClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return conn, nil | ||
} |
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,60 @@ | ||
package payer_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/api/payer" | ||
"github.com/xmtp/xmtpd/pkg/registry" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
apiTestUtils "github.com/xmtp/xmtpd/pkg/testutils/api" | ||
"google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
func formatAddress(addr string) string { | ||
chunks := strings.Split(addr, ":") | ||
return fmt.Sprintf("http://localhost:%s", chunks[len(chunks)-1]) | ||
} | ||
|
||
func TestClientManager(t *testing.T) { | ||
server1, _, cleanup1 := apiTestUtils.NewTestAPIServer(t) | ||
defer cleanup1() | ||
server2, _, cleanup2 := apiTestUtils.NewTestAPIServer(t) | ||
defer cleanup2() | ||
|
||
nodeRegistry := registry.NewFixedNodeRegistry([]registry.Node{ | ||
{ | ||
NodeID: 100, | ||
HttpAddress: formatAddress(server1.Addr().String()), | ||
}, | ||
{ | ||
NodeID: 200, | ||
HttpAddress: formatAddress(server2.Addr().String()), | ||
}, | ||
}) | ||
|
||
cm := payer.NewClientManager(testutils.NewLog(t), nodeRegistry) | ||
|
||
client1, err := cm.GetClient(100) | ||
require.NoError(t, err) | ||
require.NotNil(t, client1) | ||
|
||
healthClient := grpc_health_v1.NewHealthClient(client1) | ||
healthResponse, err := healthClient.Check( | ||
context.Background(), | ||
&grpc_health_v1.HealthCheckRequest{}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, grpc_health_v1.HealthCheckResponse_SERVING, healthResponse.Status) | ||
|
||
client2, err := cm.GetClient(200) | ||
require.NoError(t, err) | ||
require.NotNil(t, client2) | ||
|
||
_, err = cm.GetClient(300) | ||
require.Error(t, 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.