当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
55 Star 269 Fork 100

fagongzi/gateway
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
io.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
张旭 提交于 2017-06-29 21:17 . dev: modify log package
package proxy
import (
"io"
"net/http"
"sync"
"time"
"github.com/valyala/fasthttp"
)
type writeFlusher interface {
io.Writer
http.Flusher
}
type maxLatencyWriter struct {
dst writeFlusher
latency time.Duration
lk sync.Mutex // protects Write + Flush
done chan bool
}
func (m *maxLatencyWriter) Write(p []byte) (int, error) {
m.lk.Lock()
defer m.lk.Unlock()
return m.dst.Write(p)
}
func (m *maxLatencyWriter) flushLoop() {
t := time.NewTicker(m.latency)
defer t.Stop()
for {
select {
case <-m.done:
if onExitFlushLoop != nil {
onExitFlushLoop()
}
return
case <-t.C:
m.lk.Lock()
m.dst.Flush()
m.lk.Unlock()
}
}
}
func (m *maxLatencyWriter) stop() { m.done <- true }
// onExitFlushLoop is a callback set by tests to detect the state of the
// flushLoop() goroutine.
var onExitFlushLoop func()
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
type requestCanceler interface {
CancelRequest(*http.Request)
}
type runOnFirstRead struct {
io.Reader
fn func() // Run before first Read, then set to nil
}
func (c *runOnFirstRead) Read(bs []byte) (int, error) {
if c.fn != nil {
c.fn()
c.fn = nil
}
return c.Reader.Read(bs)
}
func copyRequest(req *fasthttp.Request) *fasthttp.Request {
newreq := fasthttp.AcquireRequest()
newreq.Reset()
req.CopyTo(newreq)
return newreq
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fagongzi/gateway.git
git@gitee.com:fagongzi/gateway.git
fagongzi
gateway
gateway
v2.0.0-beta

搜索帮助

0d507c66 1850385 C8b1a773 1850385