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,108 +37,157 @@ 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) buf := make([]byte, bufferSize)
errorLog := log.New(os.Stderr, "", log.LstdFlags) for {
select {
case <-ctx.Done():
return nil
default:
count, err := os.Stdin.Read(buf)
if err != nil {
return err
}
if count == 0 {
time.Sleep(readInterval)
continue
}
data := buf[:count]
err = cobs.Verify(data, cobsConfig)
if err != nil {
return err
}
c.responses <- cobs.Decode(data, cobsConfig)
}
}
}
func (c *clientSimulator) handleCommands(ctx context.Context) error {
for {
select {
case command := <-c.commands:
commandBytes, err := msgpack.Marshal(command)
if err != nil {
return fmt.Errorf("could not marshal command bytes: %w", err)
}
_, err = os.Stdout.Write(cobs.Encode(commandBytes, cobsConfig))
if err != nil {
return fmt.Errorf("write failed: %w", err)
}
responseBytes := <-c.responses
var response messages.Response
err = msgpack.Unmarshal(responseBytes, &response)
if err != nil {
return fmt.Errorf("could not unmarshal msgpack data: %w", err)
}
c.errorLog.Printf("received response: %+v", response)
case <-ctx.Done():
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 := sync.WaitGroup{}
wg.Add(1) wg.Add(2)
done := make(chan struct{}) var inputError, commandError error
frame := make(chan []byte)
go func(done chan struct{}) { go func(inputErr error) {
buf := make([]byte, bufferSize) inputError = c.handleInput(ctx)
for { cancel()
select {
case <-done:
wg.Done()
return wg.Done()
}(inputError)
default: go func(commandErr error) {
count, err := os.Stdin.Read(buf) commandErr = c.handleCommands(ctx)
if err != nil {
errors <- err
wg.Done() cancel()
return wg.Done()
} }(commandError)
if count == 0 { var result error
time.Sleep(readInterval)
continue if err := c.writeTestCommands(ctx); err != nil {
} c.errorLog.Printf("test commands failed: %v", err)
data := buf[:count]
err = cobs.Verify(data, cobsConfig)
if err != nil {
errors <- err
wg.Done()
return
}
frame <- cobs.Decode(data, cobsConfig)
}
}
}(done)
err := writeTestCommands(frame, errorLog)
if err != nil {
errorLog.Printf("could not write test commands")
}
err = <-errors
if err != nil {
errorLog.Printf("error: %v", err)
} }
cancel()
wg.Wait() wg.Wait()
}
func writeTestCommands(responses chan []byte, errorLog *log.Logger) error { if inputError != nil {
messages.RegisterGeneratedResolver() c.errorLog.Printf("reading input failed: %v", inputError)
commands := []messages.Command{
{
Code: messages.CmdHealth,
TimeStamp: time.Now().UTC(),
},
} }
for _, command := range commands { if commandError != nil {
commandBytes, err := msgpack.Marshal(command) c.errorLog.Printf("sending commands failed: %v", commandError)
if err != nil {
return fmt.Errorf("could not marshal command bytes: %w", err)
}
_, err = os.Stdout.Write(cobs.Encode(commandBytes, cobsConfig))
if err != nil {
return fmt.Errorf("write failed: %w", err)
}
responseBytes := <-responses
var response messages.Response
err = msgpack.Unmarshal(responseBytes, &response)
if err != nil {
return fmt.Errorf("could not unmarshal msgpack data: %w", err)
}
errorLog.Printf("received response: %+v", response)
} }
return nil return result
} }