Skip to content

Commit c78a51b

Browse files
Fix
1 parent 9ae4f51 commit c78a51b

File tree

4 files changed

+99
-97
lines changed

4 files changed

+99
-97
lines changed

code/go/0chain.net/blobber/main.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package main
22

33
import (
4-
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/handler"
4+
"github.com/0chain/blobber/code/go/0chain.net/core/common"
55
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
66
"github.com/0chain/blobber/code/go/0chain.net/core/node"
7-
"go.uber.org/zap"
87
)
98

109
func main() {
@@ -46,20 +45,12 @@ func main() {
4645
prepareBlobber(node.Self.ID)
4746

4847
go func() {
49-
logging.Logger.Info("Jayash registering on chain")
5048
if err := registerOnChain(); err != nil {
5149
logging.Logger.Error("Error register on blockchain" + err.Error())
5250
panic(err)
5351
}
54-
logging.Logger.Info("Jayash registered on blockchain", zap.Any("blobber_id", node.Self.ID))
55-
handler.BlobberRegisteredMutex.Lock()
56-
logging.Logger.Info("Jayash acquired lock on register")
57-
handler.BlobberRegistered = true
58-
logging.Logger.Info("Jayash set blobber registered to true")
59-
handler.BlobberRegisteredMutex.Unlock()
60-
logging.Logger.Info("Jayash released lock on register")
61-
62-
logging.Logger.Info("Blobber registered on blockchain", zap.Any("blobber_id", node.Self.ID), zap.Any("blobberRegistered", handler.BlobberRegistered))
52+
53+
common.SetBlobberRegistered(true)
6354
}()
6455

6556
if err := setStorageScConfigFromChain(); err != nil {

code/go/0chain.net/blobbercore/handler/handler.go

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -135,150 +135,149 @@ func SetupSwagger() {
135135
http.Handle("/docs1", sh1)
136136
}
137137

138-
func WithBlobberRegisteredCondition(handler common.ReqRespHandlerf) common.ReqRespHandlerf {
139-
logging.Logger.Info("1Jayash acquired lock on check")
140-
BlobberRegisteredMutex.Lock()
141-
logging.Logger.Info("2Jayash acquired lock on check")
142-
if !BlobberRegistered {
143-
logging.Logger.Info("1Jayash condition met")
144-
BlobberRegisteredMutex.Unlock()
145-
logging.Logger.Info("1Jayash released lock on check")
146-
return func(w http.ResponseWriter, r *http.Request) {
138+
func WithBlobberRegistered(h http.Handler) http.Handler {
139+
if !common.IsBlobberRegistered() {
140+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
147141
w.WriteHeader(http.StatusServiceUnavailable)
148142
w.Write([]byte("Blobber not registered yet"))
149-
}
143+
})
150144
}
151145

152-
logging.Logger.Info("Jayash condition failed")
153-
BlobberRegisteredMutex.Unlock()
154-
logging.Logger.Info("Jayash released lock on check")
155-
return handler
146+
return h
156147
}
157148

158149
/*setupHandlers sets up the necessary API end points */
159150
func setupHandlers(r *mux.Router) {
160151
ConfigRateLimits()
161-
r.Use(UseRecovery, UseCors)
152+
153+
s := r.NewRoute().Subrouter()
154+
s.Use(UseRecovery, UseCors, WithBlobberRegistered)
155+
156+
s.HandleFunc("/_stats", RateLimitByCommmitRL(StatsHandler))
162157

163158
//object operations
164-
r.HandleFunc("/v1/connection/create/{allocation}",
165-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(CreateConnectionHandler))))).
159+
s.HandleFunc("/v1/connection/create/{allocation}",
160+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(CreateConnectionHandler)))).
166161
Methods(http.MethodPost)
167162

168-
r.HandleFunc("/v1/connection/redeem/{allocation}",
169-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToByteStream(WithConnection(RedeemHandler))))).
163+
s.HandleFunc("/v1/connection/redeem/{allocation}",
164+
RateLimitByGeneralRL(common.ToByteStream(WithConnection(RedeemHandler)))).
170165
Methods(http.MethodPost, http.MethodOptions)
171166

172-
r.HandleFunc("/v1/file/rename/{allocation}",
173-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(RenameHandler))))).
167+
s.HandleFunc("/v1/file/rename/{allocation}",
168+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(RenameHandler)))).
174169
Methods(http.MethodPost, http.MethodOptions)
175170

