Skip to content

Commit a1bc0b0

Browse files
authored
Close Scheduler in the delay example (#39)
1 parent b3d36e3 commit a1bc0b0

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

go/delay.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"strconv"
65
"time"
76

@@ -35,20 +34,20 @@ func (p *delayFilter) RequestHeaders(e gosdk.EnvoyHttpFilter, endOfStream bool)
3534
// Check if the headers contain the "do-delay" header to trigger the delay.
3635
if _, ok := e.GetRequestHeader("do-delay"); !ok {
3736
// If the header is not present, continue the request processing.
38-
fmt.Println("gosdk: RequestHeaders, do-delay header not found, continuing request processing")
3937
return gosdk.RequestHeadersStatusContinue
4038
}
4139

4240
schduler := e.NewScheduler()
4341
now := time.Now()
4442
p.onRequestHeaders = now
4543
go func() {
44+
// Scheduler must be closed to avoid memory leaks.
45+
defer schduler.Close()
4646
// Simulate some delay.
4747
time.Sleep(2 * time.Second)
4848
// Commit the event to continue the request processing.
4949
schduler.Commit(0)
5050
}()
51-
fmt.Printf("gosdk: RequestHeaders, delaying request processing for 2 seconds at %s\n", now)
5251
return gosdk.RequestHeadersStatusStopIteration
5352
}
5453

@@ -57,7 +56,6 @@ func (p *delayFilter) Sheduled(e gosdk.EnvoyHttpFilter, eventID uint64) {
5756
if eventID != 0 {
5857
panic("unexpected eventID in Sheduled: " + strconv.Itoa(int(eventID)))
5958
}
60-
fmt.Println("gosdk: Sheduled, continuing request processing after delay")
6159
p.delayLapsed = time.Since(p.onRequestHeaders)
6260
// We can insert some headers at this phase.
6361
e.SetRequestHeader("delay-filter-on-scheduled", []byte("yes"))

go/gosdk/gosdk.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ type Scheduler interface {
8080
// Commit commits the event to the Envoy filter on the correct worker thread.
8181
// The eventID is a unique identifier for the event, and it can be used to distinguish between different events.
8282
Commit(eventID uint64)
83+
// Close closes the scheduler and releases any resources associated with it.
84+
// This must be called when the scheduler is no longer needed to avoid memory leaks.
8385
Close()
8486
}
8587

@@ -99,7 +101,8 @@ type HttpFilter interface {
99101
ResponseBody(e EnvoyHttpFilter, endOfStream bool) ResponseBodyStatus
100102
// TODO: add ResponseTrailers support.
101103

102-
// Scheuled is called when the filter is scheduled to run.
104+
// Scheuled is called when the filter is scheduled to run on the Envoy worker thread.
105+
// Such event is created via [Scheduler.Commit] and the eventID is the unique identifier for the event.
103106
Sheduled(e EnvoyHttpFilter, eventID uint64)
104107

105108
// Destroy is called when the stream is destroyed.

0 commit comments

Comments
 (0)