kv

package module
v12.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: ISC Imports: 21 Imported by: 1

README

Kilovolt

Websocket-based Key-value store, can use many databases on the backend. Has a slim set of features (get/set/pub/sub), mostly what's needed for strimertul.

Drivers

To use kilovolt, you will need a database driver.

Official drivers exist for the current databases:

Database Driver module
Pebble strimertul/kilovolt-driver-pebble
BadgerDB strimertul/kv-badgerdb (deprecated)

If you have built a driver, feel free to submit a just send a patch request to strimertul-devel or email me to have it added to this README!

Go mod and git.sr.ht

Due to Google's aggressive behavior and refusal to conform to standard internet "don't be a dick" code (aka DDoSsing), you will need to bypass GOPROXY to be able to clone this repository, like this:

export GOPRIVATE=git.sr.ht

Clients

We maintain a few libraries to interact with Kilovolt, you can find a list in the wiki.

If you don't find one that suits you, just write one yourself, I promise it's really simple! See PROTOCOL.md for all you'll need to implement to make it work.

License

Most of the code here is based on Gorilla Websocket's chat example, which is licensed under BSD-2-Clause (see LICENSE-gorilla).

The entire project is licensed under ISC (see LICENSE).

Documentation

Index

Constants

View Source
const (
	CmdProtoVersion      = "version"
	CmdReadKey           = "kget"
	CmdReadBulk          = "kget-bulk"
	CmdReadPrefix        = "kget-all"
	CmdWriteKey          = "kset"
	CmdWriteBulk         = "kset-bulk"
	CmdRemoveKey         = "kdel"
	CmdSubscribeKey      = "ksub"
	CmdSubscribePrefix   = "ksub-prefix"
	CmdUnsubscribeKey    = "kunsub"
	CmdUnsubscribePrefix = "kunsub-prefix"
	CmdListKeys          = "klist"
	CmdAuthRequest       = "klogin"
	CmdAuthChallenge     = "kauth"
	CmdInternalClientID  = "_uid"
)

Commands

View Source
const ProtoVersion = "v10"

Variables

View Source
var ErrClientNotFound = errors.New("client not found")
View Source
var (
	ErrorKeyNotFound = errors.New("key not found")
)

Functions

func MakeBackend

func MakeBackend() *mapkv

Types

type AuthType

type AuthType string
const (
	AuthTypeChallenge   AuthType = "challenge"
	AuthTypeInteractive AuthType = "ask"
)

type Client

type Client interface {
	Options() ClientOptions
	Close()

	SendMessage([]byte)
	SendJSON(any)

	SetUID(int64)
	UID() int64
}

Client is a middleman between the websocket connection and the hub.

type ClientOptions

type ClientOptions struct {
	// Adds a prefix to all key operations to restrict them to a namespace
	Namespace string

	Logger *slog.Logger
}

ClientOptions is a list of tunable options for clients

type Driver

type Driver interface {
	Get(key string) (string, error)
	GetBulk(keys []string) (map[string]string, error)
	GetPrefix(prefix string) (map[string]string, error)
	Set(key string, value string) error
	SetBulk(kv map[string]string) error
	Delete(key string) error
	List(prefix string) ([]string, error)
}

type ErrCode

type ErrCode string
const (
	ErrServerError      ErrCode = "server error"
	ErrInvalidFmt       ErrCode = "invalid message format"
	ErrMissingParam     ErrCode = "required parameter missing"
	ErrUnknownCmd       ErrCode = "unknown command"
	ErrAuthNotInit      ErrCode = "authentication not initialized"
	ErrAuthFailed       ErrCode = "authentication failed"
	ErrAuthRequired     ErrCode = "authentication required"
	ErrAuthNotRequired  ErrCode = "authentication not required"
	ErrAuthNotSupported ErrCode = "authentication method not supported"
)

type Error

type Error struct {
	Ok        bool    `json:"ok"`
	Error     ErrCode `json:"error"`
	Details   string  `json:"details"`
	RequestID string  `json:"request_id,omitempty"`
}

