Skip to content

Commit

Permalink
kite: don't explicitely close sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed Jun 21, 2016
1 parent 58415aa commit c209bfc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
13 changes: 12 additions & 1 deletion bug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"github.com/koding/kite"
)

func TestKite_SeparateKiteClient(t *testing.T) {
func TestKite_MultipleDial(t *testing.T) {
esrv := kite.New("echo-server", "0.0.0")
esrv.Config.DisableAuthentication = true
if err := esrv.Config.ReadEnvironmentVariables(); err != nil {
t.Fatal(err)
}

esrv.HandleFunc("echo", func(r *kite.Request) (interface{}, error) {
var arg string

Expand All @@ -28,6 +32,9 @@ func TestKite_SeparateKiteClient(t *testing.T) {

ts := httptest.NewServer(esrv)
ecli := kite.New("echo-client", "0.0.0")
if err := ecli.Config.ReadEnvironmentVariables(); err != nil {
t.Fatal(err)
}

esrv.SetLogLevel(kite.DEBUG)
ecli.SetLogLevel(kite.DEBUG)
Expand All @@ -38,6 +45,10 @@ func TestKite_SeparateKiteClient(t *testing.T) {
t.Fatalf("dialing echo-server kite error: %s", err)
}

if err := c.Dial(); err != nil {
t.Fatalf("dialing echo-server kite error: %s", err)
}

resp, err := c.Tell("echo", "Hello world!")
if err != nil {
t.Fatalf("Tell()=%s", err)
Expand Down
16 changes: 1 addition & 15 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kite

import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -304,14 +302,6 @@ func (c *Client) RemoteAddr() string {
return websocketsession.RemoteAddr()
}

// randomStringLength is used to generate a session_id.
func randomStringLength(length int) string {
size := (length * 6 / 8) + 1
r := make([]byte, size)
rand.Read(r)
return base64.URLEncoding.EncodeToString(r)[:length]
}

// run consumes incoming dnode messages. Reconnects if necessary.
func (c *Client) run() {
err := c.readLoop()
Expand Down Expand Up @@ -491,7 +481,7 @@ func (c *Client) sendHub() {
// And get rid of the timeout workaround.
c.LocalKite.Log.Error("error sending %q: %s", msg, err)

if err == sockjsclient.ErrSessionClosed {
if err == sockjsclient.ErrSessionClosed || strings.Contains(err.Error(), errClosing) {
return
}
}
Expand Down Expand Up @@ -765,10 +755,6 @@ func (c *Client) getSession() sockjs.Session {

func (c *Client) setSession(session sockjs.Session) {
c.m.Lock()
if c.session != nil {
c.session.Close(3000, "Go away!")
}

c.session = session
c.m.Unlock()
}
Expand Down
10 changes: 5 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"sync"
)

// An error string equivalent to net.errClosing for using with http.Serve()
// during a graceful exit. Needed to declare here again because it is not
// exported by "net" package.
const errClosing = "use of closed network connection"

// Run is a blocking method. It runs the kite server and then accepts requests
// asynchronously. It supports graceful restart via SIGUSR2.
func (k *Kite) Run() {
Expand All @@ -21,11 +26,6 @@ func (k *Kite) Run() {
os.Exit(0)
}

// An error string equivalent to net.errClosing for using with http.Serve()
// during a graceful exit. Needed to declare here again because it is not
// exported by "net" package.
const errClosing = "use of closed network connection"

err := k.listenAndServe()
if err != nil {
if strings.Contains(err.Error(), errClosing) {
Expand Down

0 comments on commit c209bfc

Please sign in to comment.