176-
r.HandleFunc("/v1/file/copy/{allocation}",
177-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(CopyHandler))))).
171+
s.HandleFunc("/v1/file/copy/{allocation}",
172+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(CopyHandler)))).
178173
Methods(http.MethodPost, http.MethodOptions)
179174

180-
r.HandleFunc("/v1/file/move/{allocation}",
181-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(MoveHandler))))).
175+
s.HandleFunc("/v1/file/move/{allocation}",
176+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(MoveHandler)))).
182177
Methods(http.MethodPost, http.MethodOptions)
183178

184-
r.HandleFunc("/v1/dir/{allocation}",
185-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(CreateDirHandler))))).
179+
s.HandleFunc("/v1/dir/{allocation}",
180+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(CreateDirHandler)))).
186181
Methods(http.MethodPost, http.MethodDelete, http.MethodOptions)
187182

188-
r.HandleFunc("/v1/connection/commit/{allocation}",
189-
RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToStatusCode(WithStatusConnectionForWM(CommitHandler)))))
183+
s.HandleFunc("/v1/connection/commit/{allocation}",
184+
RateLimitByCommmitRL(common.ToStatusCode(WithStatusConnectionForWM(CommitHandler))))
190185

191-
r.HandleFunc("/v1/connection/rollback/{allocation}",
192-
RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToStatusCode(WithStatusConnectionForWM(RollbackHandler)))))
186+
s.HandleFunc("/v1/connection/rollback/{allocation}",
187+
RateLimitByCommmitRL(common.ToStatusCode(WithStatusConnectionForWM(RollbackHandler))))
193188

194189
//object info related apis
195-
r.HandleFunc("/allocation",
196-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(AllocationHandler)))))
190+
s.HandleFunc("/allocation",
191+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(AllocationHandler))))
197192

198-
r.HandleFunc("/v1/file/meta/{allocation}",
199-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(FileMetaHandler)))))
193+
s.HandleFunc("/v1/file/meta/{allocation}",
194+
RateLimitByGeneralRL(common.ToJSONResponse(WithReadOnlyConnection(FileMetaHandler))))
200195

201-
r.HandleFunc("/v1/file/stats/{allocation}",
202-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(FileStatsHandler)))))
196+
s.HandleFunc("/v1/file/stats/{allocation}",
197+
RateLimitByGeneralRL(common.ToJSONResponse(WithReadOnlyConnection(FileStatsHandler))))
203198

204-
r.HandleFunc("/v1/file/referencepath/{allocation}",
205-
RateLimitByObjectRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(ReferencePathHandler)))))
199+
s.HandleFunc("/v1/file/referencepath/{allocation}",
200+
RateLimitByObjectRL(common.ToJSONResponse(WithReadOnlyConnection(ReferencePathHandler))))
206201

207-
r.HandleFunc("/v1/file/latestwritemarker/{allocation}",
208-
RateLimitByObjectRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(WriteMarkerHandler)))))
202+
s.HandleFunc("/v1/file/latestwritemarker/{allocation}",
203+
RateLimitByObjectRL(common.ToJSONResponse(WithReadOnlyConnection(WriteMarkerHandler))))
209204

210-
r.HandleFunc("/v1/file/objecttree/{allocation}",
211-
RateLimitByObjectRL(WithBlobberRegisteredCondition(common.ToStatusCode(WithStatusReadOnlyConnection(ObjectTreeHandler))))).
205+
s.HandleFunc("/v1/file/objecttree/{allocation}",
206+
RateLimitByObjectRL(common.ToStatusCode(WithStatusReadOnlyConnection(ObjectTreeHandler)))).
212207
Methods(http.MethodGet, http.MethodOptions)
213208

214-
r.HandleFunc("/v1/file/refs/{allocation}",
215-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(RefsHandler))))).
209+
s.HandleFunc("/v1/file/refs/{allocation}",
210+
RateLimitByGeneralRL(common.ToJSONResponse(WithReadOnlyConnection(RefsHandler)))).
216211
Methods(http.MethodGet, http.MethodOptions)
217212

218-
r.HandleFunc("/v1/file/refs/recent/{allocation}",
219-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(RecentRefsRequestHandler))))).
213+
s.HandleFunc("/v1/file/refs/recent/{allocation}",
214+
RateLimitByGeneralRL(common.ToJSONResponse(WithReadOnlyConnection(RecentRefsRequestHandler)))).
220215
Methods(http.MethodGet, http.MethodOptions)
221216

222217
// admin related
223218
// Allowing admin api for debugging purpose only. Later on commented out line should be
224219
// uncommented and line below it should be deleted
225220

