-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修改函数名NewWriter为NewIWriter,增加IWriteCloser
- Loading branch information
1 parent
7bc014b
commit 70a98db
Showing
6 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
) | ||
|
||
func NewIWriteCloser(writeCloser WriteCloser) *IWriteCloser { | ||
return NewIWriteCloserWithContext(context.Background(), writeCloser) | ||
} | ||
|
||
func NewIWriteCloserWithContext(ctx context.Context, writeCloser WriteCloser) *IWriteCloser { | ||
return &IWriteCloser{ | ||
IWriter: NewIWriter(writeCloser), | ||
ICloser: NewICloserWithContext(ctx, writeCloser), | ||
} | ||
} | ||
|
||
type IWriteCloser struct { | ||
*IWriter | ||
*ICloser | ||
queue chan []byte //写入队列 | ||
running uint32 //是否在运行 | ||
} | ||
|
||
func (this *IWriteCloser) GetKey() string { | ||
return this.IWriter.GetKey() | ||
} | ||
|
||
// SetKey 设置唯一标识 | ||
func (this *IWriteCloser) SetKey(key string) *IWriteCloser { | ||
this.IWriter.SetKey(key) | ||
this.ICloser.SetKey(key) | ||
return this | ||
} | ||
|
||
// SetPrintFunc 设置打印函数 | ||
func (this *IWriteCloser) SetPrintFunc(fn PrintFunc) *IWriteCloser { | ||
this.IWriter.SetPrintFunc(fn) | ||
this.ICloser.SetPrintFunc(fn) //错误信息按ASCII编码? | ||
return this | ||
} | ||
|
||
func (this *IWriteCloser) Debug(b ...bool) *IWriteCloser { | ||
this.IWriter.Debug(b...) | ||
this.ICloser.Debug(b...) | ||
return this | ||
} | ||
|
||
// WriteQueue 写入队列 | ||
func (this *IWriteCloser) WriteQueue(p []byte) *IWriteCloser { | ||
this.runQueue() | ||
this.queue <- p | ||
return this | ||
} | ||
|
||
// TryWriteQueue 尝试写入队列 | ||
func (this *IWriteCloser) TryWriteQueue(p []byte) *IWriteCloser { | ||
this.runQueue() | ||
select { | ||
case this.queue <- p: | ||
default: | ||
} | ||
return this | ||
} | ||
|
||
func (this *IWriteCloser) runQueue() { | ||
if this.queue == nil { | ||
this.queue = this.NewWriteQueue(this.Ctx()) | ||
} | ||
if atomic.SwapUint32(&this.running, 1) == 0 { | ||
go this.For(func() error { | ||
_, err := this.Write(<-this.queue) | ||
return err | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters