forked from kyverno/kyverno-envoy-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: factorise server run code (kyverno#151)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
612712c
commit d7350c6
Showing
4 changed files
with
96 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"go.uber.org/multierr" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
) | ||
|
||
func Run(ctx context.Context, server *http.Server, certFile, keyFile string) error { | ||
defer fmt.Println("Server stopped") | ||
// track shutdown error | ||
var shutdownErr error | ||
// track serve error | ||
serveErr := func(ctx context.Context) error { | ||
// create a wait group | ||
var group wait.Group | ||
// wait all tasks in the group are over | ||
defer group.Wait() | ||
// create a cancellable context | ||
ctx, cancel := context.WithCancel(ctx) | ||
// cancel context at the end | ||
defer cancel() | ||
// shutdown server when context is cancelled | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// wait context cancelled | ||
<-ctx.Done() | ||
fmt.Println("Server shutting down...") | ||
// create a context with timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
shutdownErr = server.Shutdown(ctx) | ||
}) | ||
serve := func() error { | ||
fmt.Printf("Server starting at %s...\n", server.Addr) | ||
if certFile != "" && keyFile != "" { | ||
// server over https | ||
return server.ListenAndServeTLS(certFile, keyFile) | ||
} else { | ||
// server over http | ||
return server.ListenAndServe() | ||
} | ||
} | ||
// server closed is not an error | ||
if err := serve(); !errors.Is(err, http.ErrServerClosed) { | ||
return err | ||
} | ||
return nil | ||
}(ctx) | ||
// return error if any | ||
return multierr.Combine(serveErr, shutdownErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package signals | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDo(t *testing.T) { | ||
err := Do(context.Background(), func(ctx context.Context) error { | ||
return nil | ||
}) | ||
assert.NoError(t, err) | ||
} |