session

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2024 License: Apache-2.0 Imports: 52 Imported by: 0

Documentation

Index

Constants

View Source
const ApiRequestMaxDuration = time.Minute

Variables

View Source
var ApiRouter = http.NewServeMux()

Functions

func CreateAPIHost

func CreateAPIHost(session *Session) *vhttp.VirtualHost

func GetManifestPathFromArgs added in v0.2.0

func GetManifestPathFromArgs(args []string) string

func Match

func Match[T any, U any](path string, cb func(*Session, *http.Request, T) (U, error))

func MatchLocked

func MatchLocked[T any, U any](path string, cb func(*Session, *http.Request, T) (U, error))

func OpenAndServe added in v0.2.0

func OpenAndServe(manifestPath string)

func Run added in v0.2.0

func Run(args []string)

func ServeRPC

func ServeRPC(originalReq *http.Request, method string, body json.RawMessage) (out json.RawMessage, err error)

Forwards to serve HTTP

Types

type HostsLine

type HostsLine struct {
	Hostname string
	IP       string
}

func (*HostsLine) UnmarshalYAML

func (h *HostsLine) UnmarshalYAML(node *yaml.Node) error

type IPInfoOptions

type IPInfoOptions struct {
	Disable    bool     `yaml:"disable,omitempty"`
	MaxmindKey string   `yaml:"maxmind,omitempty"`
	Mark       []string `yaml:"mark,omitempty"`
}

func (IPInfoOptions) CreateProvider

func (i IPInfoOptions) CreateProvider() (info netx.IPInfoProvider)

type IPInfoQuery

type IPInfoQuery struct {
	IP string `json:"ip"`
}

type IPInfoResult

type IPInfoResult struct {
	IP      string `json:"ip"`
	ASN     uint32 `json:"asn"`
	Desc    string `json:"desc"`
	Country string `json:"country"`
	Flags   uint32 `json:"flags"`
}

type JetManifest

type JetManifest struct {
	Streams map[string]JetStreamManifest           `yaml:"streams,omitempty"`
	KV      map[string]jetstream.KeyValueConfig    `yaml:"kv,omitempty"`
	Obj     map[string]jetstream.ObjectStoreConfig `yaml:"obj,omitempty"`
}

func (*JetManifest) Init

type JetStreamManifest

type JetStreamManifest struct {
	Stream    jetstream.StreamConfig              `yaml:"stream"`
	Consumers map[string]jetstream.ConsumerConfig `yaml:"consumers"`
}

func (*JetStreamManifest) Init

type KVCompareAndSwap

type KVCompareAndSwap struct {
	Expected json.RawMessage `json:"expected"`
	Desired  json.RawMessage `json:"desired"`
}

type KVCompareAndSwapResult

type KVCompareAndSwapResult struct {
	OK       bool            `json:"ok"`
	Diff     json.RawMessage `json:"diff"`
	Revision uint64          `json:"revision"`
	Value    json.RawMessage `json:"value"`
}

type KVSetIf

type KVSetIf struct {
	Revision uint64          `json:"revision"`
	Value    json.RawMessage `json:"value"`
}

type KVState

type KVState struct {
	Revision uint64          `json:"revision"`
	Value    json.RawMessage `json:"value"`
}

type Lambda

type Lambda struct {
	ID        string      `json:"id"`
	Timestamp time.Time   `json:"timestamp"`
	Code      string      `json:"code"`
	Headers   http.Header `json:"headers"`
	// contains filtered or unexported fields
}

type LambdaID

type LambdaID struct {
	MachineID config.MachineID
	UID       snowflake.ID
}

func NewLambdaID

func NewLambdaID() LambdaID

func (LambdaID) IsZero

func (l LambdaID) IsZero() bool

func (LambdaID) MarshalBinary

func (l LambdaID) MarshalBinary() ([]byte, error)

func (LambdaID) MarshalText

func (l LambdaID) MarshalText() ([]byte, error)

func (LambdaID) Peer

func (l LambdaID) Peer(session *Session) *xpost.Peer

func (LambdaID) String

func (l LambdaID) String() string

func (*LambdaID) UnmarshalBinary

func (l *LambdaID) UnmarshalBinary(data []byte) error

func (*LambdaID) UnmarshalText

func (l *LambdaID) UnmarshalText(data []byte) error

type LockedHandler

type LockedHandler struct {
	Handler http.Handler
}

func (LockedHandler) ServeHTTP

func (h LockedHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Manifest

type Manifest struct {
	Root         string                              `yaml:"root,omitempty"`          // Root directory
	ServiceRoot  string                              `yaml:"service_root,omitempty"`  // Service root directory
	Services     OrderedMap[string, service.Service] `yaml:"services,omitempty"`      // Services
	Server       map[string]*Server                  `yaml:"server,omitempty"`        // Virtual hosts
	IPInfo       IPInfoOptions                       `yaml:"ipinfo,omitempty"`        // IP information provider
	Env          map[string]string                   `yaml:"env,omitempty"`           // Environment variables
	Runners      map[string]*Runner                  `yaml:"runners,omitempty"`       // Runners
	Jet          JetManifest                         `yaml:"jet,omitempty"`           // JetStream configuration
	Hosts        []HostsLine                         `yaml:"hosts,omitempty"`         // Hostname to IP mapping
	CustomErrors string                              `yaml:"custom_errors,omitempty"` // Path to custom error pages
}

func LoadManifest

func LoadManifest(manifestPath string) (*Manifest, error)

type OrderedMap

type OrderedMap[K comparable, V any] []lo.Tuple2[K, V]

func (OrderedMap[K, V]) ForEach

func (m OrderedMap[K, V]) ForEach(fn func(k K, v V))

func (OrderedMap[K, V]) Get

func (m OrderedMap[K, V]) Get(key K) (v V, ok bool)

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V)

func (*OrderedMap[K, V]) UnmarshalYAML

func (m *OrderedMap[K, V]) UnmarshalYAML(node *yaml.Node) error

type PullResult

type PullResult struct {
	From    revision.Reference `json:"from"`    // From reference
	To      revision.Reference `json:"to"`      // To reference
	Changed bool               `json:"changed"` // True if the repository was changed
}

type RepoInfo

type RepoInfo struct {
	Sys    string             `json:"sys"`    // Repository system
	Ref    revision.Reference `json:"ref"`    // Repository reference
	Remote revision.Reference `json:"remote"` // Remote repository reference
	URL    string             `json:"url"`    // Remote repository URL
}

type Runner

type Runner struct {
	Route    vhttp.HandleMux   `yaml:"route,omitempty"`     // HTTP route for the task
	Content  string            `yaml:"content,omitempty"`   // Content type for the task
	Method   string            `yaml:"method,omitempty"`    // HTTP method for the task
	Schedule []ScheduledRunner `yaml:"schedule,omitempty"`  // Schedule for the task
	Timeout  util.Duration     `yaml:"timeout,omitempty"`   // Timeout for the task
	NakDelay util.Duration     `yaml:"nak_delay,omitempty"` // Delay before NAKing a message
	Rate     rate.Rate         `yaml:"rate,omitempty"`      // Rate limit for the task
	Serial   bool              `yaml:"serial,omitempty"`    // Process messages serially
	Oneshot  bool              `yaml:"oneshot,omitempty"`   // Terminate after the first message
	NoMeta   bool              `yaml:"no_meta,omitempty"`   // Do not include metadata in the request
	Verbose  bool              `yaml:"verbose,omitempty"`   // Log verbose messages
}

func (*Runner) ConsumeContext

func (t *Runner) ConsumeContext(ctx context.Context, cns jetstream.Consumer) error

func (*Runner) CreateRequest

func (t *Runner) CreateRequest(ctx context.Context, subject string, data []byte, meta *jetstream.MsgMetadata) (request *http.Request, err error)

func (*Runner) Listen

func (t *Runner) Listen(ctx context.Context, gw *enats.Gateway, topic string) (context.CancelFunc, error)

func (*Runner) Serve

func (t *Runner) Serve(ctx context.Context, msg jetstream.Msg)

func (*Runner) ServeMsg

func (t *Runner) ServeMsg(ctx context.Context, subject string, data []byte, meta *jetstream.MsgMetadata) (err error)

func (*Runner) ServeNow

func (t *Runner) ServeNow(ctx context.Context, msg jetstream.Msg)

type ScheduledRunner

type ScheduledRunner struct {
	Interval util.Duration `yaml:"interval,omitempty"`
	Topic    string        `yaml:"topic,omitempty"`
	Payload  any           `yaml:"payload,omitempty"`
}

func (*ScheduledRunner) Run

func (sch *ScheduledRunner) Run(ctx context.Context, idx int, gw *enats.Gateway, topic, queueName string)

type Server

type Server struct {
	vhttp.VirtualHostOptions `yaml:",inline"`
	Router                   vhttp.HandleMux `yaml:"router,omitempty"`
}

func (*Server) CreateVirtualHost

func (sv *Server) CreateVirtualHost() *vhttp.VirtualHost

type ServiceCommandResult

type ServiceCommandResult struct {
	Count int `json:"count"`
}

type ServiceEntry

type ServiceEntry struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type ServiceHealth

type ServiceHealth struct {
	Status  string `json:"status"`        // Status
	Healthy int    `json:"healthy"`       // Number of healthy instances
	Total   int    `json:"total"`         // Total number of instances
	Err     string `json:"err,omitempty"` // Error message
}

func (*ServiceHealth) Fill

