fix regex, remove unsafe pointer usage, and add graceful HTTP shutdown

- Fix versionMatcher regex: gorilla/mux does not support (?:) syntax,
  and -dirty suffix was required instead of optional
- Replace unsafe.Pointer LUN casts with binary.LittleEndian.Uint64
  in sbc.go, scsi.go, and target.go
- Implement graceful HTTP server shutdown with 5s timeout using
  srv.Shutdown() instead of raw listener close
- Replace golang.org/x/net/context with standard library context
- Respect existing req.Cancel value in canceler to avoid overwriting
- Add early context cancellation check in Do() to fail fast

Based on review of PR #120 by @orzhang, with fixes applied.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lei Xue
2026-03-14 18:27:56 +08:00
parent 345b8b2507
commit f1cec4f5d4
6 changed files with 21 additions and 10 deletions

View File

@@ -19,11 +19,13 @@ package apiserver
import (
"crypto/tls"
"context"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"
systemdActivation "github.com/coreos/go-systemd/activation"
"github.com/docker/go-connections/sockets"
@@ -34,12 +36,11 @@ import (
"github.com/gostor/gotgt/pkg/apiserver/router/lu"
"github.com/gostor/gotgt/pkg/apiserver/router/target"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
// versionMatcher defines a variable matcher to be parsed by the router
// when a request is about to be served.
const versionMatcher = "/v{version:[0-9.]+(?:-dirty)}"
const versionMatcher = "/v{version:[0-9.]+(-dirty)?}"
// Config provides the configuration for the API server
type Config struct {
@@ -137,7 +138,9 @@ func (s *HTTPServer) Serve() error {
// Close closes the HTTPServer from listening for the inbound requests.
func (s *HTTPServer) Close() error {
return s.l.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return s.srv.Shutdown(ctx)
}
func (s *Server) initTCPSocket(addr string) (l net.Listener, err error) {