Let the client send health checks periodically

This commit is contained in:
Jan Dittberner 2022-08-04 10:16:41 +02:00
parent 82a1284073
commit 64dd9429be

View file

@ -20,16 +20,16 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
"sync" "sync"
"time" "time"
"git.cacert.org/cacert-gosigner/pkg/messages"
"github.com/justincpresley/go-cobs" "github.com/justincpresley/go-cobs"
"github.com/shamaton/msgpackgen/msgpack" "github.com/shamaton/msgpackgen/msgpack"
"git.cacert.org/cacert-gosigner/pkg/messages"
) )
const cobsDelimiter = 0x00 const cobsDelimiter = 0x00
@ -37,39 +37,64 @@ const cobsDelimiter = 0x00
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true} var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
func main() { func main() {
errorLog := log.New(os.Stderr, "", log.LstdFlags)
sim := &clientSimulator{
errorLog: errorLog,
}
err := sim.Run()
if err != nil {
errorLog.Printf("simulator returned an error: %v", err)
}
}
type clientSimulator struct {
errorLog *log.Logger
commands chan messages.Command
responses chan []byte
}
func (c *clientSimulator) writeTestCommands(ctx context.Context) error {
messages.RegisterGeneratedResolver()
const healthInterval = 10 * time.Second
timer := time.NewTimer(healthInterval)
for {
select {
case <-ctx.Done():
_ = timer.Stop()
return nil
case <-timer.C:
c.commands <- messages.Command{
Code: messages.CmdHealth,
TimeStamp: time.Now().UTC(),
}
timer.Reset(healthInterval)
}
}
}
func (c *clientSimulator) handleInput(ctx context.Context) error {
const ( const (
bufferSize = 1024 * 1024 bufferSize = 1024 * 1024
readInterval = 50 * time.Millisecond readInterval = 50 * time.Millisecond
) )
errors := make(chan error)
errorLog := log.New(os.Stderr, "", log.LstdFlags)
wg := sync.WaitGroup{}
wg.Add(1)
done := make(chan struct{})
frame := make(chan []byte)
go func(done chan struct{}) {
buf := make([]byte, bufferSize) buf := make([]byte, bufferSize)
for { for {
select { select {
case <-done: case <-ctx.Done():
wg.Done() return nil
return
default: default:
count, err := os.Stdin.Read(buf) count, err := os.Stdin.Read(buf)
if err != nil { if err != nil {
errors <- err return err
wg.Done()
return
} }
if count == 0 { if count == 0 {
@ -82,42 +107,18 @@ func main() {
err = cobs.Verify(data, cobsConfig) err = cobs.Verify(data, cobsConfig)
if err != nil { if err != nil {
errors <- err return err
wg.Done()
return
} }
frame <- cobs.Decode(data, cobsConfig) c.responses <- cobs.Decode(data, cobsConfig)
} }
} }
}(done)
err := writeTestCommands(frame, errorLog)
if err != nil {
errorLog.Printf("could not write test commands")
} }
err = <-errors func (c *clientSimulator) handleCommands(ctx context.Context) error {
if err != nil { for {
errorLog.Printf("error: %v", err) select {
} case command := <-c.commands:
wg.Wait()
}
func writeTestCommands(responses chan []byte, errorLog *log.Logger) error {
messages.RegisterGeneratedResolver()
commands := []messages.Command{
{
Code: messages.CmdHealth,
TimeStamp: time.Now().UTC(),
},
}
for _, command := range commands {
commandBytes, err := msgpack.Marshal(command) commandBytes, err := msgpack.Marshal(command)
if err != nil { if err != nil {
return fmt.Errorf("could not marshal command bytes: %w", err) return fmt.Errorf("could not marshal command bytes: %w", err)
@ -128,7 +129,7 @@ func writeTestCommands(responses chan []byte, errorLog *log.Logger) error {
return fmt.Errorf("write failed: %w", err) return fmt.Errorf("write failed: %w", err)
} }
responseBytes := <-responses responseBytes := <-c.responses
var response messages.Response var response messages.Response
@ -137,8 +138,56 @@ func writeTestCommands(responses chan []byte, errorLog *log.Logger) error {
return fmt.Errorf("could not unmarshal msgpack data: %w", err) return fmt.Errorf("could not unmarshal msgpack data: %w", err)
} }
errorLog.Printf("received response: %+v", response) c.errorLog.Printf("received response: %+v", response)
} case <-ctx.Done():
return nil return nil
} }
}
}
func (c *clientSimulator) Run() error {
ctx, cancel := context.WithCancel(context.Background())
c.commands = make(chan messages.Command)
c.responses = make(chan []byte)
wg := sync.WaitGroup{}
wg.Add(2)
var inputError, commandError error
go func(inputErr error) {
inputError = c.handleInput(ctx)
cancel()
wg.Done()
}(inputError)
go func(commandErr error) {
commandErr = c.handleCommands(ctx)
cancel()
wg.Done()
}(commandError)
var result error
if err := c.writeTestCommands(ctx); err != nil {
c.errorLog.Printf("test commands failed: %v", err)
}
cancel()
wg.Wait()
if inputError != nil {
c.errorLog.Printf("reading input failed: %v", inputError)
}
if commandError != nil {
c.errorLog.Printf("sending commands failed: %v", commandError)
}
return result
}