-
Notifications
You must be signed in to change notification settings - Fork 67
Synchronize gateway controller replicas based on DB backed eventing Mechanism. #1354
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
db93033
Add Eventhub Package
VirajSalaka ab9b5fc
Synchronize based on eventing mechanism
VirajSalaka 8746985
Testcases for EventListener
VirajSalaka e17ba7d
Event based test cases for handlers.go
VirajSalaka 4deb3a3
Merge branch 'main' into sync-fix-v3-temp-2
VirajSalaka f64ffdd
Fix test cases
VirajSalaka 7db5b0e
Bug fix: API Undeployment does not work due to bug in Status Update
VirajSalaka 51ffb51
Merge branch 'main' into sync-fix-v3
VirajSalaka 7203d9b
Fix Review Comments
VirajSalaka 822ed4d
Reduce Poll time
VirajSalaka 15f8eed
Increase test time
VirajSalaka 30a5725
Fix integration test failures
VirajSalaka 7482615
Merge branch 'main' into sync-fix-v3
VirajSalaka 0559ccf
List all APIs from the database
VirajSalaka dbfc7a9
List all APIs from the database - fix test cases
VirajSalaka b60d9ad
Merge branch 'main' into sync-fix-v3
VirajSalaka 1ec238b
Fix API Key Related issues
VirajSalaka f2f9f98
Fix Makefile
VirajSalaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,41 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package eventhub | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| const apiKeyEntityIDSeparator = "_" | ||
|
|
||
| // BuildAPIKeyEntityID returns the composite entity ID used for API key events. | ||
| func BuildAPIKeyEntityID(apiID, keyID string) string { | ||
| return apiID + apiKeyEntityIDSeparator + keyID | ||
| } | ||
|
|
||
| // ParseAPIKeyEntityID splits the composite entity ID used for API key events. | ||
| func ParseAPIKeyEntityID(entityID string) (string, string, error) { | ||
| separatorIndex := strings.LastIndex(entityID, apiKeyEntityIDSeparator) | ||
| if separatorIndex <= 0 || separatorIndex == len(entityID)-1 { | ||
| return "", "", fmt.Errorf("invalid API key entity ID: %q", entityID) | ||
| } | ||
|
|
||
| return entityID[:separatorIndex], entityID[separatorIndex+1:], nil | ||
| } | ||
This file contains hidden or 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,40 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package eventhub | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAPIKeyEntityIDRoundTrip(t *testing.T) { | ||
| entityID := BuildAPIKeyEntityID("api_id", "keyID") | ||
|
|
||
| apiID, keyID, err := ParseAPIKeyEntityID(entityID) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "api_id", apiID) | ||
| assert.Equal(t, "keyID", keyID) | ||
| } | ||
|
|
||
| func TestParseAPIKeyEntityIDInvalid(t *testing.T) { | ||
| _, _, err := ParseAPIKeyEntityID("invalid") | ||
| require.Error(t, err) | ||
| } |
This file contains hidden or 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,63 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package eventhub | ||
|
|
||
| import "time" | ||
|
|
||
| const defaultGatewayStatePageSize = 200 | ||
|
|
||
| // EventhubImpl defines the backend interface for pluggable event hub implementations | ||
| type EventhubImpl interface { | ||
| // Initialize sets up the backend | ||
| Initialize() error | ||
| // RegisterGateway registers a new gateway for event tracking. | ||
| RegisterGateway(gatewayID string) error | ||
| // Publish publishes an event for a gateway. | ||
| Publish(gatewayID string, event Event) error | ||
| // Subscribe subscribes to events for a gateway, returning a channel. | ||
| Subscribe(gatewayID string) (<-chan Event, error) | ||
| // Unsubscribe removes a specific subscription for a gateway. | ||
| Unsubscribe(gatewayID string, subscriber <-chan Event) error | ||
| // UnsubscribeAll removes all subscriptions for a gateway. | ||
| UnsubscribeAll(gatewayID string) error | ||
| // Cleanup removes events older than the retention period | ||
| Cleanup(retentionPeriod time.Duration) error | ||
| // CleanupRange removes events in a time range for a gateway. | ||
| CleanupRange(gatewayID string, before time.Time) error | ||
| // Close gracefully shuts down the backend | ||
| Close() error | ||
| } | ||
|
|
||
| // SQLBackendConfig holds configuration for the SQL backend | ||
| type SQLBackendConfig struct { | ||
| PollInterval time.Duration | ||
| CleanupInterval time.Duration | ||
| RetentionPeriod time.Duration | ||
| GatewayStatePageSize int | ||
| } | ||
|
|
||
| // DefaultSQLBackendConfig returns a SQLBackendConfig with sensible defaults | ||
| func DefaultSQLBackendConfig() SQLBackendConfig { | ||
| return SQLBackendConfig{ | ||
| PollInterval: 2 * time.Second, | ||
| CleanupInterval: 5 * time.Minute, | ||
| RetentionPeriod: 1 * time.Hour, | ||
| GatewayStatePageSize: defaultGatewayStatePageSize, | ||
| } | ||
| } |
This file contains hidden or 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,85 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package eventhub | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| // eventHub is the main EventHub implementation that delegates to a backend | ||
| type eventHub struct { | ||
| backend EventhubImpl | ||
| logger *slog.Logger | ||
| config Config | ||
| } | ||
|
|
||
| // New creates a new EventHub backed by SQLite | ||
| func New(db *sql.DB, logger *slog.Logger, config Config) EventHub { | ||
| backendConfig := DefaultSQLBackendConfig() | ||
| backendConfig.PollInterval = config.PollInterval | ||
| backendConfig.CleanupInterval = config.CleanupInterval | ||
| backendConfig.RetentionPeriod = config.RetentionPeriod | ||
| backend := NewSQLBackend(db, logger, backendConfig) | ||
| return &eventHub{ | ||
| backend: backend, | ||
| logger: logger, | ||
| config: config, | ||
| } | ||
| } | ||
|
|
||
| // NewWithBackend creates a new EventHub with a custom backend | ||
| func NewWithBackend(backend EventhubImpl, logger *slog.Logger) EventHub { | ||
| return &eventHub{ | ||
| backend: backend, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| func (h *eventHub) Initialize() error { | ||
| return h.backend.Initialize() | ||
| } | ||
|
|
||
| func (h *eventHub) RegisterGateway(gatewayID string) error { | ||
| return h.backend.RegisterGateway(gatewayID) | ||
| } | ||
|
|
||
| func (h *eventHub) PublishEvent(gatewayID string, event Event) error { | ||
| return h.backend.Publish(gatewayID, event) | ||
| } | ||
|
|
||
| func (h *eventHub) Subscribe(gatewayID string) (<-chan Event, error) { | ||
| return h.backend.Subscribe(gatewayID) | ||
| } | ||
|
|
||
| func (h *eventHub) Unsubscribe(gatewayID string, subscriber <-chan Event) error { | ||
| return h.backend.Unsubscribe(gatewayID, subscriber) | ||
| } | ||
|
|
||
| func (h *eventHub) UnsubscribeAll(gatewayID string) error { | ||
| return h.backend.UnsubscribeAll(gatewayID) | ||
| } | ||
|
|
||
| func (h *eventHub) CleanUpEvents() error { | ||
| return h.backend.Cleanup(h.config.RetentionPeriod) | ||
| } | ||
|
|
||
| func (h *eventHub) Close() error { | ||
| return h.backend.Close() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.