func (h *ServiceHealth) Fill(sv *ServiceState)

type ServiceInfo

type ServiceInfo struct {
	ServiceMetrics
	ServiceHealth
}

func (*ServiceInfo) Fill

func (m *ServiceInfo) Fill(sv *ServiceState)

type ServiceInvalidate

type ServiceInvalidate struct {
	Invalidate bool `json:"invalidate"`
}

type ServiceList

type ServiceList struct {
	Services []ServiceEntry `json:"services"`
}

type ServiceMetrics

type ServiceMetrics struct {
	ID        snowflake.ID              `json:"id"`
	Type      string                    `json:"type"`
	Server    lb.LoadBalancerMetrics    `json:"server"`
	Processes []service.ProcTreeMetrics `json:"processes"`
}

func (*ServiceMetrics) Fill

func (m *ServiceMetrics) Fill(sv *ServiceState)

type ServiceState

type ServiceState struct {
	service.Instance

	ID snowflake.ID
	// contains filtered or unexported fields
}

func (*ServiceState) Err

func (s *ServiceState) Err() error

func (*ServiceState) GetLoadBalancer

func (s *ServiceState) GetLoadBalancer() (*lb.LoadBalancer, bool)

func (*ServiceState) GetProcessTrees

func (s *ServiceState) GetProcessTrees() ([]service.ProcessTree, bool)

func (*ServiceState) Shutdown

func (s *ServiceState) Shutdown(ctx context.Context)

func (*ServiceState) Stop

func (s *ServiceState) Stop()

type Session

type Session struct {
	ID      snowflake.ID
	Context context.Context
	Cancel  context.CancelFunc
	Server  *vhttp.Server

	Nats     *enats.Gateway
	Peerlist *xpost.Peerlist

	ManifestPath      string // immut
	ServiceMap        concurrent.Map[string, *ServiceState]
	TaskSubscriptions []context.CancelFunc
	util.TimedMutex
	// contains filtered or unexported fields
}

func New

func New(path string) (s *Session, err error)

func RequestSession

func RequestSession(r *http.Request) *Session

func (*Session) Close

func (s *Session) Close() error

func (*Session) Manifest

func (s *Session) Manifest() *Manifest

func (*Session) Open

func (s *Session) Open(ctx context.Context) error

func (*Session) Reload

func (s *Session) Reload(invalidate bool) error

func (*Session) ReloadLocked

func (s *Session) ReloadLocked(invalidate bool) error

func (*Session) ResolveNats

func (s *Session) ResolveNats() *enats.Client

func (*Session) ResolveService

func (s *Session) ResolveService(sv string) vhttp.Handler

func (*Session) RestartService

func (s *Session) RestartService(match *string, invalidate bool) int

func (*Session) Shutdown

func (s *Session) Shutdown(ctx context.Context)

func (*Session) StartService

func (s *Session) StartService(name string, sv service.Service, invalidate bool) (*ServiceState, error)

func (*Session) StopService

func (s *Session) StopService(match *string) int

type SessionMetrics

type SessionMetrics struct {
	NumClients int                            `json:"num_clients"`
	Clients    map[string]vhttp.ClientMetrics `json:"sessions"`
}

type SystemMetrics

type SystemMetrics struct {
	MachineID            string             `json:"machine_id"`
	CPU                  cpu.InfoStat       `json:"cpu"`
	Load                 float64            `json:"load"`
	Rx                   float64            `json:"rx"`
	Tx                   float64            `json:"tx"`
	FreeDisk             uint64             `json:"freedisk"`
	FreeMem              uint64             `json:"freemem"`
	TotalDisk            uint64             `json:"totaldisk"`
	TotalMem             uint64             `json:"totalmem"`
	Uptime               uint64             `json:"uptime"` // seconds
	Hostname             string             `json:"hostname"`
	UID                  string             `json:"uid"`
	ProcessCount         uint64             `json:"process_count"`
	OS                   string             `json:"os"`
	KernelVersion        string             `json:"kernel_version"`
	KernelArch           string             `json:"kernel_arch"`
	VirtualizationSystem string             `json:"virtualization_system"`
	VirtualizationRole   string             `json:"virtualization_role"`
	RTT                  map[string]float64 `json:"rtt"`
}

func GetSystemMetrics

func GetSystemMetrics(session *Session) (m SystemMetrics)

type TypedHandler

type TypedHandler[T any, U any] struct {
	Callback func(*Session, *http.Request, T) (U, error)
}

Handler: func(arg T, req *http.Request) (result U, err error)

func (TypedHandler[T, U]) ServeHTTP

func (h TypedHandler[T, U]) ServeHTTP(w http.ResponseWriter, req *http.Request)

type UpdateParams

type UpdateParams struct {
	Invalidate bool `json:"invalidate"` // True if the session should be invalidated
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL