forked from valyala/fastrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_ctx.go
120 lines (99 loc) · 2.69 KB
/
request_ctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package tlv
import (
"bufio"
"fmt"
"github.com/valyala/fasthttp"
"net"
"sync"
)
// RequestCtx implements fastrpc.HandlerCtx
type RequestCtx struct {
// ConcurrencyLimitErrorHandler is called each time concurrency limit
// is reached on the fastrpc.Server.
ConcurrencyLimitErrorHandler func(ctx *RequestCtx, concurrency int)
Request Request
Response Response
conn net.Conn
logger ctxLogger
}
// ConcurrencyLimitError implements the corresponding method
// of fastrpc.HandlerCtx.
func (ctx *RequestCtx) ConcurrencyLimitError(concurrency int) {
if ctx.ConcurrencyLimitErrorHandler != nil {
ctx.ConcurrencyLimitErrorHandler(ctx, concurrency)
}
}
// Init implements the corresponding method of fastrpc.HandlerCtx.
func (ctx *RequestCtx) Init(conn net.Conn, logger fasthttp.Logger) {
ctx.Request.Reset()
ctx.Response.Reset()
ctx.conn = conn
ctx.logger.ctx = ctx
ctx.logger.logger = logger
}
// ReadRequest implements the corresponding method of fastrpc.HandlerCtx.
func (ctx *RequestCtx) ReadRequest(br *bufio.Reader) error {
return ctx.Request.ReadRequest(br)
}
// WriteResponse implements the corresponding method of fastrpc.HandlerCtx.
func (ctx *RequestCtx) WriteResponse(bw *bufio.Writer) error {
return ctx.Response.WriteResponse(bw)
}
// Conn returns connection associated with the current RequestCtx.
func (ctx *RequestCtx) Conn() net.Conn {
return ctx.conn
}
// Logger returns logger associated with the current RequestCtx.
func (ctx *RequestCtx) Logger() fasthttp.Logger {
return &ctx.logger
}
// Write appends p to ctx.Response's value.
//
// It implements io.Writer.
func (ctx *RequestCtx) Write(p []byte) (int, error) {
return ctx.Response.Write(p)
}
// RemoteAddr returns client address for the given request.
//
// Always returns non-nil result.
func (ctx *RequestCtx) RemoteAddr() net.Addr {
addr := ctx.conn.RemoteAddr()
if addr == nil {
return zeroTCPAddr
}
return addr
}
// LocalAddr returns server address for the given request.
//
// Always returns non-nil result.
func (ctx *RequestCtx) LocalAddr() net.Addr {
addr := ctx.conn.LocalAddr()
if addr == nil {
return zeroTCPAddr
}
return addr
}
// RemoteIP returns client ip for the given request.
//
// Always returns non-nil result.
func (ctx *RequestCtx) RemoteIP() net.IP {
x, ok := ctx.RemoteAddr().(*net.TCPAddr)
if !ok {
return net.IPv4zero
}
return x.IP
}
var zeroTCPAddr = &net.TCPAddr{
IP: net.IPv4zero,
}
type ctxLogger struct {
sync.Mutex
ctx *RequestCtx
logger fasthttp.Logger
}
func (cl *ctxLogger) Printf(format string, args ...interface{}) {
cl.Lock()
defer cl.Unlock()
ctx, msg := cl.ctx, fmt.Sprintf(format, args...)
cl.logger.Printf("%s<->%s - %s", ctx.LocalAddr(), ctx.RemoteAddr(), msg)
}