Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: builder session manager env var config #19

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (s *Ethereum) APIs() []rpc.API {
Service: backends.NewEthBackendServer(s.APIBackend),
})

sessionManager := suave_builder.NewSessionManager(s.blockchain, s.txPool, &suave_builder.Config{})
sessionManager := suave_builder.NewSessionManager(s.blockchain, s.txPool, suave_builder.NewConfig())

apis = append(apis, rpc.API{
Namespace: "suavex",
Expand Down
44 changes: 35 additions & 9 deletions suave/builder/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"math/big"
"os"
"strconv"
"sync"
"time"

Expand All @@ -25,6 +27,36 @@ type Config struct {
MaxConcurrentSessions int
}

func NewConfig() *Config {
cfg := &Config{
GasCeil: 1000000000000000000,
SessionIdleTimeout: 5 * time.Second,
MaxConcurrentSessions: 16,
}

// apply env if exists
var err error
if valStr := os.Getenv("GAS_CEIL"); valStr != "" {
if cfg.GasCeil, err = strconv.ParseUint(valStr, 10, 64); err != nil {
panic(fmt.Sprintf("failed to parse GAS_CEIL flag as uint: %v", err))
}
}

if valStr := os.Getenv("SESSION_IDLE_TIMEOUT"); valStr != "" {
if cfg.SessionIdleTimeout, err = time.ParseDuration(valStr); err != nil {
panic(fmt.Sprintf("failed to parse SESSION_IDLE_TIMEOUT flag as duration: %v", err))
}
}

if valStr := os.Getenv("MAX_CONCURRENT_SESSIONS"); valStr != "" {
if cfg.MaxConcurrentSessions, err = strconv.Atoi(valStr); err != nil {
panic(fmt.Sprintf("failed to parse MAX_CONCURRENT_SESSIONS flag as int: %v", err))
}
}

return cfg
}

type SessionManager struct {
sem chan struct{}
sessions map[string]*miner.Builder
Expand All @@ -36,14 +68,8 @@ type SessionManager struct {
}

func NewSessionManager(blockchain *core.BlockChain, pool *txpool.TxPool, config *Config) *SessionManager {
if config.GasCeil == 0 {
config.GasCeil = 1000000000000000000
}
if config.SessionIdleTimeout == 0 {
config.SessionIdleTimeout = 5 * time.Second
}
if config.MaxConcurrentSessions <= 0 {
config.MaxConcurrentSessions = 16 // chosen arbitrarily
if config == nil {
panic("empty session manager config")
}

sem := make(chan struct{}, config.MaxConcurrentSessions)
Expand Down Expand Up @@ -197,7 +223,7 @@ func (s *SessionManager) Bid(sessionId string, blsPubKey phase0.BLSPubKey) (*api
}

func (s *SessionManager) GetBalance(sessionId string, addr common.Address) (*big.Int, error) {
builder, err := s.getSession(sessionId)
builder, err := s.getSession(sessionId, false)
if err != nil {
return nil, err
}
Expand Down
23 changes: 11 additions & 12 deletions suave/builder/session_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
)

func TestSessionManager_SessionTimeout(t *testing.T) {
mngr, _ := newSessionManager(t, &Config{
SessionIdleTimeout: 500 * time.Millisecond,
})
config := NewConfig()
config.SessionIdleTimeout = 500 * time.Millisecond
mngr, _ := newSessionManager(t, config)

args := &api.BuildBlockArgs{}

Expand All @@ -43,10 +43,10 @@ func TestSessionManager_MaxConcurrentSessions(t *testing.T) {
const d = time.Millisecond * 100
args := &api.BuildBlockArgs{}

mngr, _ := newSessionManager(t, &Config{
MaxConcurrentSessions: 1,
SessionIdleTimeout: d,
})
config := NewConfig()
config.MaxConcurrentSessions = 1
config.SessionIdleTimeout = d
mngr, _ := newSessionManager(t, config)

t.Run("SessionAvailable", func(t *testing.T) {
sess, err := mngr.NewSession(context.TODO(), args)
Expand Down Expand Up @@ -76,9 +76,9 @@ func TestSessionManager_MaxConcurrentSessions(t *testing.T) {
}

func TestSessionManager_SessionRefresh(t *testing.T) {
mngr, _ := newSessionManager(t, &Config{
SessionIdleTimeout: 500 * time.Millisecond,
})
config := NewConfig()
config.SessionIdleTimeout = 500 * time.Millisecond
mngr, _ := newSessionManager(t, config)

args := &api.BuildBlockArgs{}
id, err := mngr.NewSession(context.TODO(), args)
Expand All @@ -103,8 +103,7 @@ func TestSessionManager_SessionRefresh(t *testing.T) {
}

func TestSessionManager_StartSession(t *testing.T) {
// test that the session starts and it can simulate transactions
mngr, bMock := newSessionManager(t, &Config{})
mngr, bMock := newSessionManager(t, NewConfig())

args := &api.BuildBlockArgs{}
id, err := mngr.NewSession(context.TODO(), args)
Expand Down
Loading