Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAPPL-20] Support per-method handlers in GatewayConnector #14367

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

bolekk
Copy link
Contributor

@bolekk bolekk commented Sep 6, 2024

Making GatewayConnector compatible with the new design, where each capability is able to add its own handler independently.

@bolekk bolekk changed the title [CAPPL-20] Add AddHandler() method for GatewayConnector [CAPPL-20] Support per-method handlers in GatewayConnector Sep 6, 2024
@bolekk bolekk force-pushed the feature/CAAPL-20-gateway-connector-add-handler branch 3 times, most recently from 7cbd5dd to 5046485 Compare September 6, 2024 20:58
@bolekk bolekk marked this pull request as ready for review September 6, 2024 20:59
@bolekk bolekk requested review from a team as code owners September 6, 2024 20:59
@bolekk bolekk requested review from samsondav and justinkaseman and removed request for a team September 6, 2024 20:59
Making GatewayConnector compatible with the new design, where each capability is able to add its own handler independently.
@bolekk bolekk force-pushed the feature/CAAPL-20-gateway-connector-add-handler branch from 5046485 to 8138054 Compare September 6, 2024 21:08
@@ -76,8 +77,8 @@ type gatewayState struct {
wsClient network.WebSocketClient
}

func NewGatewayConnector(config *ConnectorConfig, signer Signer, handler GatewayConnectorHandler, clock clockwork.Clock, lggr logger.Logger) (GatewayConnector, error) {
if config == nil || signer == nil || handler == nil || clock == nil || lggr == nil {
func NewGatewayConnector(config *ConnectorConfig, signer Signer, clock clockwork.Clock, lggr logger.Logger) (GatewayConnector, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not give it an array of handlers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have them when the connector is constructed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we had a single handler before for calling the connector, why wouldn't we have one still? Is it because the service_wrapper is being created and it won't be ready at connector construction time?

Copy link
Contributor Author

@bolekk bolekk Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had one handler ready here before only because in Functions the Connector object was created by a job spec. Now that you moved it to be a core service, it's not the case any more. Core services (now including Gateway Connector) are all created first, before iterating over job specs and creating services that they need. In the re-design proposal (see doc I shared earlier today), each standard capability will add its own connector handler, when neccesary.

@@ -125,6 +126,22 @@ func NewGatewayConnector(config *ConnectorConfig, signer Signer, handler Gateway
return connector, nil
}

func (c *gatewayConnector) AddHandler(methods []string, handler GatewayConnectorHandler) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe addHandlers that takes an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each capability will only add one handler (to support itself).

return errors.New("cannot add a nil handler")
}
for _, method := range methods {
if _, exists := c.handlers[method]; exists {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why aren't you getting index from the range and then using that on c.handlers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c.handlers map is indexed by strings, not integers. Also here I only check for existence before adding later in the subsequent loop.

@@ -194,9 +216,6 @@ func (c *gatewayConnector) reconnectLoop(gatewayState *gatewayState) {
func (c *gatewayConnector) Start(ctx context.Context) error {
return c.StartOnce("GatewayConnector", func() error {
c.lggr.Info("starting gateway connector")
if err := c.handler.Start(ctx); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any tests that had to change because the Start and Close were removed. How is it tested that the all the handlers are started and stopped?

Copy link
Contributor Author

@bolekk bolekk Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines were removed from connector_test.go:

handler.On("Start", mock.Anything).Return(nil)
handler.On("Close").Return(nil)

@DavidOrchard
Copy link
Contributor

I think I see that Start happens after all the handlers are added. Is there a race condition where an AddHandler could be called after the Start and it would never get started? Do we need a guard for that?

@bolekk
Copy link
Contributor Author

bolekk commented Sep 6, 2024

I think I see that Start happens after all the handlers are added. Is there a race condition where an AddHandler could be called after the Start and it would never get started? Do we need a guard for that?

Handlers are not being started and closed by the Connector any more. They can be added at any time, even after Start()-ing the Connector. It's up to the owner (i.e. the capability) now to manage its handler's lifecycle.

@bolekk bolekk added this pull request to the merge queue Sep 9, 2024
Merged via the queue into develop with commit cd8be70 Sep 9, 2024
135 of 136 checks passed
@bolekk bolekk deleted the feature/CAAPL-20-gateway-connector-add-handler branch September 9, 2024 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants