robin

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2019 License: MIT Imports: 8 Imported by: 2

README

robin

GitHub FOSSA Status Go Report Card Build Status codecov

Features

Fiber

  • GoroutineSingle - a fiber backed by a dedicated goroutine. Every job is executed by a goroutine.
  • GoroutineMulti - a fiber backed by more goroutine. Each job is executed by a new goroutine.

Channels

  • Channels callback is executed for each message received.

Cron

Golang job scheduling for humans. It is inspired by schedule.

Memory Cache

Implements an in-memory cache key:value (similar to C# MemoryCache)

Usage

Quick Start

1.Install

  go get github.com/jiansoft/robin

2.Use examples

import (
    "log"
    "time"
    
    "github.com/jiansoft/robin"
)

func main() {
    
    //Keep an item in memory 
    robin.Memory().Keep("qq", "Qoo", time.Second)
    //Read returns the value if the key exists in the cache and it's not expired.
    val, ok := robin.Memory().Read("qq")
    //Have eturns true if the memory has the item and it's not expired.
    yes := robin.Memory().Have("qq")
    //Removes an item from the memory
    robin.Memory().Forget("qq")

    //The method is going to execute only once after 2000 ms.
    robin.Delay(2000).Do(runCron, "a Delay 2000 ms")
    
    minute := 11
    second := 50
    
    //Every Friday is going to execute once at 14:11:50 (HH:mm:ss).
    robin.EveryFriday().At(14, minute, second).Do(runCron, "Friday")

    //Every N day  is going to execute once at 14:11:50(HH:mm:ss)
    robin.Every(1).Days().At(14, minute, second).Do(runCron, "Days")

    //Every N hours is going to execute once at 11:50:00(HH:mm:ss).
    robin.Every(1).Hours().At(0, minute, second).Do(runCron, "Every 1 Hours")

    //Every N minutes is going to execute once at 50(ss).
    robin.Every(1).Minutes().At(0, 0, second).Do(runCron, "Every 1 Minutes")

    //Every N seconds is going to execute once
    robin.Every(10).Seconds().Do(runCron, "Every 10 Seconds")
    
    p1 := player{Nickname: "Player 1"}
    p2 := player{Nickname: "Player 2"}
    p3 := player{Nickname: "Player 3"}
    p4 := player{Nickname: "Player 4"}
    
    //Create a channel
    channel := robin.NewChannel()
    
    //Four player subscribe the channel
    channel.Subscribe(p1.eventFinalBossResurge)
    channel.Subscribe(p2.eventFinalBossResurge)
    p3Subscribe := channel.Subscribe(p3.eventFinalBossResurge)
    p4Subscribe := channel.Subscribe(p4.eventFinalBossResurge)
    
    //Publish a message to the channel and then the four subscribers of the channel will 
    //receives the message each that "The boss resurge first." .
    channel.Publish("The boss resurge first.")
    
    //Unsubscribe p3 and p4 from the channel.
    channel.Unsubscribe(p3Subscribe)
    p4Subscribe.Unsubscribe()
    
    //This time just p1 and p2 receives the message that "The boss resurge second.".
    channel.Publish("The boss resurge second.")
    
    //Unsubscribe all subscribers from the channel
    channel.Clear()
    
    //The channel is empty so no one can receive the message
    channel.Publish("The boss resurge third.")
}

func runCron(s string) {
    log.Printf("I am %s CronTest %v\n", s, time.Now())
}

type player struct {
	Nickname string
}
func (p player) eventFinalBossResurge(someBossInfo string) {
	log.Printf("%s receive a message : %s", p.Nickname, someBossInfo)
}

More example

License

Copyright (c) 2017

Released under the MIT license:

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(a int) int

Abs Returns the absolute value of a specified int number.

func AbsForInt64 added in v1.0.9

func AbsForInt64(n int64) int64

AbsForInt64 Returns the absolute value of a specified int64 number.

Types

type Channel added in v1.0.10

type Channel struct {
	sync.Map
}

Channel is a struct that has a member variable to store subscribers

func NewChannel

func NewChannel() *Channel

NewChannel new a Channel instance

func (*Channel) Clear added in v1.0.10

func (c *Channel) Clear()

Clear empty the subscribers

func (*Channel) Count added in v1.0.10

func (c *Channel) Count() int

Count returns a number that how many subscribers in the Channel.

func (*Channel) Publish added in v1.0.10

func (c *Channel) Publish(msg ...interface{})

Publish a message to all subscribers

func (*Channel) Subscribe added in v1.0.10

func (c *Channel) Subscribe(taskFun interface{}, params ...interface{}) *Subscriber

Subscribe to register a receiver to receive the Channel's message

func (*Channel) Unsubscribe added in v1.0.10

func (c *Channel) Unsubscribe(subscriber interface{})

Unsubscribe remove the subscriber from the channel

type ConcurrentQueue

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

ConcurrentQueue A "thread" safe string to anything items.

func NewConcurrentQueue

func NewConcurrentQueue() *ConcurrentQueue

NewConcurrentQueue ConcurrentQueue Constructors

func (ConcurrentQueue) Clean added in v1.1.0

func (c ConcurrentQueue) Clean()

Clean remove all element in the ConcurrentQueue.

func (ConcurrentQueue) Count

func (c ConcurrentQueue) Count() int

Count Gets the number of elements contained in the ConcurrentQueue.

func (*ConcurrentQueue) Enqueue

func (c *ConcurrentQueue) Enqueue(item interface{})

Enqueue Adds an object to the end of the ConcurrentQueue.

func (*ConcurrentQueue) TryDequeue

func (c *ConcurrentQueue) TryDequeue() (interface{}, bool)

TryDequeue Tries to remove and return the interface{} at the beginning of the concurrent queue.

func (*ConcurrentQueue) TryPeek

func (c *ConcurrentQueue) TryPeek() (interface{}, bool)

TryPeek Tries to return an interface{} from the beginning of the ConcurrentQueue without removing it.

type Disposable

type Disposable interface {
	Dispose()
}

Disposable an interface just has only one function

type Fiber

type Fiber interface {
	Start()
	Stop()
	Dispose()
	Enqueue(taskFun interface{}, params ...interface{})
	EnqueueWithTask(task Task)
	Schedule(firstInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)
	ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)
}

