diff options
| -rw-r--r-- | cmd/gorum/main.go | 24 | ||||
| -rw-r--r-- | internal/client/client.go | 17 | ||||
| -rw-r--r-- | internal/run.go | 4 | ||||
| -rw-r--r-- | internal/server/server.go | 15 |
4 files changed, 56 insertions, 4 deletions
diff --git a/cmd/gorum/main.go b/cmd/gorum/main.go index 223da9d..e39f620 100644 --- a/cmd/gorum/main.go +++ b/cmd/gorum/main.go @@ -4,6 +4,10 @@ import ( "context" "flag" "fmt" + "log" + "os" + "os/signal" + "syscall" "codeberg.org/snonux/gorum/internal" ) @@ -21,8 +25,24 @@ func main() { return } - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := contextWithSignal() defer cancel() - internal.Run(ctx, *configFile) + internal.Start(ctx, *configFile) + + <-ctx.Done() + log.Println("Good bye!") +} + +func contextWithSignal() (context.Context, context.CancelFunc) { + ctx, cancel := context.WithCancel(context.Background()) + + go func() { + ch := make(chan os.Signal) + signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) + <-ch + cancel() + }() + + return ctx, cancel } diff --git a/internal/client/client.go b/internal/client/client.go index 299623d..eab3514 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -2,9 +2,26 @@ package client import ( "context" + "log" + "time" "codeberg.org/snonux/gorum/internal/config" ) func Start(ctx context.Context, conf config.Config) { + go func() { + for { + log.Println("Client server") + start(ctx, conf) + + select { + case <-time.After(time.Second): + case <-ctx.Done(): + return + } + } + }() +} + +func start(ctx context.Context, conf config.Config) { } diff --git a/internal/run.go b/internal/run.go index e1139d7..1473719 100644 --- a/internal/run.go +++ b/internal/run.go @@ -8,12 +8,12 @@ import ( "codeberg.org/snonux/gorum/internal/server" ) -func Run(ctx context.Context, configFile string) { +func Start(ctx context.Context, configFile string) { conf, err := config.New(configFile) if err != nil { panic(err) } - go client.Start(ctx, conf) + client.Start(ctx, conf) server.Start(ctx, conf) } diff --git a/internal/server/server.go b/internal/server/server.go index 55d7c9a..d388e01 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -11,6 +11,21 @@ import ( ) func Start(ctx context.Context, conf config.Config) { + go func() { + for { + log.Println("Starting server") + start(ctx, conf) + + select { + case <-time.After(time.Second): + case <-ctx.Done(): + return + } + } + }() +} + +func start(ctx context.Context, conf config.Config) { ch := make(chan vote.Vote) quo := make(quorum.Quorum) |
