Example #1
0
File: queue.go Project: nbusy/nbusy
// NewQueue creates a new queue object.
func NewQueue() Queue {
	q := Queue{
		conns:   cmap.New(),
		reqs:    cmap.New(),
		mutexes: cmap.New(),
	}

	return q
}
Example #2
0
File: conn.go Project: nbusy/nbusy
// NewConn creates a new Conn object.
func NewConn() (*Conn, error) {
	id, err := shortid.UUID()
	if err != nil {
		return nil, err
	}

	c := &Conn{
		ID:             id,
		Session:        cmap.New(),
		resRoutes:      cmap.New(),
		deadline:       time.Second * time.Duration(300),
		disconnHandler: func(c *Conn) {},
	}
	c.connected.Store(false)
	return c, nil
}
Example #3
0
// NewServer creates a new Neptulon server.
// addr should be formatted as host:port (i.e. 127.0.0.1:3000)
func NewServer(addr string) *Server {
	s := &Server{
		addr:           addr,
		conns:          cmap.New(),
		disconnHandler: func(c *Conn) {},
	}
	s.running.Store(false)
	return s
}
Example #4
0
// NewSender creates a new Sender middleware.
func NewSender(m *Middleware, send func(connID string, msg []byte) error) Sender {
	s := Sender{
		send:      send,
		resRoutes: cmap.New(),
		m:         m,
	}

	return s
}
Example #5
0
func newReqCtx(conn *Conn, id, method string, params json.RawMessage, mw []func(ctx *ReqCtx) error) *ReqCtx {
	return &ReqCtx{
		Conn:    conn,
		Session: cmap.New(),
		ID:      id,
		Method:  method,
		params:  params,
		mw:      mw,
	}
}
Example #6
0
// Example demonstrating the use concurrent-map.
func Example() {
	m := cmap.New()
	m.Set("foo", "bar")

	if val, ok := m.GetOk("foo"); ok {
		bar := val.(string)
		log.Println(bar)
	}

	m.Delete("foo")
}
Example #7
0
func TestPrefix(t *testing.T) {
	out := captureOutput(func() {
		err := LoggerWithPrefix("test123")(&neptulon.ReqCtx{Session: cmap.New()})
		if err != nil {
			t.Fatal("didn't expect error from logger")
		}
	})

	if !strings.Contains(out, "test123") {
		log.Fatalf("malformed log output: %v", out)
	}
}
Example #8
0
func TestErrResLog(t *testing.T) {
	ctx := &neptulon.ReqCtx{
		Session: cmap.New(),
		ID:      "1234",
		Method:  "wow.method",
		Err:     &neptulon.ResError{Code: 98765},
	}

	out := captureOutput(func() {
		err := Logger(ctx)
		if err != nil {
			t.Fatal("didn't expect error from logger")
		}
	})

	if strings.Contains(out, "my response") || !strings.Contains(out, "98765") {
		log.Fatalf("malformed log output: %v", out)
	}
}