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
import (
"context"
"fmt"
"log"
"os"
"sync"
"time"
"git.cacert.org/cacert-gosigner/pkg/messages"
"github.com/justincpresley/go-cobs"
"github.com/shamaton/msgpackgen/msgpack"
"git.cacert.org/cacert-gosigner/pkg/messages"
)
const cobsDelimiter = 0x00
@ -37,108 +37,157 @@ const cobsDelimiter = 0x00
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
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 (
bufferSize = 1024 * 1024
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.Add(1)
wg.Add(2)
done := make(chan struct{})
frame := make(chan []byte)
var inputError, commandError error
go func(done chan struct{}) {
buf := make([]byte, bufferSize)
go func(inputErr error) {
inputError = c.handleInput(ctx)
for {
select {
case <-done:
wg.Done()
cancel()
return
wg.Done()
}(inputError)
default:
count, err := os.Stdin.Read(buf)
if err != nil {
errors <- err
go func(commandErr error) {
commandErr = c.handleCommands(ctx)
wg.Done()
cancel()
return
}
wg.Done()
}(commandError)
if count == 0 {
time.Sleep(readInterval)
var result error
continue
}
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)
if err := c.writeTestCommands(ctx); err != nil {
c.errorLog.Printf("test commands failed: %v", err)
}
cancel()
wg.Wait()
}
func writeTestCommands(responses chan []byte, errorLog *log.Logger) error {
messages.RegisterGeneratedResolver()
commands := []messages.Command{
{
Code: messages.CmdHealth,
TimeStamp: time.Now().UTC(),
},
if inputError != nil {
c.errorLog.Printf("reading input failed: %v", inputError)
}
for _, command := range 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 := <-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)
if commandError != nil {
c.errorLog.Printf("sending commands failed: %v", commandError)
}
return nil
return result
}