-
-
Notifications
You must be signed in to change notification settings - Fork 591
Refactor server/pubsub into interface #6318
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
6543
merged 20 commits into
woodpecker-ci:main
from
6543-forks:server/move-pubsub-into-interface
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ad2e5e6
start
6543 61e3920
refactor pubsub to use topics
6543 080cae6
Merge branch 'main' into server/move-pubsub-into-interface
6543 08cbb0a
Update server/pubsub/pubsub.go
6543 12db803
Merge branch 'main' into server/move-pubsub-into-interface
6543 b06b9d3
document the PubSub interface contract
6543 b483d2c
RWMutex
6543 9000c87
harden publish func of memory pubsub impl.
6543 b5b4897
Apply suggestion from @6543
6543 66f5648
Merge branch 'main' into server/move-pubsub-into-interface
6543 2cbc896
Add integration test for pubsub
6543 e76365d
Merge branch 'main' into server/move-pubsub-into-interface
6543 d9640ae
Merge branch 'main' into server/move-pubsub-into-interface
6543 eb96174
Update server/pubsub/memory/pub.go
6543 cf07394
Merge branch 'main' into server/move-pubsub-into-interface
6543 b4f6773
Apply suggestion from @6543
6543 ab68c53
Merge branch 'main' into server/move-pubsub-into-interface
6543 9a4a4a6
fix lint
6543 538e714
fix root cause
6543 d4908bd
Revert "fix root cause" (wrong pull)
6543 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
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,89 @@ | ||
| // Copyright 2026 Woodpecker Authors | ||
| // | ||
| // Licensed 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 memory | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "slices" | ||
| "sync" | ||
|
|
||
| "github.com/rs/zerolog/log" | ||
|
6543 marked this conversation as resolved.
|
||
|
|
||
| "go.woodpecker-ci.org/woodpecker/v3/server/pubsub" | ||
| ) | ||
|
|
||
| type publisher struct { | ||
| sync.RWMutex | ||
|
|
||
| subs map[*pubsub.Receiver][]string | ||
| } | ||
|
|
||
| // New creates an in-memory publisher. | ||
| func New() pubsub.PubSub { | ||
| return &publisher{ | ||
| subs: make(map[*pubsub.Receiver][]string), | ||
| } | ||
| } | ||
|
|
||
| func (p *publisher) Publish(_ context.Context, topics pubsub.Topics, message pubsub.Message) error { | ||
| if len(topics) == 0 { | ||
| return fmt.Errorf("%w: specify at least one", pubsub.ErrNoTopic) | ||
| } | ||
|
|
||
| p.RLock() | ||
| defer p.RUnlock() | ||
|
|
||
| for s, tl := range p.subs { | ||
| // callback is from outside so just make sure it still exists | ||
| if s == nil || *s == nil { | ||
| log.Error().Msg("found nil callback func in subscribers!") | ||
| continue | ||
| } | ||
|
|
||
| for t := range topics { | ||
| if slices.Contains(tl, t) { | ||
| go (*s)(message) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *publisher) Subscribe(c context.Context, topics pubsub.Topics, receiver pubsub.Receiver) error { | ||
| if len(topics) == 0 { | ||
| return fmt.Errorf("%w: subscribe to at least one", pubsub.ErrNoTopic) | ||
| } | ||
|
|
||
| var tl []string | ||
| for k := range topics { | ||
| tl = append(tl, k) | ||
| } | ||
|
|
||
| defer func() { | ||
| p.Lock() | ||
| delete(p.subs, &receiver) | ||
| p.Unlock() | ||
| }() | ||
|
|
||
| p.Lock() | ||
| p.subs[&receiver] = tl | ||
| p.Unlock() | ||
|
|
||
| <-c.Done() | ||
| return 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
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.