autohttp

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

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

Go to latest
Published: Jul 9, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

autohttp

使用Go语言编写,对于HTTP请求进行发送,如果失败会暂存并重试。

使用

在运行模块之前,需要初始化相关配置信息,具体说明参见代码中的注释:

import (
    "gitee.com/xiaochengtech/autohttp"
)

func main() {
    // 设置全局信息
    autohttp.xxx = ...
    ...
}
数据库

模块会自动管理数据库,需要在数据库中,新建一张表,至少应包含如下字段:

字段名 类型 是否主键 默认值 备注
id uint64 - 主键,HTTP请求的唯一ID

Documentation

Index

Constants

View Source
const (
	StatusSending uint8 = iota + 1 // 发送中
	StatusSuccess                  // 成功
	StatusFailure                  // 失败
	StatusStopped                  // 停止重试
)

传输状态

Variables

View Source
var AutoCleanEnable = false

是否开启自动清理,默认不开启。 如果开启自动清理,则会根据选项不同,自动删除已完成记录或者未完成记录。

View Source
var AutoCleanFailureTime = int64(0)

自动清理多久之前的失败记录,单位秒。 仅在AutoCleanEnable=true时有效。 大于0表示开启,等于0表示不开启。

View Source
var AutoCleanLoopInterval = int64(3600)

自动清理循环的轮询时间间隔,单位秒,默认1小时。

View Source
var AutoCleanStoppedTime = int64(0)

自动清理多久之前的已停止记录,单位秒。 仅在AutoCleanEnable=true时有效。 大于0表示开启,等于0表示不开启。

View Source
var AutoCleanSuccessTime = int64(0)

自动清理多久之前的成功记录,单位秒。 仅在AutoCleanEnable=true时有效。 大于0表示开启,等于0表示不开启。

View Source
var DefaultRetryType = true

默认重试类型。 true表示周期性重试,false表示阶梯性重试。

View Source
var LoopInterval = int64(3)

主循环多长时间检查一次,单位秒,默认3秒。 每次检查需要轮询数据库,所以需要根据实际情况调整。

View Source
var RetryLadderDefaultInterval = []int64{}

阶梯性重试默认间隔,单位秒,默认为空。 样例:[]int64{60, 5*60, 30*60, 1*3600},表示在1分钟、5分钟、30分钟、1小时后重试。

View Source
var RetryStepDefaultInterval = int64(60)

周期性重试默认间隔,单位秒,默认为60秒。

View Source
var RetryStepDefaultTime = int64(0)

周期性重试次数,默认为0,0表示不限制次数。

View Source
var TaskAddIfSuccess = false

成功后是否写入任务,默认不开启。

Functions

func Get

func Get(
	ctx context.Context,
	businessType string,
	businessId string,
	url string,
	header map[string]string,
) (err error)

发送GET请求。

func PostForm

func PostForm(
	ctx context.Context,
	businessType string,
	businessId string,
	apiUrl string,
	header map[string]string,
	body interface{},
) (err error)

POST请求,application/x-www-form-urlencoded

func PostJson

func PostJson(
	ctx context.Context,
	businessType string,
	businessId string,
	url string,
	header map[string]string,
	body interface{},
) (err error)

POST请求,application/json

Types

type DatabaseCallbackInterface

type DatabaseCallbackInterface interface {
	// 创建初始任务
	CreateTask(c context.Context, item *Item) (err error)
	// 删除任务
	DeleteTask(c context.Context, taskId uint64) (err error)
	// 查询待重试的任务(仅ID)
	IncompleteTaskIdList(c context.Context, currentTime int64) (list []uint64, err error)
	// 待清理任务列表,status表示状态,timestamp表示某个Unix时间点之前
	TaskToCleanList(c context.Context, status uint8, timestamp int64) (list []uint64, err error)
	// 查询任务详细信息
	TaskDetail(c context.Context, taskId uint64) (item Item, err error)
	// 查询任务详细信息
	TaskDetailByBusiness(c context.Context, businessType string, businessId string) (item Item, err error)
	// 更新任务
	UpdateTask(c context.Context, item *Item) (err error)
}

数据库操作类回调函数

var DatabaseCallback DatabaseCallbackInterface = nil

全局数据库回调方法

type Item

type Item struct {
	ID                  uint64            // 唯一标识ID
	BusinessType        string            // 业务类型,用户自定义
	BusinessId          string            // 业务主键,同一业务类型不能出现重复
	Url                 string            // HTTP请求URL
	Method              string            // HTTP请求方法
	Header              map[string]string // HTTP请求头
	Body                []byte            // HTTP请求Body
	RetryType           bool              // 重试类型
	RetryLadderInterval []int64           // 阶梯性重试的间隔
	RetryStepInterval   int64             // 周期性重试的间隔
	RetryStepTime       int64             // 周期性重试的次数
	RetryCount          int64             // 重试计数
	Status              uint8             // 状态,参见常量StatusXXX
	RequestTime         int64             // 首次发起请求时间,Unix时间戳,单位秒
	LastRetryTime       int64             // 上一次重试的时间,Unix时间戳,单位秒
	NextRetryTime       int64             // 下一次重试时间,Unix时间戳,单位秒
	SuccessTime         int64             // 成功时间,Unix时间戳,单位秒
}

数据库项的通用格式

type StatusCallbackInterface

type StatusCallbackInterface interface {
	// 如果要实现发送前修改Item,则复写此方法
	OnTaskBuilding(c context.Context, item *Item)
	// 结果判断
	OnTaskResultValid(c context.Context, item Item, resultBytes []byte) (err error)
	// 更新任务状态
	OnTaskStatusUpdate(c context.Context, item Item)
}

状态类的回调,需要用户自己实现

var StatusCallback StatusCallbackInterface = nil

全局状态类回调方法

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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