diff --git a/client.go b/client.go index e6c4041..50a2d72 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ import ( "github.com/injoyai/conv" "github.com/injoyai/ios" "github.com/injoyai/ios/client" + "github.com/injoyai/ios/module/common" "github.com/injoyai/logs" "github.com/injoyai/tdx/protocol" "runtime/debug" @@ -15,6 +16,16 @@ import ( "time" ) +const ( + LevelNone = common.LevelNone + LevelDebug = common.LevelDebug + LevelWrite = common.LevelWrite + LevelRead = common.LevelRead + LevelInfo = common.LevelInfo + LevelError = common.LevelError + LevelAll = common.LevelAll +) + // WithDebug 是否打印通讯数据 func WithDebug(b ...bool) client.Option { return func(c *client.Client) { @@ -22,6 +33,12 @@ func WithDebug(b ...bool) client.Option { } } +func WithLevel(level int) client.Option { + return func(c *client.Client) { + c.Logger.SetLevel(level) + } +} + // WithRedial 断线重连 func WithRedial(b ...bool) client.Option { return func(c *client.Client) { @@ -31,6 +48,7 @@ func WithRedial(b ...bool) client.Option { // DialDefault 默认连接方式 func DialDefault(op ...client.Option) (cli *Client, err error) { + op = append([]client.Option{WithRedial()}, op...) return DialHostsRange(Hosts, op...) } @@ -63,7 +81,8 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) { } cli.Client, err = client.Dial(dial, func(c *client.Client) { - c.Logger.Debug(false) //关闭日志打印 + c.Logger.Debug(true) //关闭日志打印 + c.Logger.SetLevel(LevelInfo) //设置日志级别 c.Logger.WithHEX() //以HEX显示 c.SetOption(op...) //自定义选项 c.Event.OnReadFrom = protocol.ReadFrom //分包 @@ -86,16 +105,6 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) { go cli.Client.Run() - /* - 部分接口需要通过代码信息计算得出 - */ - codesOnce.Do(func() { - //初始化代码管理 - if DefaultCodes == nil { - DefaultCodes, err = NewCodes(cli, "./codes.db") - } - }) - return cli, err } diff --git a/codes.go b/codes.go index 929d8cd..10d08fd 100644 --- a/codes.go +++ b/codes.go @@ -8,17 +8,13 @@ import ( "math" "os" "path/filepath" - "sync" "time" "xorm.io/core" "xorm.io/xorm" ) -// 增加单例,部分数据需要通过Codes里面的信息计算 -var ( - DefaultCodes *Codes - codesOnce sync.Once -) +// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算 +var DefaultCodes *Codes func NewCodes(c *Client, filename string) (*Codes, error) { diff --git a/dial.go b/dial.go index 02ec6b3..fbff896 100644 --- a/dial.go +++ b/dial.go @@ -73,7 +73,8 @@ func NewRangeDial(hosts []string) ios.DialFunc { } if i < len(hosts)-1 { //最后一个错误返回出去 - logs.Err(err) + logs.Err(err, "等待2秒后尝试下一个服务地址...") + <-time.After(time.Second * 2) } } return diff --git a/manage.go b/manage.go index e78148b..f716621 100644 --- a/manage.go +++ b/manage.go @@ -16,19 +16,18 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) { if cfg == nil { cfg = &ManageConfig{} } - if len(cfg.Hosts) == 0 { - cfg.Hosts = Hosts - } if cfg.CodesDir == "" { cfg.CodesDir = DefaultDatabaseDir } if cfg.WorkdayDir == "" { cfg.WorkdayDir = DefaultDatabaseDir } + if cfg.Dial == nil { + cfg.Dial = DialDefault + } //代码 - DefaultCodes = &Codes{} - codesClient, err := DialHostsRange(cfg.Hosts, op...) + codesClient, err := cfg.Dial(op...) if err != nil { return nil, err } @@ -37,18 +36,17 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) { if err != nil { return nil, err } - DefaultCodes = codes //连接池 p, err := NewPool(func() (*Client, error) { - return DialHostsRange(cfg.Hosts, op...) + return cfg.Dial(op...) }, cfg.Number) if err != nil { return nil, err } //工作日 - workdayClient, err := DialHostsRange(cfg.Hosts, op...) + workdayClient, err := cfg.Dial(op...) if err != nil { return nil, err } @@ -85,8 +83,8 @@ func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) { } type ManageConfig struct { - Hosts []string //服务端IP - Number int //客户端数量 - CodesDir string //代码数据库位置 - WorkdayDir string //工作日数据库位置 + Number int //客户端数量 + CodesDir string //代码数据库位置 + WorkdayDir string //工作日数据库位置 + Dial func(op ...client.Option) (cli *Client, err error) //默认连接方式 } diff --git a/protocol/model_kline.go b/protocol/model_kline.go index 258b71e..d133d3a 100644 --- a/protocol/model_kline.go +++ b/protocol/model_kline.go @@ -77,7 +77,10 @@ func (this *Kline) RisePrice() Price { // RiseRate 涨跌比例/涨跌幅,第一个数据不准,仅做参考 func (this *Kline) RiseRate() float64 { - return float64(this.RisePrice()) / float64(this.Open) * 100 + if this.Last == 0 { + return float64(this.Close-this.Open) / float64(this.Open) * 100 + } + return float64(this.Close-this.Last) / float64(this.Last) * 100 } type kline struct{} diff --git a/workday.go b/workday.go index d90f90b..ee8a4b9 100644 --- a/workday.go +++ b/workday.go @@ -77,7 +77,7 @@ func (this *Workday) Update() error { now := time.Now() if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() { - resp, err := this.Client.GetKlineDayAll("sh000001") + resp, err := this.Client.GetIndexDayAll("sh000001") if err != nil { logs.Err(err) return err