forked from rosa/hakurei
cmd/mbf: optionally ignore reply
An acknowledgement is not always required in this use case. This change also adds 64 bits of connection configuration for future expansion. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -27,21 +28,28 @@ func daemonDeadline() time.Time {
|
|||||||
return time.Now().Add(daemonTimeout)
|
return time.Now().Add(daemonTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// remoteNoReply notifies that the client will not receive a cure reply.
|
||||||
|
remoteNoReply = 1 << iota
|
||||||
|
)
|
||||||
|
|
||||||
// cureFromIR services an IR curing request.
|
// cureFromIR services an IR curing request.
|
||||||
func cureFromIR(
|
func cureFromIR(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
cache *pkg.Cache,
|
cache *pkg.Cache,
|
||||||
conn net.Conn,
|
conn net.Conn,
|
||||||
) (pkg.Artifact, error) {
|
) (pkg.Artifact, error) {
|
||||||
go func() {
|
go func() { <-ctx.Done(); _ = conn.SetDeadline(time.Now()) }()
|
||||||
<-ctx.Done()
|
|
||||||
_ = conn.SetDeadline(time.Now())
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := conn.SetReadDeadline(daemonDeadline()); err != nil {
|
if err := conn.SetReadDeadline(daemonDeadline()); err != nil {
|
||||||
return nil, errors.Join(err, conn.Close())
|
return nil, errors.Join(err, conn.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var flagsWire [8]byte
|
||||||
|
if _, err := io.ReadFull(conn, flagsWire[:]); err != nil {
|
||||||
|
return nil, errors.Join(err, conn.Close())
|
||||||
|
}
|
||||||
|
flags := binary.LittleEndian.Uint64(flagsWire[:])
|
||||||
|
|
||||||
a, decodeErr := cache.NewDecoder(conn).Decode()
|
a, decodeErr := cache.NewDecoder(conn).Decode()
|
||||||
if decodeErr != nil {
|
if decodeErr != nil {
|
||||||
_, err := conn.Write([]byte("\x00" + decodeErr.Error()))
|
_, err := conn.Write([]byte("\x00" + decodeErr.Error()))
|
||||||
@@ -49,10 +57,11 @@ func cureFromIR(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pathname, _, cureErr := cache.Cure(a)
|
pathname, _, cureErr := cache.Cure(a)
|
||||||
|
if flags&remoteNoReply != 0 {
|
||||||
|
return a, errors.Join(cureErr, conn.Close())
|
||||||
|
}
|
||||||
if err := conn.SetWriteDeadline(daemonDeadline()); err != nil {
|
if err := conn.SetWriteDeadline(daemonDeadline()); err != nil {
|
||||||
if !testing.Testing() || !errors.Is(err, io.ErrClosedPipe) {
|
return a, errors.Join(cureErr, err, conn.Close())
|
||||||
return a, errors.Join(err, conn.Close())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if cureErr != nil {
|
if cureErr != nil {
|
||||||
_, err := conn.Write([]byte("\x00" + cureErr.Error()))
|
_, err := conn.Write([]byte("\x00" + cureErr.Error()))
|
||||||
@@ -66,7 +75,12 @@ func cureFromIR(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// serve services connections from a [net.UnixListener].
|
// serve services connections from a [net.UnixListener].
|
||||||
func serve(ctx context.Context, log *log.Logger, cm *cache, ul *net.UnixListener) error {
|
func serve(
|
||||||
|
ctx context.Context,
|
||||||
|
log *log.Logger,
|
||||||
|
cm *cache,
|
||||||
|
ul *net.UnixListener,
|
||||||
|
) error {
|
||||||
ul.SetUnlinkOnClose(true)
|
ul.SetUnlinkOnClose(true)
|
||||||
if cm.c == nil {
|
if cm.c == nil {
|
||||||
if err := cm.open(); err != nil {
|
if err := cm.open(); err != nil {
|
||||||
@@ -116,16 +130,19 @@ func cureRemote(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
addr *net.UnixAddr,
|
addr *net.UnixAddr,
|
||||||
a pkg.Artifact,
|
a pkg.Artifact,
|
||||||
|
flags uint64,
|
||||||
) (*check.Absolute, error) {
|
) (*check.Absolute, error) {
|
||||||
conn, err := net.DialUnix("unix", nil, addr)
|
conn, err := net.DialUnix("unix", nil, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
go func() { <-ctx.Done(); _ = conn.SetDeadline(time.Now()) }()
|
||||||
|
|
||||||
go func() {
|
if n, flagErr := conn.Write(binary.LittleEndian.AppendUint64(nil, flags)); flagErr != nil {
|
||||||
<-ctx.Done()
|
return nil, errors.Join(flagErr, conn.Close())
|
||||||
_ = conn.SetDeadline(time.Now())
|
} else if n != 8 {
|
||||||
}()
|
return nil, errors.Join(io.ErrShortWrite, conn.Close())
|
||||||
|
}
|
||||||
|
|
||||||
if err = pkg.NewIR().EncodeAll(conn, a); err != nil {
|
if err = pkg.NewIR().EncodeAll(conn, a); err != nil {
|
||||||
return nil, errors.Join(err, conn.Close())
|
return nil, errors.Join(err, conn.Close())
|
||||||
@@ -133,6 +150,10 @@ func cureRemote(
|
|||||||
return nil, errors.Join(err, conn.Close())
|
return nil, errors.Join(err, conn.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if flags&remoteNoReply != 0 {
|
||||||
|
return nil, conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
payload, recvErr := io.ReadAll(conn)
|
payload, recvErr := io.ReadAll(conn)
|
||||||
if err = errors.Join(recvErr, conn.Close()); err != nil {
|
if err = errors.Join(recvErr, conn.Close()); err != nil {
|
||||||
if errors.Is(err, os.ErrDeadlineExceeded) {
|
if errors.Is(err, os.ErrDeadlineExceeded) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -17,7 +18,7 @@ import (
|
|||||||
"hakurei.app/message"
|
"hakurei.app/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCureFromIR(t *testing.T) {
|
func TestNoReply(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
if !daemonDeadline().IsZero() {
|
if !daemonDeadline().IsZero() {
|
||||||
t.Fatal("daemonDeadline did not return the zero value")
|
t.Fatal("daemonDeadline did not return the zero value")
|
||||||
@@ -45,7 +46,11 @@ func TestCureFromIR(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if _err := c.EncodeAll(
|
if _, _err := client.Write(
|
||||||
|
binary.LittleEndian.AppendUint64(nil, remoteNoReply),
|
||||||
|
); _err != nil {
|
||||||
|
panic(_err)
|
||||||
|
} else if _err = c.EncodeAll(
|
||||||
client,
|
client,
|
||||||
pkg.NewFile("check", []byte{0}),
|
pkg.NewFile("check", []byte{0}),
|
||||||
); _err != nil {
|
); _err != nil {
|
||||||
@@ -105,7 +110,7 @@ func TestDaemon(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
var p *check.Absolute
|
var p *check.Absolute
|
||||||
p, err = cureRemote(ctx, &addr, pkg.NewFile("check", []byte{0}))
|
p, err = cureRemote(ctx, &addr, pkg.NewFile("check", []byte{0}), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cureRemote: error = %v", err)
|
t.Fatalf("cureRemote: error = %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -392,10 +392,11 @@ func main() {
|
|||||||
|
|
||||||
{
|
{
|
||||||
var (
|
var (
|
||||||
flagDump string
|
flagDump string
|
||||||
flagEnter bool
|
flagEnter bool
|
||||||
flagExport string
|
flagExport string
|
||||||
flagRemote bool
|
flagRemote bool
|
||||||
|
flagNoReply bool
|
||||||
)
|
)
|
||||||
c.NewCommand(
|
c.NewCommand(
|
||||||
"cure",
|
"cure",
|
||||||
@@ -474,8 +475,12 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
case flagRemote:
|
case flagRemote:
|
||||||
pathname, err := cureRemote(ctx, &addr, rosa.Std.Load(p))
|
var flags uint64
|
||||||
if err == nil {
|
if flagNoReply {
|
||||||
|
flags |= remoteNoReply
|
||||||
|
}
|
||||||
|
pathname, err := cureRemote(ctx, &addr, rosa.Std.Load(p), flags)
|
||||||
|
if !flagNoReply && err == nil {
|
||||||
log.Println(pathname)
|
log.Println(pathname)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@@ -497,6 +502,10 @@ func main() {
|
|||||||
&flagRemote,
|
&flagRemote,
|
||||||
"daemon", command.BoolFlag(false),
|
"daemon", command.BoolFlag(false),
|
||||||
"Cure artifact on the daemon",
|
"Cure artifact on the daemon",
|
||||||
|
).Flag(
|
||||||
|
&flagNoReply,
|
||||||
|
"no-reply", command.BoolFlag(false),
|
||||||
|
"Do not receive a reply from the daemon",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user