Fiber define some function

type GoroutineMulti

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

GoroutineMulti a fiber backed by more goroutine. Each job is executed by a new goroutine.

func NewGoroutineMulti

func NewGoroutineMulti() *GoroutineMulti

NewGoroutineMulti create a GoroutineMulti instance

func (*GoroutineMulti) Dispose

func (g *GoroutineMulti) Dispose()

Dispose stop the fiber and release resource

func (*GoroutineMulti) Enqueue

func (g *GoroutineMulti) Enqueue(taskFun interface{}, params ...interface{})

Enqueue use the fiber to execute a task

func (*GoroutineMulti) EnqueueWithTask

func (g *GoroutineMulti) EnqueueWithTask(task Task)

EnqueueWithTask use the fiber to execute a task

func (*GoroutineMulti) Schedule

func (g *GoroutineMulti) Schedule(firstInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)

Schedule execute the task once at the specified time that depends on parameter firstInMs.

func (*GoroutineMulti) ScheduleOnInterval

func (g *GoroutineMulti) ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)

ScheduleOnInterval execute the task once at the specified time that depends on parameters both firstInMs and regularInMs.

func (*GoroutineMulti) Start

func (g *GoroutineMulti) Start()

Start the fiber work now

func (*GoroutineMulti) Stop

func (g *GoroutineMulti) Stop()

Stop the fiber work

type GoroutineSingle

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

GoroutineSingle a fiber backed by a dedicated goroutine. Every job is executed by a goroutine.

func NewGoroutineSingle

func NewGoroutineSingle() *GoroutineSingle

NewGoroutineSingle create a GoroutineSingle instance

func (*GoroutineSingle) Dispose

func (g *GoroutineSingle) Dispose()

Dispose stop the fiber and release resource

func (*GoroutineSingle) Enqueue

func (g *GoroutineSingle) Enqueue(taskFun interface{}, params ...interface{})

Enqueue use the fiber to execute a task

func (*GoroutineSingle) EnqueueWithTask

func (g *GoroutineSingle) EnqueueWithTask(task Task)

EnqueueWithTask enqueue the parameter task into the queue waiting for executing.

func (*GoroutineSingle) Schedule

func (g *GoroutineSingle) Schedule(firstInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)

Schedule execute the task once at the specified time that depends on parameter firstInMs.

func (*GoroutineSingle) ScheduleOnInterval

func (g *GoroutineSingle) ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)

ScheduleOnInterval execute the task once at the specified time that depends on parameters both firstInMs and regularInMs.

func (*GoroutineSingle) Start

func (g *GoroutineSingle) Start()

Start the fiber work now

func (*GoroutineSingle) Stop

func (g *GoroutineSingle) Stop()

Stop the fiber work

type IScheduler

type IScheduler interface {
	Schedule(firstInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)
	ScheduleOnInterval(firstInMs int64, regularInMs int64, taskFun interface{}, params ...interface{}) (d Disposable)
	Enqueue(taskFun interface{}, params ...interface{})
	EnqueueWithTask(task Task)
	Remove(d Disposable)
	Dispose()
}

IScheduler an interface that for GoroutineMulti and GoroutineSingle use.

type Item added in v1.1.0

type Item struct {
	Value    interface{}
	Priority int64
	Index    int
	sync.Mutex
}

Item store data in the PriorityQueue

