mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	* update github.com/PuerkitoBio/goquery * update github.com/alecthomas/chroma * update github.com/blevesearch/bleve/v2 * update github.com/caddyserver/certmagic * update github.com/go-enry/go-enry/v2 * update github.com/go-git/go-billy/v5 * update github.com/go-git/go-git/v5 * update github.com/go-redis/redis/v8 * update github.com/go-testfixtures/testfixtures/v3 * update github.com/jaytaylor/html2text * update github.com/json-iterator/go * update github.com/klauspost/compress * update github.com/markbates/goth * update github.com/mattn/go-isatty * update github.com/mholt/archiver/v3 * update github.com/microcosm-cc/bluemonday * update github.com/minio/minio-go/v7 * update github.com/prometheus/client_golang * update github.com/unrolled/render * update github.com/xanzy/go-gitlab * update github.com/yuin/goldmark * update github.com/yuin/goldmark-highlighting Co-authored-by: techknowlogick <techknowlogick@gitea.io>
		
			
				
	
	
		
			129 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
		
			Vendored
		
	
	
	
			
		
		
	
	
			129 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
		
			Vendored
		
	
	
	
| package pool
 | |
| 
 | |
| import (
 | |
| 	"bufio"
 | |
| 	"context"
 | |
| 	"net"
 | |
| 	"sync/atomic"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/go-redis/redis/v8/internal"
 | |
| 	"github.com/go-redis/redis/v8/internal/proto"
 | |
| )
 | |
| 
 | |
| var noDeadline = time.Time{}
 | |
| 
 | |
| type Conn struct {
 | |
| 	usedAt  int64 // atomic
 | |
| 	netConn net.Conn
 | |
| 
 | |
| 	rd *proto.Reader
 | |
| 	bw *bufio.Writer
 | |
| 	wr *proto.Writer
 | |
| 
 | |
| 	Inited    bool
 | |
| 	pooled    bool
 | |
| 	createdAt time.Time
 | |
| }
 | |
| 
 | |
| func NewConn(netConn net.Conn) *Conn {
 | |
| 	cn := &Conn{
 | |
| 		netConn:   netConn,
 | |
| 		createdAt: time.Now(),
 | |
| 	}
 | |
| 	cn.rd = proto.NewReader(netConn)
 | |
| 	cn.bw = bufio.NewWriter(netConn)
 | |
| 	cn.wr = proto.NewWriter(cn.bw)
 | |
| 	cn.SetUsedAt(time.Now())
 | |
| 	return cn
 | |
| }
 | |
| 
 | |
| func (cn *Conn) UsedAt() time.Time {
 | |
| 	unix := atomic.LoadInt64(&cn.usedAt)
 | |
| 	return time.Unix(unix, 0)
 | |
| }
 | |
| 
 | |
| func (cn *Conn) SetUsedAt(tm time.Time) {
 | |
| 	atomic.StoreInt64(&cn.usedAt, tm.Unix())
 | |
| }
 | |
| 
 | |
| func (cn *Conn) SetNetConn(netConn net.Conn) {
 | |
| 	cn.netConn = netConn
 | |
| 	cn.rd.Reset(netConn)
 | |
| 	cn.bw.Reset(netConn)
 | |
| }
 | |
| 
 | |
| func (cn *Conn) Write(b []byte) (int, error) {
 | |
| 	return cn.netConn.Write(b)
 | |
| }
 | |
| 
 | |
| func (cn *Conn) RemoteAddr() net.Addr {
 | |
| 	if cn.netConn != nil {
 | |
| 		return cn.netConn.RemoteAddr()
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
 | |
| 	if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	return fn(cn.rd)
 | |
| }
 | |
| 
 | |
| func (cn *Conn) WithWriter(
 | |
| 	ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
 | |
| ) error {
 | |
| 	if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	if cn.bw.Buffered() > 0 {
 | |
| 		cn.bw.Reset(cn.netConn)
 | |
| 	}
 | |
| 
 | |
| 	if err := fn(cn.wr); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	if err := cn.bw.Flush(); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	internal.WritesCounter.Add(ctx, 1)
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (cn *Conn) Close() error {
 | |
| 	return cn.netConn.Close()
 | |
| }
 | |
| 
 | |
| func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time {
 | |
| 	tm := time.Now()
 | |
| 	cn.SetUsedAt(tm)
 | |
| 
 | |
| 	if timeout > 0 {
 | |
| 		tm = tm.Add(timeout)
 | |
| 	}
 | |
| 
 | |
| 	if ctx != nil {
 | |
| 		deadline, ok := ctx.Deadline()
 | |
| 		if ok {
 | |
| 			if timeout == 0 {
 | |
| 				return deadline
 | |
| 			}
 | |
| 			if deadline.Before(tm) {
 | |
| 				return deadline
 | |
| 			}
 | |
| 			return tm
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	if timeout > 0 {
 | |
| 		return tm
 | |
| 	}
 | |
| 
 | |
| 	return noDeadline
 | |
| }
 |