Skip to content

Commit 922f4af

Browse files
authored
Merge pull request #91 from planetary-social/simplify-backpressure-handling
Simplify backpressure handling
2 parents ea4e456 + 54acc65 commit 922f4af

File tree

4 files changed

+8
-36
lines changed

4 files changed

+8
-36
lines changed

cmd/send-all-events-to-relay/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (u *EventUploader) worker(ctx context.Context) {
148148
func (u *EventUploader) sendEvent(ctx context.Context, event domain.Event) error {
149149
for {
150150
if err := u.eventSender.SendEvent(ctx, u.address, event); err != nil {
151-
if errors.Is(err, relays.BackPressureError) {
151+
if errors.Is(err, relays.ErrEventReplaced) {
152152
u.eventsRelayReplaced.Add(1)
153153
u.allEvents.Add(1)
154154
} else {

service/domain/relays/relay_connection.go

+2-30
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ type RelayConnection struct {
7070
eventsToSendMutex sync.Mutex
7171
newEventsCh chan domain.Event
7272
rateLimitNoticeBackoffManager *RateLimitNoticeBackoffManager
73-
cancelBackPressure context.CancelFunc
7473
}
7574

7675
func NewRelayConnection(
@@ -90,7 +89,6 @@ func NewRelayConnection(
9089
eventsToSend: make(map[domain.EventId]*eventToSend),
9190
newEventsCh: make(chan domain.Event),
9291
rateLimitNoticeBackoffManager: rateLimitNoticeBackoffManager,
93-
cancelBackPressure: nil,
9492
}
9593
}
9694

@@ -108,10 +106,6 @@ func (r *RelayConnection) Run(ctx context.Context) {
108106
if r.Address().HostWithoutPort() == "relay.nos.social" {
109107
// We control relay.nos.social, so we don't backoff here
110108
backoff = 0
111-
} else if errors.Is(err, BackPressureError) {
112-
// Only calling r.cancelBackPressure() can resolve the backpressure
113-
r.WaitUntilNoBackPressure(ctx)
114-
backoff = 0
115109
}
116110

117111
select {
@@ -123,20 +117,6 @@ func (r *RelayConnection) Run(ctx context.Context) {
123117
}
124118
}
125119

126-
func (r *RelayConnection) WaitUntilNoBackPressure(ctx context.Context) {
127-
var resolvedBackPressureCtx context.Context
128-
var cancelBackPressure context.CancelFunc
129-
130-
r.setStateWithFn(RelayConnectionStateBackPressured, func() {
131-
resolvedBackPressureCtx, cancelBackPressure = context.WithCancel(ctx)
132-
r.cancelBackPressure = cancelBackPressure
133-
})
134-
135-
<-resolvedBackPressureCtx.Done()
136-
137-
r.setState(RelayConnectionStateDisconnected)
138-
}
139-
140120
func (r *RelayConnection) State() RelayConnectionState {
141121
r.stateMutex.Lock()
142122
defer r.stateMutex.Unlock()
@@ -356,8 +336,8 @@ func (r *RelayConnection) run(ctx context.Context) error {
356336
}
357337

358338
if r.state == RelayConnectionStateBackPressured {
359-
// Load shedding under backpressure
360-
continue
339+
// Load shedding under backpressure, we just disconnect
340+
return BackPressureError
361341
}
362342

363343
if err := r.handleMessage(messageBytes); err != nil {
@@ -613,14 +593,6 @@ func (r *RelayConnection) setState(state RelayConnectionState) {
613593
r.state = state
614594
}
615595

616-
func (r *RelayConnection) setStateWithFn(state RelayConnectionState, fn func()) {
617-
r.stateMutex.Lock()
618-
defer r.stateMutex.Unlock()
619-
620-
r.state = state
621-
fn()
622-
}
623-
624596
func (r *RelayConnection) manageSubs(ctx context.Context, conn Connection) error {
625597
defer conn.Close()
626598

service/domain/relays/relay_connections.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@ func (d *RelayConnections) SendEvent(ctx context.Context, relayAddress domain.Re
7676

7777
func (d *RelayConnections) NotifyBackPressure() {
7878
for _, connection := range d.connections {
79-
if connection.Address().HostWithoutPort() != "relay.nos.social" {
79+
if connection.Address().HostWithoutPort() != "relay.nos.social" && connection.State() == RelayConnectionStateConnected {
8080
connection.setState(RelayConnectionStateBackPressured)
8181
}
8282
}
8383
}
8484

8585
func (d *RelayConnections) ResolveBackPressure() {
8686
for _, connection := range d.connections {
87-
if connection.cancelBackPressure != nil {
88-
connection.cancelBackPressure()
89-
connection.cancelBackPressure = nil
87+
if connection.Address().HostWithoutPort() != "relay.nos.social" && connection.State() == RelayConnectionStateBackPressured {
88+
connection.setState(RelayConnectionStateConnected)
9089
}
9190
}
9291
}
@@ -115,6 +114,7 @@ func (d *RelayConnections) storeMetrics() {
115114
}
116115
d.metrics.ReportRelayConnectionsState(m)
117116
}
117+
118118
func (r *RelayConnections) getRateLimitNoticeBackoffManager(relayAddress domain.RelayAddress) *RateLimitNoticeBackoffManager {
119119
rateLimitNoticeBackoffManager, exists := r.rateLimitNoticeBackoffManagers[relayAddress.HostWithoutPort()]
120120
if !exists {

service/ports/sqlitepubsub/event_saved.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s *EventSavedEventSubscriber) Run(ctx context.Context) error {
5757
fmt.Sprintf("Queue size %d > %d. Sending backpressure signal to slow down", queueSize, backPressureThreshold),
5858
)
5959
s.handler.NotifyBackPressure()
60-
} else if queueSize < backPressureThreshold/2 {
60+
} else {
6161
s.handler.ResolveBackPressure()
6262
}
6363

0 commit comments

Comments
 (0)