gotimer

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

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

Go to latest
Published: Apr 29, 2018 License: MIT Imports: 5 Imported by: 0

README

gotimer GoDoc

gotimer is a Go library for timer as a service or a module of service.

Features

  • easy to use
  • auto register the event handler
  • use handler name to add timer
  • can add many timer at the same time and supports various timers mode
  • pass the parameters to timer when add
  • sync run or async run

Installation

Standard go get:

$ go get github.com/rangechow/gotimer

Usage at a glance

  1. design the common form timer handler as the method of type.
package main

import (
    "context"
    "fmt"
)

type exampleSvr int

func (s *exampleSvr) CheckTimer(ctx context.Context, id string) error {
    fmt.Println("check timer uid = " + id)
    return nil
}

  1. init timer service and sync run
package main

import (
    "fmt"
    "time"

    "github.com/rangechow/gotimer"
)


func main() {
    
    s := new(exampleSvr)

    var timer gotimer.TimerService
    timer.Register(s)
   
    go func() {
      _, err := timer.AddTimer(time.Millisecond*1000, true, "CheckTimer", "10001")

      if err != nil {
          fmt.Printf("add timer failed %v", err)
          return
      }
    }

    timer.Run()
}


  1. or async run as a module of service
package main

import (
    "fmt"
    "time"

    "github/rangechow/gotimer"
)

func main() {
    
    s := new(exampleSvr)

    var timer gotimer.timerService
    timer.Register(s)
    timer.AsyncRun()

    _, err := timer.AddTimer(time.Millisecond*1000, true, "CheckTimer", 10001)

    if err != nil {
        fmt.Printf("add timer failed %v", err)
        return
    }

    // you service run forever
    // in this exampe, it will end
}


Some details

when designing a stateful service, we often need a timer to trigger some active actions or events. Timer process flow: register handler --> run timer service --> add timer --> trigger

register handler

Timer handler is part of service, so we used a registration model similar to GRPC. The difference is that we do not have a pre-generated interface. We used the mode of handler.

func (receiver) (context, interface{}) (error)

At the beginning, we call the registration api, passing our service. Timer will auto register handler. Sometimes some extra functions are registered, but it doesn't matter, we won't call it. When we register handler, we also need to specify the timer's accuracy and maximum timeout.

run timer service

As part of the service, the timer will execute asynchronously on one goroutine. Therefore, we do not need to worry about the race condition of the timer data. In addition, you can use the timer service synchronously. Timer process flow change to : register handler --> add timer --> run timer service --> trigger

add timer

There are general two types of timers:

  1. one shot at specified time
  2. repeat interval time

We have api support for the above operation. And Trigger a handler by specifying handler name. When we add the timer, we need to specify the name of the handler, and we can pass the corresponding parameter to the handler. The Add APIs will return sequence number, it would be used to delete the unexpired timer.

trigger

We use priority queue management timer. Each tick, we will delete the timer in the trash, and then checking the timer expired in the queue. If the timer expires, the handler will execute in a goroutine.

API Documentation

see the Godoc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TimerService

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

TimerService is timer manager, It can be used independently or as a module of the service

func (*TimerService) AddOneshotTimer

func (t *TimerService) AddOneshotTimer(datatime time.Time, handlerName string, args interface{}) (int, error)

AddOneshotTimer will add a timer that trigger once at specified time. It wrap AddTimer.

func (*TimerService) AddTimer

func (t *TimerService) AddTimer(interval time.Duration, isRepeate bool, handlerName string, args interface{}) (int, error)

AddTimer can add a timer that trigger from now until interval. If you want the timer trigger repeate, you could set isRepeate true. It is a blocking function that waiting timer's sequence

func (*TimerService) AsyncRun

func (t *TimerService) AsyncRun()

AsyncRun function asynchronous execution timer service.

func (*TimerService) DelTimer

func (t *TimerService) DelTimer(seq int)

DelTimer send a sequence of deleting timer to timer goroutine. The seq will push to a slice waiting for delete.

func (*TimerService) Register

func (t *TimerService) Register(svr interface{}) error

Register add handler from type method to timer. The name of the handler must be unique

func (*TimerService) Run

func (t *TimerService) Run()

Run function synchronous execution timer service.

func (*TimerService) SetAccuracy

func (t *TimerService) SetAccuracy(accuracy time.Duration) error

SetAccuracy can change the accuracy of the timer. default accuracy is 100 Millisecond. It is only valid before run timer.

func (*TimerService) StopTimerService

func (t *TimerService) StopTimerService()

You can stop the timer service.

Jump to

Keyboard shortcuts

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