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
4 changes: 2 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
shutdownTracker: shutdowncheck.NewShutdownTracker(chainDb),
}
bcVersion := rawdb.ReadDatabaseVersion(chainDb)
var dbVer = "<nil>"
dbVer := "<nil>"
allnil marked this conversation as resolved.
Show resolved Hide resolved
if bcVersion != nil {
dbVer = fmt.Sprintf("%d", *bcVersion)
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func (s *Ethereum) APIs() []rpc.API {
Service: backends.NewEthBackendServer(s.APIBackend),
})

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

apis = append(apis, rpc.API{
Namespace: "suavex",
Expand Down
44 changes: 36 additions & 8 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 Down Expand Up @@ -37,6 +39,38 @@ type Config struct {
MaxConcurrentSessions int
}

func getEnvAsUint64(key string, defaultVal uint64) uint64 {
valStr := os.Getenv(key)
if val, err := strconv.ParseUint(valStr, 10, 64); err == nil {
return val
}
return defaultVal
}

func getEnvAsInt(key string, defaultVal int) int {
valStr := os.Getenv(key)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}

func getEnvAsDuration(key string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(key)
if val, err := time.ParseDuration(valStr); err == nil {
allnil marked this conversation as resolved.
Show resolved Hide resolved
return val
}
return defaultVal
}

func NewConfigFromEnv() *Config {
allnil marked this conversation as resolved.
Show resolved Hide resolved
return &Config{
GasCeil: getEnvAsUint64("GAS_CEIL", 1000000000000000000),
SessionIdleTimeout: getEnvAsDuration("SESSION_IDLE_TIMEOUT", 5*time.Second),
MaxConcurrentSessions: getEnvAsInt("MAX_CONCURRENT_SESSIONS", 16),
}
}

type SessionManager struct {
sem chan struct{}
sessions map[string]*builder
Expand All @@ -47,14 +81,8 @@ type SessionManager struct {
}

func NewSessionManager(blockchain blockchain, 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
31 changes: 13 additions & 18 deletions suave/builder/session_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
)

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

id, err := mngr.NewSession(context.TODO())
require.NoError(t, err)
Expand All @@ -36,10 +36,10 @@ func TestSessionManager_MaxConcurrentSessions(t *testing.T) {

const d = time.Millisecond * 100

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

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

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

id, err := mngr.NewSession(context.TODO())
require.NoError(t, err)
Expand All @@ -95,8 +95,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, NewConfigFromEnv())

id, err := mngr.NewSession(context.TODO())
require.NoError(t, err)
Expand All @@ -107,17 +106,13 @@ func TestSessionManager_StartSession(t *testing.T) {
require.NotNil(t, receipt)
}

func newSessionManager(t *testing.T, cfg *Config) (*SessionManager, *blockchainMock) {
if cfg == nil {
cfg = &Config{}
}

func newSessionManager(t *testing.T, config *Config) (*SessionManager, *blockchainMock) {
state := newMockState(t)

bMock := &blockchainMock{
state: state,
}
return NewSessionManager(bMock, cfg), bMock
return NewSessionManager(bMock, config), bMock
}

type blockchainMock struct {
Expand Down