Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
injoyai committed Feb 18, 2023
1 parent 76da0a3 commit 5daaccb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
31 changes: 13 additions & 18 deletions io_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/injoyai/base/maps"
"github.com/injoyai/conv"
"io"
"sync"
"time"
)

Expand Down Expand Up @@ -54,15 +53,13 @@ func NewClientWithContext(ctx context.Context, i ReadWriteCloser) *Client {
return c
}
c := &Client{
i: i,
tag: maps.NewSafe(),

IReadCloser: NewIReadCloserWithContext(ctx, i),
IWriter: NewWriter(i),

timerKeep: time.NewTimer(0),
timer: time.NewTimer(0),
timeout: 0,
i: i,
tag: maps.NewSafe(),
timerKeep: time.NewTimer(0),
timer: time.NewTimer(0),
timeout: 0,
}

c.SetKey(fmt.Sprintf("%p", i))
Expand All @@ -79,18 +76,16 @@ Client 通用IO客户端
可以作为普通的io.ReadWriteCloser(Run函数不执行)
*/
type Client struct {
i ReadWriteCloser //接口
mu sync.Mutex //锁
tag *maps.Safe //标签

*IReadCloser
*IWriter

timer *time.Timer //超时定时器,时间范围内没有发送数据或者接收数据,则断开链接
timeout time.Duration //超时时间
timerKeep *time.Timer //正常通讯不发送心跳
keepAlive time.Duration //保持连接
createTime time.Time //创建时间,链接成功时间
i ReadWriteCloser //接口
tag *maps.Safe //标签
timer *time.Timer //超时定时器,时间范围内没有发送数据或者接收数据,则断开链接
timeout time.Duration //超时时间
timerKeep *time.Timer //正常通讯不发送心跳
keepAlive time.Duration //保持连接
createTime time.Time //创建时间,链接成功时间
}

//================================Nature================================
Expand Down Expand Up @@ -261,7 +256,7 @@ func (this *Client) Redial(fn ...func(ctx context.Context, c *Client)) *Client {
this.ICloser.Print(NewMessageFormat(" 连接断开(%v),未设置重连或主动关闭", this.ICloser.Err()), TagErr, this.GetKey())
return
}
this.ICloser.Print(NewMessageFormat("连接断开(%v),重连成功", this.ICloser.Err()), TagErr, this.GetKey())
this.ICloser.Print(NewMessageFormat("连接断开(%v),重连成功", this.ICloser.Err()), TagInfo, this.GetKey())
redialFunc := this.IReadCloser.redialFunc
key := this.GetKey()
*this = *NewClient(readWriteCloser)
Expand Down
5 changes: 1 addition & 4 deletions io_i_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,10 @@ func (this *ICloser) Redial(ctx context.Context) ReadWriteCloser {
}
readWriteCloser, err := this.redialFunc()
if err == nil {
if readWriteCloser != nil {
this.IPrinter.Print(NewMessageFormat("连接服务端成功..."), TagInfo, this.GetKey())
}
//上下文关闭
return readWriteCloser
}
this.IPrinter.Print(NewMessageFormat("%v,等待%d秒重试", dealErr(err), t/time.Second), TagErr, this.GetKey())
this.Print(NewMessageFormat("%v,等待%d秒重试", dealErr(err), t/time.Second), TagErr, this.GetKey())
if t < time.Second*32 {
t = 2 * t
timer.Reset(t)
Expand Down
9 changes: 3 additions & 6 deletions io_i_read_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ func NewIReadCloserWithContext(ctx context.Context, readCloser ReadCloser) *IRea
if c, ok := readCloser.(*IReadCloser); ok && c != nil {
return c
}
entity := &IReadCloser{
return &IReadCloser{
IReader: NewIReader(readCloser),
ICloser: NewICloserWithContext(ctx, readCloser),
running: 0,
}
entity.SetCloseFunc(func(ctx context.Context, msg Message) {
entity.cancel()
})
return entity
}

type IReadCloser struct {
Expand All @@ -35,7 +31,6 @@ type IReadCloser struct {
//================================Nature================================

func (this *IReadCloser) SetKey(key string) *IReadCloser {
defer recover()
this.IPrinter.SetKey(key)
this.ICloser.SetKey(key)
return this
Expand Down Expand Up @@ -90,6 +85,8 @@ func (this *IReadCloser) Run() error {
if atomic.SwapUint32(&this.running, 1) == 1 {
return nil
}
//新建客户端时已经能确定连接成功,为了让用户控制是否输出,所以在Run的时候打印
this.Print(NewMessage("连接服务端成功..."), TagInfo, this.GetKey())
for {
select {
case <-this.Done():
Expand Down

0 comments on commit 5daaccb

Please sign in to comment.