-
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.
## tl;dr - Introduces a new `ClientManager` for managing gRPC client connections - Implements a `RegistrationFunc` callback for registering gRPC services - Refactors `NewAPIServer` to use the `RegistrationFunc` instead of direct service creation - Adds `BuildClient` method to `Node` struct for creating gRPC client connections - Updates test utilities to use the new service registration approach ## Why a registration func? Two reasons: 1. I wanted to get the dependencies and logic of building the individual services out of the `api` module and make that solely focused on creating an API server 2. Some server's will have just a payer API, some will have a message and payer API. I wanted to move that decision making up a layer in the stack. ## AI Assisted Notes This PR introduces a `ClientManager` to handle gRPC client connections efficiently. It implements a `RegistrationFunc` callback for registering gRPC services, allowing for more flexible service configuration. The `NewAPIServer` function has been refactored to use this new `RegistrationFunc` instead of creating services directly. A `BuildClient` method has been added to the `Node` struct, simplifying the process of creating gRPC client connections. Test utilities have been updated to reflect these changes and use the new service registration approach. These changes improve the modularity and flexibility of the API server setup and client connection management.
- Loading branch information
Showing
8 changed files
with
257 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,74 @@ | ||
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() | ||
|
||
// Check again under the write lock | ||
if existing, ok := c.connections[nodeID]; ok { | ||
return existing, nil | ||
} | ||
|
||
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.