add blocked mode

This commit is contained in:
Joost Jager 2023-02-13 14:28:12 +01:00
parent 56fb959379
commit 0cd238a52d
No known key found for this signature in database
GPG key ID: B9A26449A5528325
14 changed files with 103 additions and 12 deletions

View file

@ -27,6 +27,7 @@ const (
Mode_MODE_FAIL Mode = 0
Mode_MODE_QUEUE Mode = 1
Mode_MODE_QUEUE_PEER_INITIATED Mode = 2
Mode_MODE_BLOCK Mode = 3
)
// Enum value maps for Mode.
@ -35,11 +36,13 @@ var (
0: "MODE_FAIL",
1: "MODE_QUEUE",
2: "MODE_QUEUE_PEER_INITIATED",
3: "MODE_BLOCK",
}
Mode_value = map[string]int32{
"MODE_FAIL": 0,
"MODE_QUEUE": 1,
"MODE_QUEUE_PEER_INITIATED": 2,
"MODE_BLOCK": 3,
}
)
@ -832,11 +835,12 @@ var file_circuitbreaker_proto_rawDesc = []byte{
0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52,
0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x6a, 0x65,
0x63, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x72, 0x65, 0x6a, 0x65, 0x63, 0x74,
0x2a, 0x44, 0x0a, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x0d, 0x0a, 0x09, 0x4d, 0x4f, 0x44, 0x45,
0x2a, 0x54, 0x0a, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x0d, 0x0a, 0x09, 0x4d, 0x4f, 0x44, 0x45,
0x5f, 0x46, 0x41, 0x49, 0x4c, 0x10, 0x00, 0x12, 0x0e, 0x0a, 0x0a, 0x4d, 0x4f, 0x44, 0x45, 0x5f,
0x51, 0x55, 0x45, 0x55, 0x45, 0x10, 0x01, 0x12, 0x1d, 0x0a, 0x19, 0x4d, 0x4f, 0x44, 0x45, 0x5f,
0x51, 0x55, 0x45, 0x55, 0x45, 0x5f, 0x50, 0x45, 0x45, 0x52, 0x5f, 0x49, 0x4e, 0x49, 0x54, 0x49,
0x41, 0x54, 0x45, 0x44, 0x10, 0x02, 0x32, 0xc1, 0x04, 0x0a, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69,
0x41, 0x54, 0x45, 0x44, 0x10, 0x02, 0x12, 0x0e, 0x0a, 0x0a, 0x4d, 0x4f, 0x44, 0x45, 0x5f, 0x42,
0x4c, 0x4f, 0x43, 0x4b, 0x10, 0x03, 0x32, 0xc1, 0x04, 0x0a, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x12, 0x59, 0x0a, 0x07, 0x47, 0x65, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x1e, 0x2e,
0x63, 0x69, 0x72, 0x63, 0x75, 0x69, 0x74, 0x62, 0x72, 0x65, 0x61, 0x6b, 0x65, 0x72, 0x2e, 0x47,
0x65, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e,

View file

@ -55,9 +55,9 @@ enum Mode {
MODE_FAIL = 0;
MODE_QUEUE = 1;
MODE_QUEUE_PEER_INITIATED = 2;
MODE_BLOCK = 3;
}
message ClearLimitRequest {
string node = 1;
}

View file

@ -6,6 +6,7 @@ const (
ModeFail Mode = iota
ModeQueue
ModeQueuePeerInitiated
ModeBlock
)
func (m Mode) String() string {
@ -19,6 +20,9 @@ func (m Mode) String() string {
case ModeQueuePeerInitiated:
return "QUEUE_PEER_INITIATED"
case ModeBlock:
return "BLOCK"
default:
panic("unknown mode")
}

23
db.go
View file

@ -30,6 +30,26 @@ var migrations = &migrate.MemoryMigrationSource{
`,
},
},
{
Id: "2",
Up: []string{
`
ALTER TABLE limits RENAME TO limits_old;
CREATE TABLE IF NOT EXISTS limits (
peer TEXT PRIMARY KEY NOT NULL,
htlc_max_pending INTEGER NOT NULL,
htlc_max_hourly_rate INTEGER NOT NULL,
mode TEXT CHECK(mode IN ('FAIL', 'QUEUE', 'QUEUE_PEER_INITIATED', 'BLOCK')) NOT NULL DEFAULT 'FAIL'
);
INSERT INTO limits(peer, htlc_max_pending, htlc_max_hourly_rate, mode)
SELECT peer, htlc_max_pending, htlc_max_hourly_rate, mode FROM limits_old;
DROP TABLE limits_old;
`,
},
},
},
}
@ -138,6 +158,9 @@ func (d *Db) GetLimits(ctx context.Context) (*Limits, error) {
case "QUEUE_PEER_INITIATED":
limit.Mode = ModeQueuePeerInitiated
case "BLOCK":
limit.Mode = ModeBlock
default:
return nil, errors.New("unknown mode")
}

View file

@ -279,6 +279,11 @@ func (p *peerController) run(ctx context.Context) error {
mode := p.cfg.Mode
switch {
// Don't check limits in block mode and move onwards to failing the
// htlc.
case mode == ModeBlock:
logger.Infow("Htlc blocked")
// If there is a queue, then don't jump the queue.
case queue.Len() > 0:

View file

@ -305,3 +305,47 @@ func TestNewPeer(t *testing.T) {
cancel()
require.ErrorIs(t, <-exit, context.Canceled)
}
func TestBlocked(t *testing.T) {
defer Timeout()()
cfg := &Limits{
PerPeer: map[route.Vertex]Limit{
{2}: {
MaxHourlyRate: 1800,
Mode: ModeBlock,
},
},
}
client := newLndclientMock(testChannels)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log := zaptest.NewLogger(t).Sugar()
p := NewProcess(client, log, cfg)
exit := make(chan error)
go func() {
exit <- p.Run(ctx)
}()
var chanId uint64 = 2
key := circuitKey{
channel: chanId,
htlc: 5,
}
interceptReq := &interceptedEvent{
circuitKey: key,
}
// Htlc blocked.
client.htlcInterceptorRequests <- interceptReq
resp := <-client.htlcInterceptorResponses
require.False(t, resp.resume)
cancel()
require.ErrorIs(t, <-exit, context.Canceled)
}

View file

@ -91,6 +91,9 @@ func unmarshalLimit(rpcLimit *circuitbreakerrpc.Limit) (Limit, error) {
case circuitbreakerrpc.Mode_MODE_QUEUE_PEER_INITIATED:
limit.Mode = ModeQueuePeerInitiated
case circuitbreakerrpc.Mode_MODE_BLOCK:
limit.Mode = ModeBlock
default:
return Limit{}, errors.New("unknown mode")
}
@ -199,6 +202,9 @@ func marshalLimit(limit Limit) (*circuitbreakerrpc.Limit, error) {
case ModeQueuePeerInitiated:
rpcLimit.Mode = circuitbreakerrpc.Mode_MODE_QUEUE_PEER_INITIATED
case ModeBlock:
rpcLimit.Mode = circuitbreakerrpc.Mode_MODE_BLOCK
default:
return nil, errors.New("unknown mode")
}

View file

@ -1,7 +1,7 @@
{
"files": {
"main.css": "/static/css/main.1d740b7c.css",
"main.js": "/static/js/main.6f9e9d2a.js",
"main.js": "/static/js/main.b21ce40c.js",
"static/js/787.5fcbf511.chunk.js": "/static/js/787.5fcbf511.chunk.js",
"static/media/primeicons.svg": "/static/media/primeicons.e1441b135b3ca9ad643c.svg",
"static/media/primeicons.eot": "/static/media/primeicons.d44157bdfa026dc877af.eot",
@ -11,11 +11,11 @@
"static/media/logo.svg": "/static/media/logo.6ce24c58023cc2f8fd88fe9d219db6c6.svg",
"index.html": "/index.html",
"main.1d740b7c.css.map": "/static/css/main.1d740b7c.css.map",
"main.6f9e9d2a.js.map": "/static/js/main.6f9e9d2a.js.map",
"main.b21ce40c.js.map": "/static/js/main.b21ce40c.js.map",
"787.5fcbf511.chunk.js.map": "/static/js/787.5fcbf511.chunk.js.map"
},
"entrypoints": [
"static/css/main.1d740b7c.css",
"static/js/main.6f9e9d2a.js"
"static/js/main.b21ce40c.js"
]
}

View file

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.png"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>Circuit Breaker</title><script defer="defer" src="/static/js/main.6f9e9d2a.js"></script><link href="/static/css/main.1d740b7c.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.png"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>Circuit Breaker</title><script defer="defer" src="/static/js/main.b21ce40c.js"></script><link href="/static/css/main.1d740b7c.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -199,7 +199,8 @@ function App() {
const modes = [
{ label: 'Fail', value: 'MODE_FAIL' },
{ label: 'Queue', value: 'MODE_QUEUE' },
{ label: 'Queue peer initiated', value: 'MODE_QUEUE_PEER_INITIATED' }
{ label: 'Queue peer initiated', value: 'MODE_QUEUE_PEER_INITIATED' },
{ label: 'Block', value: 'MODE_BLOCK' }
];
const modesWithUseDefault = [...modes, { label: 'Use default', value: 'MODE_DEFAULT' }]
@ -230,6 +231,10 @@ function App() {
mode = 'Queue peer initiated'
break;
case 'MODE_BLOCK':
mode = 'Block'
break;
case 'MODE_DEFAULT':
mode = 'Use default'
break;