type Hello

type Hello struct {
	CmdType string `json:"type"`
	Version string `json:"version"`
}

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

func NewHub

func NewHub(db Driver, options HubOptions) (*Hub, error)

func (*Hub) AddClient

func (hub *Hub) AddClient(client Client)

func (*Hub) Close

func (hub *Hub) Close()

func (*Hub) CreateWebsocketClient

func (hub *Hub) CreateWebsocketClient(w http.ResponseWriter, r *http.Request, options ClientOptions)

CreateWebsocketClient upgrades a HTTP request to websocket and makes it a client for the hub

func (*Hub) RemoveClient

func (hub *Hub) RemoveClient(client Client)

func (*Hub) Run

func (hub *Hub) Run()

func (*Hub) SendMessage

func (hub *Hub) SendMessage(msg Message)

func (*Hub) SetAuthenticated

func (hub *Hub) SetAuthenticated(id int64, authenticated bool) error

func (*Hub) SetOptions

func (hub *Hub) SetOptions(options HubOptions)

func (*Hub) UseInteractiveAuth

func (hub *Hub) UseInteractiveAuth(fn InteractiveFn)

type HubOptions

type HubOptions struct {
	Password string
	Context  context.Context
	Logger   *slog.Logger
}

type InteractiveFn

type InteractiveFn func(client Client, message map[string]interface{}) bool

type LocalClient

type LocalClient struct {
	// contains filtered or unexported fields
}

func NewLocalClient

func NewLocalClient(options ClientOptions) *LocalClient

func (*LocalClient) Close

func (c *LocalClient) Close()

func (*LocalClient) MakeRequest

func (c *LocalClient) MakeRequest(cmd string, data map[string]any) (Message, <-chan any)

func (*LocalClient) Options

func (c *LocalClient) Options() ClientOptions

func (*LocalClient) Run

func (c *LocalClient) Run()

func (*LocalClient) SendJSON

func (c *LocalClient) SendJSON(data any)

func (*LocalClient) SendMessage

func (c *LocalClient) SendMessage(data []byte)

func (*LocalClient) SetKeySubCallback

func (c *LocalClient) SetKeySubCallback(key string, callback SubscriptionCallback) int64

func (*LocalClient) SetPrefixSubCallback

func (c *LocalClient) SetPrefixSubCallback(key string, callback SubscriptionCallback) int64

func (*LocalClient) SetUID

func (c *LocalClient) SetUID(uid int64)

func (*LocalClient) UID

func (c *LocalClient) UID() int64

func (*LocalClient) UnsetCallback

func (c *LocalClient) UnsetCallback(id int64)

func (*LocalClient) Wait

func (c *LocalClient) Wait()

type Message

type Message struct {
	Client Client
	Data   []byte
}

type Push

type Push struct {
	CmdType  string `json:"type"`
	Key      string `json:"key"`
	NewValue string `json:"new_value"`
}

type Request

type Request struct {
	CmdName   string                 `json:"command"`
	RequestID string                 `json:"request_id,omitempty"`
	Data      map[string]interface{} `json:"data"`
}

type Response

type Response struct {
	CmdType   string      `json:"type"`
	Ok        bool        `json:"ok"`
	RequestID string      `json:"request_id,omitempty"`
	Data      interface{} `json:"data,omitempty"`
}

type SubscriptionCallback

type SubscriptionCallback func(key string, value string)

type WebsocketClient

type WebsocketClient struct {
	// contains filtered or unexported fields
}

func (*WebsocketClient) Close

func (c *WebsocketClient) Close()

func (*WebsocketClient) Options

func (c *WebsocketClient) Options() ClientOptions

func (*WebsocketClient) SendJSON

func (c *WebsocketClient) SendJSON(data any)

func (*WebsocketClient) SendMessage

func (c *WebsocketClient) SendMessage(data []byte)

func (*WebsocketClient) SetUID

func (c *WebsocketClient) SetUID(uid int64)

func (*WebsocketClient) UID

func (c *WebsocketClient) UID() int64

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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