Skip to content

Commit 82cde88

Browse files
committed
优化代码
1 parent 14db895 commit 82cde88

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

http/server/handler.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type httpServer struct {
4242
prefix *btree.BTree
4343
filter Filter
4444
listener net.Listener
45-
sync.RWMutex
45+
mu sync.RWMutex
4646
}
4747

4848
func newHTTPServer() *httpServer {
@@ -94,8 +94,8 @@ func RegisterHandler(call func(http.ResponseWriter, *http.Request), method, path
9494
call: call,
9595
}
9696

97-
server.Lock()
98-
defer server.Unlock()
97+
server.mu.Lock()
98+
defer server.mu.Unlock()
9999

100100
if _, ok := server.path[h.path]; ok {
101101
return errors.Errorf("exist url:%v %v", method, path)
@@ -157,8 +157,8 @@ func register(obj interface{}, path string, isPrefix bool) error {
157157
path = "/" + path
158158
}
159159

160-
server.Lock()
161-
defer server.Unlock()
160+
server.mu.Lock()
161+
defer server.mu.Unlock()
162162

163163
rv := reflect.ValueOf(obj)
164164
for i := 0; i < rv.NumMethod(); i++ {
@@ -224,8 +224,8 @@ func register(obj interface{}, path string, isPrefix bool) error {
224224

225225
//AddFilter 添加过滤函数.
226226
func AddFilter(filter Filter) {
227-
server.Lock()
228-
defer server.Unlock()
227+
server.mu.Lock()
228+
defer server.mu.Unlock()
229229
server.filter = filter
230230
}
231231

@@ -239,8 +239,8 @@ func parseRequestValues(path string, ur handlerRegexp) context.Context {
239239
}
240240

241241
func getHandler(method, path string) (func(http.ResponseWriter, *http.Request), context.Context) {
242-
server.RLock()
243-
defer server.RUnlock()
242+
server.mu.RLock()
243+
defer server.mu.RUnlock()
244244

245245
path = method + path
246246

@@ -258,6 +258,7 @@ func getHandler(method, path string) (func(http.ResponseWriter, *http.Request),
258258
//log.Debugf("path:%v, prefix:%v, ok:%v", path, p.path, ok)
259259
return !ok
260260
})
261+
// log.Debugf("ok:%v, prefix:%v", ok, p)
261262

262263
if !ok {
263264
return nil, nil
@@ -268,7 +269,7 @@ func getHandler(method, path string) (func(http.ResponseWriter, *http.Request),
268269
if len(ue.keys) == 0 {
269270
return ue.call, nil
270271
}
271-
// log.Debugf("uri exp:%v, path:%v", ue, path)
272+
// log.Debugf("uri exp:%v, path:%v", ue, path)
272273
return ue.call, parseRequestValues(path, ue)
273274
}
274275
}
@@ -303,7 +304,7 @@ func (s *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
303304
nr = nr.WithContext(ctx)
304305
}
305306

306-
log.Debugf("%v %v %v %v", r.RemoteAddr, r.Method, r.URL, h)
307+
log.Debugf("%v %v %v h:%#v", r.RemoteAddr, r.Method, r.URL, h)
307308

308309
h(w, nr)
309310

@@ -315,30 +316,20 @@ func defaultFilter(_ http.ResponseWriter, r *http.Request) *http.Request {
315316
}
316317

317318
//Start 启动httpServer.
318-
func Start(addr string) error {
319+
func Start(addr string) (net.Listener, error) {
319320
ln, err := net.Listen("tcp", addr)
320321
if err != nil {
321-
return errors.Trace(err)
322+
return nil, errors.Trace(err)
322323
}
323324

324-
server.Lock()
325+
server.mu.Lock()
325326
server.listener = ln
326-
server.Unlock()
327+
server.mu.Unlock()
327328

328-
return http.Serve(ln, server)
329-
}
330-
331-
//Listener 获取监听地址.
332-
func Listener() net.Listener {
333-
server.Lock()
334-
defer server.Unlock()
335-
return server.listener
336-
}
337-
338-
//Stop 停止httpServer监听, 进行中的任务并不会因此而停止.
339-
func Stop() error {
340-
server.Lock()
341-
defer server.Unlock()
342-
343-
return server.listener.Close()
329+
go func() {
330+
if err = http.Serve(ln, server); err != nil {
331+
log.Errorf("Serve error:%v", err)
332+
}
333+
}()
334+
return ln, nil
344335
}

orm/db.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type DB struct {
1616
UserName string
1717
Passwd string
1818
Charset string
19-
TimeOut string
19+
TimeOut int
2020
}
2121

22-
//NewDB create db instance.
23-
func NewDB(ip string, port int, dbName, user, pass, charset, timeout string) *DB {
22+
//NewDB create db instance, timeout 单位:秒.
23+
func NewDB(ip string, port int, dbName, user, pass, charset string, timeout int) *DB {
2424
return &DB{
2525
IP: ip,
2626
Port: port,
@@ -69,8 +69,8 @@ func (db *DB) getOpt() string {
6969
opts = append(opts, fmt.Sprintf("charset=%s", db.Charset))
7070
}
7171

72-
if len(db.TimeOut) > 0 {
73-
opts = append(opts, fmt.Sprintf("timeout=%ss", db.TimeOut))
72+
if db.TimeOut > 0 {
73+
opts = append(opts, fmt.Sprintf("timeout=%ds", db.TimeOut))
7474
}
7575

7676
return strings.Join(opts, "&")

0 commit comments

Comments
 (0)