counter

package module
v0.0.0-...-83ca984 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2017 License: MIT Imports: 4 Imported by: 0

README

counter

GoDoc Build Status Coverage Status Go Report Card

This is a Go library to increment a counter for a window of time (say last 60 second). Its resolution is 1 second.

Results will be periodically saved in a local file.

Installation

$ go get github.com/kavehmz/counter

Usage

# to test it as a http server you can do:
$ go run -race example/main.go
package main

import (
    "fmt"
    "github.com/kavehmz/counter"
)

func main() {
    c := counter.Init("/tmp/counter", time.Second, 60)
    ch := make(chan int)
    c.Inc(ch)
    fmt.Println(<-ch)
}

Algorithm

This lib is using and travesing a linked list to keep track of counts.

Documentation

Overview

Package counter implements a counter which will persist the result in a local file.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter defines parameter for the counter.

Example
tmpfile, _ := ioutil.TempFile("", "test")
defer os.Remove(tmpfile.Name())

c, _ := Init(tmpfile.Name(), 0, 2)
ch := make(chan int)
c.Inc(ch)
fmt.Println(<-ch)
c.Inc(ch)
fmt.Println(<-ch)
c.Inc(ch)
fmt.Println(<-ch)
Output:

1
2
3

func Init

func Init(fn string, btime time.Duration, h int) (*Counter, error)

Init will setup a counter and loads the initial value if the file exists It accepts a filename and buffersize and length of history to keep

func (*Counter) Inc

func (c *Counter) Inc(ret chan int)

Inc will increment the counter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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