关于golang:聊聊storagetapper的server

47次阅读

共计 1450 个字符,预计需要花费 4 分钟才能阅读完成。

本文次要钻研一下 storagetapper 的 server

server

storagetapper/server/server.go

var server *http.Server
var mutex = sync.Mutex{}

func init() {http.HandleFunc("/health", healthCheck)
    http.HandleFunc("/schema", schemaCmd)
    http.HandleFunc("/cluster", clusterInfoCmd)
    http.HandleFunc("/table", tableCmd)
    http.HandleFunc("/config", configCmd)
    http.HandleFunc("/", indexCmd)
}

//StartHTTPServer starts listening and serving traffic on configured port and sets up http routes.
func StartHTTPServer(port int) {state.EmitRegisteredTablesCount()
    mutex.Lock()

    server = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: nil}
    log.Debugf("HTTP server is listening on %v port", port)

    mutex.Unlock()

    err := server.ListenAndServe()
    if err != nil && err != http.ErrServerClosed {log.E(err)
    }
}

//Shutdown gracefully stops the server
func Shutdown() {mutex.Lock()
    defer mutex.Unlock()
    if server == nil {return}
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()
    log.E(server.Shutdown(ctx))
    server = nil
}

storagetapper 的 server 提供了 StartHTTPServer、Shutdown 办法;其 init 办法注册了 /health/schema/cluster/table/config/ 这几个 url

healthCheck

storagetapper/server/server.go

//healthCheck handles call to the health check endpoint
func healthCheck(w http.ResponseWriter, r *http.Request) {w.Header().Add("Content-Type", "text/plain")
    w.WriteHeader(http.StatusOK)
    if _, err := w.Write([]byte("OK")); err != nil {log.Errorf("Health check failed: %s\n", err)
    }
}

healthCheck 返回 200,文本内容为 OK

小结

storagetapper 的 server 提供了 StartHTTPServer、Shutdown 办法;其 init 办法注册了 /health/schema/cluster/table/config/ 这几个 url。

doc

  • storagetapper

正文完
 0