-
Notifications
You must be signed in to change notification settings - Fork 36
/
gater.go
100 lines (85 loc) · 2.31 KB
/
gater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gater
import (
"context"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/bnb-chain/greenfield-storage-provider/base/gfspapp"
"github.com/bnb-chain/greenfield-storage-provider/core/module"
"github.com/bnb-chain/greenfield-storage-provider/core/rcmgr"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
)
const ReadHeaderTimeout = 20 * time.Minute
var _ module.Modular = &GateModular{}
type GateModular struct {
env string
domain string
httpAddress string
baseApp *gfspapp.GfSpBaseApp
scope rcmgr.ResourceScope
httpServer *http.Server
maxListReadQuota int64
maxPayloadSize uint64
spID uint32
spCachePool *SPCachePool
}
func (g *GateModular) Name() string {
return module.GateModularName
}
func (g *GateModular) Start(ctx context.Context) error {
scope, err := g.baseApp.ResourceManager().OpenService(g.Name())
if err != nil {
return err
}
g.scope = scope
go g.server(ctx)
g.spCachePool = NewSPCachePool(g.baseApp.Consensus())
return nil
}
func (g *GateModular) server(ctx context.Context) {
router := mux.NewRouter().SkipClean(true)
if g.baseApp.EnableMetrics() {
router.Use(metrics.DefaultHTTPServerMetrics.InstrumentationHandler)
}
g.RegisterHandler(router)
g.httpServer = &http.Server{
Addr: g.httpAddress,
Handler: router,
ReadHeaderTimeout: ReadHeaderTimeout,
}
if err := g.httpServer.ListenAndServe(); err != nil {
log.Errorw("failed to listen", "error", err)
return
}
}
func (g *GateModular) Stop(ctx context.Context) error {
g.scope.Release()
_ = g.httpServer.Shutdown(ctx)
return nil
}
func (g *GateModular) ReserveResource(ctx context.Context, state *rcmgr.ScopeStat) (rcmgr.ResourceScopeSpan, error) {
span, err := g.scope.BeginSpan()
if err != nil {
return nil, err
}
err = span.ReserveResources(state)
if err != nil {
return nil, err
}
return span, nil
}
func (g *GateModular) ReleaseResource(ctx context.Context, span rcmgr.ResourceScopeSpan) {
span.Done()
}
func (g *GateModular) getSPID() (uint32, error) {
if g.spID != 0 {
return g.spID, nil
}
spInfo, err := g.baseApp.Consensus().QuerySP(context.Background(), g.baseApp.OperatorAddress())
if err != nil {
return 0, err
}
g.spID = spInfo.GetId()
return g.spID, nil
}