-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GHO-2783 Make sender node parallelizable (#69)
* GHO-2783 Refactor nodes to use base type * GHO-2783 Make sender node parallelizable * linting * lint
- Loading branch information
Showing
22 changed files
with
1,302 additions
and
926 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package node | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/ghostsecurity/reaper/backend/workflow/transmission" | ||
"github.com/google/uuid" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type noInjections struct{} | ||
|
||
func (n noInjections) GetInjections() map[string]transmission.Transmission { | ||
return nil | ||
} | ||
|
||
type base struct { | ||
*VarStorage | ||
id uuid.UUID | ||
name string | ||
t Type | ||
readonly bool | ||
busy bool | ||
last time.Time | ||
busyMu sync.RWMutex | ||
} | ||
|
||
func newBase(name string, t Type, readonly bool, vars *VarStorage) *base { | ||
return &base{ | ||
VarStorage: vars, | ||
id: uuid.New(), | ||
name: name, | ||
t: t, | ||
readonly: readonly, | ||
} | ||
} | ||
|
||
func (b *base) Busy() bool { | ||
b.busyMu.RLock() | ||
defer b.busyMu.RUnlock() | ||
return b.busy | ||
} | ||
|
||
func (b *base) LastInput() time.Time { | ||
b.busyMu.RLock() | ||
defer b.busyMu.RUnlock() | ||
return b.last | ||
} | ||
|
||
func (b *base) setBusy(busy bool) { | ||
b.busyMu.Lock() | ||
defer b.busyMu.Unlock() | ||
b.busy = busy | ||
b.last = time.Now() | ||
} | ||
|
||
func (b *base) ID() uuid.UUID { | ||
return b.id | ||
} | ||
|
||
func (b *base) SetID(id uuid.UUID) { | ||
b.id = id | ||
} | ||
|
||
func (b *base) Name() string { | ||
return b.name | ||
} | ||
|
||
func (b *base) SetName(name string) { | ||
b.name = name | ||
} | ||
|
||
func (b *base) Type() Type { | ||
return b.t | ||
} | ||
|
||
func (b *base) IsReadOnly() bool { | ||
return b.readonly | ||
} | ||
|
||
func (b *base) GetVars() *VarStorage { | ||
return b.VarStorage | ||
} | ||
|
||
func (b *base) SetVars(vars *VarStorage) { | ||
b.VarStorage = vars | ||
} | ||
|
||
func (b *base) MergeVars(vars *VarStorage) { | ||
b.VarStorage.Merge(vars) | ||
} | ||
|
||
func (b *base) tryOut(ctx context.Context, out chan<- OutputInstance, instance OutputInstance) { | ||
select { | ||
case <-ctx.Done(): | ||
case out <- instance: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package node | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ghostsecurity/reaper/backend/workflow/transmission" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type DelayNode struct { | ||
*base | ||
noInjections | ||
} | ||
|
||
func NewDelay() *DelayNode { | ||
return &DelayNode{ | ||
base: newBase( | ||
"Delay", | ||
TypeDelay, | ||
false, | ||
NewVarStorage( | ||
Connectors{ | ||
NewConnector("input", transmission.TypeAny, true), | ||
NewConnector("delay", transmission.TypeInt, false, "in milliseconds"), | ||
}, | ||
Connectors{ | ||
NewConnector("output", transmission.TypeAny, true), | ||
}, | ||
map[string]transmission.Transmission{ | ||
"delay": transmission.NewInt(1000), | ||
}, | ||
), | ||
), | ||
} | ||
} | ||
|
||
func (n *DelayNode) Start(ctx context.Context, in <-chan Input, out chan<- OutputInstance, _ chan<- Output) error { | ||
|
||
delay, err := n.ReadInputInt("delay", nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer n.setBusy(false) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case input, ok := <-in: | ||
if !ok { | ||
return nil | ||
} | ||
|
||
n.setBusy(true) | ||
|
||
if input.Data == nil { | ||
return fmt.Errorf("input is nil") | ||
} | ||
|
||
raw, err := n.ReadValue("input", input.Data) | ||
if err != nil { | ||
return fmt.Errorf("input not found: %v", err) | ||
} | ||
|
||
time.Sleep(time.Duration(delay) * time.Millisecond) | ||
|
||
n.tryOut(ctx, out, OutputInstance{ | ||
OutputName: "output", | ||
Complete: input.Last, | ||
Data: raw, | ||
}) | ||
|
||
n.setBusy(false) | ||
} | ||
} | ||
} |
Oops, something went wrong.