go_rede

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 5 Imported by: 0

README

Rede 中文

🚀 A Rede is a fancy snooze delayed queue

You can use the Push method to set a snooze time for an element. Unless the time comes, the element will not wake up. Get the collection of elements that have woken up through the poll method.

Installation

go get -u github.com/fanjindong/go-rede

Features

  • Rede built on redis
  • Snooze time can be updated
  • Api is concise, such as Push, Poll
  • Data persistent storage

Requirements

  • Go >= 1.10.
  • Redis >= 5.0.

Quickstart

func main() {
	rd := rede.NewClient(&rede.Options{Namespaces: "demo", Addr: "127.0.0.1:6379"}) // Redis.Addr + Namespaces

	rd.Push("a", 1*time.Second)
	rd.Push("b", 1*time.Second)
	rd.Push("c", 2*time.Second)

	time.Sleep(1 * time.Second)

	cur := rd.Poll()
	for cur.Next() {
	    got, _ := cur.Get()
	    fmt.Println(got)
	}
	// out:
	// "a" 
	// "b"
}

Usage

  • Push

Push an element to rede and set a snooze time. The element will not wake up until the time is up.

  • Pull

Pull an element and remove it from rede.

  • Look

View the remaining snooze time of an element.

  • Ttn

View the remaining snooze time of the element that wakes up fastest in rede.

  • Poll

Poll the elements that have woken up.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Namespaces string
	*redis.Client
}

Client is a Redis client representing a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines.

func NewClient

func NewClient(opt *Options) *Client

func (*Client) Look

func (c *Client) Look(ctx context.Context, member string) (float64, error)

Look Show the ttl corresponding with element and without removing it from the rede.

func (*Client) Poll

func (c *Client) Poll(ctx context.Context) *pollCursor

Poll return all the expired members in rede. cur := c.Poll(ctx)

for cur.Next() {
    member, err := cur.Get()
    fmt.Println(member, err)
}

func (*Client) Pull

func (c *Client) Pull(ctx context.Context, members ...string) (int64, error)

Pull the members, remove it from the rede before it expires.

func (*Client) Push

func (c *Client) Push(ctx context.Context, member string, ttl time.Duration) (int64, error)

Push an Member into the Rede for ttl.Seconds() seconds

func (*Client) Ttn added in v0.2.0

func (c *Client) Ttn(ctx context.Context) (float64, error)

Ttn Show the time left (in seconds) until the next element will expire.

type Limiter

type Limiter interface {
	// Allow returns nil if operation is allowed or an error otherwise.
	// If operation is allowed client must ReportResult of the operation
	// whether it is a success or a failure.
	Allow() error
	// ReportResult reports the result of the previously allowed operation.
	// nil indicates a success, non-nil error usually indicates a failure.
	ReportResult(result error)
}

Limiter is the interface of a rate limiter or a circuit breaker.

type Options

type Options struct {
	Namespaces string
	// The network type, either tcp or unix.
	// Default is tcp.
	Network string
	// host:port address.
	Addr string

	// Optional password. Must match the password specified in the
	// requirepass server configuration option.
	Password string
	// Database to be selected after connecting to the server.
	DB int

	// Maximum number of retries before giving up.
	// Default is to not retry failed commands.
	MaxRetries int
	// Minimum backoff between each retry.
	// Default is 8 milliseconds; -1 disables backoff.
	MinRetryBackoff time.Duration
	// Maximum backoff between each retry.
	// Default is 512 milliseconds; -1 disables backoff.
	MaxRetryBackoff time.Duration

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration
	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
	// Default is 3 seconds.
	ReadTimeout time.Duration
	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	WriteTimeout time.Duration

	// Maximum number of socket connections.
	// Default is 10 connections per every CPU as reported by runtime.NumCPU.
	PoolSize int
	// Minimum number of idle connections which is useful when establishing
	// new connection is slow.
	MinIdleConns int
	// Connection age at which client retires (closes) the connection.
	// Default is to not close aged connections.
	MaxConnAge time.Duration
	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	PoolTimeout time.Duration
	// Amount of time after which client closes idle connections.
	// Should be less than server's timeout.
	// Default is 5 minutes. -1 disables idle timeout check.
	IdleTimeout time.Duration
	// Frequency of idle checks made by idle connections reaper.
	// Default is 1 minute. -1 disables idle connections reaper,
	// but idle connections are still discarded by the client
	// if IdleTimeout is set.
	IdleCheckFrequency time.Duration

	// TLS Config to use. When set TLS will be negotiated.
	TLSConfig *tls.Config

	// Limiter interface used to implemented circuit breaker or rate limiter.
	Limiter Limiter
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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