ghit

package module
v0.0.0-...-0bc71fc Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: MIT Imports: 11 Imported by: 0

README

ghit

import "github.com/chg1f/ghit"

Index

Constants

const (
    DefaultNodesKeySuffix = "NODES"
    DefaultNodeExpire     = time.Hour
)

Variables

var (
    EnableLimit = true

    DefaultKeyQPSSuffix = ":QPS"
)
var (
    NodeID string
)

type Hitter

type Hitter struct {
    *Node
    // contains filtered or unexported fields
}
Example

{
	hitter, err := NewHitter(&HitterOption{
		NodeOption: &NodeOption{
			Redis:     redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
			KeyPrefix: "ghit:",
		},
		ExpireSpec:     "@midnight",
		QPSShakeCoeff:  2,
		FollowInterval: time.Hour * 24,
		Key:            "run",
		EnableLimit:    true,
		Limit:          1,
	})
	if err != nil {
		panic(err)
	}
	defer hitter.Close()
	if ok, err := hitter.Hit(); err != nil {

	} else if !ok {
		return
	}
}

func NewHitter
func NewHitter(opt *HitterOption) (*Hitter, error)
func (*Hitter) Background
func (h *Hitter) Background()
func (*Hitter) Close
func (h *Hitter) Close() error
func (*Hitter) Hit
func (h *Hitter) Hit() (ok bool, err error)
func (*Hitter) Hitted
func (h *Hitter) Hitted() int64
func (*Hitter) QPS
func (h *Hitter) QPS() float64
func (*Hitter) Sync
func (h *Hitter) Sync(now time.Time) (next time.Time, err error)

type HitterOption

type HitterOption struct {
    *NodeOption

    ExpireSpec      string
    QPSShakeCoeff   float64
    EnableMaxSync   bool
    MaxSyncInterval time.Duration
    EnableMinSync   bool
    MinSyncInterval time.Duration
    FollowInterval  time.Duration

    Key             string
    KeyQPSSuffix    string
    KeySuffixFormat string

    EnableLimit bool
    Limit       int64

    ExpiredHook func(*Hitter)
    SyncedHook  func(*Hitter)
}

type Node

type Node struct {
    // contains filtered or unexported fields
}
Example

{
	expireInterval := time.Hour
	node, err := NewNode(&NodeOption{
		Redis:          redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
		KeyPrefix:      "ghit:",
		ExpireInterval: expireInterval,
	})
	if err != nil {
		panic(err)
	}
	defer node.Close()
	node.Heartbeat()
	defer node.Dead()
	for {
		select {
		case <-time.After(expireInterval):
			node.Heartbeat()
		}
	}
}

func NewNode
func NewNode(opt *NodeOption) (*Node, error)
func (*Node) Background
func (n *Node) Background()
func (*Node) Clean
func (n *Node) Clean() error
func (*Node) Close
func (n *Node) Close() error
func (*Node) Dead
func (n *Node) Dead() error
func (*Node) Heartbeat
func (n *Node) Heartbeat() error
func (*Node) NodeID
func (n *Node) NodeID() string
func (*Node) NodeIDs
func (n *Node) NodeIDs() ([]string, error)

type NodeOption

type NodeOption struct {
    Redis          redis.Cmdable
    KeyPrefix      string
    NodesKeySuffix string
    NodeID         string
    ExpireInterval time.Duration

    HeartbeatHook func(*Node)
    DeadHook      func(*Node)
}

type Pool

type Pool struct {
    *Node
    // contains filtered or unexported fields
}
func NewPool
func NewPool(opt *PoolOption) (*Pool, error)
func (*Pool) Close
func (p *Pool) Close() error
func (*Pool) ForEach
func (p *Pool) ForEach(f func(string, *Hitter) bool)
func (*Pool) Hit
func (p *Pool) Hit(opt *HitterOption) (bool, error)
func (*Pool) LoadOrStore
func (p *Pool) LoadOrStore(opt *HitterOption) (*Hitter, error)
func (*Pool) Store
func (p *Pool) Store(opt *HitterOption) (*Hitter, error)

type PoolOption

type PoolOption struct {
    *NodeOption
}

