gbase

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 17 Imported by: 0

README

GoDoc

gbase

目标在于实现非常基础的常用库,提供简单易用的API

import "github.com/cjey/gbase"

env

检查当前版本信息

$ ./env.sh

查看changelog

$ ./changelog.sh

doc

主包会自动集成或者export子包的常用形式

查看完整的文档

# 主包
go doc -all github.com/cjey/gbase

# Context
go doc -all github.com/cjey/gbase/context

Context

Context在实现了官方的Context接口外,额外追加了环境变量和日志能力,主包在此基础上做了个应用封装

关键特性

兼容

实现了官方库的Context接口

WithX

一组操作,用于直接简单的执行Context派生

Fork

Context支持执行fork操作,将会copy当前的环境,生成新的子级环境,同时其name后会自动的被追加一个数字后缀(location保持)

一般使用场景:一次请求当中,需要开启并发操作,并发的任务当中使用fork的Context来执行操作,这样在日志输出上就可以简单的区分出子任务

如果本请求触发了一个异步任务,则需要谨慎对待,因为派生Context用于异步可能会存在副作用(常规现象,继承的Context很可能会在同步请求结束时被立即Cancel),解决的办法可以是根据情况创建一个新的NamedContext,手工继承源Context的Name和Location(按需)

At

此操作将会copy当前的环境,生成新的子级环境,同时会在原有的Location之上合并入一个新的location名称(name保持),Context支持在新的逻辑过程中自行指明一个位置名称,在日志输出时便会自动带上该位置,并且,此位置名称是会被继承的,这样在输出日志中便可以简单的查看到一定的逻辑调用层次关系

当然,这个是可选的,也不建议在大大小小所有的函数位置处使用此能力,否则跟每条日志都打印一次调用堆栈没了区别,应当根据情况用在合适的位置

Set/Get

这是一组快捷操作,等价于调用Context内的Env,用于提供类似环境变量的能力,每当有新的Context派生出来之时,即会同步派生出新的变量空间

Set操作将只针对自己当前的变量空间,不会影响上游空间,而Get操作则会优先访问当前的变量空间,如果没有找到,则会逐级向上游追溯

Debug/Info/...

这是一组快捷操作,等价于调用Context内的Logger,用于提供基本的日志操作,内部的logger选择了zap.SugaredLogger,日志格式也默认被重新调整过,如果想定制格式,可以直接自行执行全局替换

如果只是想要在默认风格上调整日志等级/日志文件/日志编码等配置,可以直接使用ReplaceZapLogger完成目标

默认:ReplaceZapLogger("debug", "stderr", "console", false)

info+json: ReplaceZapLogger("info", "stderr", "json", false)

NamedContext

Context可以支持自命名,一般用于伴随服务生命周期的任务,或者是定时执行类的任务

对于这样的用法,需要注意的是,Context创建时,其内部的logger已经固定,比如定时执行类的任务,如果在运行时需要动态调整logger,应当在每次执行时重新创建一个同名的Context

var ctx = gbase.NamedContext("ZookeeperMonitor")
ctx.Info("started")
// console encode
// 2020-04-11 21:24:45.361  info    ZookeeperMonitor started
// json encode
// {"L":"info","T":"2020-04-11 21:24:45.361","N":"ZookeeperMonitor","M":"started"}

ctx = ctx.At("GetID")
ctx.Set("id", "123")
ctx.Info("got it!", "id", ctx.GetString("id"))
// console encode
// 2020-04-11 21:24:45.361  info    ZookeeperMonitor got it! {"@": "GetID", "id": "123"}
// json encode
// {"L":"info","T":"2020-04-11 21:24:45.361","N":"ZookeeperMonitor","M":"got it!", "@": "GetID", "id": "123"}
SessionContext

还可以选择使用自动生成uuid风格名称的Context,适用于服务请求开始时,为本次请求创建一个SessionContext。

生成的Context的name即是一个带有一定规律的uuid(以BootID的前24位为前缀+12位10进制数字递增序列)

你还可以通过替换变量SessionNameGenerator的方式来实现定制session的生成规则

var ctx = gbase.SessionContext()
ctx.Info("started")
// console encode
// 2020-04-11 21:24:45.361  info    9b2119d3-7f37-4033-8c19-000000000001 started
// json encode
// {"L":"info","T":"2020-04-11 21:24:45.361","N":"9b2119d3-7f37-4033-8c19-000000000001","M":"started"}

