Skip to content

Commit

Permalink
SplitHTTP server: add ok padding (#3614)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmray authored Aug 2, 2024
1 parent 4c82ef8 commit 4b7947c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 43 deletions.
42 changes: 23 additions & 19 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,22 @@ type SplitHTTPConfig struct {
Host string `json:"host"`
Path string `json:"path"`
Headers map[string]string `json:"headers"`
ScMaxConcurrentPosts Int32Range `json:"scMaxConcurrentPosts"`
ScMaxEachPostBytes Int32Range `json:"scMaxEachPostBytes"`
ScMinPostsIntervalMs Int32Range `json:"scMinPostsIntervalMs"`
ScMaxConcurrentPosts *Int32Range `json:"scMaxConcurrentPosts"`
ScMaxEachPostBytes *Int32Range `json:"scMaxEachPostBytes"`
ScMinPostsIntervalMs *Int32Range `json:"scMinPostsIntervalMs"`
NoSSEHeader bool `json:"noSSEHeader"`
ResponseOkPadding *Int32Range `json:"responseOkPadding"`
}

func splithttpNewRandRangeConfig(input *Int32Range) *splithttp.RandRangeConfig {
if input == nil {
return nil
}

return &splithttp.RandRangeConfig{
From: input.From,
To: input.To,
}
}

// Build implements Buildable.
Expand All @@ -246,22 +258,14 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
c.Host = c.Headers["Host"]
}
config := &splithttp.Config{
Path: c.Path,
Host: c.Host,
Header: c.Headers,
ScMaxConcurrentPosts: &splithttp.RandRangeConfig{
From: c.ScMaxConcurrentPosts.From,
To: c.ScMaxConcurrentPosts.To,
},
ScMaxEachPostBytes: &splithttp.RandRangeConfig{
From: c.ScMaxEachPostBytes.From,
To: c.ScMaxEachPostBytes.To,
},
ScMinPostsIntervalMs: &splithttp.RandRangeConfig{
From: c.ScMinPostsIntervalMs.From,
To: c.ScMinPostsIntervalMs.To,
},
NoSSEHeader: c.NoSSEHeader,
Path: c.Path,
Host: c.Host,
Header: c.Headers,
ScMaxConcurrentPosts: splithttpNewRandRangeConfig(c.ScMaxConcurrentPosts),
ScMaxEachPostBytes: splithttpNewRandRangeConfig(c.ScMaxEachPostBytes),
ScMinPostsIntervalMs: splithttpNewRandRangeConfig(c.ScMinPostsIntervalMs),
NoSSEHeader: c.NoSSEHeader,
ResponseOkPadding: splithttpNewRandRangeConfig(c.ResponseOkPadding),
}
return config, nil
}
Expand Down
11 changes: 11 additions & 0 deletions transport/internet/splithttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ func (c *Config) GetNormalizedScMinPostsIntervalMs() RandRangeConfig {
return *c.ScMinPostsIntervalMs
}

func (c *Config) GetNormalizedResponseOkPadding() RandRangeConfig {
if c.ResponseOkPadding == nil || c.ResponseOkPadding.To == 0 {
return RandRangeConfig{
From: 100,
To: 1000,
}
}

return *c.ResponseOkPadding
}

func init() {
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
return new(Config)
Expand Down
64 changes: 40 additions & 24 deletions transport/internet/splithttp/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions transport/internet/splithttp/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ message Config {
RandRangeConfig scMaxEachPostBytes = 5;
RandRangeConfig scMinPostsIntervalMs = 6;
bool noSSEHeader = 7;
RandRangeConfig responseOkPadding = 8;
}

message RandRangeConfig {
Expand Down
5 changes: 5 additions & 0 deletions transport/internet/splithttp/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req

currentSession := h.upsertSession(sessionId)
scMaxEachPostBytes := int(h.ln.config.GetNormalizedScMaxEachPostBytes().To)
responseOkPadding := h.ln.config.GetNormalizedResponseOkPadding()

if request.Method == "POST" {
seq := ""
Expand Down Expand Up @@ -192,6 +193,10 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
// send a chunk immediately to enable CDN streaming.
// many CDN buffer the response headers until the origin starts sending
// the body, with no way to turn it off.
padding := int(responseOkPadding.roll())
for i := 0; i < padding; i++ {
writer.Write([]byte("o"))
}
writer.Write([]byte("ok"))
responseFlusher.Flush()

Expand Down

0 comments on commit 4b7947c

Please sign in to comment.