Commit ee22cae5 authored by alexstocks's avatar alexstocks

modify readme

parent 3d7706a1
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
> >
> 2 add clause "this.attrs = nil" in session.go:(Session)Close > 2 add clause "this.attrs = nil" in session.go:(Session)Close
> >
> 3 modify session.go:Session{*gettyConn} to session.go:Session{gettyConn} > 3 session.go:Session{*gettyConn, readDeadline, writeDeadline} -> session.go:Session{gettyConn, rDeadline, wDeadline}
> >
> 4 session.go:(Session)Reset > 4 session.go:(Session)Reset
> >
......
...@@ -24,7 +24,7 @@ import ( ...@@ -24,7 +24,7 @@ import (
const ( const (
maxReadBufLen = 4 * 1024 maxReadBufLen = 4 * 1024
netIOTimeout = 100e6 // 100ms netIOTimeout = 100e6 // 100ms
cronPeriod = 60 * 1e9 // 1 minute peroid = 60 * 1e9 // 1 minute
pendingDuration = 3e9 pendingDuration = 3e9
defaultSessionName = "Session" defaultSessionName = "Session"
outputFormat = "session %s, Read Count: %d, Write Count: %d, Read Pkg Count: %d, Write Pkg Count: %d" outputFormat = "session %s, Read Count: %d, Write Count: %d, Read Pkg Count: %d, Write Pkg Count: %d"
...@@ -99,12 +99,12 @@ type Session struct { ...@@ -99,12 +99,12 @@ type Session struct {
done chan struct{} done chan struct{}
readerDone chan struct{} // end reader readerDone chan struct{} // end reader
cronPeriod time.Duration peroid time.Duration
readDeadline time.Duration rDeadline time.Duration
writeDeadline time.Duration wDeadline time.Duration
wait time.Duration wait time.Duration
rQ chan interface{} rQ chan interface{}
wQ chan interface{} wQ chan interface{}
// attribute // attribute
attrs map[string]interface{} attrs map[string]interface{}
...@@ -115,15 +115,15 @@ type Session struct { ...@@ -115,15 +115,15 @@ type Session struct {
func NewSession(conn net.Conn) *Session { func NewSession(conn net.Conn) *Session {
return &Session{ return &Session{
name: defaultSessionName, name: defaultSessionName,
gettyConn: newGettyConn(conn), gettyConn: newGettyConn(conn),
done: make(chan struct{}), done: make(chan struct{}),
readerDone: make(chan struct{}), readerDone: make(chan struct{}),
cronPeriod: cronPeriod, peroid: peroid,
readDeadline: netIOTimeout, rDeadline: netIOTimeout,
writeDeadline: netIOTimeout, wDeadline: netIOTimeout,
wait: pendingDuration, wait: pendingDuration,
attrs: make(map[string]interface{}), attrs: make(map[string]interface{}),
} }
} }
...@@ -131,9 +131,9 @@ func (this *Session) Reset() { ...@@ -131,9 +131,9 @@ func (this *Session) Reset() {
this.name = defaultSessionName this.name = defaultSessionName
this.done = make(chan struct{}) this.done = make(chan struct{})
this.readerDone = make(chan struct{}) this.readerDone = make(chan struct{})
this.cronPeriod = cronPeriod this.peroid = peroid
this.readDeadline = netIOTimeout this.rDeadline = netIOTimeout
this.writeDeadline = netIOTimeout this.wDeadline = netIOTimeout
this.wait = pendingDuration this.wait = pendingDuration
this.attrs = make(map[string]interface{}) this.attrs = make(map[string]interface{})
} }
...@@ -179,7 +179,7 @@ func (this *Session) SetCronPeriod(period int) { ...@@ -179,7 +179,7 @@ func (this *Session) SetCronPeriod(period int) {
} }
this.lock.Lock() this.lock.Lock()
this.cronPeriod = time.Duration(period) * time.Millisecond this.peroid = time.Duration(period) * time.Millisecond
this.lock.Unlock() this.lock.Unlock()
} }
...@@ -205,26 +205,26 @@ func (this *Session) SetWQLen(writeQLen int) { ...@@ -205,26 +205,26 @@ func (this *Session) SetWQLen(writeQLen int) {
this.lock.Unlock() this.lock.Unlock()
} }
func (this *Session) SetReadDeadline(readDeadline time.Duration) { func (this *Session) SetReadDeadline(rDeadline time.Duration) {
if readDeadline < 1 { if rDeadline < 1 {
panic("@readDeadline < 1") panic("@rDeadline < 1")
} }
this.lock.Lock() this.lock.Lock()
this.readDeadline = readDeadline this.rDeadline = rDeadline
if this.writeDeadline == 0 { if this.wDeadline == 0 {
this.writeDeadline = readDeadline this.wDeadline = rDeadline
} }
this.lock.Unlock() this.lock.Unlock()
} }
func (this *Session) SetWriteDeadline(writeDeadline time.Duration) { func (this *Session) SetWriteDeadline(wDeadline time.Duration) {
if writeDeadline < 1 { if wDeadline < 1 {
panic("@writeDeadline < 1") panic("@wDeadline < 1")
} }
this.lock.Lock() this.lock.Lock()
this.writeDeadline = writeDeadline this.wDeadline = wDeadline
this.lock.Unlock() this.lock.Unlock()
} }
...@@ -312,7 +312,7 @@ func (this *Session) WritePkg(pkg interface{}) error { ...@@ -312,7 +312,7 @@ func (this *Session) WritePkg(pkg interface{}) error {
// for codecs // for codecs
func (this *Session) WriteBytes(pkg []byte) error { func (this *Session) WriteBytes(pkg []byte) error {
this.conn.SetWriteDeadline(time.Now().Add(this.readDeadline)) this.conn.SetWriteDeadline(time.Now().Add(this.rDeadline))
_, err := this.write(pkg) _, err := this.write(pkg)
return err return err
} }
...@@ -373,7 +373,7 @@ func (this *Session) handleLoop() { ...@@ -373,7 +373,7 @@ func (this *Session) handleLoop() {
this.listener.OnOpen(this) this.listener.OnOpen(this)
} }
ticker = time.NewTicker(this.cronPeriod) ticker = time.NewTicker(this.peroid)
LOOP: LOOP:
for { for {
select { select {
...@@ -469,7 +469,7 @@ func (this *Session) handlePackage() { ...@@ -469,7 +469,7 @@ func (this *Session) handlePackage() {
if exit { if exit {
break // 退出前不再读取任何packet,跳到下个for-loop处理完pktBuf中的stream break // 退出前不再读取任何packet,跳到下个for-loop处理完pktBuf中的stream
} }
this.conn.SetReadDeadline(time.Now().Add(this.readDeadline)) this.conn.SetReadDeadline(time.Now().Add(this.rDeadline))
len, err = this.read(buf) len, err = this.read(buf)
if err != nil { if err != nil {
if nerr, ok = err.(net.Error); ok && nerr.Timeout() { if nerr, ok = err.(net.Error); ok && nerr.Timeout() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment