Skip to content

Commit

Permalink
add stopCh to cmd/gate.Run method (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Aug 23, 2020
1 parent 70b2f12 commit 33256d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
19 changes: 14 additions & 5 deletions cmd/gate/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import (
"syscall"
)

func Run() (err error) {
// Run reads in and validates the config to pass into proxy.New,
// initializes the logger, runs the new proxy.Proxy and
// blocks until stopChan is triggered or an OS signal is sent.
// The proxy is already shutdown on method return.
func Run(stopCh <-chan struct{}) (err error) {
var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
return fmt.Errorf("error loading config: %w", err)
Expand All @@ -39,6 +43,7 @@ func Run() (err error) {
return fmt.Errorf("error initializing global logger: %w", err)
}

// Validate after we initialized the logger.
if err = config.Validate(&cfg); err != nil {
return fmt.Errorf("error validating config: %w", err)
}
Expand All @@ -49,11 +54,15 @@ func Run() (err error) {

p := proxy.New(cfg)
go func() {
s, ok := <-sig
if !ok {
return
select {
case <-stopCh:
case s, ok := <-sig:
if !ok {
// Sig chan was closed
return
}
zap.S().Infof("Received %s signal", s)
}
zap.S().Infof("Received %s signal", s)
p.Shutdown(&component.Text{
Content: "Gate proxy is shutting down...\nPlease reconnect in a moment!",
S: component.Style{Color: color.Red}})
Expand Down
2 changes: 1 addition & 1 deletion cmd/gate/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var rootCmd = &cobra.Command{
Long: `A high performant & paralleled Minecraft proxy server with
scalability, flexibility & excelled server version support.`,
Run: func(cmd *cobra.Command, args []string) {
if err := Run(); err != nil {
if err := Run(cmd.Context().Done()); err != nil {
cmd.PrintErr(fmt.Sprintf("Error running Gate Proxy: %v", err))
}
},
Expand Down
22 changes: 12 additions & 10 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ type Proxy struct {
servers map[string]RegisteredServer // registered backend servers: by lower case names
}

// New returns a new initialized Proxy.
func New(config config.Config) (s *Proxy) {
defer func() {
s.connect = newConnect(s)
}()
return &Proxy{
// New takes a validated config and returns a new initialized Proxy.
func New(config config.Config) (p *Proxy) {
p = &Proxy{
closed: make(chan struct{}),
config: &config,
event: event.NewManager(),
Expand All @@ -60,13 +57,18 @@ func New(config config.Config) (s *Proxy) {
servers: map[string]RegisteredServer{},
authenticator: auth.NewAuthenticator(),
}
p.connect = newConnect(p)
return p
}

// Returned by Proxy.Run if the proxy instance was already run.
var ErrProxyAlreadyRun = errors.New("proxy was already run, create a new one")

// Run runs the proxy and blocks until Shutdown is called or an error occurred.
// Run can only be called once per Proxy instance.
// Run runs the proxy, blocks until the proxy is
// Shutdown or an error occurred while starting.
// The Proxy is already shutdown on method return.
// In order to stop the Proxy call Shutdown.
// A Proxy can only be run once or ErrProxyAlreadyRun is returned.
func (p *Proxy) Run() (err error) {
if !p.runOnce.CAS(false, true) {
return ErrProxyAlreadyRun
Expand All @@ -75,10 +77,10 @@ func (p *Proxy) Run() (err error) {
return p.run() // Run and block
}

// Shutdown shuts down the Proxy and blocks until finished.
// Shutdown stops the Proxy and/or blocks until the Proxy has finished shutdown.
//
// It first stops listening for new connections, disconnects
// all existing connections with the given reason (if nil stands for no reason)
// all existing connections with the given reason (nil = blank reason)
// and waits for all event subscribers to finish.
func (p *Proxy) Shutdown(reason component.Component) {
p.closeOnce.Do(func() {
Expand Down

0 comments on commit 33256d9

Please sign in to comment.