misc

package
v0.0.0-...-aef3d86 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: MIT Imports: 17 Imported by: 0

README

misc 模块说明

时间处理相关的函数

  • TimeTick —— 计时器,通过 TimeTick.Tick() 打点计时间,返回距离上一次 Tick 过去的时间,单位 ms。
  • Timed —— 函数调用计时。

example: TestTimeRelated

import (
	"testing"
	"time"

	timerelated "github.com/memory-overflow/go-common-library/misc/time_related"
)

func f() error {
	time.Sleep(3 * time.Second)
	return nil
}

func TestTimeRelated(t *testing.T) {
	// test TimeTick
	// build tick
	tick := timerelated.BuildTimeTick()
	time.Sleep(time.Second)
	// 距离上一次打点的时间间隔
	lastTickPast := tick.Tick()
	t.Logf("after last tick time pass: %dms", lastTickPast)
	// 距离上一次打点的时间间隔
	time.Sleep(2 * time.Second)
	lastTickPast = tick.Tick()
	t.Logf("after last tick time pass: %dms", lastTickPast)

	// test Timed
	cost, _ := timerelated.Timed(f)
	t.Logf("call f time cost %dms", cost)
}

download 模块

  • DownloadFile 下载网络文件
  • DownloadFileWithLimit 下载网络文件(带文件大小限制)

example: TestDownload

import (
	"context"
	"fmt"
	"os"
	"path"
	"testing"

	"github.com/memory-overflow/go-common-library/misc"
)

func TestDownload(t *testing.T) {
	ctx := context.Background()
	uri := "http://www.baidu.com"
	dirs := "/root"
	filename := "baidu.html"
	defer os.Remove(path.Join(dirs, filename))
	// 目录不存在会自动创建
	if err := misc.DownloadFile(ctx, uri, dirs, filename); err != nil {
		t.Fatalf("download file: %v", err)
	}

	// 目录不存在会自动创建
	if err := misc.DownloadFileWithLimit(ctx, uri, dirs, filename, 1000); err != nil {
		t.Fatalf("download file: %v", err)
	}
}

goroutine_help 模块

协程相关的处理模块。

  • GoroutineHelp
    • GoroutineHelp.Recoverd:协程 recover 处理函数。
    • GoroutineHelp.SafeGo:安全的启用携程。

example: TestGoroutineHelp

import (
	"context"
	"fmt"
	"os"
	"path"
	"testing"

	"github.com/memory-overflow/go-common-library/misc"
)

func goFunc() {
	fmt.Println("start goFunc")
	panic("test panic")
}

func TestGoroutineHelp(t *testing.T) {
	help := misc.GoroutineHelp{}
	// SafeGo, 如果异常,重启协程
	help.SafeGo(goFunc, true)

	// 后面的处理,如果出现异常,捕获异常防止线程退出,recoverFunc 用来恢复携程的处理。
	defer help.Recover(func() error {
		// recover code
		return nil
	})

	// other code
	for {

	}
}

retry

Retry——带重试调用。

id 生成器

IDGenerator 对象封装了很多生成 id 的函数。

  • IDGenerator.GenerateRandomString:生成随机字符串。
  • IDGenerator.GenerateUUID:生成UUID。
  • IDGenerator.BighumpToUnderscore:大驼峰参数转换成下划线。
  • IDGenerator.UnderscoreToBighump:下划线参数转换成大驼峰。

log

  • FoldLog: 折叠日志中数据过长的字段,输入仅支持 json 字符串。

example: TestFlodLog

import (
	"context"
	"fmt"
	"os"
	"path"
	"testing"

	"github.com/memory-overflow/go-common-library/misc"
)

func TestFlodLog(t *testing.T) {
	log := `{"RetrieveInputData":"其他","RetrieveInputType":0,"FilterSet":[],"PageNumber":1,"PageSize":6,"TIBusinessID":1,"TIProjectID":1}`
	fmt.Println(misc.FoldLog([]byte(log)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadFile

func DownloadFile(ctx context.Context, url string, dirs, fileName string) error

DownloadFile 下载网络文件到服务器本地

func DownloadFileWithLimit

func DownloadFileWithLimit(ctx context.Context, url string, dirs, fileName string, maxBytes int64) error

DownloadFileWithLimit 下载网络文件到服务器本地,限制文件大小

func FoldLog

func FoldLog(content []byte) string

FoldLog 折叠 json log 中的长数据,比如图片 base64 等等

func Retry

func Retry(f RetryFunc) (err error)

Retry 带重试的调用

Types

type GoroutinFunc

type GoroutinFunc func()

GoroutinFunc ...

type GoroutineHelp

type GoroutineHelp struct {
}

GoroutineHelp ...

func (GoroutineHelp) Recover

func (GoroutineHelp) Recover(recoverFunc RecoverFunc) error

Recover panic 并打印日志, recoverFunc 用来恢复的执行函数, usgae: defer misc.Recover(nil)

func (GoroutineHelp) SafeGo

func (help GoroutineHelp) SafeGo(goFunc GoroutinFunc, recover bool)

SafeGo 安全启用携程,捕获 crash,并且重试。recover 表示捕获到异常的情况下是否需要重新开启新的携程。

type IDGenerator

type IDGenerator struct {
}

IDGenerator id 生成器

func (IDGenerator) BighumpToUnderscore

func (gen IDGenerator) BighumpToUnderscore(param string) string

BighumpToUnderscore 大驼峰参数转换成下划线

func (IDGenerator) GenerateRandomString

func (gen IDGenerator) GenerateRandomString(length int, alphanum []byte) string

GenerateRandomString 生成随机字符串, alphanum 字符集,nil 使用默认字符集。

func (IDGenerator) GenerateUUID

func (gen IDGenerator) GenerateUUID(includeSeparator bool) string

GenerateUUID 生成UUID

func (IDGenerator) UnderscoreToBighump

func (gen IDGenerator) UnderscoreToBighump(param string) string

UnderscoreToBighump 下划线参数转换成大驼峰

type RecoverFunc

type RecoverFunc func() error

RecoverFunc 恢复函数

type RetryFunc

type RetryFunc func() error

RecoverFunc 重试函数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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