goid

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2019 License: MIT Imports: 3 Imported by: 0

README

GoID

GoDoc Widget Build Status codecov Go Report Card

Hacking runtime to get goroutine id for caching meta

Usage

# Patch
go get -u github.com/go-courier/goid/patch-runtime && patch-runtime

For go module user:

need run blow

cd $GOPATH/src
mkdir -p global-tools
cd global-tools
go mod init
package goid_test

import (
	"time"
	"fmt"
	"math/rand"

	"github.com/go-courier/goid"
)

func ExampleLogIDMap() {
	for i := 0; i < 100; i ++ {
		go func() {
			// set logid at begin of goroutine
			goid.Default.Set(fmt.Sprintf("%d", rand.Int()))
			// clear at end of goroutine
			defer goid.Default.Clear()

			// do something with the cached logid
			_ = goid.Default.Get()

			time.Sleep(10 * time.Millisecond)
		}()
	}

	time.Sleep(5 * time.Millisecond)
	fmt.Println(len(goid.Default.All()))

	time.Sleep(50 * time.Millisecond)
	fmt.Println(len(goid.Default.All()))
	// Output:
	//100
	//0
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = &GoIDMetaMap{}

Functions

This section is empty.

Types

type GoIDMetaMap

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

func (*GoIDMetaMap) All

func (m *GoIDMetaMap) All() map[int64]metax.Meta

func (*GoIDMetaMap) Clear

func (m *GoIDMetaMap) Clear()

func (*GoIDMetaMap) Get

func (m *GoIDMetaMap) Get() metax.Meta

func (*GoIDMetaMap) Set

func (m *GoIDMetaMap) Set(meta metax.Meta)

func (*GoIDMetaMap) With

func (m *GoIDMetaMap) With(cb func(), metas ...metax.Meta) func()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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