Added Ctrl+C handler
This commit is contained in:
parent
720f70338f
commit
8a4e29a3ce
@ -28,9 +28,6 @@ func Run(baseCtx context.Context, addr string) {
|
|||||||
// var wg sync.WaitGroup
|
// var wg sync.WaitGroup
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
}()
|
|
||||||
for {
|
for {
|
||||||
// Print test every second
|
// Print test every second
|
||||||
select {
|
select {
|
||||||
@ -44,10 +41,12 @@ func Run(baseCtx context.Context, addr string) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Listen for an incoming connection in a goroutine.
|
// Listen for an incoming connection in a goroutine.
|
||||||
// wg.Add(1)
|
|
||||||
go func() {
|
go func() {
|
||||||
go func() {
|
go func() {
|
||||||
|
// Waiting for the context to be done
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
|
|
||||||
|
// Waiting for the listener to be closed, so the port is safely released
|
||||||
listener.Close()
|
listener.Close()
|
||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
|
|||||||
@ -1,53 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
// Queue implementation
|
|
||||||
|
|
||||||
type Node struct {
|
|
||||||
Value interface{}
|
|
||||||
Next *Node
|
|
||||||
}
|
|
||||||
|
|
||||||
type Queue struct {
|
|
||||||
Head *Node
|
|
||||||
Tail *Node
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queue) Enqueue(value interface{}) {
|
|
||||||
node := &Node{Value: value}
|
|
||||||
|
|
||||||
if q.Head == nil {
|
|
||||||
q.Head = node
|
|
||||||
q.Tail = node
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
q.Tail.Next = node
|
|
||||||
q.Tail = node
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queue) Dequeue() interface{} {
|
|
||||||
if q.Head == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
node := q.Head
|
|
||||||
q.Head = node.Next
|
|
||||||
|
|
||||||
if q.Head == nil {
|
|
||||||
q.Tail = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return node.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queue) Peek() interface{} {
|
|
||||||
if q.Head == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return q.Head.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queue) IsEmpty() bool {
|
|
||||||
return q.Head == nil
|
|
||||||
}
|
|
||||||
20
steel.go
20
steel.go
@ -4,7 +4,11 @@ import (
|
|||||||
"cimeyclust.com/steel/pkg/cmd"
|
"cimeyclust.com/steel/pkg/cmd"
|
||||||
"cimeyclust.com/steel/pkg/net"
|
"cimeyclust.com/steel/pkg/net"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -12,6 +16,9 @@ func main() {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
// Start the console handler
|
// Start the console handler
|
||||||
@ -32,8 +39,19 @@ func main() {
|
|||||||
net.Run(ctx, "localhost:8080")
|
net.Run(ctx, "localhost:8080")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
cmd.Logger.Info("Started")
|
// Handles Ctrl+C
|
||||||
|
go func() {
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-sigChan:
|
||||||
|
cmd.Logger.Info("Received stop command via Ctrl+C")
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Wait for all components to finish
|
// Wait for all components to finish
|
||||||
|
cmd.Logger.Info("Server started successfully")
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
fmt.Println("Server stopped successfully")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user