test workflow

Generated by gomarkdoc

Documentation

Index

Examples

Constants

View Source
const (
	DefaultNodesKeySuffix = "NODES"
	DefaultNodeExpire     = time.Hour
)

Variables

View Source
var (
	EnableLimit = true

	DefaultKeyQPSSuffix = ":QPS"
)
View Source
var (
	NodeID string
)

Functions

This section is empty.

Types

type Hitter

type Hitter struct {
	*Node
	// contains filtered or unexported fields
}
Example
hitter, err := NewHitter(&HitterOption{
	NodeOption: &NodeOption{
		Redis:     redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
		KeyPrefix: "ghit:",
	},
	ExpireSpec:     "@midnight",
	QPSShakeCoeff:  2,
	FollowInterval: time.Hour * 24,
	Key:            "run",
	EnableLimit:    true,
	Limit:          1,
})
if err != nil {
	panic(err)
}
defer hitter.Close()
if ok, err := hitter.Hit(); err != nil {
	// handle error
} else if !ok {
	return
}

func NewHitter

func NewHitter(opt *HitterOption) (*Hitter, error)

func (*Hitter) Background

func (h *Hitter) Background()

func (*Hitter) Close

func (h *Hitter) Close() error

func (*Hitter) Hit

func (h *Hitter) Hit() (ok bool, err error)

func (*Hitter) Hitted

func (h *Hitter) Hitted() int64

func (*Hitter) QPS

func (h *Hitter) QPS() float64

func (*Hitter) Sync

func (h *Hitter) Sync(now time.Time) (next time.Time, err error)

type HitterOption

type HitterOption struct {
	*NodeOption

	ExpireSpec      string
	QPSShakeCoeff   float64
	EnableMaxSync   bool
	MaxSyncInterval time.Duration
	EnableMinSync   bool
	MinSyncInterval time.Duration
	FollowInterval  time.Duration

	Key             string
	KeyQPSSuffix    string
	KeySuffixFormat string

	EnableLimit bool
	Limit       int64

	ExpiredHook func(*Hitter)
	SyncedHook  func(*Hitter)
}

type Node

type Node struct {
	// contains filtered or unexported fields
}
Example
expireInterval := time.Hour
node, err := NewNode(&NodeOption{
	Redis:          redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
	KeyPrefix:      "ghit:",
	ExpireInterval: expireInterval,
})
if err != nil {
	panic(err)
}
defer node.Close()
node.Heartbeat()
defer node.Dead()
for {
	select {
	case <-time.After(expireInterval):
		node.Heartbeat()
	}
}

func NewNode

func NewNode(opt *NodeOption) (*Node, error)

func (*Node) Background

func (n *Node) Background()

func (*Node) Clean

func (n *Node) Clean() error

func (*Node) Close

func (n *Node) Close() error

func (*Node) Dead

func (n *Node) Dead() error

func (*Node) Heartbeat

func (n *Node) Heartbeat() error

func (*Node) NodeID

func (n *Node) NodeID() string

func (*Node) NodeIDs

func (n *Node) NodeIDs() ([]string, error)

type NodeOption

type NodeOption struct {
	Redis          redis.Cmdable
	KeyPrefix      string
	NodesKeySuffix string
	NodeID         string
	ExpireInterval time.Duration

	HeartbeatHook func(*Node)
	DeadHook      func(*Node)
}

type Pool

type Pool struct {
	*Node
	// contains filtered or unexported fields
}

func NewPool

func NewPool(opt *PoolOption) (*Pool, error)

func (*Pool) Close

func (p *Pool) Close() error

func (*Pool) ForEach

func (p *Pool) ForEach(f func(string, *Hitter) bool)

func (*Pool) Hit

func (p *Pool) Hit(opt *HitterOption) (bool, error)

func (*Pool) LoadOrStore

func (p *Pool) LoadOrStore(opt *HitterOption) (*Hitter, error)

func (*Pool) Store

func (p *Pool) Store(opt *HitterOption) (*Hitter, error)

type PoolOption

type PoolOption struct {
	*NodeOption
}

Jump to

Keyboard shortcuts

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