2023-07-12 22:37:31 +00:00
|
|
|
package tailnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"golang.org/x/xerrors"
|
2024-01-22 07:07:50 +00:00
|
|
|
|
|
|
|
"github.com/coder/coder/v2/tailnet/proto"
|
2023-07-12 22:37:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MultiAgentConn interface {
|
2024-01-22 07:07:50 +00:00
|
|
|
UpdateSelf(node *proto.Node) error
|
2023-07-12 22:37:31 +00:00
|
|
|
SubscribeAgent(agentID uuid.UUID) error
|
|
|
|
UnsubscribeAgent(agentID uuid.UUID) error
|
2024-01-22 07:07:50 +00:00
|
|
|
NextUpdate(ctx context.Context) (*proto.CoordinateResponse, bool)
|
2023-07-12 22:37:31 +00:00
|
|
|
AgentIsLegacy(agentID uuid.UUID) bool
|
|
|
|
Close() error
|
|
|
|
IsClosed() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiAgent struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
2023-07-19 16:11:11 +00:00
|
|
|
ID uuid.UUID
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
AgentIsLegacyFunc func(agentID uuid.UUID) bool
|
2024-01-22 07:07:50 +00:00
|
|
|
OnSubscribe func(enq Queue, agent uuid.UUID) error
|
2023-07-12 22:37:31 +00:00
|
|
|
OnUnsubscribe func(enq Queue, agent uuid.UUID) error
|
2024-01-22 07:07:50 +00:00
|
|
|
OnNodeUpdate func(id uuid.UUID, node *proto.Node) error
|
2023-09-21 19:30:48 +00:00
|
|
|
OnRemove func(enq Queue)
|
2023-07-12 22:37:31 +00:00
|
|
|
|
2023-09-21 19:30:48 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
2023-07-19 16:11:11 +00:00
|
|
|
closed bool
|
2023-09-21 19:30:48 +00:00
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
updates chan *proto.CoordinateResponse
|
2023-07-12 22:37:31 +00:00
|
|
|
closeOnce sync.Once
|
|
|
|
start int64
|
|
|
|
lastWrite int64
|
|
|
|
// Client nodes normally generate a unique id for each connection so
|
|
|
|
// overwrites are really not an issue, but is provided for compatibility.
|
|
|
|
overwrites int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) Init() *MultiAgent {
|
2024-01-22 07:07:50 +00:00
|
|
|
m.updates = make(chan *proto.CoordinateResponse, 128)
|
2023-07-12 22:37:31 +00:00
|
|
|
m.start = time.Now().Unix()
|
2023-09-21 19:30:48 +00:00
|
|
|
m.ctx, m.ctxCancel = context.WithCancel(context.Background())
|
2023-07-12 22:37:31 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-09-21 19:30:48 +00:00
|
|
|
func (*MultiAgent) Kind() QueueKind {
|
|
|
|
return QueueKindClient
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (m *MultiAgent) UniqueID() uuid.UUID {
|
|
|
|
return m.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) AgentIsLegacy(agentID uuid.UUID) bool {
|
|
|
|
return m.AgentIsLegacyFunc(agentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrMultiAgentClosed = xerrors.New("multiagent is closed")
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
func (m *MultiAgent) UpdateSelf(node *proto.Node) error {
|
2023-07-12 22:37:31 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
if m.closed {
|
|
|
|
return ErrMultiAgentClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.OnNodeUpdate(m.ID, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) SubscribeAgent(agentID uuid.UUID) error {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
if m.closed {
|
|
|
|
return ErrMultiAgentClosed
|
|
|
|
}
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
err := m.OnSubscribe(m, agentID)
|
2023-07-12 22:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) UnsubscribeAgent(agentID uuid.UUID) error {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
if m.closed {
|
|
|
|
return ErrMultiAgentClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.OnUnsubscribe(m, agentID)
|
|
|
|
}
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
func (m *MultiAgent) NextUpdate(ctx context.Context) (*proto.CoordinateResponse, bool) {
|
2023-07-12 22:37:31 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, false
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
case resp, ok := <-m.updates:
|
|
|
|
return resp, ok
|
2023-07-12 22:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
func (m *MultiAgent) Enqueue(resp *proto.CoordinateResponse) error {
|
2023-07-12 22:37:31 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
if m.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
return m.enqueueLocked(resp)
|
2023-07-12 22:37:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 07:07:50 +00:00
|
|
|
func (m *MultiAgent) enqueueLocked(resp *proto.CoordinateResponse) error {
|
2023-07-12 22:37:31 +00:00
|
|
|
atomic.StoreInt64(&m.lastWrite, time.Now().Unix())
|
|
|
|
|
|
|
|
select {
|
2024-01-22 07:07:50 +00:00
|
|
|
case m.updates <- resp:
|
2023-07-12 22:37:31 +00:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return ErrWouldBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) Name() string {
|
|
|
|
return m.ID.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) Stats() (start int64, lastWrite int64) {
|
|
|
|
return m.start, atomic.LoadInt64(&m.lastWrite)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) Overwrites() int64 {
|
|
|
|
return m.overwrites
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) IsClosed() bool {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiAgent) CoordinatorClose() error {
|
|
|
|
m.mu.Lock()
|
|
|
|
if !m.closed {
|
|
|
|
m.closed = true
|
|
|
|
close(m.updates)
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-21 19:30:48 +00:00
|
|
|
func (m *MultiAgent) Done() <-chan struct{} {
|
|
|
|
return m.ctx.Done()
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (m *MultiAgent) Close() error {
|
|
|
|
_ = m.CoordinatorClose()
|
2023-09-21 19:30:48 +00:00
|
|
|
m.ctxCancel()
|
|
|
|
m.closeOnce.Do(func() { m.OnRemove(m) })
|
2023-07-12 22:37:31 +00:00
|
|
|
return nil
|
|
|
|
}
|