Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions pkg/loop/internal/net/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,26 @@ type BrokerConfig struct {
type BrokerExt struct {
Broker Broker
BrokerConfig

// State management hook for refresh functionality
onRefreshComplete func(ctx context.Context) error
hooksMu sync.RWMutex
}

// WithName returns a new [*BrokerExt] with Name added to the logger.
// Note: The refresh hook is not shared with the named broker to avoid concurrency issues.
func (b *BrokerExt) WithName(name string) *BrokerExt {
bn := *b
bn.Logger = logger.Named(b.Logger, name)
return &bn
return &BrokerExt{
Broker: b.Broker,
BrokerConfig: BrokerConfig{
StopCh: b.StopCh,
Logger: logger.Named(b.Logger, name),
GRPCOpts: b.GRPCOpts,
},
// Don't share the refresh hook - each named broker manages its own
onRefreshComplete: nil,
hooksMu: sync.RWMutex{},
}
}

// NewClientConn return a new *clientConn backed by this *BrokerExt.
Expand Down Expand Up @@ -147,6 +160,25 @@ func (b *BrokerExt) CloseAll(deps ...Resource) {
}
}

// SetOnRefreshComplete sets a hook to be called after successful connection refresh.
func (b *BrokerExt) SetOnRefreshComplete(hook func(ctx context.Context) error) {
b.hooksMu.Lock()
defer b.hooksMu.Unlock()
b.onRefreshComplete = hook
}

// executeOnRefreshComplete executes the refresh completion hook if it exists.
func (b *BrokerExt) executeOnRefreshComplete(ctx context.Context) error {
b.hooksMu.RLock()
hook := b.onRefreshComplete
b.hooksMu.RUnlock()

if hook != nil {
return hook(ctx)
}
return nil
}

type Resource struct {
io.Closer
Name string
Expand Down
7 changes: 7 additions & 0 deletions pkg/loop/internal/net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func (c *clientConn) refresh(ctx context.Context, orig *grpc.ClientConn) *grpc.C
c.CloseAll(c.deps...)
return false
}

// Execute refresh completion hook after successful connection
if err := c.BrokerExt.executeOnRefreshComplete(ctx); err != nil {
// Don't fail the refresh, but log the error
c.Logger.Errorw("Refresh completion hook failed", "err", err, "conn", c.name)
}

return true
}

Expand Down
Loading
Loading