11##KMS
22
3- ![ Project status] ( https://img.shields.io/badge/version-1.1 .0-green.svg )
3+ ![ Project status] ( https://img.shields.io/badge/version-1.2 .0-green.svg )
44[ ![ Build Status] ( https://semaphoreci.com/api/v1/joeybloggs/kms/branches/master/badge.svg )] ( https://semaphoreci.com/joeybloggs/kms )
55[ ![ Coverage Status] ( https://coveralls.io/repos/github/go-playground/kms/badge.svg )] ( https://coveralls.io/github/go-playground/kms )
66[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/go-playground/kms )] ( https://goreportcard.com/report/github.com/go-playground/kms )
@@ -15,38 +15,16 @@ Sure there are other libraries, but they are focused on handling graceful shutdo
1515code such as graceful shutdown of an http server, but this library allows you to glue together multiple
1616unrelated(or related) services running within the same process/application.
1717
18- eg.
18+ Doesn't Go 1.8 handle graceful shutdown?
19+ ------------
20+ Yes and No, it does not yet handle Hijacked connections, like WebSockets, see:
21+ - https://github.com/golang/go/issues/4674#issuecomment-257549871
22+ - https://github.com/golang/go/issues/17721#issuecomment-257572027
1923
20- ``` go
21-
22- // start CRON service running tasks..
23- go startCRON ()
24-
25- // listen for shutdown signal(s), non-blocking
26- kms.Listen (false )
27-
28- // serve http site ( using built in http listener)
29- // this site also has WebSocket functionality
30- kmshttp.ListenAndServe (" :3007" , nil )
24+ but this package does; furthermore this package isn't just for graceful http shutdown but graceful shutdown of anything and everything.
3125
32- ```
33-
34- This library will allow you to gracefully shutdown the http server, any WebSockets and any CRON jobs that may be running using the following:
35-
36- ``` go
37-
38- kms.Wait ()
39- kms.Done ()
40-
41- // chaining is also supported eg. defer kms.Wait().Done()
42-
43- <- kms.ShutdownInitiated ()
44-
45- and is some cases
46-
47- <- kms.ShutdownComplete ()
48-
49- ```
26+ When Go 1.8 comes out I will transparently make changes to let the std lib handle idle connections as
27+ it has lower level access to them, but continue to handle Hijacked connections.
5028
5129Installation
5230-----------
@@ -59,77 +37,36 @@ go get -u github.com/go-playground/kms
5937
6038Built In
6139--------
62- There are a few built in graceful shutdown helpers using the kms package for TCP, Unix Sockets and HTTP graceful shutdown.
40+ There are a few built in graceful shutdown helpers using the kms package for:
41+ - TCP
42+ - Unix Sockets
43+ - HTTP(S) graceful shutdown.
6344
64- Example
45+ Examples
6546-------
47+ [ see here] ( https://github.com/go-playground/kms/tree/master/examples ) for more
48+
6649``` go
6750package main
6851
6952import (
70- " fmt"
7153 " net/http"
7254 " time"
7355
7456 " github.com/go-playground/kms"
7557 " github.com/go-playground/kms/kmsnet/kmshttp"
7658)
7759
78- var (
79- // simple variable to prove the CRON job finishes gracefully.
80- complete bool
81- )
82-
8360func main () {
8461
85- go fakeWebsocketHandler ()
86- go cronJob ()
87-
88- kms.Listen (false )
62+ // listen for shutdown signal(s) with timeout, non-blocking
63+ kms.ListenTimeout (false , time.Minute *3 )
8964
9065 http.HandleFunc (" /" , func (w http.ResponseWriter , r *http.Request ) {
9166 w.Write ([]byte (" Home" ))
9267 })
9368
9469 kmshttp.ListenAndServe (" :3007" , nil )
95-
96- fmt.Println (" CRON completed gracefully? " , complete)
97- }
98-
99- // crude CRON job examples
100- func cronJob () {
101-
102- // a long running job that fires every x minutes
103- for {
104- time.Sleep (time.Second * 1 )
105- kms.Wait ()
106- complete = false
107-
108- // long running DB calls
109- time.Sleep (time.Second * 10 )
110-
111- complete = true
112- kms.Done ()
113- }
114- }
115-
116- // faking a single WebSocket, just to show how
117- func fakeWebsocketHandler () {
118-
119- message := make (chan []byte )
120-
121- FOR:
122- for {
123- select {
124- case <- kms.ShutdownInitiated ():
125- close (message)
126- // close WebSocket connection here
127- break FOR
128- case b := <- message:
129- fmt.Println (string (b))
130- }
131- }
132-
13370}
13471```
13572
0 commit comments