Skip to content

Commit 8a5d69d

Browse files
acwulyssa
andauthored
Expose the ability to set HTTP and TCP keepalives on dynamic backends. (#140)
* Add primitive versions of the keepalive flags. * Add higher-level implementations of the primitives --------- Co-authored-by: Ulyssa <[email protected]>
1 parent 8c029b5 commit 8a5d69d

File tree

2 files changed

+114
-19
lines changed

2 files changed

+114
-19
lines changed

fsthttp/backend.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,52 @@ func (b *BackendOptions) PoolConnections(poolingOn bool) *BackendOptions {
311311
return b
312312
}
313313

314+
// HTTPKeepaliveTime configures how long to allow HTTP connections to remain
315+
// idle in a connection pool before it should be considered closed.
316+
func (b *BackendOptions) HTTPKeepaliveTime(time time.Duration) *BackendOptions {
317+
b.abiOpts.HTTPKeepaliveTime(time)
318+
return b
319+
}
320+
321+
// TCPKeepaliveEnable sets whether or not to use TCP keepalives to try to
322+
// maintain the connetion to the backend.
323+
func (b *BackendOptions) TCPKeepaliveEnable(enable bool) *BackendOptions {
324+
b.abiOpts.TCPKeepaliveEnable(enable)
325+
return b
326+
}
327+
328+
// TCPKeepaliveInterval sets the interval to use when sending TCP keepalive
329+
// probes. Intervals of less than 1 second will be rounded up to 1 second.
330+
//
331+
// Setting this value implicitly enables TCP keepalives. If you are calling both
332+
// this method and `TCPKeepAliveEnable` with dynamically loaded or generated
333+
// values, make sure to call `TCPKeepAliveEnable` last.
334+
func (b *BackendOptions) TCPKeepaliveInterval(interval time.Duration) *BackendOptions {
335+
if interval < time.Second {
336+
interval = time.Second
337+
}
338+
339+
b.abiOpts.TCPKeepaliveInterval(interval)
340+
341+
return b
342+
}
343+
344+
// TCPKeepaliveProbes sets how many unanswered TCP probes we should send to the
345+
// backend before we consider the connection dead. Setting this value
346+
// implicitly enables TCP keepalives.
347+
func (b *BackendOptions) TCPKeepaliveProbes(count uint32) *BackendOptions {
348+
b.abiOpts.TCPKeepaliveProbes(count)
349+
return b
350+
}
351+
352+
// TCPKeepaliveTime sets how long to wait after the last data was sent before
353+
// starting to send keepalive probes. Setting this value implicitly enables
354+
// TCP keepalives.
355+
func (b *BackendOptions) TCPKeepaliveTime(interval time.Duration) *BackendOptions {
356+
b.abiOpts.TCPKeepaliveTime(interval)
357+
return b
358+
}
359+
314360
// UseGRPC sets whether or not to connect to the backend via gRPC
315361
func (b *BackendOptions) UseGRPC(v bool) *BackendOptions {
316362
b.abiOpts.UseGRPC(v)

internal/abi/fastly/types.go

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,11 @@ type purgeOptions struct {
717717
// $ca_cert
718718
// $ciphers
719719
// $sni_hostname
720-
// $dont_pool))
720+
// $dont_pool
721+
// $client_cert
722+
// $grpc
723+
// $keepalive
724+
// ))
721725

722726
type backendConfigOptionsMask prim.U32
723727

@@ -737,6 +741,7 @@ const (
737741
backendConfigOptionsMaskDontPool backendConfigOptionsMask = 1 << 12 // $dont_pool
738742
backendConfigOptionsMaskClientCert backendConfigOptionsMask = 1 << 13 // $client_cert
739743
backendConfigOptionsMaskGRPC backendConfigOptionsMask = 1 << 14 // $grpc
744+
backendConfigOptionsMaskKeepalive backendConfigOptionsMask = 1 << 15 // $keepalive
740745
)
741746

742747
// witx:
@@ -761,27 +766,37 @@ const (
761766
// (field $client_certificate (@witx pointer (@witx char8)))
762767
// (field $client_certificate_len u32)
763768
// (field $client_key $secret_handle)
769+
// (field $http_keepalive_time_ms $timeout_ms)
770+
// (field $tcp_keepalive_enable u32)
771+
// (field $tcp_keepalive_interval_secs $timeout_secs)
772+
// (field $tcp_keepalive_probes $probe_count)
773+
// (field $tcp_keepalive_time_secs $timeout_secs)
764774
// ))
765775

766776
type backendConfigOptions struct {
767-
hostOverridePtr prim.Pointer[prim.Char8]
768-
hostOverrideLen prim.U32
769-
connectTimeoutMs prim.U32
770-
firstByteTimeout prim.U32
771-
betweenBytesTimeout prim.U32
772-
sslMinVersion TLSVersion
773-
sslMaxVersion TLSVersion
774-
certHostnamePtr prim.Pointer[prim.Char8]
775-
certHostnameLen prim.U32
776-
caCertPtr prim.Pointer[prim.Char8]
777-
caCertLen prim.U32
778-
ciphersPtr prim.Pointer[prim.Char8]
779-
ciphersLen prim.U32
780-
sniHostnamePtr prim.Pointer[prim.Char8]
781-
sniHostnameLen prim.U32
782-
clientCertPtr prim.Pointer[prim.Char8]
783-
clientCertLen prim.U32
784-
clientCertKey secretHandle
777+
hostOverridePtr prim.Pointer[prim.Char8]
778+
hostOverrideLen prim.U32
779+
connectTimeoutMs prim.U32
780+
firstByteTimeout prim.U32
781+
betweenBytesTimeout prim.U32
782+
sslMinVersion TLSVersion
783+
sslMaxVersion TLSVersion
784+
certHostnamePtr prim.Pointer[prim.Char8]
785+
certHostnameLen prim.U32
786+
caCertPtr prim.Pointer[prim.Char8]
787+
caCertLen prim.U32
788+
ciphersPtr prim.Pointer[prim.Char8]
789+
ciphersLen prim.U32
790+
sniHostnamePtr prim.Pointer[prim.Char8]
791+
sniHostnameLen prim.U32
792+
clientCertPtr prim.Pointer[prim.Char8]
793+
clientCertLen prim.U32
794+
clientCertKey secretHandle
795+
httpKeepaliveTimeMs prim.U32
796+
tcpKeepaliveEnable prim.U32
797+
tcpKeepaliveIntervalSecs prim.U32
798+
tcpKeepaliveProbes prim.U32
799+
tcpKeepaliveTimeSecs prim.U32
785800
}
786801

787802
// witx:
@@ -913,6 +928,40 @@ func (b *BackendConfigOptions) UseGRPC(v bool) {
913928
}
914929
}
915930

931+
func (b *BackendConfigOptions) HTTPKeepaliveTime(t time.Duration) {
932+
b.mask |= backendConfigOptionsMaskKeepalive
933+
b.opts.httpKeepaliveTimeMs = prim.U32(t.Milliseconds())
934+
}
935+
936+
func (b *BackendConfigOptions) TCPKeepaliveEnable(v bool) {
937+
b.mask |= backendConfigOptionsMaskKeepalive
938+
if v {
939+
b.opts.tcpKeepaliveEnable = prim.U32(1)
940+
} else {
941+
b.opts.tcpKeepaliveEnable = prim.U32(0)
942+
}
943+
}
944+
945+
func (b *BackendConfigOptions) TCPKeepaliveInterval(t time.Duration) {
946+
b.mask |= backendConfigOptionsMaskKeepalive
947+
b.opts.tcpKeepaliveEnable = prim.U32(1)
948+
b.opts.tcpKeepaliveIntervalSecs = prim.U32(t.Seconds())
949+
}
950+
951+
func (b *BackendConfigOptions) TCPKeepaliveProbes(count uint32) {
952+
if count > 0 {
953+
b.mask |= backendConfigOptionsMaskKeepalive
954+
b.opts.tcpKeepaliveEnable = prim.U32(1)
955+
b.opts.tcpKeepaliveProbes = prim.U32(count)
956+
}
957+
}
958+
959+
func (b *BackendConfigOptions) TCPKeepaliveTime(t time.Duration) {
960+
b.mask |= backendConfigOptionsMaskKeepalive
961+
b.opts.tcpKeepaliveEnable = prim.U32(1)
962+
b.opts.tcpKeepaliveTimeSecs = prim.U32(t.Seconds())
963+
}
964+
916965
// witx:
917966
//
918967
// (typename $send_error_detail_tag

0 commit comments

Comments
 (0)