type Job

type Job struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Job store some infomartion for cron use.

func Delay

func Delay(delayInMs int64) *Job

Delay The job executes will delay N interval.

func Every

func Every(interval int64) *Job

Every the job will execute every N everyUnit(ex atHour、atMinute、atSecond、millisecond etc..).

func EveryFriday

func EveryFriday() *Job

EveryFriday the job will execute every Friday

func EveryMonday

func EveryMonday() *Job

EveryMonday the job will execute every Monday

func EverySaturday

func EverySaturday() *Job

EverySaturday the job will execute every Saturday

func EverySunday

func EverySunday() *Job

EverySunday the job will execute every Sunday .

func EveryThursday

func EveryThursday() *Job

EveryThursday the job will execute every Thursday

func EveryTuesday

func EveryTuesday() *Job

EveryTuesday the job will execute every Tuesday

func EveryWednesday

func EveryWednesday() *Job

EveryWednesday the job will execute every Wednesday

func Everyday added in v1.0.7

func Everyday() *Job

Everyday the job will execute every day

func RightNow

func RightNow() *Job

RightNow The job executes immediately.

func (*Job) AfterExecuteTask added in v1.0.1

func (j *Job) AfterExecuteTask() *Job

AfterExecuteTask waiting for the job execute finish then calculating the job next execution time just for delay model、every N second and every N millisecond If you want some job every N minute、hour or day do once and want to calculate next execution time by after the job executed. Please use interval unit that Seconds or Milliseconds

func (*Job) At

func (j *Job) At(hh int, mm int, ss int) *Job

At the time specified at execution time

func (*Job) BeforeExecuteTask added in v1.0.1

func (j *Job) BeforeExecuteTask() *Job

BeforeExecuteTask to calculate next execution time immediately don't wait

func (*Job) Between added in v1.0.12

func (j *Job) Between(f time.Time, t time.Time) *Job

Between the job will be executed only between an assigned period (from f to f time HH:mm:ss.ff).

func (*Job) Days

func (j *Job) Days() *Job

Days a time interval of execution

func (*Job) Dispose

func (j *Job) Dispose()

Dispose Job's Dispose

func (*Job) Do

func (j *Job) Do(fun interface{}, params ...interface{}) Disposable

Do some job needs to execute.

func (*Job) Hours

func (j *Job) Hours() *Job

Hours a time interval of execution

func (*Job) Milliseconds added in v1.0.9

func (j *Job) Milliseconds() *Job

Milliseconds a time interval of execution

func (*Job) Minutes

func (j *Job) Minutes() *Job

Minutes a time interval of execution

func (*Job) Seconds

func (j *Job) Seconds() *Job

Seconds a time interval of execution

func (*Job) Times added in v1.0.9

func (j *Job) Times(times int64) *Job

Times set the job maximum number of executed times

type MemoryCache added in v1.1.0

type MemoryCache interface {
	Keep(key string, value interface{}, ttl time.Duration) error
	Forget(key string)
	Read(string) (interface{}, bool)
	Have(string) bool
}

func Memory added in v1.1.0

func Memory() MemoryCache

Memory returns memoryCacheStore instance.

type PriorityQueue added in v1.1.0

type PriorityQueue []*Item

A PriorityQueue implements heap.Interface and holds Items. ie. the 0th element is the lowest value

func NewPriorityQueue added in v1.1.0

func NewPriorityQueue(capacity int) *PriorityQueue

func (PriorityQueue) Len added in v1.1.0

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less added in v1.1.0

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop added in v1.1.0

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push added in v1.1.0

func (pq *PriorityQueue) Push(x interface{})

func (*PriorityQueue) PushItem added in v1.1.0

func (pq *PriorityQueue) PushItem(item *Item)

func (PriorityQueue) Swap added in v1.1.0

func (pq PriorityQueue) Swap(i, j int)

func (*PriorityQueue) TryDequeue added in v1.1.0

func (pq *PriorityQueue) TryDequeue(limit int64) (*Item, bool)

TryDequeue

func (*PriorityQueue) Update added in v1.1.0

func (pq *PriorityQueue) Update(item *Item)

update modifies the priority of an Item in the queue.

type Subscriber added in v1.0.10

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

Subscriber is a struct for register to a channel

func (*Subscriber) Unsubscribe added in v1.0.10

func (c *Subscriber) Unsubscribe()

Unsubscribe remove the subscriber from the channel

type Task added in v1.0.6

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

Task a struct

type UntilJob added in v1.0.12

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

func (*UntilJob) Do added in v1.0.12

func (u *UntilJob) Do(fun interface{}, params ...interface{}) Disposable

Do

type Worker added in v1.0.12

type Worker interface {
	Do(interface{}, ...interface{}) Disposable
}

func Until added in v1.0.12

func Until(time time.Time) Worker

Until

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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