Skip to content

Commit f985ed5

Browse files
committed
TUN-5128: Enforce maximum grace period
This maximum grace period will be honored by Cloudflare edge such that either side will close the connection after unregistration at most by this time (3min as of this commit): - If the connection is unused, it is already closed as soon as possible. - If the connection is still used, it is closed on the cloudflared configured grace-period. Even if cloudflared does not close the connection by the grace-period time, the edge will do so.
1 parent d54c8cc commit f985ed5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

cmd/cloudflared/tunnel/cmd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ func StartServer(
384384
observer.RegisterSink(app)
385385
}
386386

387-
return waitToShutdown(&wg, cancel, errC, graceShutdownC, c.Duration("grace-period"), log)
387+
gracePeriod, err := gracePeriod(c)
388+
if err != nil {
389+
return err
390+
}
391+
return waitToShutdown(&wg, cancel, errC, graceShutdownC, gracePeriod, log)
388392
}
389393

390394
func waitToShutdown(wg *sync.WaitGroup,

cmd/cloudflared/tunnel/configuration.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10+
"time"
1011

1112
"github.com/google/uuid"
1213
homedir "github.com/mitchellh/go-homedir"
@@ -260,9 +261,13 @@ func prepareTunnelConfig(
260261
}
261262

262263
originProxy := origin.NewOriginProxy(ingressRules, warpRoutingService, tags, log)
264+
gracePeriod, err := gracePeriod(c)
265+
if err != nil {
266+
return nil, ingress.Ingress{}, err
267+
}
263268
connectionConfig := &connection.Config{
264269
OriginProxy: originProxy,
265-
GracePeriod: c.Duration("grace-period"),
270+
GracePeriod: gracePeriod,
266271
ReplaceExisting: c.Bool("force"),
267272
}
268273
muxerConfig := &connection.MuxerConfig{
@@ -300,6 +305,14 @@ func prepareTunnelConfig(
300305
}, ingressRules, nil
301306
}
302307

308+
func gracePeriod(c *cli.Context) (time.Duration, error) {
309+
period := c.Duration("grace-period")
310+
if period > connection.MaxGracePeriod {
311+
return time.Duration(0), fmt.Errorf("grace-period must be equal or less than %v", connection.MaxGracePeriod)
312+
}
313+
return period, nil
314+
}
315+
303316
func isWarpRoutingEnabled(warpConfig config.WarpRoutingConfig, isNamedTunnel bool) bool {
304317
return warpConfig.Enabled && isNamedTunnel
305318
}

connection/connection.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
const (
1919
lbProbeUserAgentPrefix = "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/;"
2020
LogFieldConnIndex = "connIndex"
21+
MaxGracePeriod = time.Minute * 3
2122
)
2223

2324
var switchingProtocolText = fmt.Sprintf("%d %s", http.StatusSwitchingProtocols, http.StatusText(http.StatusSwitchingProtocols))

0 commit comments

Comments
 (0)