Skip to content

Commit

Permalink
Adds a flag to allow proxy server to stop automatically after some ti…
Browse files Browse the repository at this point in the history
…me (#11)

* add up time flag

* changed to duration

* fix comments
  • Loading branch information
yanweiguo committed Mar 3, 2022
1 parent e043ec8 commit 6c0fc2d
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var (
flagAudience = flag.String("audience", "", "override JWT audience value (aud)")
flagToken = flag.String("token", "", "override OIDC token")
flagPrependUserAgent = flag.Bool("prepend-user-agent", true, "prepend a custom User-Agent header to requests")
flagServerUpTime = flag.String("server-up-time", "", "Time duration the proxy server will run. For example, 1h, 1m30s. Empty means forever")
)

func main() {
Expand All @@ -78,6 +79,14 @@ func realMain(ctx context.Context) error {
if *flagBind == "" {
return fmt.Errorf("missing -bind")
}
var d time.Duration
if *flagServerUpTime != "" {
var err error
d, err = time.ParseDuration(*flagServerUpTime)
if err != nil {
return fmt.Errorf("error parsing -server-up-time: %w", err)
}
}

// Build the remote host URL.
host, err := smartBuildHost(*flagHost)
Expand Down Expand Up @@ -132,11 +141,21 @@ func realMain(ctx context.Context) error {
}()

// Wait for stop
select {
case err := <-errCh:
return fmt.Errorf("server error: %w", err)
case <-ctx.Done():
fmt.Fprint(os.Stderr, "\nserver is shutting down...\n")
if *flagServerUpTime != "" {
select {
case err := <-errCh:
return fmt.Errorf("server error: %w", err)
case <-time.After(d):
case <-ctx.Done():
fmt.Fprint(os.Stderr, "\nserver is shutting down...\n")
}
} else {
select {
case err := <-errCh:
return fmt.Errorf("server error: %w", err)
case <-ctx.Done():
fmt.Fprint(os.Stderr, "\nserver is shutting down...\n")
}
}

// Attempt graceful shutdown.
Expand Down

0 comments on commit 6c0fc2d

Please sign in to comment.