Documentation
¶
Overview ¶
Package idletracker contains a context that is done when a server has not had active (stream) connections for some time.
This can be used to stop idle services, and works well in conjunction with socket-activated daemons.
Example ¶
package main
import (
"context"
"log"
"net"
"net/http"
"time"
"github.com/wmark/idletracker"
)
func main() {
ctx, cancelFn := context.WithCancel(context.Background())
lingerCtx := idletracker.NewIdleTracker(ctx, 15*time.Minute)
server := &http.Server{
IdleTimeout: 2 * time.Minute,
ConnState: lingerCtx.ConnState,
}
ln, _ := net.Listen("tcp", "[::1]:0")
go func() {
<-lingerCtx.Done()
server.Shutdown(ctx)
}()
err := server.Serve(ln)
if err != nil && err != http.ErrServerClosed {
// …
log.Fatalf("serve.Serve: %v\n", err)
}
cancelFn()
}
Index ¶
- type IdleTracker
- func (t *IdleTracker) ConnState(conn net.Conn, state http.ConnState)
- func (t *IdleTracker) Deadline() (deadline time.Time, ok bool)
- func (t *IdleTracker) Done() <-chan struct{}
- func (t *IdleTracker) Err() error
- func (*IdleTracker) String() string
- func (t *IdleTracker) Value(key interface{}) interface{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IdleTracker ¶
type IdleTracker struct {
// contains filtered or unexported fields
}
IdleTracker maintains a list of dangling connections and a timer. It can be used in place of a Context with a deadline, to limit any residual work to the lifetime of the managed server.
func NewIdleTracker ¶
func NewIdleTracker(parent context.Context, patience time.Duration) *IdleTracker
NewIdleTracker returns an instance with a running deadline timer. That is, even absent any original connection, the service will have a lifetime.
Don't reuse this as its assumption is that a server that has been torn down won't be revived.
func (*IdleTracker) ConnState ¶
func (t *IdleTracker) ConnState(conn net.Conn, state http.ConnState)
ConnState implements the net/http.Server.ConnState interface.
func (*IdleTracker) Deadline ¶
func (t *IdleTracker) Deadline() (deadline time.Time, ok bool)
Deadline implements the context.Context interface but breaks the promise of always returning the same deadline.
func (*IdleTracker) Done ¶
func (t *IdleTracker) Done() <-chan struct{}
Done implements the context.Context interface.
func (*IdleTracker) Err ¶
func (t *IdleTracker) Err() error
Err implements the context.Context interface.
func (*IdleTracker) String ¶
func (*IdleTracker) String() string
String implements the fmt.Stringer interface.
func (*IdleTracker) Value ¶
func (t *IdleTracker) Value(key interface{}) interface{}
Value implements the context.Context interface.