ctx = ctx.ForkAt("AsyncGetID")
ctx.Info("show default session", "session", GetSesson(ctx))
// console encode
// 2020-04-11 21:24:45.361  info    9b2119d3-7f37-4033-8c19-000000000001.1 show default session {"@": "AsyncGetID", "session": "9b2119d3-7f37-4033-8c19-000000000001.1"}
// json encode
// {"L":"info","T":"2020-04-11 21:24:45.361","N":"9b2119d3-7f37-4033-8c19-000000000001.1","M":"show default session", "@": "AsyncGetID", "session": "9b2119d3-7f37-4033-8c19-000000000001.1"}

SetSession(ctx, "7ec17674-1360-4fb1-9245-bd8d8d5866c4")
ctx.Info("use my own session", "session", GetSession(ctx))
// console encode
// 2020-04-11 21:24:45.361  info    9b2119d3-7f37-4033-8c19-000000000001.1 use my own session {"@": "AsyncGetID", "session": "7ec17674-1360-4fb1-9245-bd8d8d5866c4"}
// json encode
// {"L":"info","T":"2020-04-11 21:24:45.361","N":"9b2119d3-7f37-4033-8c19-000000000001.1","M":"use my own session", "@": "AsyncGetID", "session": "7ec17674-1360-4fb1-9245-bd8d8d5866c4"}
ToContext

NamedContext和SessionContext都使用官方context的Background作为内部context,如果需要使用自定义的context或者将官方的context执行转换,则可以使用ToSessionContextToNamedContext

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BootID   = uuid.New().String()
	BootTime = time.Now()
)
View Source
var SessionNameGenerator = func() func() string {
	var counter uint64
	return func() string {
		// return bootid related sequential name
		const h = "000000000000"
		var seq = atomic.AddUint64(&counter, 1)
		var s = strconv.FormatUint(seq, 10)
		return BootID[:24] + h[:12-len(s)] + s
	}
}()

SessionNameGenerator used to generate session name automatically, you can replace it

Functions

func GetRealSession

func GetRealSession(ctx Context) string

GetRealSession use to get real session name

func GetSession

func GetSession(ctx Context) string

GetSession use to get session name if it has real session, otherwise return context name instead

func LiveProcessName added in v0.0.5

func LiveProcessName(ctx Context, interval time.Duration, liveName func(int) string) chan struct{}

LiveProcessName change current process name every interval duration. You can use this for exposing your core runtime information to maintainer.

func NewZapLogger

func NewZapLogger(level, outpath, encoding string, showCaller bool) (*zap.Logger, error)

NewZapLogger return default log style

func ReplaceZapLogger

func ReplaceZapLogger(level, outpath, encoding string, showCaller bool) (func(), error)

ReplaceZapLogger use default log style to replace zap global logger

func ResolveTCPAddr added in v0.0.5

func ResolveTCPAddr(ctx Context, raw string, defaultPort uint16) (*net.TCPAddr, error)

ResolveTCPAddr use 0.0.0.0 as default ip to completion the given raw ip "" => 0.0.0.0:defaultPort 80 => 0.0.0.0:80 1.1.1.1 => 1.1.1.1:defaultPort

func SetSession

func SetSession(ctx Context, session string)

SetSession use to set real session name

func WriteMyPID added in v0.0.5

func WriteMyPID(ctx Context, appname string)

WriteMyPID write current process pid to /var/run/<appname>.pid If write failed, but the file content is equal to current process id, that's also ok. Any error will be ignored.

Types

type Context

type Context = context.Context

type alias

func NamedContext

func NamedContext(name string) Context

NamedContext return new context use specified session name

func SessionContext

func SessionContext() Context

SessionContext return new context use auto session name that generated by SessionNameGenerator

func SimpleContext

func SimpleContext() Context

SimpleContext return new context without name

func ToNamedContext

func ToNamedContext(gctx gcontext.Context, name string) Context

ToNamedContext return new context use specified official context and name

func ToSessionContext

func ToSessionContext(gctx gcontext.Context) Context

ToSessionContext return new context use specified official context with auto session name that generated by SessionNameGenerator

func ToSimpleContext

func ToSimpleContext(gctx gcontext.Context) Context

ToSimpleContext return new context use specified official context without name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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