Skip to content
Merged
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
6 changes: 2 additions & 4 deletions go/delay.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"strconv"
"time"

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

schduler := e.NewScheduler()
now := time.Now()
p.onRequestHeaders = now
go func() {
// Scheduler must be closed to avoid memory leaks.
defer schduler.Close()
// Simulate some delay.
time.Sleep(2 * time.Second)
// Commit the event to continue the request processing.
schduler.Commit(0)
}()
fmt.Printf("gosdk: RequestHeaders, delaying request processing for 2 seconds at %s\n", now)
return gosdk.RequestHeadersStatusStopIteration
}

Expand All @@ -57,7 +56,6 @@ func (p *delayFilter) Sheduled(e gosdk.EnvoyHttpFilter, eventID uint64) {
if eventID != 0 {
panic("unexpected eventID in Sheduled: " + strconv.Itoa(int(eventID)))
}
fmt.Println("gosdk: Sheduled, continuing request processing after delay")
p.delayLapsed = time.Since(p.onRequestHeaders)
// We can insert some headers at this phase.
e.SetRequestHeader("delay-filter-on-scheduled", []byte("yes"))
Expand Down
5 changes: 4 additions & 1 deletion go/gosdk/gosdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type Scheduler interface {
// Commit commits the event to the Envoy filter on the correct worker thread.
// The eventID is a unique identifier for the event, and it can be used to distinguish between different events.
Commit(eventID uint64)
// Close closes the scheduler and releases any resources associated with it.
// This must be called when the scheduler is no longer needed to avoid memory leaks.
Close()
}

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

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

// Destroy is called when the stream is destroyed.
Expand Down
Loading