Skip to content

Commit

Permalink
introduce const
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthPestilane committed Sep 10, 2021
1 parent 05ed5b1 commit 1b2934a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 11 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,29 @@ type ServerOption struct {
WriteTimeout time.Duration // sets the timeout for connection write.
Packer Packer // packs and unpacks packet payload, default packer is the packet.DefaultPacker.
Codec Codec // encodes and decodes the message data, can be nil.
RespQueueSize int // sets the response channel size of session, 1024 will be used if < 0.
ReqQueueSize int // sets the request channel size of router, 1024 will be used if < 0.
ReqQueueSize int // sets the request channel size of router, DefaultReqQueueSize will be used if < 0.
RespQueueSize int // sets the response channel size of session, DefaultRespQueueSize will be used if < 0.
DoNotPrintRoutes bool // whether to print registered route handlers to the console.
}

// ErrServerStopped is returned when server stopped.
var ErrServerStopped = fmt.Errorf("server stopped")

const (
DefaultReqQueueSize = 1024
DefaultRespQueueSize = 1024
)

// NewServer creates a Server according to opt.
func NewServer(opt *ServerOption) *Server {
if opt.Packer == nil {
opt.Packer = NewDefaultPacker()
}
if opt.RespQueueSize < 0 {
opt.RespQueueSize = 1024
}
if opt.ReqQueueSize < 0 {
opt.ReqQueueSize = 1024
opt.ReqQueueSize = DefaultRespQueueSize
}
if opt.RespQueueSize < 0 {
opt.RespQueueSize = DefaultReqQueueSize
}
return &Server{
socketReadBufferSize: opt.SocketReadBufferSize,
Expand Down
12 changes: 9 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import (

func TestNewServer(t *testing.T) {
s := NewServer(&ServerOption{
ReadTimeout: 0,
WriteTimeout: 0,
Codec: &JsonCodec{},
ReadTimeout: 0,
WriteTimeout: 0,
Codec: &JsonCodec{},
ReqQueueSize: -1,
RespQueueSize: -1,
})
assert.NotNil(t, s.accepting)
assert.IsType(t, s.Packer, NewDefaultPacker())
assert.Equal(t, s.Codec, &JsonCodec{})
assert.Equal(t, s.respQueueSize, DefaultRespQueueSize)
assert.Equal(t, cap(s.router.reqQueue), DefaultReqQueueSize)
assert.NotNil(t, s.accepting)
assert.NotNil(t, s.stopped)
}

func TestServer_Serve(t *testing.T) {
Expand Down

0 comments on commit 1b2934a

Please sign in to comment.