226-
r.HandleFunc("/_debug", common.AuthenticateAdmin(common.ToJSONResponse(DumpGoRoutines)))
227-
// r.HandleFunc("/_debug", RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToJSONResponse(DumpGoRoutines)))
228-
r.HandleFunc("/_config", common.AuthenticateAdmin(common.ToJSONResponse(GetConfig)))
229-
// r.HandleFunc("/_config", RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToJSONResponse(GetConfig)))
230-
// r.HandleFunc("/_stats", common.AuthenticateAdmin(StatsHandler)))
231-
r.HandleFunc("/_stats", RateLimitByCommmitRL(WithBlobberRegisteredCondition(StatsHandler)))
221+
s.HandleFunc("/_debug", common.AuthenticateAdmin(common.ToJSONResponse(DumpGoRoutines)))
222+
// s.HandleFunc("/_debug", RateLimitByCommmitRL(common.ToJSONResponse(DumpGoRoutines)))
223+
s.HandleFunc("/_config", common.AuthenticateAdmin(common.ToJSONResponse(GetConfig)))
224+
// s.HandleFunc("/_config", RateLimitByCommmitRL(common.ToJSONResponse(GetConfig)))
225+
// s.HandleFunc("/_stats", common.AuthenticateAdmin(StatsHandler)))
226+
s.HandleFunc("/_stats", RateLimitByCommmitRL(StatsHandler))
232227

233-
r.HandleFunc("/_logs", RateLimitByCommmitRL(common.ToJSONResponse(GetLogs)))
228+
s.HandleFunc("/_logs", RateLimitByCommmitRL(common.ToJSONResponse(GetLogs)))
234229

235-
r.HandleFunc("/_blobber_info", RateLimitByCommmitRL(common.ToJSONResponse(GetBlobberInfo)))
236-
237-
// r.HandleFunc("/_cleanupdisk", common.AuthenticateAdmin(common.ToJSONResponse(WithReadOnlyConnection(CleanupDiskHandler)))))
238-
// r.HandleFunc("/_cleanupdisk", RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(CleanupDiskHandler)))))
239-
r.HandleFunc("/challengetimings", WithBlobberRegisteredCondition(common.AuthenticateAdmin(common.ToJSONResponse(GetChallengeTimings))))
240-
// r.HandleFunc("/challengetimings", RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToJSONResponse(GetChallengeTimings)))
241-
r.HandleFunc("/challenge-timings-by-challengeId", RateLimitByCommmitRL(WithBlobberRegisteredCondition(common.ToJSONResponse(GetChallengeTiming))))
230+
// s.HandleFunc("/_cleanupdisk", common.AuthenticateAdmin(common.ToJSONResponse(WithReadOnlyConnection(CleanupDiskHandler)))))
231+
// s.HandleFunc("/_cleanupdisk", RateLimitByCommmitRL(common.ToJSONResponse(WithReadOnlyConnection(CleanupDiskHandler)))))
232+
s.HandleFunc("/challengetimings", common.AuthenticateAdmin(common.ToJSONResponse(GetChallengeTimings)))
233+
// s.HandleFunc("/challengetimings", RateLimitByCommmitRL(common.ToJSONResponse(GetChallengeTimings)))
234+
s.HandleFunc("/challenge-timings-by-challengeId", RateLimitByCommmitRL(common.ToJSONResponse(GetChallengeTiming)))
242235

243236
// Generate auth ticket
244-
r.HandleFunc("/v1/auth/generate", Authenticate0Box(common.ToJSONResponse(GenerateAuthTicket)))
237+
s.HandleFunc("/v1/auth/generate", Authenticate0Box(common.ToJSONResponse(GenerateAuthTicket)))
245238

246239
//marketplace related
247-
r.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
248-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(InsertShare))))).
240+
s.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
241+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(InsertShare)))).
249242
Methods(http.MethodOptions, http.MethodPost)
250243

251-
r.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
252-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(RevokeShare))))).
244+
s.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
245+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(RevokeShare)))).
253246
Methods(http.MethodOptions, http.MethodDelete)
254247

255248
// list files shared in this allocation
256-
r.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
257-
RateLimitByGeneralRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(ListShare))))).
249+
s.HandleFunc("/v1/marketplace/shareinfo/{allocation}",
250+
RateLimitByGeneralRL(common.ToJSONResponse(WithConnection(ListShare)))).
258251
Methods(http.MethodOptions, http.MethodGet)
259252

260253
// lightweight http handler without heavy postgres transaction to improve performance
261254

