limit plugin api connections
This commit is contained in:
parent
2a40999782
commit
fc81ebe650
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/Neur0toxine/sshpoke/internal/config"
|
"github.com/Neur0toxine/sshpoke/internal/config"
|
||||||
"github.com/Neur0toxine/sshpoke/internal/logger"
|
"github.com/Neur0toxine/sshpoke/internal/logger"
|
||||||
@ -12,12 +13,15 @@ import (
|
|||||||
"github.com/Neur0toxine/sshpoke/internal/server/driver/util"
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrAlreadyConnected = errors.New("already connected")
|
||||||
|
|
||||||
// Plugin driver uses RPC to communicate with external plugin.
|
// Plugin driver uses RPC to communicate with external plugin.
|
||||||
type Plugin struct {
|
type Plugin struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
name string
|
name string
|
||||||
params Params
|
params Params
|
||||||
send *Queue[model.Event]
|
send *Queue[model.Event]
|
||||||
|
listening atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventStream interface {
|
type EventStream interface {
|
||||||
@ -57,6 +61,11 @@ func (d *Plugin) Token() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Plugin) Listen(ctx context.Context, stream EventStream) error {
|
func (d *Plugin) Listen(ctx context.Context, stream EventStream) error {
|
||||||
|
if d.listening.Load() {
|
||||||
|
return ErrAlreadyConnected
|
||||||
|
}
|
||||||
|
d.listening.Store(true)
|
||||||
|
defer d.listening.Store(false)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
Loading…
Reference in New Issue
Block a user