PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/server/stamped.go

http://github.com/petar/GoHTTP
Go | 94 lines | 71 code | 15 blank | 8 comment | 0 complexity | a49cc41df34cf5b55149dfa76cfb84d2 MD5 | raw file
 1// Copyright 2009 The Go Authors. All rights reserved.
 2// Use of this source code is governed by a BSD-style
 3// license that can be found in the LICENSE file.
 4
 5package server
 6
 7import (
 8	"bufio"
 9	"net"
10	"sync"
11	"time"
12	"net/http"
13	"net/http/httputil"
14)
15
16// StampedServerConn is an httputil.ServerConn which additionally
17// keeps track of the last time the connection performed I/O.
18type StampedServerConn struct {
19	*httputil.ServerConn
20	stamp int64
21	lk    sync.Mutex
22}
23
24func NewStampedServerConn(c net.Conn, r *bufio.Reader) *StampedServerConn {
25	return &StampedServerConn{
26		ServerConn: http.NewServerConn(c, r),
27		stamp:      time.Nanoseconds(),
28	}
29}
30
31func (ssc *StampedServerConn) touch() {
32	ssc.lk.Lock()
33	defer ssc.lk.Unlock()
34	ssc.stamp = time.Nanoseconds()
35}
36
37func (ssc *StampedServerConn) GetStamp() int64 {
38	ssc.lk.Lock()
39	defer ssc.lk.Unlock()
40	return ssc.stamp
41}
42
43func (ssc *StampedServerConn) Read() (req *http.Request, err error) {
44	ssc.touch()
45	defer ssc.touch()
46	return ssc.ServerConn.Read()
47}
48
49func (ssc *StampedServerConn) Write(req *http.Request, resp *http.Response) (err error) {
50	ssc.touch()
51	defer ssc.touch()
52	return ssc.ServerConn.Write(req, resp)
53}
54
55// StampedClientConn is an httputil.ClientConn which additionally
56// keeps track of the last time the connection performed I/O.
57type StampedClientConn struct {
58	*httputil.ClientConn
59	stamp int64
60	lk    sync.Mutex
61}
62
63func NewStampedClientConn(c net.Conn, r *bufio.Reader) *StampedClientConn {
64	return &StampedClientConn{
65		ClientConn: http.NewClientConn(c, r),
66		stamp:      time.Nanoseconds(),
67	}
68}
69
70func (scc *StampedClientConn) touch() {
71	scc.lk.Lock()
72	defer scc.lk.Unlock()
73	scc.stamp = time.Nanoseconds()
74}
75
76func (scc *StampedClientConn) GetStamp() int64 {
77	scc.lk.Lock()
78	defer scc.lk.Unlock()
79	return scc.stamp
80}
81
82func (scc *StampedClientConn) Read(req *http.Request) (resp *http.Response, err error) {
83	scc.touch()
84	defer scc.touch()
85	return scc.ClientConn.Read(req)
86}
87
88func (scc *StampedClientConn) Write(req *http.Request) (err error) {
89	scc.touch()
90	defer scc.touch()
91	return scc.ClientConn.Write(req)
92}
93
94// XXX: Should ClientConn.Do be wrapped as well?