From 2074b583996e95f7ba1be3e66b117e66d0a7bb48 Mon Sep 17 00:00:00 2001 From: Vladimir Avtsenov Date: Fri, 30 Aug 2024 04:29:05 +0300 Subject: [PATCH] check instance is running --- kvas2.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kvas2.go b/kvas2.go index 09ee134..fc5d526 100644 --- a/kvas2.go +++ b/kvas2.go @@ -15,6 +15,7 @@ import ( ) var ( + ErrAlreadyRunning = errors.New("already running") ErrGroupIDConflict = errors.New("group id conflict") ) @@ -32,9 +33,17 @@ type App struct { DNSOverrider *iptablesHelper.DNSOverrider Records *Records Groups map[int]*Group + + isRunning bool } func (a *App) Listen(ctx context.Context) []error { + if a.isRunning { + return []error{ErrAlreadyRunning} + } + a.isRunning = true + defer func() { a.isRunning = false }() + errs := make([]error, 0) isError := make(chan struct{}) @@ -158,6 +167,13 @@ func (a *App) AppendGroup(group *models.Group) error { Group: group, } + if a.isRunning { + err := a.Groups[group.ID].Enable() + if err != nil { + return fmt.Errorf("failed to enable appended group: %w", err) + } + } + return nil }