Skip to content

Commit

Permalink
return 503 if cluster was not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Feb 22, 2024
1 parent 8ce5c6b commit 3fbbf2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 6 additions & 6 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Cluster struct {
enabled atomic.Bool
disabled chan struct{}
waitEnable []chan struct{}
shouldEnable bool
shouldEnable atomic.Bool
reconnectCount int
socket *socket.Socket
cancelKeepalive context.CancelFunc
Expand Down Expand Up @@ -216,7 +216,7 @@ func (cr *Cluster) Connect(ctx context.Context) bool {
})
engio.OnDisconnect(func(_ *engine.Socket, err error) {
if config.Advanced.ExitWhenDisconnected {
if cr.shouldEnable {
if cr.shouldEnable.Load() {
logErrorf("Cluster disconnected from remote; exit.")
os.Exit(0x08)
}
Expand All @@ -231,7 +231,7 @@ func (cr *Cluster) Connect(ctx context.Context) bool {
cr.reconnectCount++
logErrorf("Failed to connect to the center server (%d/%d): %v", cr.reconnectCount, maxReconnectCount, err)
if cr.reconnectCount >= maxReconnectCount {
if cr.shouldEnable {
if cr.shouldEnable.Load() {
logErrorf("Cluster failed to connect too much times; exit.")
os.Exit(0x08)
}
Expand All @@ -250,7 +250,7 @@ func (cr *Cluster) Connect(ctx context.Context) bool {
logInfo("Preparing to connect to center server")
})
cr.socket.OnConnect(func(*socket.Socket, string) {
if cr.shouldEnable {
if cr.shouldEnable.Load() {
if err := cr.Enable(ctx); err != nil {
logErrorf("Cannot enable cluster: %v; exit.", err)
os.Exit(0x08)
Expand Down Expand Up @@ -304,7 +304,7 @@ func (cr *Cluster) Enable(ctx context.Context) (err error) {
return
}

cr.shouldEnable = true
cr.shouldEnable.Store(true)

logInfo("Sending enable packet")
resCh, err := cr.socket.EmitWithAck("enable", Map{
Expand Down Expand Up @@ -415,7 +415,7 @@ func (cr *Cluster) Disable(ctx context.Context) (ok bool) {
cr.mux.Lock()
defer cr.mux.Unlock()

cr.shouldEnable = false
cr.shouldEnable.Store(false)

if !cr.enabled.Load() {
logDebug("Extra disable")
Expand Down
11 changes: 10 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,20 @@ func (cr *Cluster) handleDownload(rw http.ResponseWriter, req *http.Request, has
return
}

if !cr.shouldEnable.Load() {
// do not serve file if cluster is not enabled yet
const hint = "Cluster is not enabled yet"
rw.Header().Set("Content-Length", strconv.Itoa(len(hint)))
rw.WriteHeader(http.StatusServiceUnavailable)
rw.Write(([]byte)(hint))
return
}

var err error
// check if file was indexed in the fileset
size, ok := cr.CachedFileSize(hash)
if !ok {
logInfof("Downloading %s", hash)
logInfof("Downloading %s from handler", hash)
if err := cr.DownloadFile(req.Context(), hash); err != nil {
logErrorf("Could not download %s: %v", hash, err)
rw.WriteHeader(http.StatusNotFound)
Expand Down

0 comments on commit 3fbbf2e

Please sign in to comment.