tiwatch

package module
v0.0.0-...-a3f186e Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

tiwatch

Watch API at scale, using TiDB as backend

Usage

package main

import (
	"flag"
	"math/rand"
	"time"

	"github.com/c4pt0r/log"
	"github.com/c4pt0r/tiwatch"
)

var (
	watchOnly = flag.Bool("watch-only", false, "watch only")
)

func main() {
	flag.Parse()
	dsn := "root:@tcp(localhost:4000)/test"
	w := tiwatch.New(dsn, "default")
	err := w.Init()
	if err != nil {
		panic(err)
	}

	ch := w.Watch("hello")

	if !*watchOnly {
		go func() {
			for {
				log.I("SEND")
				w.Set("hello", time.Now().String())
				time.Sleep(time.Second)

				if rand.Intn(2) == 0 {
					log.I("DEL")
					w.Delete("hello")
				}
			}
		}()
	}
	for {
		select {
		case m := <-ch:
			log.Infof("RECV: %s", m)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PollDuration time.Duration = time.Second
)

Functions

This section is empty.

Types

type Op

type Op struct {
	Type OpType
	Key  string
	Val  string
}

type OpType

type OpType int
const (
	TypeDelete OpType = iota
	TypeUpdate
)

type TiWatch

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

TiWatch, a PoC implementation of Etcd's important APIs: Watch, Get, Set The core idea is: 1. TiDB is a scalable database with **SQL** semantics. 2. TiDB supports secondary indexes. very fast lookup. 3. TiDB supports transactions, with pessimistic concurrency control 4. TiDB's pessimistic concurrency control is based on MVCC and pessimistic lock is in-memory 5. TiDB's lock is row-level, totally scalable. 6. TiDB uses multi-raft architecture to achieve strong consistency and auto-failover

func New

func New(dsn string, namespace string) *TiWatch

func (*TiWatch) Close

func (b *TiWatch) Close() error

func (*TiWatch) DB

func (b *TiWatch) DB() *sql.DB

func (*TiWatch) Delete

func (b *TiWatch) Delete(key string) error

func (*TiWatch) Get

func (b *TiWatch) Get(key string) (string, bool, error)

func (*TiWatch) Init

func (b *TiWatch) Init() error

func (*TiWatch) Set

func (b *TiWatch) Set(key string, value string) error

func (*TiWatch) Watch

func (b *TiWatch) Watch(key string) <-chan Op

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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