Skip to content

Commit d3512f8

Browse files
committed
refactor: some code improves
1 parent c9bb37f commit d3512f8

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

atreugo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import (
1313

1414
var (
1515
tcpNetworks = []string{"tcp", "tcp4", "tcp6"}
16-
validNetworks = append(tcpNetworks, []string{"unix"}...)
16+
validNetworks = append(tcpNetworks, "unix")
1717

1818
defaultLogger Logger = log.New(os.Stderr, "", log.LstdFlags)
1919
)
2020

2121
// New create a new instance of Atreugo Server.
2222
func New(cfg Config) *Atreugo {
2323
if cfg.Network != "" && !strings.Include(validNetworks, cfg.Network) {
24-
panic("Invalid network: " + cfg.Network)
24+
panic("invalid network: " + cfg.Network)
2525
}
2626

2727
if cfg.Network == "" {
@@ -250,7 +250,7 @@ func (s *Atreugo) Serve(ln net.Listener) error {
250250
// If you pass multiples hostnames, all of them will have the same behaviour.
251251
func (s *Atreugo) NewVirtualHost(hostnames ...string) *Router {
252252
if len(hostnames) == 0 {
253-
panic("At least 1 hostname is required")
253+
panic("at least 1 hostname is required")
254254
}
255255

256256
if s.virtualHosts == nil {
@@ -264,7 +264,7 @@ func (s *Atreugo) NewVirtualHost(hostnames ...string) *Router {
264264

265265
for _, name := range hostnames {
266266
if s.virtualHosts[name] != nil {
267-
panicf("a router is already registered for virtual host '%s'", name)
267+
panicf("a router is already registered for virtual host: %s", name)
268268
}
269269

270270
s.virtualHosts[name] = vHost.router.Handler

atreugo_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,15 @@ func TestAtreugo_NewVirtualHost(t *testing.T) { //nolint:funlen
562562
conflictHosts := []conflictArgs{
563563
{
564564
hostnames: []string{hostname},
565-
wantErrMsg: fmt.Sprintf("a router is already registered for virtual host '%s'", hostname),
565+
wantErrMsg: fmt.Sprintf("a router is already registered for virtual host: %s", hostname),
566566
},
567567
{
568568
hostnames: []string{},
569-
wantErrMsg: "At least 1 hostname is required",
569+
wantErrMsg: "at least 1 hostname is required",
570570
},
571571
{
572572
hostnames: []string{"localhost", "localhost"},
573-
wantErrMsg: fmt.Sprintf("a router is already registered for virtual host '%s'", hostname),
573+
wantErrMsg: fmt.Sprintf("a router is already registered for virtual host: %s", hostname),
574574
},
575575
}
576576

atreugo_unix.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
// ServeGracefully serves incoming connections from the given listener with graceful shutdown
1313
//
1414
// It's blocked until the given listener returns permanent error.
15-
//
16-
// WARNING: Windows is not supportted.
1715
func (s *Atreugo) ServeGracefully(ln net.Listener) error {
1816
s.cfg.GracefulShutdown = true
1917

context.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212
"github.com/valyala/fasthttp"
1313
)
1414

15-
var attachedCtxKey = fmt.Sprintf("__attachedCtx::%s__", bytes.Rand(make([]byte, 15)))
15+
var (
16+
attachedCtxKey = fmt.Sprintf("__attachedCtx::%s__", bytes.Rand(make([]byte, 15)))
1617

17-
var requestCtxPool = &sync.Pool{
18-
New: func() interface{} {
19-
return new(RequestCtx)
20-
},
21-
}
18+
requestCtxPool = sync.Pool{
19+
New: func() interface{} {
20+
return new(RequestCtx)
21+
},
22+
}
23+
)
2224

2325
// AcquireRequestCtx returns an empty RequestCtx instance from request context pool.
2426
//
@@ -110,19 +112,16 @@ func (ctx *RequestCtx) MatchedRoutePath() []byte {
110112
//
111113
// instead of:
112114
//
113-
// ctx.AttachContext(context.WithValue(context.Background(), "myKey", "myValue"))
115+
// ctx.AttachContext(context.WithValue(myCtx, "myKey", "myValue"))
114116
// ctx.Value("myKey")
115117
//
116118
// to avoid extra allocation.
117119
func (ctx *RequestCtx) Value(key interface{}) interface{} {
118120
if atomic.CompareAndSwapInt32(&ctx.searchingOnAttachedCtx, 0, 1) {
119121
defer atomic.StoreInt32(&ctx.searchingOnAttachedCtx, 0)
120122

121-
extraCtx := ctx.AttachedContext()
122-
if extraCtx != nil {
123-
val := extraCtx.Value(key)
124-
125-
return val
123+
if extraCtx := ctx.AttachedContext(); extraCtx != nil {
124+
return extraCtx.Value(key)
126125
}
127126
}
128127

listener_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (s *Atreugo) getListener() (net.Listener, error) {
1717

1818
if s.cfg.Network == "unix" {
1919
if err := os.Remove(s.cfg.Addr); err != nil && !os.IsNotExist(err) {
20-
return nil, wrapErrorf(err, "unexpected error when trying to remove unix socket file %q", s.cfg.Addr)
20+
return nil, wrapErrorf(err, "unexpected error when trying to remove unix socket file: %q", s.cfg.Addr)
2121
}
2222
}
2323

@@ -28,7 +28,7 @@ func (s *Atreugo) getListener() (net.Listener, error) {
2828

2929
if s.cfg.Network == "unix" {
3030
if err := s.cfg.chmodUnixSocketFunc(s.cfg.Addr); err != nil {
31-
return nil, err
31+
return nil, wrapError(err, "failed to chmod unix socket file")
3232
}
3333
}
3434

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func (r *Router) ServeFile(url, filePath string) *Path {
387387
// communication with a proxy).
388388
func (r *Router) Path(method, url string, viewFn View) *Path {
389389
if method != strings.ToUpper(method) {
390-
panic("The http method '" + method + "' must be in uppercase")
390+
panicf("http method '%s' must be in uppercase", method)
391391
}
392392

393393
p := &Path{

0 commit comments

Comments
 (0)