Skip to content

Commit

Permalink
Go HTTP Filter: Expose envoy concurrency and request worker_id in pub…
Browse files Browse the repository at this point in the history
…lic API (envoyproxy#32107)

Adds new functions to the Go SDK EnvoyConcurrency() and StreamInfo.WorkerID(). This information will allow Go HTTP plugins to optimize their performance in certain use cases. Follow up from envoyproxy#31987

cc @doujiang24

Commit Message:
Additional Description:
Risk Level:
Testing:
Docs Changes:
Release Notes:

Signed-off-by: Braden Bassingthwaite <[email protected]>
  • Loading branch information
bbassingthwaite authored and spacewander committed Jun 25, 2024
1 parent b15d46c commit 343858d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion contrib/golang/common/go/api/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ type StreamInfo interface {
FilterState() FilterState
// VirtualClusterName returns the name of the virtual cluster which got matched
VirtualClusterName() (string, bool)

// WorkerID returns the ID of the Envoy worker thread
WorkerID() uint32
// Some fields in stream info can be fetched via GetProperty
// For example, startTime() is equal to GetProperty("request.time")
}
Expand Down
4 changes: 4 additions & 0 deletions contrib/golang/filters/http/source/go/pkg/http/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ func (s *streamInfo) VirtualClusterName() (string, bool) {
return cAPI.HttpGetStringValue(unsafe.Pointer(s.request), ValueVirtualClusterName)
}

func (s *streamInfo) WorkerID() uint32 {
return uint32(s.request.req.worker_id)
}

type filterState struct {
request *httpRequest
}
Expand Down
19 changes: 19 additions & 0 deletions contrib/golang/filters/http/source/go/pkg/http/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,32 @@ var ErrDupRequestKey = errors.New("dup request key")

var Requests = &requestMap{}

var (
initialized = false
envoyConcurrency uint32
)

// EnvoyConcurrency returns the concurrency Envoy was set to run at. This can be used to optimize HTTP filters that need
// memory per worker thread to avoid locks.
//
// Note: Do not use inside of an `init()` function, the value will not be populated yet. Use within the filters
// `StreamFilterConfigFactory` or `StreamFilterConfigParser`
func EnvoyConcurrency() uint32 {
if !initialized {
panic("concurrency has not yet been initialized, do not access within an init()")
}
return envoyConcurrency
}

type requestMap struct {
initOnce sync.Once
requests []map[*C.httpRequest]*httpRequest
}

func (f *requestMap) initialize(concurrency uint32) {
f.initOnce.Do(func() {
initialized = true
envoyConcurrency = concurrency
f.requests = make([]map[*C.httpRequest]*httpRequest, concurrency)
for i := uint32(0); i < concurrency; i++ {
f.requests[i] = map[*C.httpRequest]*httpRequest{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (n *connectionCallback) VirtualClusterName() (string, bool) {
panic("implement me")
}

func (n *connectionCallback) WorkerID() uint32 {
panic("implement me")
}

type filterState struct {
wrapper unsafe.Pointer
setFunc func(envoyFilter unsafe.Pointer, key string, value string, stateType api.StateType, lifeSpan api.LifeSpan, streamSharing api.StreamSharing)
Expand Down

0 comments on commit 343858d

Please sign in to comment.