262-
r.HandleFunc("/v1/writemarker/lock/{allocation}",
255+
s.HandleFunc("/v1/writemarker/lock/{allocation}",
263256
RateLimitByGeneralRL(WithTxHandler(LockWriteMarker))).
264257
Methods(http.MethodPost, http.MethodOptions)
265258

266-
r.HandleFunc("/v1/writemarker/lock/{allocation}/{connection}",
259+
s.HandleFunc("/v1/writemarker/lock/{allocation}/{connection}",
267260
RateLimitByGeneralRL(WithTxHandler(UnlockWriteMarker))).
268261
Methods(http.MethodDelete, http.MethodOptions)
269262

270263
// TODO: Deprecated, remove in future
271-
r.HandleFunc("/v1/hashnode/root/{allocation}",
264+
s.HandleFunc("/v1/hashnode/root/{allocation}",
272265
RateLimitByObjectRL(WithTxHandler(LoadRootHashnode))).
273266
Methods(http.MethodGet, http.MethodOptions)
274267

275-
r.HandleFunc("/v1/playlist/latest/{allocation}",
268+
s.HandleFunc("/v1/playlist/latest/{allocation}",
276269
RateLimitByGeneralRL(WithTxHandler(LoadPlaylist))).
277270
Methods(http.MethodGet, http.MethodOptions)
278271

279-
r.HandleFunc("/v1/playlist/file/{allocation}",
272+
s.HandleFunc("/v1/playlist/file/{allocation}",
280273
RateLimitByGeneralRL(WithTxHandler(LoadPlaylistFile))).
281274
Methods(http.MethodGet, http.MethodOptions)
275+
276+
r.HandleFunc("/v1/file/list/{allocation}",
277+
RateLimitByObjectRL(common.ToJSONResponse(WithReadOnlyConnection(ListHandler)))).
278+
Methods(http.MethodGet, http.MethodOptions)
279+
r.HandleFunc("/v1/file/upload/{allocation}", RateLimitByFileRL(common.ToJSONResponse(WithConnection(UploadHandler))))
280+
r.HandleFunc("/v1/file/download/{allocation}", RateLimitByFileRL(common.ToByteStream(WithConnection(DownloadHandler)))).Methods(http.MethodGet, http.MethodOptions)
282281
}
283282

284283
func WithReadOnlyConnection(handler common.JSONResponderF) common.JSONResponderF {

code/go/0chain.net/blobbercore/handler/handler_main.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,12 @@ import (
88
"github.com/0chain/blobber/code/go/0chain.net/core/common"
99
"github.com/gorilla/mux"
1010
"net/http"
11-
"sync"
12-
)
13-
14-
var (
15-
BlobberRegisteredMutex sync.Mutex
16-
BlobberRegistered bool
1711
)
1812

1913
/* SetupHandlers sets up the necessary API end points */
2014
func SetupHandlers(r *mux.Router) {
15+
r.HandleFunc("/_blobber_info", RateLimitByCommmitRL(common.ToJSONResponse(GetBlobberInfo)))
2116
setupHandlers(r)
22-
23-
r.HandleFunc("/v1/file/list/{allocation}",
24-
RateLimitByObjectRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithReadOnlyConnection(ListHandler))))).
25-
Methods(http.MethodGet, http.MethodOptions)
26-
r.HandleFunc("/v1/file/upload/{allocation}", RateLimitByFileRL(WithBlobberRegisteredCondition(common.ToJSONResponse(WithConnection(UploadHandler)))))
27-
r.HandleFunc("/v1/file/download/{allocation}", RateLimitByFileRL(WithBlobberRegisteredCondition(common.ToByteStream(WithConnection(DownloadHandler))))).Methods(http.MethodGet, http.MethodOptions)
2817
}
2918

3019
func ListHandler(ctx context.Context, r *http.Request) (interface{}, error) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package common
2+
3+
import "sync"
4+
5+
// Private variables
6+
var (
7+
blobberRegisteredMutex sync.Mutex
8+
blobberRegistered bool
9+
)
10+
11+
// Getter for blobberRegistered
12+
func IsBlobberRegistered() bool {
13+
blobberRegisteredMutex.Lock()
14+
defer blobberRegisteredMutex.Unlock()
15+
return blobberRegistered
16+
}
17+
18+
// Setter for blobberRegistered
19+
func SetBlobberRegistered(registered bool) {
20+
blobberRegisteredMutex.Lock()
21+
defer blobberRegisteredMutex.Unlock()
22+
blobberRegistered = registered
23+
}

0 commit comments

Comments
 (0)