From fa846337a3ac0de8cd5baa784cbb1791c496a2a7 Mon Sep 17 00:00:00 2001 From: milarin Date: Wed, 10 Jan 2024 22:18:11 +0100 Subject: [PATCH] IsReady function implemented --- send_command.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/send_command.go b/send_command.go index 27455aa..cc566f2 100644 --- a/send_command.go +++ b/send_command.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "net" + "time" "git.milar.in/milarin/channel" ) @@ -27,6 +28,34 @@ type Event[T any] struct { Reason string `json:"reason"` } +func IsReady(ctx context.Context, socket string) <-chan bool { + out := make(chan bool, 1) + + go func() { + defer close(out) + + ticker := time.NewTicker(100 * time.Millisecond) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + conn, err := net.Dial("unix", socket) + if err == nil { + defer conn.Close() + out <- true + return + } + case <-ctx.Done(): + out <- false + return + } + } + }() + + return out +} + func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) { conn, err := net.Dial("unix", socket) if err != nil {