Skip to content

Commit b561f98

Browse files
Merge pull request #10 from goinsane/develop
v1.2.3
2 parents 6d650bb + 59c5bd3 commit b561f98

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2019, go-accepter authors.
1+
Copyright (c) 2019, accepter authors.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without
@@ -8,7 +8,7 @@ modification, are permitted provided that the following conditions are met:
88
* Redistributions in binary form must reproduce the above copyright
99
notice, this list of conditions and the following disclaimer in the
1010
documentation and/or other materials provided with the distribution.
11-
* Neither the name of the go-accepter authors nor the
11+
* Neither the name of the accepter authors nor the
1212
names of its contributors may be used to endorse or promote products
1313
derived from this software without specific prior written permission.
1414

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# accepter
22

3-
[![GoDoc](https://godoc.org/github.com/orkunkaraduman/go-accepter?status.svg)](https://godoc.org/github.com/orkunkaraduman/go-accepter)
3+
[![GoDoc](https://godoc.org/github.com/goinsane/accepter?status.svg)](https://godoc.org/github.com/goinsane/accepter)
44

55
Package accepter provides an Accepter and utilities for net.Listener.
66
It is similar with GoLang's http.Server.
@@ -11,6 +11,6 @@ It is similar with GoLang's http.Server.
1111
* changed Handler.Serve arguments to (ctx, conn) from (conn, closeCh)
1212
* removed panic recovering for Handler.Serve(...)
1313
* removed Accepter.ErrorLog
14-
* removed TCPListenAndServe and TCPListenAndServeTLS
15-
* added ListenAndServe and ListenAndServeTLS
14+
* added ListenAndServe and ListenAndServeTLS instead of TCPListenAndServe and TCPListenAndServeTLS
1615
* added go.mod support
16+
* added SetMaxTempDelay(...)

accepter.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/tls"
77
"net"
88
"sync"
9+
"sync/atomic"
910
"time"
1011
)
1112

@@ -27,6 +28,16 @@ type Accepter struct {
2728
connsMu sync.RWMutex
2829
}
2930

31+
var (
32+
maxTempDelay time.Duration
33+
)
34+
35+
// SetMaxTempDelay sets maximum temporary error wait duration as concurrent-safe.
36+
// Zero or negative values mean to wait forever. By default, zero.
37+
func SetMaxTempDelay(d time.Duration) {
38+
atomic.StoreInt64((*int64)(&maxTempDelay), int64(d))
39+
}
40+
3041
// Shutdown gracefully shuts down the Accepter without interrupting any
3142
// connections. Shutdown works by first closing the Accepter's underlying Listener, then
3243
// cancels the context on Serve method of Handler, and then waiting indefinitely for
@@ -127,7 +138,7 @@ func (a *Accepter) Serve(lis net.Listener) (err error) {
127138
a.ctx, a.ctxCancel = context.WithCancel(context.Background())
128139
defer a.ctxCancel()
129140
a.conns = make(map[net.Conn]struct{})
130-
var tempDelay time.Duration
141+
var tempDelay, totalDelay time.Duration
131142
for {
132143
var conn net.Conn
133144
conn, err = lis.Accept()
@@ -139,6 +150,10 @@ func (a *Accepter) Serve(lis net.Listener) (err error) {
139150
default:
140151
}
141152
if ne, ok := err.(net.Error); ok && ne.Temporary() {
153+
maxDelay := time.Duration(atomic.LoadInt64((*int64)(&maxTempDelay)))
154+
if maxDelay > 0 && totalDelay > maxDelay {
155+
return
156+
}
142157
if tempDelay == 0 {
143158
tempDelay = 5 * time.Millisecond
144159
} else {
@@ -148,11 +163,13 @@ func (a *Accepter) Serve(lis net.Listener) (err error) {
148163
tempDelay = max
149164
}
150165
time.Sleep(tempDelay)
166+
totalDelay += tempDelay
151167
continue
152168
}
153169
return
154170
}
155171
tempDelay = 0
172+
totalDelay = 0
156173
go a.serve(conn)
157174
}
158175
}

examples/echo/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"log"
88
"net"
99

10-
accepter "github.com/orkunkaraduman/go-accepter"
10+
"github.com/goinsane/accepter"
1111
)
1212

1313
func main() {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/orkunkaraduman/go-accepter
1+
module github.com/goinsane/accepter
22

33
go 1.1

0 commit comments

Comments
 (0)