Compare commits

..

428 Commits

Author SHA1 Message Date
noah metz 8cb97d2350 update 2024-03-31 18:58:27 -07:00
noah metz c29981da20 Updated MessageQueue 2024-03-31 17:02:30 -07:00
noah metz 810e17990c Made default queue size 2, and not sure how that sped up the test to pre-change speeds 2024-03-31 15:24:34 -07:00
noah metz d0d07d464d Remove debug print 2024-03-31 15:19:08 -07:00
noah metz 11e7df2bde Changed from a static channel queue to a dynamic queue for nodes 2024-03-31 15:18:47 -07:00
noah metz 3eee736f97 Moved SendMsg and RecvMsg to one object 2024-03-30 23:36:50 -07:00
noah metz 7e157068d7 Removed database update every signal process(need to find a replacement still), updated graphiql cmd, and made lockable more efficient at high numbers of requirements 2024-03-30 22:57:18 -07:00
noah metz b2d84b2453 Moved db from badger to an interface 2024-03-30 14:42:06 -07:00
noah metz 66d5e3f260 Changed serialization to not allocate any memory, expects to be passed enough memory to serialize the type 2024-03-28 20:23:22 -07:00
noah metz 1eff534e1a Fixes and optimizations 2024-03-28 19:28:07 -07:00
noah metz 3d28c703db Removed event.go 2024-03-25 18:50:53 -06:00
noah metz a4115a4f99 Updated gql subscriptions to be send less messages 2024-03-25 18:49:52 -06:00
noah metz ab76f09923 Got GQL subscriptions working for lockable_state 2024-03-23 03:23:00 -06:00
noah metz 6850031e80 Removed log lines, and fixed lock fail logic 2024-03-23 02:51:46 -06:00
noah metz 0b93c90aa9 Removed uneeded lockable field 2024-03-23 02:23:42 -06:00
noah metz 2db4655670 Rewrote lockable.go 2024-03-23 02:21:27 -06:00
noah metz d7b07df798 Reorganized to cleanup 2024-03-21 14:22:34 -06:00
noah metz 0bced58fd1 Fixed GQL issues, started docs 2024-03-21 14:13:54 -06:00
noah metz 8f9a759b26 Added GQL enum 2024-03-17 14:25:34 -06:00
noah metz c0407b094c Enabled GQL tests and got them working to a bare minimum 2024-03-10 16:31:14 -06:00
noah metz c591fa5ace Got node resolution working(I think), and save/load from DB 2024-03-10 15:41:13 -06:00
noah metz f8dad12fdb Got some basic node resolving working 2024-03-09 22:09:40 -07:00
noah metz eef8451566 Basic gql functionality, need to work on resolving nodeIDs by sending/waiting for read signals 2024-03-08 17:43:30 -07:00
noah metz 7e143c9d93 Added graphiql command, fixed map GQL naming, and made serialization interface use generics 2024-03-08 14:35:23 -07:00
noah metz 7314c74087 Rewrote serialization 2024-03-08 00:22:51 -07:00
noah metz 1eb6479169 Fixed race condition with load order 2024-03-04 22:11:40 -07:00
noah metz e16bec3997 Started graphql and serialization come together 2024-03-04 21:30:11 -07:00
noah metz 6942dc02db Major cleanup 2024-03-04 17:30:42 -07:00
noah metz e5776e0a14 Updated 100 test to 1000 2024-03-03 16:38:40 -07:00
noah metz 8927077167 More cleanup, moved initialization to interface instead of signals 2024-03-03 16:37:03 -07:00
noah metz faab7eb52c Cleanup(disabled gql test temporarily) 2024-03-03 15:45:45 -07:00
noah metz 61565fa18c Made lockable abort fully if everything unlocked on first error 2023-11-19 00:28:08 -07:00
noah metz 9c534a1d33 Upped gql subscriber channel size from 1 to 100 2023-11-15 09:33:02 -07:00
noah metz c33f37e4cd Made NewSuccessSignal return correct type 2023-11-14 16:40:17 -07:00
noah metz b42753b575 Reduced test from 1000 to 100 2023-11-14 15:51:24 -07:00
noah metz df9707309f Added time to event_ext.UpdateState, added event start time to gql 2023-11-13 17:29:53 -07:00
noah metz 646e6592f0 Not sure tbh 2023-11-13 13:23:58 -07:00
noah metz c5f95d431d Changed event strings to EventCommand or EventControl 2023-11-12 13:36:11 -07:00
noah metz 59424cecc1 Added some logging and fixes 2023-11-11 14:52:08 -07:00
noah metz 786f374b5f Added unlock state change to lockable timeout handling 2023-11-11 14:21:10 -07:00
noah metz c54101f4a3 Don't have time to explain :/ 2023-11-11 13:53:41 -07:00
noah metz 520219f7b0 Removed superfluous writeheader call 2023-11-07 20:58:03 -07:00
noah metz 8b91d0af0c Changed WaitInfo struct to include reason 2023-11-07 20:51:34 -07:00
noah metz 8bb1dacf23 Added logging 2023-11-06 01:28:17 -07:00
noah metz 6580725241 fixed errors 2023-11-06 00:59:25 -07:00
noah metz 58675377fd Moved writes to be per-extension 2023-11-06 00:50:29 -07:00
noah metz dee7b917f7 Simplified TestEventExt with ValidateTransition 2023-11-05 23:32:13 -07:00
noah metz 8d1e273331 Reworked changes to include map[ExtType][]string 2023-11-05 21:18:37 -07:00
noah metz 42e4a8f7ea Serialization optimization 2023-11-04 23:21:43 -06:00
noah metz c773ea2b14 Changed event to require parent 2023-11-04 18:56:35 -06:00
noah metz ddb3854d00 Handle timeouts better in lockable 2023-11-03 22:54:28 -06:00
noah metz f41160da68 Cleaned up Lockable 2023-11-03 22:51:54 -06:00
noah metz 3052f8099f Changed QueueTimeout to not require WaitMap 2023-11-03 21:53:49 -06:00
noah metz f5a08bbc48 Moved WaitInfo to node.go, added *Node methods for handling WaitMap's 2023-11-03 21:41:06 -06:00
noah metz 57156251cd More signal prints 2023-11-03 00:59:48 -06:00
noah metz 8e3510129c Added 'ready' state between init and start for events, and added String for more signals 2023-11-02 20:58:38 -06:00
noah metz 3a6e562390 Added more signal string representations, and fixed event/gql bugs 2023-11-01 19:32:36 -06:00
noah metz a061d6850c Added TestEventExt and TestEvent, added return of queued signals to WaitForResponse 2023-10-30 19:40:30 -06:00
noah metz 2081771135 Sort maps when serializing so they can be serialized consistently 2023-10-30 13:23:08 -06:00
noah metz dbe819fd05 Updated 1k test 2023-10-30 01:42:36 -06:00
noah metz 39d3288094 Changed to linked list for serialization 2023-10-30 01:25:18 -06:00
noah metz 0e8590e22a Added call to PostDeserialize that I removed by accident 2023-10-29 18:43:43 -06:00
noah metz 84aee24a21 Reworked serialization to split type/value serilization/deserialization 2023-10-29 18:26:14 -06:00
noah metz 8a973c38b5 Save node on parsing queued signal, look into saving after signals queued as well to mirror(or saving signal queue to a different db key) 2023-10-17 15:17:44 -06:00
noah metz 0eab243659 Fixed file resolution 2023-10-17 15:11:58 -06:00
noah metz a568adc156 Fixed serilize test 2023-10-16 23:08:23 -06:00
noah metz 266e353c5f Fixed duration serialization 2023-10-16 10:03:51 -06:00
noah metz 95939fb020 Added more serialized stuf 2023-10-16 00:54:10 -06:00
noah metz 92bb4bf976 removed todo 2023-10-15 22:44:35 -06:00
noah metz 193952e84d Fixed cache bug 2023-10-15 22:43:11 -06:00
noah metz d930d78351 Added logging 2023-10-15 20:35:02 -06:00
noah metz e299e77e78 Made Tree serialize the same always by sorting before serializing 2023-10-15 18:34:34 -06:00
noah metz b9bf61cf68 fix 2023-10-15 16:29:16 -06:00
noah metz 4b7bc93914 Moved groups to use SubGroups instead so one node can support many sub_groups(admin, ref, user, etc.) to reduce signals sent 2023-10-15 15:14:33 -06:00
noah metz 0159d0dd5a Fixed GQL test auth 2023-10-14 16:01:45 -06:00
noah metz 76e1e9a17a Fixed verification of authorized signals 2023-10-14 15:53:20 -06:00
noah metz b3bbf71c22 Removed unused functions 2023-10-14 15:31:18 -06:00
noah metz 08c36e0505 Added authorization check if non-nil when parsing node messages 2023-10-14 15:16:56 -06:00
noah metz c4df57a932 Added Authorization to not pass node private keys 2023-10-14 15:05:23 -06:00
noah metz 8c80ec9dd6 Added gv tag to ACLProxyPolicy so that it actually serializes/deserializes 2023-10-13 14:12:30 -06:00
noah metz 187ffb1324 Added ACLProxyPolicy 2023-10-13 13:45:30 -06:00
noah metz 16e25c009f Added ACLExt and tests 2023-10-13 00:32:24 -06:00
noah metz c63ad91252 Added mutations for add/remove members 2023-10-11 19:08:12 -06:00
noah metz b32f264879 Updated gql test with working subscribe 2023-10-11 19:04:43 -06:00
noah metz ae289705bb Added add/remove member to group, and test for group 2023-10-11 18:11:24 -06:00
noah metz 190824e710 gql fixes 2023-10-11 17:13:36 -06:00
noah metz c4de49099b Updated graphiql handler to use authToken() where available 2023-10-10 22:25:12 -06:00
noah metz 0a936f50f8 Set ID and direction of created signals 2023-10-10 19:35:42 -06:00
noah metz 0941c6c64e Added gql_signal.go 2023-10-10 18:25:30 -06:00
noah metz 92d8dfd006 Made status signal printable 2023-10-10 12:53:31 -06:00
noah metz f82bbabc66 Removed extension type from Changes, and made serializable 2023-10-10 11:23:44 -06:00
noah metz 542c5c18af Added code to stop gql server on shutdown 2023-10-08 23:53:38 -06:00
noah metz 6381713972 Reduced signal log spam 2023-10-08 02:38:18 -06:00
noah metz 34162023cb Removed log statement, and moved initial node write to after parsing of creation signal 2023-10-08 02:35:11 -06:00
noah metz 56f3cce415 Simplified lockable changes 2023-10-08 02:22:02 -06:00
noah metz 7234b11643 Simplified changes 2023-10-07 23:08:18 -06:00
noah metz 302f0f42fe Added 'changed' field to 'Process' 2023-10-07 23:00:07 -06:00
noah metz 7451e8e960 Added StoppedSignal as stopped status(no db write happens after processing) and added helper functions to stop nodes. 2023-10-06 20:04:53 -06:00
noah metz 9eadb00397 Added default group policy 2023-10-03 21:18:06 -06:00
noah metz e042384b3f Made fixes for group 2023-10-03 20:14:26 -06:00
noah metz 3ef0a98a17 Fixed broken deserialization of NodeID and uuid.UUID 2023-10-01 20:59:45 -06:00
noah metz bb28d9bc32 Fixed handling of lockable self-success messages 2023-10-01 20:54:53 -06:00
noah metz 96408259d1 Lots of stuff, but mostly sped up NodeID and UUID serialization, can probably generalize to speed up all fixed size arrays 2023-10-01 20:45:44 -06:00
noah metz 5a86334d5a Moved ReqID out of SignalHeader and into ResponseHeader, added ResponseSignal interface for signals that are responses to others 2023-10-01 16:45:03 -06:00
noah metz e93fe50b5f Update for tm 2023-09-27 19:47:48 -06:00
noah metz e013edc656 Fixed bugs found developing tm 2023-09-27 18:28:56 -06:00
noah metz d4e0d855c7 Added serialization for Tree and SerializedType. Changed policies to an array instead of a map 2023-09-20 19:14:28 -06:00
noah metz 0fc6215448 Allow resolution of non-acl fields 2023-09-20 11:28:44 -06:00
noah metz b09e150c46 Added PostDeserialize to GQLExt to initialize temporary maps/slices 2023-09-20 11:05:47 -06:00
noah metz d86d424cd7 Changed Header to return a non-pointer, and removed extension type from GetCtx generic 2023-09-18 21:43:39 -06:00
noah metz ff7046badf Removed unecessary capnp dependency 2023-09-18 20:11:50 -06:00
noah metz ab5b922a7d Forward status signals to resolvers 2023-09-18 19:55:55 -06:00
noah metz d34304f6ad Added logic to not re-resolve nodes if they're cached, and cache nodes as they're resolved 2023-09-18 12:02:30 -06:00
noah metz 9ffa9d6cb2 Reorganized GQL files, and moved from modifying the GQL library to using a NodeCache in ResolveContext 2023-09-18 11:15:58 -06:00
noah metz 21224e8837 Updated with graphql changed, need to make Self delete source of status on message received 2023-09-17 16:00:24 -06:00
noah metz 6bfe339854 Modified graphql library to store the result of the last resolution in a subscription and pass it to the next resolution 2023-09-14 15:50:08 -06:00
noah metz ecaf35f05d Simplified gql resolution a bit, need to work on subscriptions 2023-09-13 16:27:55 -06:00
noah metz eb30b477d5 Passing tests 2023-09-13 10:56:49 -06:00
noah metz 5c70d1b18d Moved type registration to signal/extension/policy registration 2023-09-12 20:30:18 -06:00
noah metz de1a229db6 Made it illegal to serialize/deserialize unregistered structs, and added StartSignal and StopSignal 2023-09-12 19:48:16 -06:00
noah metz de54c87e43 Added serialization of StatusSignal 2023-09-12 19:40:06 -06:00
noah metz dac0f1f273 Removed extra methods from ListenerExt 2023-09-12 19:01:35 -06:00
noah metz 07ce005365 Got serialization to the point that TestGQLDB is passing 2023-09-12 19:00:48 -06:00
noah metz c4e5054e07 Fixed array serialization and added NodeID serialization 2023-09-12 16:56:01 -06:00
noah metz b47c95c5ad Copied slice serialize/deserialize to array. Need to find out why the typestack is becoming corrupt now 2023-09-11 21:54:00 -06:00
noah metz 045304f9f6 Moved int, struct, and interface serialization to functions to make registering types easy 2023-09-11 21:47:53 -06:00
noah metz d0f0fb1b82 Fixed first round of issues with node serialization, now need to actually deserialize structs 2023-09-06 18:29:35 -06:00
noah metz fa5facc5fc Added deserialize for map 2023-09-05 10:48:04 -06:00
noah metz b766aadef9 Added deserialization for map type, nil map, and empty map 2023-09-05 01:02:41 -06:00
noah metz 47107dec1c Moved type definitions to variables 2023-09-05 00:53:58 -06:00
noah metz 15793e1415 Fixed pointer, list, and map serialization after testing nil map types 2023-09-05 00:46:49 -06:00
noah metz e2f34150ef Moved serialization to serialize.go and removed n parameter from DeserializeValue 2023-09-05 00:08:09 -06:00
noah metz 06513a5ad6 Added serialization tests, added deserializers for most Kinds, reworked struct serialization 2023-09-03 17:50:12 -06:00
noah metz 799b6404dd Added deserialize to pointer 2023-09-02 18:49:37 -06:00
noah metz 1888cf428d Think i finished serialization(other than missing types/kinds), now onto deserialization 2023-09-02 17:30:52 -06:00
noah metz 857f04efe3 Ch-ch-ch-ch-changes 2023-08-31 22:31:29 -06:00
noah metz 4daec4d601 Cleanup and move away from capnp to custom TLV serialization 2023-08-31 19:50:32 -06:00
noah metz 7bed89701d Made NewErrorSignal use fmt.Sprintf and tage variable args 2023-08-28 15:09:01 -06:00
noah metz ba344bddcf Fixed policy serializing and added default policy to allow receiving errors, read results, and status updates 2023-08-28 14:52:28 -06:00
noah metz 4ce2a642c5 Changed TestLink to test add and remove 2023-08-15 19:28:15 -06:00
noah metz f398c9659e Removed LinkStartSignal 2023-08-15 19:18:21 -06:00
noah metz 98f05d57f9 Added signals to add and remove requirements from a lockable node 2023-08-15 19:17:13 -06:00
noah metz 98c0b7e807 Fixed lockable to properly fail 2023-08-15 18:23:06 -06:00
noah metz b446c9078a Added WaitForResult and removed context arg from WaitForSignal 2023-08-12 11:26:19 -06:00
noah metz d663314def Test updates 2023-08-11 16:00:36 -06:00
noah metz e26ddcae37 Moved listener to listener.go and user.go to group.go. Fixed some GQL resolving 2023-08-11 13:01:32 -06:00
noah metz 20c7a38044 Policy fun 2023-08-10 23:43:10 -06:00
noah metz f31beade29 Added signature to all signals(signature of serialized signal + source + dest so technically vulnerable to replay) to use for ACL 2023-08-08 14:00:17 -06:00
noah metz 96e842decf Fixed some of the issues with GQL tests, still need to fix ACL 2023-08-07 20:44:56 -06:00
noah metz 8770d6f433 Moved policies to node instead of an extension, need to fix gql tests 2023-08-07 20:26:02 -06:00
noah metz 1d91854f6f Moved to x25519 for EC operations 2023-08-06 12:47:47 -06:00
noah metz 7d0af0eb5b Added NewSignal that extensions can process when a node is newly created 2023-08-04 19:47:17 -06:00
noah metz 0f7a0debd6 haha 2023-08-01 20:58:27 -06:00
noah metz 147f44e5ff hehe 2023-08-01 20:55:15 -06:00
noah metz 42cd8f4188 Added AuthorizedSignal wrapper to wrap a signal with an ecdsa signature to prove it originated from a different node 2023-08-01 14:09:29 -06:00
noah metz b9a2cceaf1 Moved gql response channel interaction into methods 2023-07-31 21:03:48 -06:00
noah metz df09433b88 Changed resolves to map back to request channel specifically instead of to the context 2023-07-31 20:53:56 -06:00
noah metz 47151905a0 fixed NodeInterfaceDefaultIsType 2023-07-31 20:12:06 -06:00
noah metz 3a53163f36 Fixed node interface type resolver 2023-07-31 19:53:29 -06:00
noah metz c515128743 GQL fix 2023-07-31 19:48:05 -06:00
noah metz 5f2b97a75b Policy load fix 2023-07-31 19:44:52 -06:00
noah metz 5bef8d96ba GQL Interface resolve logging 2023-07-31 19:38:30 -06:00
noah metz ed9c353b95 Added read result to default policy allow for all nodes 2023-07-31 19:22:33 -06:00
noah metz 3bc427f2a9 Added default node policy that allows any node to send a response. 2023-07-31 19:16:23 -06:00
noah metz 6895aa7c8e gql fix :) 2023-07-31 18:49:56 -06:00
noah metz 0cc7174667 gql fix 2023-07-31 18:47:58 -06:00
noah metz 059c36663b GQL fix 2023-07-31 18:43:17 -06:00
noah metz b06c741ee3 GQL fix 2023-07-31 18:38:40 -06:00
noah metz 42597057af Fixed IDSignal 2023-07-31 18:31:32 -06:00
noah metz 09c25b1e48 Moved UUID field to all signals to nodes can wait for responses to specific signals 2023-07-31 18:29:26 -06:00
noah metz e7d94414d5 Added stopServer gql mutation 2023-07-31 17:27:54 -06:00
noah metz d8355ab786 Added Self query to gql 2023-07-31 17:15:52 -06:00
noah metz 887a976263 Write node to DB every signal 2023-07-31 16:37:32 -06:00
noah metz 771bf53356 Added logging to gql process 2023-07-31 16:32:57 -06:00
noah metz 0313d6a33f Added start signal, and used it to restore gql server if was previously running 2023-07-31 16:25:18 -06:00
noah metz 064dc72820 Added State to GQLExt, TODO: add a start signal that gets sent to a node when it's loop is started so it's extensions can perform activation sequences 2023-07-30 23:59:04 -06:00
noah metz 0424a3970f Changed lockable responses to errors 2023-07-30 23:52:33 -06:00
noah metz fde2f3ddd4 Renamed signals and added ErrorSignal 2023-07-30 23:42:47 -06:00
noah metz 1af94520a8 Added option to wait for signal indefinitely 2023-07-30 13:19:51 -06:00
noah metz 4e31a6763d removed prints 2023-07-30 11:49:35 -06:00
noah metz b4e6123d4c test 2023-07-30 11:29:58 -06:00
noah metz e34d3ec981 node logging 2023-07-30 11:27:23 -06:00
noah metz 31f6c12f14 test 2023-07-30 11:25:03 -06:00
noah metz ec9a29573a test again 2023-07-30 11:21:40 -06:00
noah metz 395a75fcb8 fix time comparison 2023-07-30 11:19:34 -06:00
noah metz 9f9e65cf54 temp 2023-07-30 11:16:50 -06:00
noah metz da58b04774 fix 2023-07-30 11:12:47 -06:00
noah metz 7810859bca more logging 2023-07-30 11:07:41 -06:00
noah metz ad6ea0cc59 more node signal queue logging 2023-07-30 11:02:22 -06:00
noah metz 96c2b84b6f * 2023-07-30 10:09:04 -06:00
noah metz 79e40bf3f3 Fix signal queue 2023-07-30 01:29:15 -06:00
noah metz f56f92a58b Updated test with group ext gql 2023-07-29 23:24:48 -06:00
noah metz e92b2e508d Added tests for lockable gql fields 2023-07-29 22:16:54 -06:00
noah metz dca4de183e Think I got the interface registering figured out, need to test it out with self-referrential fields(list and single) from LockableExt 2023-07-29 19:16:33 -06:00
noah metz fad8d8123c Did most of the work to move node interface creation to GQLExtContext 2023-07-29 18:27:52 -06:00
noah metz 891e69c775 Added more sane methods to register GQL fields, need to do the same for itnerfaces and their defaults 2023-07-29 17:23:25 -06:00
noah metz d6a35247b0 Add tests for gql server type in gql 2023-07-29 16:34:21 -06:00
noah metz 1a3a07336a Added back GQL functionality, TODO pool channels for resolve executions instead of creating new ones and GCing them every time 2023-07-29 16:00:01 -06:00
noah metz 2dfa10b1f6 Renamed GQL functions to remove GQL 2023-07-29 11:03:41 -06:00
noah metz c60393d390 gql changes 2023-07-29 00:40:18 -06:00
noah metz 641bd8febe Moved ecdh to it's own extension 2023-07-29 00:28:44 -06:00
noah metz f87571edcf Moved test 'WaitForX' functions to a generic function in signal.go that can be used to wait for arbitrary signals 2023-07-28 19:32:27 -06:00
noah metz 5fb1cb6d17 Changed node ids to be derived from signing keys 2023-07-28 15:07:38 -06:00
noah metz b92cebbe74 Added message buffer size to node header and NewNode 2023-07-28 13:45:14 -06:00
noah metz 5678c79798 Made 10k test run faster, need to look at node memory usage 2023-07-28 13:12:17 -06:00
noah metz 5f409def03 Removed extra logging from tests 2023-07-28 12:46:27 -06:00
noah metz 61de2669e2 Added test to create 10K lockables and link them to a single lockable 2023-07-28 12:46:06 -06:00
noah metz 08288f88af Added basic test for read result with ACL 2023-07-28 11:59:01 -06:00
noah metz fc69bc3d0d Added read signal, and modified policies to use non-pointer receivers 2023-07-28 11:21:18 -06:00
noah metz b3de3144cc Reworked actions to be lists of parts, and added wildcards for both multi-level and single-level 2023-07-28 10:04:31 -06:00
noah metz 27687add1b Changed acl from using SignalType to Action, and added Permission() method to Signal interface that returns the Action the signal is trying to take 2023-07-28 00:39:27 -06:00
noah metz a16cf6bb38 Added merging to policies, need to make another interface for the shared all/per node policies to make a shared loading function 2023-07-28 00:32:43 -06:00
noah metz fb7e6d02f4 Fixed json parsing, not sure if it broke or just wasn't running before 2023-07-28 00:04:18 -06:00
noah metz d40e561728 Moved Hash function to single implementation 2023-07-27 23:26:58 -06:00
noah metz f314b46415 Fixed imports 2023-07-27 23:17:44 -06:00
noah metz 027c3d4c96 Moved ExtType and PolicyType definitions to one block 2023-07-27 23:15:58 -06:00
noah metz c763725a34 Fixed lockable link sequences 2023-07-27 23:05:19 -06:00
noah metz 027bb74887 Added unlock, need to update link to match pattern and see if it can be generalized 2023-07-27 22:25:00 -06:00
noah metz a1ce4238cc Implemented locking over signals, TODO: implement unlock 2023-07-27 19:53:43 -06:00
noah metz a44b00bc97 Made it easier to make a lockable extension 2023-07-27 18:37:06 -06:00
noah metz 200e19eea7 Added signalling to lockable test 2023-07-27 18:16:37 -06:00
noah metz 98893de442 Added naive locking sequence with short test 2023-07-27 18:08:43 -06:00
noah metz 78c29d2f74 Changed context to dynamically load nodes as they're signalled 2023-07-27 16:48:39 -06:00
noah metz 7ebb519cd0 Started lockable_test.go 2023-07-27 16:21:27 -06:00
noah metz 9d31394707 Remvoed Node.Signal and replaced with Context.Send 2023-07-27 16:13:43 -06:00
noah metz 6b375245df Added comments and switched from a bool/mutex combo to prevent double-start to an atomic bool 2023-07-27 16:06:56 -06:00
noah metz 7965f8fbe6 Moved signal function to context 2023-07-27 15:49:21 -06:00
noah metz d729698523 Removed commented out code 2023-07-27 15:29:56 -06:00
noah metz 26d122e3c5 Removed thread.go 2023-07-27 15:27:22 -06:00
noah metz 3ad969a5ca Switched from thread being the callback engine to node being the callback engine 2023-07-27 15:27:14 -06:00
noah metz 7a7a9c95a3 Fixed gql_test.go 2023-07-27 12:20:49 -06:00
noah metz c62ef57fe7 Removed uncessary permission from gql_test 2023-07-27 12:04:29 -06:00
noah metz 544264f06b Cleaned up gql_test by putting the listener extension on gql 2023-07-27 12:03:39 -06:00
noah metz c34d717b52 Cleaned up initialization 2023-07-27 11:33:11 -06:00
noah metz 95a2f46d28 Fixed policies in existing test 2023-07-27 10:04:39 -06:00
noah metz 70baca9e9c Added some policies back, and changed the policy check to pass errors instead of booleans 2023-07-27 09:32:33 -06:00
noah metz 4fa88dc056 Changed SendUpdate to Node.Process, and changed principal to ID to prepare for decoupling nodes 2023-07-27 01:30:32 -06:00
noah metz 81c2e11304 Renamed SendSignal to Node.Process 2023-07-27 01:15:53 -06:00
noah metz cc807b3982 Added helper function to create base gql server 2023-07-27 00:57:18 -06:00
noah metz 186123ce01 Added SimpleListenerNode to test suite 2023-07-27 00:30:24 -06:00
noah metz 494d212051 Simplified gql node/interface creation with helper functions 2023-07-26 23:57:50 -06:00
noah metz 34082630b2 Added default schema back without crashing 2023-07-26 20:26:41 -06:00
noah metz 294bcfb518 Re-added all files after rework, still need to add more complex tests 2023-07-26 15:52:40 -06:00
noah metz 89c8fba39b Added RequirementOfPolicy and thread extension child loading 2023-07-26 15:40:33 -06:00
noah metz d2f3daf5a6 Changed NewNode to return a pointer and add the node to the context 2023-07-26 15:08:14 -06:00
noah metz 755edf8558 Added ParentOfPolicy and ChildOfPolicy 2023-07-26 13:28:03 -06:00
noah metz fa6142d880 Started adding back gql tests 2023-07-26 11:56:10 -06:00
noah metz c4156ee146 Added basic test that loads node with only acl extension 2023-07-26 00:42:12 -06:00
noah metz f1c0f1e7de Added gql to the rework 2023-07-26 00:18:11 -06:00
noah metz ff813d6c2b Moved from inheritance to extensions 2023-07-25 21:43:15 -06:00
noah metz b3f6ea67c9 Added Group node 2023-07-25 09:51:55 -06:00
noah metz 4e69ba19f6 Added UserOfPolicy to context 2023-07-25 09:31:57 -06:00
noah metz 54e25ba602 Added attempt at UserOfPolicy 2023-07-25 00:57:54 -06:00
noah metz 89b082fc8e Cleaned up policy.go and added start of UserOfPolicy(need to finish allows) 2023-07-25 00:50:26 -06:00
noah metz a72124e659 Added NodeWithUsers interface to encompass any nodes that have a Users map 2023-07-25 00:27:39 -06:00
noah metz a2395189a8 Added LoadJSONNode to abstract out repeated json unmarshaling 2023-07-25 00:19:39 -06:00
noah metz 59df9f04d5 Added more policy types, removed tags 2023-07-24 22:52:15 -06:00
noah metz f118dde221 Add policies to write context 2023-07-24 20:45:07 -06:00
noah metz 6a2ed50578 Fixed thread reference to node references in thread.go 2023-07-24 20:41:58 -06:00
noah metz cfc1048007 fix 2 electric boogaloo 2023-07-24 20:37:41 -06:00
noah metz 264f31ad9a fix 2023-07-24 20:34:29 -06:00
noah metz 5c3a14bdbd fix 2023-07-24 20:27:02 -06:00
noah metz bd9e0a6d6f fix 2023-07-24 20:24:17 -06:00
noah metz 8c12eae3a7 fix 2023-07-24 20:21:14 -06:00
noah metz 8e191543d6 Added ClearActionQueue 2023-07-24 19:00:44 -06:00
noah metz 24285ba5da Fix 2023-07-24 18:47:24 -06:00
noah metz 42fcbc240a Added logging 2023-07-24 18:44:09 -06:00
noah metz c014ceb12c Fixed action queue 2023-07-24 18:42:51 -06:00
noah metz aa960b9420 Added started signal to ThreadStart 2023-07-24 18:39:33 -06:00
noah metz cf35d8a86d Added PolicyMap 2023-07-24 18:04:16 -06:00
noah metz 3fa550ab29 reworked thread to use queue instead of single action 2023-07-24 17:49:26 -06:00
noah metz 2e77b811d5 Add policies to get tests to pass 2023-07-24 17:17:12 -06:00
noah metz 7d04923b3b Fixed interface conversion panics 2023-07-24 17:07:27 -06:00
noah metz fc2e36043f Tests compile and run 2023-07-24 16:04:56 -06:00
noah metz 201ee7234b Remove policies read requirement to signal 2023-07-24 01:48:03 -06:00
noah metz fa10ccd743 Attempt to fix DependencyPolicy 2023-07-24 01:41:47 -06:00
noah metz c64dd728ed fix 2023-07-24 01:22:43 -06:00
noah metz 3ce1e3ad91 Added DependencyPolicy 2023-07-24 01:12:30 -06:00
noah metz a97b47af1b fix 2023-07-23 23:55:32 -06:00
noah metz 8d94e04b07 Fixed lockable lock permissions 2023-07-23 23:54:05 -06:00
noah metz fc6c198ae3 Moved gql thread actions/handlers to module functions 2023-07-23 23:01:45 -06:00
noah metz 32ac1f618d Added fixed resource/action order in Node.Allowed, added ACL check when signaling, nodes must have signal.{type} permission to send a signal of {type} to a node 2023-07-23 21:17:31 -06:00
noah metz dfa420757b Moved status signals to StatusSignal type with status string as type, so they can be ACLd 2023-07-23 21:14:15 -06:00
noah metz d56245c5cf Added ExtractList to extract list of type from ResolveParams 2023-07-23 20:27:37 -06:00
noah metz 63fb5e3187 Fixed AuthHandler to not require write permission on user 2023-07-23 19:46:18 -06:00
noah metz 054fe3c0ec Fixed starting of gql server to correctly start children that should start 2023-07-23 19:04:04 -06:00
noah metz 8fb0cbc982 Reworked use/update to require a read/write context be initialized before starting, still need to figure out if brittle locking is the solution to potential deadlock, and implement if so 2023-07-23 17:57:47 -06:00
noah metz 575912d56f Fixed compile errors 2023-07-22 21:24:54 -06:00
noah metz e347a3f232 start maniacal rewrite, main goal is to combine node and lockable to remove any sync mutex deadlocks. Another goal is to make read contexts get copies of the state to ensure they don't modify and no lock is required to ensure no value changes, and write contexts use the lockable locks instead of mutex 2023-07-22 20:21:17 -06:00
noah metz 76512afd4e First pass of gql read permission 2023-07-21 19:16:30 -06:00
noah metz 97815c86ff Added GetResolveContext 2023-07-21 18:51:42 -06:00
noah metz 6d6effadec Removed types from context 2023-07-21 18:18:26 -06:00
noah metz e59b4f57fd fixed user TypeOf 2023-07-21 18:09:13 -06:00
noah metz e51e823b19 Fixed default thread fields 2023-07-21 18:07:49 -06:00
noah metz f09a1db2d4 Moved common graphql fields to functions instead of redefining every definition 2023-07-21 18:06:53 -06:00
noah metz 3b35885c90 Fix 2023-07-21 17:50:43 -06:00
noah metz d4fcd80ff8 Added Singleton and Field to simplify GQL definitions 2023-07-21 17:49:19 -06:00
noah metz 551370e541 Split gql_graph.go to gql_*.go 2023-07-21 15:16:35 -06:00
noah metz 0428645be3 Added ExtractParam and ExtractID 2023-07-21 14:28:53 -06:00
noah metz 000d369251 Moved ThreadStartChild to graphvent 2023-07-21 14:05:39 -06:00
noah metz 76af80e2f8 Fixed mutation names 2023-07-21 14:00:45 -06:00
noah metz d3f435351c Moved StartChild mutation to graphvent 2023-07-21 13:59:52 -06:00
noah metz 767fc0bd3c Updated test to use PerTagPolicy with default gql user tag 2023-07-21 13:56:44 -06:00
noah metz 34549f471a Added PerTagPolicy 2023-07-21 13:55:27 -06:00
noah metz 6bd009b43e Changed ACL to get passed entire node attempting action instead of just ID 2023-07-21 13:34:47 -06:00
noah metz 6cf2d2d957 Added SimplePolicy 2023-07-21 13:33:04 -06:00
noah metz 230ff2b883 First implementation of policies 2023-07-21 12:09:29 -06:00
noah metz 81e49b505b Moved self-signed certificate creation to NewGQLThread 2023-07-21 01:21:53 -06:00
noah metz 2c1c75572e Added save/load to tls state 2023-07-21 01:11:59 -06:00
noah metz cf248ee5b0 Add write to user when updating server 2023-07-21 01:06:38 -06:00
noah metz d4fb5a0922 Added TLS to GQLThread 2023-07-21 01:05:24 -06:00
noah metz 7435728321 Fixed thread.go 2023-07-21 00:06:11 -06:00
noah metz c3058fbd3d Added more to policy, and updated lockable to use better IDs 2023-07-21 00:02:53 -06:00
noah metz 61fd7d61aa Added policy.go 2023-07-20 23:19:10 -06:00
noah metz a88c704c57 Added Users query to GQLThread 2023-07-20 22:17:45 -06:00
noah metz 3cbb6c69e5 type name update 2023-07-20 22:08:45 -06:00
noah metz 99d4f18daf Moved user to user.go 2023-07-20 22:08:28 -06:00
noah metz e50b550cd7 Renamed GQLUser to User 2023-07-20 22:03:25 -06:00
noah metz 4dc42a813e GQLUser improvement 2023-07-20 00:24:22 -06:00
noah metz 2fde6ae282 Added KDF for shared secret to shared ecdsa key, and added signature to AuthRespJSON 2023-07-19 21:28:48 -06:00
noah metz 374fd6e487 Changed NodeID from string to UUID, filled in more auth 2023-07-19 20:03:13 -06:00
noah metz 6d0925f20f Added signature to auth response 2023-07-19 14:50:42 -06:00
noah metz 84af718071 Started to add authentication for GQL endpoint 2023-07-19 14:45:05 -06:00
noah metz d227331fe8 Return error for no auth 2023-07-13 18:28:02 -06:00
noah metz 372c465472 gql.go 2023-07-13 18:23:57 -06:00
noah metz 893fb8c4c4 require auth for gql 2023-07-13 18:21:33 -06:00
noah metz 5d23646cd5 Make restore use interface instead of struct 2023-07-12 22:31:13 -06:00
noah metz ddc3528538 Clear timeout_action when clearing timeout on thread 2023-07-12 21:12:29 -06:00
noah metz a9b40db0de Fixed typo in log 2023-07-11 17:22:14 -06:00
noah metz 1fbfdf3b8c Added more logging to ThreadRestore 2023-07-11 17:16:51 -06:00
noah metz f64455f8ed Moved GQL restore to ThreadRestore to be reused by other classes. And GQLThreadInfo to ParentThreadInfo 2023-07-11 16:54:09 -06:00
noah metz 6c38089aee Improved handling of nil ThreadInfo 2023-07-11 16:39:47 -06:00
noah metz ad407ff183 Added State to GQL Thread interface 2023-07-11 16:24:19 -06:00
noah metz 89f17b1b45 mend 2023-07-11 16:20:00 -06:00
noah metz e5ba988f01 Allow locking of already owned resources, considered nop and continues 2023-07-11 16:17:21 -06:00
noah metz 88d8113b5c Added AllowedToTakeLock to SimpleThread that allows children to inherit locks 2023-07-11 16:13:57 -06:00
noah metz 7a95af6db2 Added nil check to deserializeinfo 2023-07-11 00:23:07 -06:00
noah metz c30b9d082a Added AGPLv3 LICENSE 2023-07-10 22:39:37 -06:00
noah metz 743569e088 Added more doc comments 2023-07-10 22:31:43 -06:00
noah metz fb7fb5938d Changed NodeDef to use example instead of manually passing string and type 2023-07-10 21:24:47 -06:00
noah metz e12d02eb3f Cleaned up GQL context 2023-07-10 21:15:01 -06:00
noah metz a9431ecf73 Added db write log 2023-07-10 19:04:15 -06:00
noah metz 14b084b081 Fixed always initializing timeout on thread load. TODO: look at why loading it on thread load was causing the aborted event to not be sent 2023-07-10 01:07:56 -06:00
noah metz 6a0a0762ad Finished rework of context 2023-07-09 20:30:19 -06:00
noah metz 7aedf553ee Updated gql.go 2023-07-09 19:33:18 -06:00
noah metz 269e7a57e2 Added correct Type for simple_thread 2023-07-09 16:03:42 -06:00
noah metz b846bbb812 Updated thread.go and thread_test.go 2023-07-09 15:59:41 -06:00
noah metz 5c416a4a3f We do a little refactoring, hehe 2023-07-09 14:30:30 -06:00
noah metz a9d1fe5793 moved to ThreadLoop from separate StartThread and RestoreThread functions 2023-07-06 16:57:51 -06:00
noah metz 0e2b6ccb2a enhance tests for race conditions 2023-07-05 23:54:11 -06:00
noah metz ff46af585d Added fixes to pass go vet 2023-07-05 14:50:21 -06:00
noah metz a7f721e0cd Allow loading of nil thread_info 2023-07-04 19:46:14 -06:00
noah metz c42ca80d47 Modified SendUpdate to require it to be called from inside a UseStates context. 2023-07-04 18:45:23 -06:00
noah metz ce831af290 last fix 2023-07-03 19:46:01 -06:00
noah metz a23dfd2b81 test 2023-07-03 19:38:55 -06:00
noah metz dd6b0de414 fix 2023-07-03 19:37:04 -06:00
noah metz e5c4069cfc fix 2023-07-03 19:22:32 -06:00
noah metz 81945ab191 test 2023-07-03 19:19:23 -06:00
noah metz 543aec054e Logging 2023-07-03 19:16:17 -06:00
noah metz f44752d988 GQLWS Logging 2023-07-03 19:13:29 -06:00
noah metz fd72ad3901 Fixed gql subscription typo 2023-07-03 19:04:51 -06:00
noah metz 9485ef9772 Added self subscription 2023-07-03 18:58:15 -06:00
noah metz 53b93d5e70 Removed child_state from FindChild 2023-07-03 18:08:32 -06:00
noah metz 4b64fb1ef2 Removed state argument from LockLockable and UnlockLockable 2023-07-03 17:13:00 -06:00
noah metz 5bdc06bf0f Added UnlinkLockables and UnlinkThreads 2023-07-03 16:37:54 -06:00
noah metz 64171c6c85 Added UnlinkLockables and UnlinkThreads 2023-07-03 16:03:56 -06:00
noah metz 34f8a9f009 Fixed typo in thread loading that resulted in the parent being loaded as an owner instead 2023-07-03 13:14:48 -06:00
noah metz a05d847863 test 2023-07-02 12:54:29 -06:00
noah metz 8a5de1b0de fix 2023-07-02 12:51:22 -06:00
noah metz 300e735065 Added restore actions, and gql should run them 2023-07-02 12:47:45 -06:00
noah metz 8d041fa896 Added SetTimeout back to Thread 2023-07-02 12:23:35 -06:00
noah metz 6355cc4226 Moved timeout info to thread state, and fixed restore to add handlers and actions 2023-07-02 12:17:53 -06:00
noah metz c9d9b9ac03 Update a bunch of stuff 2023-07-02 12:14:04 -06:00
noah metz 9b61adc375 Added check to allow restored threads to be run in RunThread 2023-07-02 11:26:58 -06:00
noah metz 06a6b0f09a Name json fields for gql thread info 2023-07-02 10:49:42 -06:00
noah metz 1585034777 Fixed gql threadstate 2023-07-02 10:46:53 -06:00
noah metz ce0634cd61 Only lock thread during RunThread if it's not already owned by the thread 2023-07-02 10:39:15 -06:00
noah metz 3ebb6fe223 Added fail to LockLockables if owner is trying to lock again 2023-07-02 10:34:25 -06:00
noah metz fa22d8f2a9 Log change 2023-07-02 10:09:46 -06:00
noah metz 1c6983ff4a Exposed load function maps in graph context constructor 2023-07-02 09:34:36 -06:00
noah metz 638148afab Updated FindChild to use UseMoreStates and expect to get passed currently held states 2023-07-02 09:05:34 -06:00
noah metz 97c1405e3c Fixed paramter order of GQLContext init 2023-07-01 13:47:12 -06:00
noah metz b7a7d509e6 Exposed gql context initialization 2023-07-01 13:41:38 -06:00
noah metz c2d09a07e3 Fixed type resolution that was missed 2023-07-01 13:11:46 -06:00
noah metz 9cb1d26405 Added test to display GQLThread serialized data 2023-07-01 13:06:39 -06:00
noah metz d2b32bac5e Moved GQL context information out of node runtime state and into context 2023-07-01 13:03:28 -06:00
noah metz 04771b7816 Added BaseLockable and BaseThread nested saving/loading 2023-06-30 13:25:35 -06:00
noah metz 41d88b9d86 Removed print from test 2023-06-28 23:52:56 -06:00
noah metz 84a700909d Updated intances of state updating to use new API 2023-06-28 23:51:44 -06:00
noah metz e862f9e49c Add UseMoreStates and UpdateMoreStates that should only be called from eachother 2023-06-28 21:49:23 -06:00
noah metz 2c0fced413 Fix BaseLockable gql type 2023-06-28 01:53:39 -06:00
noah metz 82ae5279b0 test 2023-06-28 01:49:45 -06:00
noah metz e75171a9c5 test 2023-06-28 01:47:45 -06:00
noah metz 308b23ac69 fix 2023-06-28 01:45:57 -06:00
noah metz 59a9459d00 test 2023-06-28 01:44:00 -06:00
noah metz 7944cb694a fix 2023-06-28 01:41:55 -06:00
noah metz 524717e35e fix 2023-06-28 01:40:32 -06:00
noah metz 31ecc869ec Add more gql logging 2023-06-28 01:35:17 -06:00
noah metz 43dd555801 Add more gql logging 2023-06-28 01:32:37 -06:00
noah metz 65c653b896 Add more gql logging 2023-06-28 01:31:19 -06:00
noah metz 4db1b6446c Fix typo 2023-06-28 01:27:47 -06:00
noah metz 5b2b33d930 Add Lockable fields to thread in gql 2023-06-28 01:26:44 -06:00
noah metz 3e27d1d6b0 Add owner checks to LinkLockable 2023-06-28 01:02:43 -06:00
noah metz a696213e38 Rework locking to hold all locks before changing any state 2023-06-28 00:48:49 -06:00
noah metz ee98e13044 Log unlock 2023-06-27 18:48:45 -06:00
noah metz cf11176cff Fix gql base types 2023-06-27 13:07:23 -06:00
noah metz ff68f131e5 Try to get interface type differently 2023-06-27 13:00:53 -06:00
noah metz 18703d1a6d Fix pointer type 2023-06-27 12:58:21 -06:00
noah metz b174727bda Try to pass reflect.Type as pointer 2023-06-27 12:57:00 -06:00
noah metz 88a55cd095 Enhance logging 2023-06-27 12:55:53 -06:00
noah metz bed500e2db Added more gql logging 2023-06-27 12:53:10 -06:00
noah metz 5a694dc067 Updated gql to use base types as fallback 2023-06-27 10:30:11 -06:00
noah metz 7b6ceaaad2 Removed _state from json attributes 2023-06-26 23:20:12 -06:00
noah metz 1ce98e2719 Renamed delegation_map to locks_held 2023-06-26 23:18:54 -06:00
noah metz 856867fb10 Fix locking 2023-06-26 23:15:40 -06:00
noah metz cf8648f615 Chane UnlockLockable recursiveness 2023-06-26 23:12:43 -06:00
noah metz 6c1887ed76 Change lockable recursiveness 2023-06-26 23:10:18 -06:00
noah metz 94ef8ca3d0 Add ReadDBStateCopy 2023-06-26 23:03:11 -06:00
noah metz b145148471 Added Logj 2023-06-26 22:39:24 -06:00
noah metz 583e1957c1 Export BaseSignal fields 2023-06-26 22:30:02 -06:00
noah metz 10bd1ee823 Change BaseSignal.String() to use json.Marshal 2023-06-26 22:06:09 -06:00
noah metz 62d56f208a Add ChildGo to conveniently start children 2023-06-26 21:20:04 -06:00
noah metz 4c462a1ec5 Export InfoType for BaseThreadState to make it modifiable in libraries 2023-06-26 17:03:09 -06:00
noah metz dc7f742f32 Updated to make it easier to create modules 2023-06-26 01:16:44 -06:00
noah metz 26f57daf23 Changed NewLockable to NewBaseLockable 2023-06-25 22:23:57 -06:00
noah metz 10ab3a48cf made gql_test.go pass 2023-06-25 22:19:05 -06:00
noah metz bb3c80dbc7 Fixed internal signalling for threads, and update signal type in gql 2023-06-25 21:00:00 -06:00
noah metz 7b84c8bc54 Removed obsolete Makefile 2023-06-25 20:43:48 -06:00
29 changed files with 5615 additions and 2666 deletions

3
.gitignore vendored

@ -1,12 +1,15 @@
# Ignore everything # Ignore everything
* *
!/go-capnp
# But not these files... # But not these files...
!/.gitignore !/.gitignore
!*.go !*.go
*.capnp.go
!go.sum !go.sum
!go.mod !go.mod
!*.capnp
!README.md !README.md
!LICENSE !LICENSE

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "graphql"]
path = graphql
url = https://github.com/graphql-go/graphql

@ -0,0 +1,662 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.

@ -1,16 +1,4 @@
BINARY_DIR=./bin .PHONY: test
BINARY_NAME=event_managear
BINARY_PATH=${BINARY_DIR}/${BINARY_NAME}
build:
go build -o ${BINARY_PATH} event_manager
clean:
rm -rf bin
go clean
run: build
${BINARY_PATH}
test: test:
go test clear && go test

@ -0,0 +1,48 @@
package main
import (
"fmt"
badger "github.com/dgraph-io/badger/v3"
gv "github.com/mekkanized/graphvent"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
check(err)
ctx, err := gv.NewContext(&gv.BadgerDB{
DB: db,
}, gv.NewConsoleLogger([]string{"test"}))
check(err)
gql_ext, err := gv.NewGQLExt(ctx, ":8080", nil, nil)
check(err)
listener_ext := gv.NewListenerExt(1000)
n1, err := gv.NewNode(ctx, nil, "LockableNode", 1000, gv.NewLockableExt(nil))
check(err)
n2, err := gv.NewNode(ctx, nil, "LockableNode", 1000, gv.NewLockableExt([]gv.NodeID{n1.ID}))
check(err)
n3, err := gv.NewNode(ctx, nil, "LockableNode", 1000, gv.NewLockableExt(nil))
check(err)
_, err = gv.NewNode(ctx, nil, "LockableNode", 1000, gql_ext, listener_ext, gv.NewLockableExt([]gv.NodeID{n2.ID, n3.ID}))
check(err)
for true {
select {
case message := <- listener_ext.Chan:
fmt.Printf("Listener Message: %+v\n", message)
}
}
}

File diff suppressed because it is too large Load Diff

284
db.go

@ -0,0 +1,284 @@
package graphvent
import (
"encoding/binary"
"fmt"
"reflect"
"sync"
badger "github.com/dgraph-io/badger/v3"
)
type Database interface {
WriteNodeInit(*Context, *Node) error
WriteNodeChanges(*Context, *Node, map[ExtType]Changes) error
LoadNode(*Context, NodeID) (*Node, error)
}
const WRITE_BUFFER_SIZE = 1000000
type BadgerDB struct {
*badger.DB
sync.Mutex
buffer [WRITE_BUFFER_SIZE]byte
}
func (db *BadgerDB) WriteNodeInit(ctx *Context, node *Node) error {
if node == nil {
return fmt.Errorf("Cannot serialize nil *Node")
}
return db.Update(func(tx *badger.Txn) error {
db.Lock()
defer db.Unlock()
// Get the base key bytes
id_ser, err := node.ID.MarshalBinary()
if err != nil {
return err
}
cur := 0
// Write Node value
written, err := Serialize(ctx, node, db.buffer[cur:])
if err != nil {
return err
}
err = tx.Set(id_ser, db.buffer[cur:cur+written])
if err != nil {
return err
}
cur += written
// Write empty signal queue
sigqueue_id := append(id_ser, []byte(" - SIGQUEUE")...)
written, err = Serialize(ctx, node.SignalQueue, db.buffer[cur:])
if err != nil {
return err
}
err = tx.Set(sigqueue_id, db.buffer[cur:cur+written])
if err != nil {
return err
}
cur += written
// Write node extension list
ext_list := []ExtType{}
for ext_type := range(node.Extensions) {
ext_list = append(ext_list, ext_type)
}
written, err = Serialize(ctx, ext_list, db.buffer[cur:])
if err != nil {
return err
}
ext_list_id := append(id_ser, []byte(" - EXTLIST")...)
err = tx.Set(ext_list_id, db.buffer[cur:cur+written])
if err != nil {
return err
}
cur += written
// For each extension:
for ext_type, ext := range(node.Extensions) {
ext_info, exists := ctx.Extensions[ext_type]
if exists == false {
return fmt.Errorf("Cannot serialize node with unknown extension %s", reflect.TypeOf(ext))
}
ext_value := reflect.ValueOf(ext).Elem()
ext_id := binary.BigEndian.AppendUint64(id_ser, uint64(ext_type))
// Write each field to a seperate key
for field_tag, field_info := range(ext_info.Fields) {
field_value := ext_value.FieldByIndex(field_info.Index)
field_id := make([]byte, len(ext_id) + 8)
tmp := binary.BigEndian.AppendUint64(ext_id, uint64(GetFieldTag(string(field_tag))))
copy(field_id, tmp)
written, err := SerializeValue(ctx, field_value, db.buffer[cur:])
if err != nil {
return fmt.Errorf("Extension serialize err: %s, %w", reflect.TypeOf(ext), err)
}
err = tx.Set(field_id, db.buffer[cur:cur+written])
if err != nil {
return fmt.Errorf("Extension set err: %s, %w", reflect.TypeOf(ext), err)
}
cur += written
}
}
return nil
})
}
func (db *BadgerDB) WriteNodeChanges(ctx *Context, node *Node, changes map[ExtType]Changes) error {
return db.Update(func(tx *badger.Txn) error {
db.Lock()
defer db.Unlock()
// Get the base key bytes
id_bytes := ([16]byte)(node.ID)
cur := 0
// Write the signal queue if it needs to be written
if node.writeSignalQueue {
node.writeSignalQueue = false
sigqueue_id := append(id_bytes[:], []byte(" - SIGQUEUE")...)
written, err := Serialize(ctx, node.SignalQueue, db.buffer[cur:])
if err != nil {
return fmt.Errorf("SignalQueue Serialize Error: %+v, %w", node.SignalQueue, err)
}
err = tx.Set(sigqueue_id, db.buffer[cur:cur+written])
if err != nil {
return fmt.Errorf("SignalQueue set error: %+v, %w", node.SignalQueue, err)
}
cur += written
}
// For each ext in changes
for ext_type, changes := range(changes) {
ext_info, exists := ctx.Extensions[ext_type]
if exists == false {
return fmt.Errorf("%s is not an extension in ctx", ext_type)
}
ext, exists := node.Extensions[ext_type]
if exists == false {
return fmt.Errorf("%s is not an extension in %s", ext_type, node.ID)
}
ext_id := binary.BigEndian.AppendUint64(id_bytes[:], uint64(ext_type))
ext_value := reflect.ValueOf(ext)
// Write each field
for _, tag := range(changes) {
field_info, exists := ext_info.Fields[tag]
if exists == false {
return fmt.Errorf("Cannot serialize field %s of extension %s, does not exist", tag, ext_type)
}
field_value := ext_value.FieldByIndex(field_info.Index)
field_id := make([]byte, len(ext_id) + 8)
tmp := binary.BigEndian.AppendUint64(ext_id, uint64(GetFieldTag(string(tag))))
copy(field_id, tmp)
written, err := SerializeValue(ctx, field_value, db.buffer[cur:])
if err != nil {
return fmt.Errorf("Extension serialize err: %s, %w", reflect.TypeOf(ext), err)
}
err = tx.Set(field_id, db.buffer[cur:cur+written])
if err != nil {
return fmt.Errorf("Extension set err: %s, %w", reflect.TypeOf(ext), err)
}
cur += written
}
}
return nil
})
}
func (db *BadgerDB) LoadNode(ctx *Context, id NodeID) (*Node, error) {
var node *Node = nil
err := db.View(func(tx *badger.Txn) error {
// Get the base key bytes
id_ser, err := id.MarshalBinary()
if err != nil {
return fmt.Errorf("Failed to serialize node_id: %w", err)
}
// Get the node value
node_item, err := tx.Get(id_ser)
if err != nil {
return fmt.Errorf("Failed to get node_item: %w", NodeNotFoundError)
}
err = node_item.Value(func(val []byte) error {
ctx.Log.Logf("db", "DESERIALIZE_NODE(%d bytes): %+v", len(val), val)
node, err = Deserialize[*Node](ctx, val)
return err
})
if err != nil {
return fmt.Errorf("Failed to deserialize Node %s - %w", id, err)
}
// Get the signal queue
sigqueue_id := append(id_ser, []byte(" - SIGQUEUE")...)
sigqueue_item, err := tx.Get(sigqueue_id)
if err != nil {
return fmt.Errorf("Failed to get sigqueue_id: %w", err)
}
err = sigqueue_item.Value(func(val []byte) error {
node.SignalQueue, err = Deserialize[[]QueuedSignal](ctx, val)
return err
})
if err != nil {
return fmt.Errorf("Failed to deserialize []QueuedSignal for %s: %w", id, err)
}
// Get the extension list
ext_list_id := append(id_ser, []byte(" - EXTLIST")...)
ext_list_item, err := tx.Get(ext_list_id)
if err != nil {
return err
}
var ext_list []ExtType
ext_list_item.Value(func(val []byte) error {
ext_list, err = Deserialize[[]ExtType](ctx, val)
return err
})
// Get the extensions
for _, ext_type := range(ext_list) {
ext_id := binary.BigEndian.AppendUint64(id_ser, uint64(ext_type))
ext_info, exists := ctx.Extensions[ext_type]
if exists == false {
return fmt.Errorf("Extension %s not in context", ext_type)
}
ext := reflect.New(ext_info.Type)
for field_tag, field_info := range(ext_info.Fields) {
field_id := binary.BigEndian.AppendUint64(ext_id, uint64(GetFieldTag(string(field_tag))))
field_item, err := tx.Get(field_id)
if err != nil {
return fmt.Errorf("Failed to find key for %s:%s(%x) - %w", ext_type, field_tag, field_id, err)
}
err = field_item.Value(func(val []byte) error {
value, _, err := DeserializeValue(ctx, val, field_info.Type)
if err != nil {
return err
}
ext.Elem().FieldByIndex(field_info.Index).Set(value)
return nil
})
if err != nil {
return err
}
}
node.Extensions[ext_type] = ext.Interface().(Extension)
}
return nil
})
if err != nil {
return nil, err
} else if node == nil {
return nil, fmt.Errorf("Tried to return nil *Node from BadgerDB.LoadNode without error")
}
return node, nil
}

@ -0,0 +1,16 @@
package graphvent
type Tag string
type Changes []Tag
// Extensions are data attached to nodes that process signals
type Extension interface {
// Called to process incoming signals, returning changes and messages to send
Process(*Context, *Node, NodeID, Signal) ([]Message, Changes)
// Called when the node is loaded into a context(creation or move), so extension data can be initialized
Load(*Context, *Node) error
// Called when the node is unloaded from a context(deletion or move), so extension data can be cleaned up
Unload(*Context, *Node)
}

@ -1,32 +1,36 @@
module github.com/mekkanized/graphvent module github.com/mekkanized/graphvent
go 1.20 go 1.22.0
require ( require (
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/gobwas/ws v1.2.1
github.com/google/uuid v1.3.0
github.com/graphql-go/graphql v0.8.1
github.com/rs/zerolog v1.29.1
golang.org/x/net v0.7.0
)
require (
filippo.io/edwards25519 v1.0.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
github.com/dgraph-io/badger/v4 v4.1.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/protobuf v1.3.1 // indirect github.com/golang/protobuf v1.3.1 // indirect
github.com/golang/snappy v0.0.3 // indirect github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/graphql-go/graphql v0.8.1 // indirect
github.com/graphql-go/handler v0.2.3 // indirect
github.com/klauspost/compress v1.12.3 // indirect github.com/klauspost/compress v1.12.3 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/rs/zerolog v1.29.1 // indirect github.com/stretchr/testify v1.8.2 // indirect
go.opencensus.io v0.22.5 // indirect go.opencensus.io v0.22.5 // indirect
golang.org/x/net v0.7.0 // indirect golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/sys v0.6.0 // indirect golang.org/x/sys v0.13.0 // indirect
) )

@ -1,5 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
@ -14,20 +17,17 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg= github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg=
github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw=
github.com/dgraph-io/badger/v4 v4.1.0 h1:E38jc0f+RATYrycSUf9LMv/t47XAy+3CApyYSq4APOQ=
github.com/dgraph-io/badger/v4 v4.1.0/go.mod h1:P50u28d39ibBRmIJuQC/NSdBOg46HnHw7al2SW5QRHg=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
@ -51,15 +51,10 @@ github.com/google/flatbuffers v1.12.1 h1:MVlul7pQNoDzWRLTw5imwYsl+usrS1TXG2H4jg6
github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc=
github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
github.com/graphql-go/graphql v0.8.1 h1:p7/Ou/WpmulocJeEx7wjQy611rtXGQaAcXGqanuMMgc= github.com/graphql-go/graphql v0.8.1 h1:p7/Ou/WpmulocJeEx7wjQy611rtXGQaAcXGqanuMMgc=
github.com/graphql-go/graphql v0.8.1/go.mod h1:nKiHzRM0qopJEwCITUuIsxk9PlVlwIiiI8pnJEhordQ= github.com/graphql-go/graphql v0.8.1/go.mod h1:nKiHzRM0qopJEwCITUuIsxk9PlVlwIiiI8pnJEhordQ=
github.com/graphql-go/handler v0.2.3 h1:CANh8WPnl5M9uA25c2GBhPqJhE53Fg0Iue/fRNla71E=
github.com/graphql-go/handler v0.2.3/go.mod h1:leLF6RpV5uZMN1CdImAxuiayrYYhOk33bZciaUGaXeU=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@ -69,8 +64,6 @@ github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU=
github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
@ -78,16 +71,17 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc= github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
@ -96,23 +90,29 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI=
go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
@ -142,11 +142,12 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@ -170,4 +171,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

692
gql.go

@ -1,21 +1,81 @@
package graphvent package graphvent
import ( import (
"net/http"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/parser"
"github.com/graphql-go/graphql/language/source"
"github.com/graphql-go/graphql/language/ast"
"context" "context"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"os"
"net"
"net/http"
"reflect" "reflect"
"fmt"
"sync" "sync"
"time"
"github.com/gobwas/ws" "github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil" "github.com/gobwas/ws/wsutil"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/parser"
"github.com/graphql-go/graphql/language/source"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"github.com/google/uuid"
) )
func PrepResolve(p graphql.ResolveParams) (*ResolveContext, error) {
resolve_context, ok := p.Context.Value("resolve").(*ResolveContext)
if ok == false {
return nil, fmt.Errorf("Bad resolve in params context")
}
return resolve_context, nil
}
// TODO: Make composabe by checking if K is a slice, then recursing in the same way that ExtractList does
func ExtractParam[K interface{}](p graphql.ResolveParams, name string) (K, error) {
var zero K
arg_if, ok := p.Args[name]
if ok == false {
return zero, fmt.Errorf("No Arg of name %s", name)
}
arg, ok := arg_if.(K)
if ok == false {
return zero, fmt.Errorf("Failed to cast arg %s(%+v) to %+v", name, arg_if, reflect.TypeOf(zero))
}
return arg, nil
}
func ExtractList[K interface{}](p graphql.ResolveParams, name string) ([]K, error) {
var zero K
arg_list, err := ExtractParam[[]interface{}](p, name)
if err != nil {
return nil, err
}
ret := make([]K, len(arg_list))
for i, val := range(arg_list) {
val_conv, ok := arg_list[i].(K)
if ok == false {
return nil, fmt.Errorf("Failed to cast arg %s[%d](%+v) to %+v", name, i, val, reflect.TypeOf(zero))
}
ret[i] = val_conv
}
return ret, nil
}
func GraphiQLHandler() func(http.ResponseWriter, *http.Request) { func GraphiQLHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r * http.Request) { return func(w http.ResponseWriter, r * http.Request) {
graphiql_string := fmt.Sprintf(` graphiql_string := fmt.Sprintf(`
@ -70,15 +130,32 @@ func GraphiQLHandler() func(http.ResponseWriter, *http.Request) {
type="application/javascript" type="application/javascript"
></script> ></script>
<script> <script>
if (window.authToken === undefined) {
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
root.render(
React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({
url: '/gql',
}),
defaultEditorToolsVisibility: true,
}),
);
} else {
authToken().then(function(res){
const root = ReactDOM.createRoot(document.getElementById('graphiql')); const root = ReactDOM.createRoot(document.getElementById('graphiql'));
root.render( root.render(
React.createElement(GraphiQL, { React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({ fetcher: GraphiQL.createFetcher({
url: '/gql', url: '/gql',
headers: {
"Authorization": ` + "`${res}`" + `,
},
}), }),
defaultEditorToolsVisibility: true, defaultEditorToolsVisibility: true,
}), }),
); );
});
}
</script> </script>
</body> </body>
</html> </html>
@ -91,7 +168,7 @@ func GraphiQLHandler() func(http.ResponseWriter, *http.Request) {
} }
type GQLWSPayload struct { type GQLPayload struct {
OperationName string `json:"operationName,omitempty"` OperationName string `json:"operationName,omitempty"`
Query string `json:"query,omitempty"` Query string `json:"query,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty"` Variables map[string]interface{} `json:"variables,omitempty"`
@ -102,7 +179,7 @@ type GQLWSPayload struct {
type GQLWSMsg struct { type GQLWSMsg struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Type string `json:"type"` Type string `json:"type"`
Payload GQLWSPayload `json:"payload,omitempty"` Payload GQLPayload `json:"payload,omitempty"`
} }
func enableCORS(w *http.ResponseWriter) { func enableCORS(w *http.ResponseWriter) {
@ -112,7 +189,59 @@ func enableCORS(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Methods", "*") (*w).Header().Set("Access-Control-Allow-Methods", "*")
} }
func GQLHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Context) func(http.ResponseWriter, *http.Request) { type GQLUnauthorized string
func (e GQLUnauthorized) Is(target error) bool {
error_type := reflect.TypeOf(GQLUnauthorized(""))
target_type := reflect.TypeOf(target)
return error_type == target_type
}
func (e GQLUnauthorized) Error() string {
return fmt.Sprintf("GQL_UNAUTHORIZED_ERROR: %s", string(e))
}
func (e GQLUnauthorized) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(&struct{
Error string `json:"error"`
}{
Error: string(e),
}, "", " ")
}
// Context passed to each resolve execution
type ResolveContext struct {
// Resolution ID
ID uuid.UUID
// Channels for the gql extension to route data to this context
Chans map[uuid.UUID]chan Signal
// Graph Context this resolver is running under
Context *Context
// Pointer to the node that's currently processing this request
Server *Node
// The state data for the node processing this request
Ext *GQLExt
// Cache of resolved nodes
NodeCache map[NodeID]NodeResult
}
func NewResolveContext(ctx *Context, server *Node, gql_ext *GQLExt) (*ResolveContext, error) {
return &ResolveContext{
ID: uuid.New(),
Ext: gql_ext,
Chans: map[uuid.UUID]chan Signal{},
Context: ctx,
NodeCache: map[NodeID]NodeResult{},
Server: server,
}, nil
}
func GQLHandler(ctx *Context, server *Node, gql_ext *GQLExt) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r * http.Request) { return func(w http.ResponseWriter, r * http.Request) {
ctx.Log.Logf("gql", "GQL REQUEST: %s", r.RemoteAddr) ctx.Log.Logf("gql", "GQL REQUEST: %s", r.RemoteAddr)
enableCORS(&w) enableCORS(&w)
@ -120,19 +249,33 @@ func GQLHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Conte
for header, value := range(r.Header) { for header, value := range(r.Header) {
header_map[header] = value header_map[header] = value
} }
ctx.Log.Logm("gql", header_map, "REQUEST_HEADERS")
resolve_context, err := NewResolveContext(ctx, server, gql_ext)
if err != nil {
ctx.Log.Logf("gql", "GQL_AUTH_ERR: %s", err)
json.NewEncoder(w).Encode(GQLUnauthorized(""))
return
} else {
ctx.Log.Logf("gql", "New Query: %s", resolve_context.ID)
}
req_ctx := context.Background()
req_ctx = context.WithValue(req_ctx, "resolve", resolve_context)
str, err := io.ReadAll(r.Body) str, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
ctx.Log.Logf("gql", "failed to read request body: %s", err) ctx.Log.Logf("gql", "GQL_READ_ERR: %s", err)
json.NewEncoder(w).Encode(fmt.Sprintf("%e", err))
return return
} }
query := GQLWSPayload{} query := GQLPayload{}
json.Unmarshal(str, &query) json.Unmarshal(str, &query)
schema := ctx.Extensions[ExtTypeFor[GQLExt]()].Data.(graphql.Schema)
params := graphql.Params{ params := graphql.Params{
Schema: schema, Schema: schema,
Context: gql_ctx, Context: req_ctx,
RequestString: query.Query, RequestString: query.Query,
} }
if query.OperationName != "" { if query.OperationName != "" {
@ -141,12 +284,13 @@ func GQLHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Conte
if len(query.Variables) > 0 { if len(query.Variables) > 0 {
params.VariableValues = query.Variables params.VariableValues = query.Variables
} }
result := graphql.Do(params) result := graphql.Do(params)
if len(result.Errors) > 0 { if len(result.Errors) > 0 {
extra_fields := map[string]interface{}{} extra_fields := map[string]interface{}{}
extra_fields["body"] = string(str) extra_fields["body"] = string(str)
extra_fields["headers"] = r.Header extra_fields["headers"] = r.Header
ctx.Log.Logm("gql", extra_fields, "wrong result, unexpected errors: %v", result.Errors) ctx.Log.Logm("gql_errors", extra_fields, "wrong result, unexpected errors: %v", result.Errors)
} }
json.NewEncoder(w).Encode(result) json.NewEncoder(w).Encode(result)
} }
@ -161,7 +305,6 @@ func sendOneResultAndClose(res *graphql.Result) chan *graphql.Result {
return resultChannel return resultChannel
} }
func getOperationTypeOfReq(p graphql.Params) string{ func getOperationTypeOfReq(p graphql.Params) string{
source := source.NewSource(&source.Source{ source := source.NewSource(&source.Source{
Body: []byte(p.RequestString), Body: []byte(p.RequestString),
@ -170,7 +313,7 @@ func getOperationTypeOfReq(p graphql.Params) string{
AST, err := parser.Parse(parser.ParseParams{Source: source}) AST, err := parser.Parse(parser.ParseParams{Source: source})
if err != nil { if err != nil {
return "" return err.Error()
} }
for _, node := range AST.Definitions { for _, node := range AST.Definitions {
@ -184,33 +327,36 @@ func getOperationTypeOfReq(p graphql.Params) string{
} }
} }
} }
return "" return "END_OF_FUNCTION"
}
func GQLWSDo(ctx * GraphContext, p graphql.Params) chan *graphql.Result {
operation := getOperationTypeOfReq(p)
ctx.Log.Logf("gqlws", "GQLWSDO_OPERATION: %s %+v", operation, p.RequestString)
if operation == ast.OperationTypeSubscription {
return graphql.Subscribe(p)
}
res := graphql.Do(p)
return sendOneResultAndClose(res)
} }
func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Context) func(http.ResponseWriter, *http.Request) { func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r * http.Request) { return func(w http.ResponseWriter, r * http.Request) {
ctx.Log.Logf("gqlws_new", "HANDLING %s",r.RemoteAddr) ctx.Log.Logf("gqlws_new", "HANDLING %s",r.RemoteAddr)
enableCORS(&w)
header_map := map[string]interface{}{} header_map := map[string]interface{}{}
for header, value := range(r.Header) { for header, value := range(r.Header) {
header_map[header] = value header_map[header] = value
} }
ctx.Log.Logm("gql", header_map, "REQUEST_HEADERS")
resolve_context, err := NewResolveContext(ctx, server, gql_ext)
if err != nil {
ctx.Log.Logf("gql", "GQL_AUTH_ERR: %s", err)
return
} else {
ctx.Log.Logf("gql", "New Subscription: %s", resolve_context.ID)
}
req_ctx := context.Background()
req_ctx = context.WithValue(req_ctx, "resolve", resolve_context)
u := ws.HTTPUpgrader{ u := ws.HTTPUpgrader{
Protocol: func(protocol string) bool { Protocol: func(protocol string) bool {
ctx.Log.Logf("gqlws", "UPGRADE_PROTOCOL: %s", string(protocol)) ctx.Log.Logf("gqlws", "UPGRADE_PROTOCOL: %s", string(protocol))
return string(protocol) == "graphql-transport-ws" if string(protocol) == "graphql-transport-ws" || string(protocol) == "graphql-ws" {
return true
}
return false
}, },
} }
conn, _, _, err := u.Upgrade(r, w) conn, _, _, err := u.Upgrade(r, w)
@ -218,7 +364,6 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
defer conn.Close() defer conn.Close()
conn_state := "init" conn_state := "init"
for { for {
// TODO: Make this a select between reading client data and getting updates from the event to push to clients"
msg_raw, op, err := wsutil.ReadClientData(conn) msg_raw, op, err := wsutil.ReadClientData(conn)
ctx.Log.Logf("gqlws_hb", "MSG: %s\nOP: 0x%02x\nERR: %+v\n", string(msg_raw), op, err) ctx.Log.Logf("gqlws_hb", "MSG: %s\nOP: 0x%02x\nERR: %+v\n", string(msg_raw), op, err)
msg := GQLWSMsg{} msg := GQLWSMsg{}
@ -232,6 +377,19 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
ctx.Log.Logf("gqlws", "WS_CLIENT_ERROR: INIT WHILE IN %s", conn_state) ctx.Log.Logf("gqlws", "WS_CLIENT_ERROR: INIT WHILE IN %s", conn_state)
break break
} }
connection_params := struct {
Payload struct {
Token string `json:"token"`
} `json:"payload"`
}{}
err := json.Unmarshal([]byte(msg_raw), &connection_params)
if err != nil {
ctx.Log.Logf("gqlws", "WS_UNMARSHAL_ERROR: %s - %+v", msg_raw, err)
break
}
conn_state = "ready" conn_state = "ready"
err = wsutil.WriteServerMessage(conn, 1, []byte("{\"type\": \"connection_ack\"}")) err = wsutil.WriteServerMessage(conn, 1, []byte("{\"type\": \"connection_ack\"}"))
if err != nil { if err != nil {
@ -243,12 +401,14 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
err = wsutil.WriteServerMessage(conn, 1, []byte("{\"type\": \"pong\"}")) err = wsutil.WriteServerMessage(conn, 1, []byte("{\"type\": \"pong\"}"))
if err != nil { if err != nil {
ctx.Log.Logf("gqlws", "WS_SERVER_ERROR: FAILED TO SEND PONG") ctx.Log.Logf("gqlws", "WS_SERVER_ERROR: FAILED TO SEND PONG")
break
} }
} else if msg.Type == "subscribe" { } else if msg.Type == "subscribe" {
ctx.Log.Logf("gqlws", "SUBSCRIBE: %+v", msg.Payload) ctx.Log.Logf("gqlws", "SUBSCRIBE: %+v", msg.Payload)
schema := ctx.Extensions[ExtTypeFor[GQLExt]()].Data.(graphql.Schema)
params := graphql.Params{ params := graphql.Params{
Schema: schema, Schema: schema,
Context: gql_ctx, Context: req_ctx,
RequestString: msg.Payload.Query, RequestString: msg.Payload.Query,
} }
if msg.Payload.OperationName != "" { if msg.Payload.OperationName != "" {
@ -258,7 +418,15 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
params.VariableValues = msg.Payload.Variables params.VariableValues = msg.Payload.Variables
} }
res_chan := GQLWSDo(ctx, params) var res_chan chan *graphql.Result
operation := getOperationTypeOfReq(params)
if operation == ast.OperationTypeSubscription {
res_chan = graphql.Subscribe(params)
} else {
res := graphql.Do(params)
res_chan = sendOneResultAndClose(res)
}
go func(res_chan chan *graphql.Result) { go func(res_chan chan *graphql.Result) {
for { for {
@ -274,7 +442,7 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
if len(next.Errors) > 0 { if len(next.Errors) > 0 {
extra_fields := map[string]interface{}{} extra_fields := map[string]interface{}{}
extra_fields["query"] = string(msg.Payload.Query) extra_fields["query"] = string(msg.Payload.Query)
ctx.Log.Logm("gqlws", extra_fields, "ERROR: wrong result, unexpected errors: %+v", next.Errors) ctx.Log.Logm("gql_errors", extra_fields, "ERROR: wrong result, unexpected errors: %+v", next.Errors)
continue continue
} }
ctx.Log.Logf("gqlws", "DATA: %+v", next.Data) ctx.Log.Logf("gqlws", "DATA: %+v", next.Data)
@ -286,7 +454,7 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
msg, err := json.Marshal(GQLWSMsg{ msg, err := json.Marshal(GQLWSMsg{
ID: msg.ID, ID: msg.ID,
Type: "next", Type: "next",
Payload: GQLWSPayload{ Payload: GQLPayload{
Data: string(data), Data: string(data),
}, },
}) })
@ -312,195 +480,373 @@ func GQLWSHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Con
} }
} }
type ObjTypeMap map[reflect.Type]*graphql.Object type Interface struct {
type FieldMap map[string]*graphql.Field Interface *graphql.Interface
Default *graphql.Object
List *graphql.List
Extensions []ExtType
}
type GQLThread struct { type Type struct {
BaseThread Type *graphql.Object
extended_types ObjTypeMap List *graphql.List
extended_queries FieldMap
extended_subscriptions FieldMap
extended_mutations FieldMap
} }
type GQLThreadState struct { type Field struct {
BaseThreadState Ext ExtType
Listen string Name string
Field *graphql.Field
} }
func NewGQLThreadState(listen string) GQLThreadState { type NodeResult struct {
return GQLThreadState{ NodeID NodeID
BaseThreadState: NewBaseThreadState("GQL Server"), NodeType NodeType
Listen: listen, Data map[string]interface{}
}
type ListField struct {
ACLName string
Extension ExtType
ResolveFn func(graphql.ResolveParams, *ResolveContext, reflect.Value) ([]NodeID, error)
}
type SelfField struct {
ACLName string
Extension ExtType
ResolveFn func(graphql.ResolveParams, *ResolveContext, reflect.Value) (*NodeID, error)
}
type SubscriptionInfo struct {
ID uuid.UUID
NodeCache *map[NodeID]NodeResult
Channel chan interface{}
}
type GQLExt struct {
tcp_listener net.Listener
http_server *http.Server
http_done sync.WaitGroup
subscriptions []SubscriptionInfo
subscriptions_lock sync.RWMutex
// map of read request IDs to response channels
resolver_response map[uuid.UUID]chan Signal
resolver_response_lock sync.RWMutex
State string `gv:"state"`
TLSKey []byte `gv:"tls_key"`
TLSCert []byte `gv:"tls_cert"`
Listen string `gv:"listen" gql:"GQLListen"`
}
func (ext *GQLExt) Load(ctx *Context, node *Node) error {
ctx.Log.Logf("gql", "Loading GQL server extension on %s", node.ID)
ext.resolver_response = map[uuid.UUID]chan Signal{}
ext.subscriptions = []SubscriptionInfo{}
return ext.StartGQLServer(ctx, node)
}
func (ext *GQLExt) Unload(ctx *Context, node *Node) {
ctx.Log.Logf("gql", "Unloading GQL server extension on %s", node.ID)
err := ext.StopGQLServer()
if err != nil {
ctx.Log.Logf("gql", "Error unloading GQL server extension on %s: %s", node.ID, err)
} else {
ctx.Log.Logf("gql", "Unloaded GQL server extension on %s", node.ID)
} }
} }
var gql_actions ThreadActions = ThreadActions{ func (ext *GQLExt) AddSubscription(id uuid.UUID, ctx *ResolveContext) (chan interface{}, error) {
"start": func(ctx * GraphContext, thread Thread) (string, error) { ext.subscriptions_lock.Lock()
ctx.Log.Logf("gql", "SERVER_STARTED") defer ext.subscriptions_lock.Unlock()
server := thread.(*GQLThread)
go func() {
ctx.Log.Logf("gql", "GOROUTINE_START for %s", server.ID())
mux := http.NewServeMux() for _, info := range(ext.subscriptions) {
http_handler, ws_handler := MakeGQLHandlers(ctx, server) if info.ID == id {
mux.HandleFunc("/gql", http_handler) return nil, fmt.Errorf("%+v already in subscription list", info.ID)
mux.HandleFunc("/gqlws", ws_handler) }
mux.HandleFunc("/graphiql", GraphiQLHandler()) }
fs := http.FileServer(http.Dir("./site"))
mux.Handle("/site/", http.StripPrefix("/site", fs))
srv_if, _ := UseStates(ctx, []GraphNode{server}, func(states []NodeState)(interface{}, error){ c := make(chan interface{}, 100)
server_state := states[0].(*GQLThreadState)
return &http.Server{ ext.subscriptions = append(ext.subscriptions, SubscriptionInfo{
Addr: server_state.Listen, id,
Handler: mux, &ctx.NodeCache,
}, nil c,
}) })
srv := srv_if.(*http.Server)
http_done := &sync.WaitGroup{} return c, nil
http_done.Add(1) }
go func(srv *http.Server, http_done *sync.WaitGroup) {
defer http_done.Done() func (ext *GQLExt) RemoveSubscription(id uuid.UUID) error {
err := srv.ListenAndServe() ext.subscriptions_lock.Lock()
if err != http.ErrServerClosed { defer ext.subscriptions_lock.Unlock()
panic(fmt.Sprintf("Failed to start gql server: %s", err))
for i, info := range(ext.subscriptions) {
if info.ID == id {
ext.subscriptions[i] = ext.subscriptions[len(ext.subscriptions)]
ext.subscriptions = ext.subscriptions[:len(ext.subscriptions)-1]
return nil
}
}
return fmt.Errorf("%+v not in subscription list", id)
}
func (ext *GQLExt) FindResponseChannel(req_id uuid.UUID) chan Signal {
ext.resolver_response_lock.RLock()
response_chan, _ := ext.resolver_response[req_id]
ext.resolver_response_lock.RUnlock()
return response_chan
}
func (ext *GQLExt) GetResponseChannel(req_id uuid.UUID) chan Signal {
response_chan := make(chan Signal, 1)
ext.resolver_response_lock.Lock()
ext.resolver_response[req_id] = response_chan
ext.resolver_response_lock.Unlock()
return response_chan
}
func (ext *GQLExt) FreeResponseChannel(req_id uuid.UUID) chan Signal {
response_chan := ext.FindResponseChannel(req_id)
if response_chan != nil {
ext.resolver_response_lock.Lock()
delete(ext.resolver_response, req_id)
ext.resolver_response_lock.Unlock()
} }
}(srv, http_done) return response_chan
}
for true { func (ext *GQLExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]Message, Changes) {
// Process ReadResultSignalType by forwarding it to the waiting resolver
var changes Changes = nil
var messages []Message = nil
switch sig := signal.(type) {
case *SuccessSignal:
response_chan := ext.FreeResponseChannel(sig.ReqID)
if response_chan != nil {
select { select {
case signal:=<-server.signal: case response_chan <- sig:
if signal.Type() == "abort" || signal.Type() == "cancel" { ctx.Log.Logf("gql", "Forwarded success to resolver, %+v", sig.ReqID)
err := srv.Shutdown(context.Background()) default:
if err != nil{ ctx.Log.Logf("gql", "Resolver channel overflow %+v", sig)
panic(fmt.Sprintf("Failed to shutdown gql server: %s", err))
} }
http_done.Wait()
break
} }
ctx.Log.Logf("gql", "GOROUTINE_SIGNAL for %s: %+v", server.ID(), signal)
// Take signals to resource and send to GQL subscriptions case *ErrorSignal:
// TODO: Forward to resolver if waiting for it
response_chan := ext.FreeResponseChannel(sig.ReqID)
if response_chan != nil {
select {
case response_chan <- sig:
ctx.Log.Logf("gql", "Forwarded error to resolver, %+v", sig.Error)
default:
ctx.Log.Logf("gql", "Resolver channel overflow %+v", sig)
} }
} }
}()
return "wait", nil case *ReadResultSignal:
}, response_chan := ext.FindResponseChannel(sig.ReqID)
if response_chan != nil {
select {
case response_chan <- sig:
default:
ctx.Log.Logf("gql", "Resolver channel overflow %+v", sig)
}
}
case *StatusSignal:
ext.subscriptions_lock.RLock()
for _, sub := range(ext.subscriptions) {
_, cached := (*sub.NodeCache)[sig.Source]
if cached {
select {
case sub.Channel <- sig:
ctx.Log.Logf("gql", "forwarded status signal %+v to subscription: %s", sig, sub.ID)
default:
ctx.Log.Logf("gql", "subscription channel overflow: %s", sub.ID)
}
}
}
ext.subscriptions_lock.RUnlock()
}
return messages, changes
} }
var gql_handlers ThreadHandlers = ThreadHandlers{ var ecdsa_curves = map[uint8]elliptic.Curve{
0: elliptic.P256(),
} }
func NewGQLThread(ctx * GraphContext, listen string, requirements []Lockable, extended_types ObjTypeMap, extended_queries FieldMap, extended_mutations FieldMap, extended_subscriptions FieldMap) (*GQLThread, error) { var ecdsa_curve_ids = map[elliptic.Curve]uint8{
state := NewGQLThreadState(listen) elliptic.P256(): 0,
base_thread, err := NewBaseThread(ctx, gql_actions, gql_handlers, &state) }
var ecdh_curves = map[uint8]ecdh.Curve{
0: ecdh.P256(),
}
var ecdh_curve_ids = map[ecdh.Curve]uint8{
ecdh.P256(): 0,
}
func NewGQLExt(ctx *Context, listen string, tls_cert []byte, tls_key []byte) (*GQLExt, error) {
if tls_cert == nil || tls_key == nil {
ssl_key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil { if err != nil {
return nil, err return nil, err
} }
thread := &GQLThread { ssl_key_bytes, err := x509.MarshalPKCS8PrivateKey(ssl_key)
BaseThread: base_thread,
extended_types: extended_types,
extended_queries: extended_queries,
extended_mutations: extended_mutations,
extended_subscriptions: extended_subscriptions,
}
err = LinkLockables(ctx, thread, requirements)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return thread, nil
}
func MakeGQLHandlers(ctx * GraphContext, server * GQLThread) (func(http.ResponseWriter, *http.Request), func(http.ResponseWriter, *http.Request)) { ssl_key_pem := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: ssl_key_bytes})
valid_nodes := map[reflect.Type]*graphql.Object{}
valid_lockables := map[reflect.Type]*graphql.Object{} serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
valid_threads := map[reflect.Type]*graphql.Object{} serialNumber, _ := rand.Int(rand.Reader, serialNumberLimit)
valid_lockables[reflect.TypeOf((*BaseLockable)(nil))] = GQLTypeBaseNode() notBefore := time.Now()
for t, v := range(valid_lockables) { notAfter := notBefore.Add(365*24*time.Hour)
valid_nodes[t] = v template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"mekkanized"},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
} }
valid_threads[reflect.TypeOf((*BaseThread)(nil))] = GQLTypeBaseThread()
valid_threads[reflect.TypeOf((*GQLThread)(nil))] = GQLTypeGQLThread() ssl_cert, err := x509.CreateCertificate(rand.Reader, &template, &template, ssl_key.Public(), ssl_key)
for t, v := range(valid_threads) { if err != nil {
valid_lockables[t] = v return nil, err
valid_nodes[t] = v
} }
ssl_cert_pem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ssl_cert})
gql_types := []graphql.Type{GQLTypeSignal(), GQLTypeSignalInput()} tls_cert = ssl_cert_pem
for _, v := range(valid_nodes) { tls_key = ssl_key_pem
gql_types = append(gql_types, v)
} }
node_type := reflect.TypeOf((GraphNode)(nil)) return &GQLExt{
lockable_type := reflect.TypeOf((Lockable)(nil)) Listen: listen,
thread_type := reflect.TypeOf((Thread)(nil)) resolver_response: map[uuid.UUID]chan Signal{},
subscriptions: []SubscriptionInfo{},
TLSCert: tls_cert,
TLSKey: tls_key,
}, nil
}
for go_t, gql_t := range(server.extended_types) { // Returns "${base}/${path}" if it's a file or "${base}/${path}/index.html" if it's a directory
if go_t.Implements(node_type) { // Returns os.ErrInvalid if "${base}/${path}/index.html" is a directory
valid_nodes[go_t] = gql_t func getContentPath(base string, path string) (string, error) {
} full_path := fmt.Sprintf("%s%s", base, path)
if go_t.Implements(lockable_type) { path_info, err := os.Stat(full_path)
valid_lockables[go_t] = gql_t
if err != nil && err != os.ErrNotExist {
return "", err
} else if path_info.IsDir() == true {
index_path := ""
if path[len(path)-1] != '/' {
index_path = fmt.Sprintf("%s%s/index.html", base, path)
} else {
index_path = fmt.Sprintf("%s%sindex.html", base, path)
} }
if go_t.Implements(thread_type) { index_info, err := os.Stat(index_path)
valid_threads[go_t] = gql_t if err != nil {
return "", err
} else if index_info.IsDir() == true {
return index_path, os.ErrInvalid
} else {
return index_path, nil
} }
gql_types = append(gql_types, gql_t) } else {
return full_path, nil
} }
}
gql_queries := graphql.Fields{ func (ext *GQLExt) StartGQLServer(ctx *Context, node *Node) error {
"Self": GQLQuerySelf(), if ext.tcp_listener != nil || ext.http_server != nil {
return fmt.Errorf("listener or server is still running, stop them first")
} }
mux := http.NewServeMux()
mux.HandleFunc("/gql", GQLHandler(ctx, node, ext))
mux.HandleFunc("/gqlws", GQLWSHandler(ctx, node, ext))
for key, value := range(server.extended_queries) { mux.HandleFunc("/graphiql", GraphiQLHandler())
gql_queries[key] = value
}
gql_subscriptions := graphql.Fields{ // Server the ./site directory to /site (TODO make configurable with better defaults)
"Update": GQLSubscriptionUpdate(),
}
for key, value := range(server.extended_subscriptions) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
gql_subscriptions[key] = value custom_path, err_1 := getContentPath("./custom", r.URL.Path)
if err_1 != nil {
static_path, err_2 := getContentPath("./site", r.URL.Path)
if err_2 != nil {
ctx.Log.Logf("gql", "File Resolve errors: %s - %s", err_1, err_2)
w.WriteHeader(501)
w.Write([]byte("{\"error\": \"server_error\"}"))
} else {
ctx.Log.Logf("gql", "STATIC_FILE: %s", static_path)
file, err := os.Open(static_path)
if err != nil {
w.WriteHeader(501)
w.Write([]byte("{\"error\": \"server_error\"}"))
}
http.ServeContent(w, r, static_path, time.Time{}, file)
}
} else {
ctx.Log.Logf("gql", "CUSTOM_FILE: %s", custom_path)
file, err := os.Open(custom_path)
if err != nil {
w.WriteHeader(501)
w.Write([]byte("{\"error\": \"server_error\"}"))
} }
http.ServeContent(w, r, custom_path, time.Time{}, file)
w.WriteHeader(200)
}
})
gql_mutations := graphql.Fields{ http_server := &http.Server{
"SendUpdate": GQLMutationSendUpdate(), Addr: ext.Listen,
Handler: mux,
} }
for key, value := range(server.extended_mutations) { l, err := net.Listen("tcp", http_server.Addr)
gql_mutations[key] = value if err != nil {
return fmt.Errorf("Failed to start listener for server on %s", http_server.Addr)
} }
schemaConfig := graphql.SchemaConfig{ ext.http_done.Add(1)
Types: gql_types, go func(qql_ext *GQLExt) {
Query: graphql.NewObject(graphql.ObjectConfig{ defer ext.http_done.Done()
Name: "Query",
Fields: gql_queries, err := http_server.Serve(l)
}), if err != http.ErrServerClosed {
Mutation: graphql.NewObject(graphql.ObjectConfig{ panic(fmt.Sprintf("Failed to start gql server: %s", err))
Name: "Mutation",
Fields: gql_mutations,
}),
Subscription: graphql.NewObject(graphql.ObjectConfig{
Name: "Subscription",
Fields: gql_subscriptions,
}),
} }
}(ext)
ext.tcp_listener = l
ext.http_server = http_server
ext.State = "running"
return nil
}
schema, err := graphql.NewSchema(schemaConfig) func (ext *GQLExt) StopGQLServer() error {
if err != nil{ if ext.tcp_listener == nil || ext.http_server == nil {
panic(err) return fmt.Errorf("already shutdown, cannot shut down again" )
} }
gql_ctx := context.Background() ext.http_server.Shutdown(context.TODO())
gql_ctx = context.WithValue(gql_ctx, "valid_nodes", valid_nodes) ext.http_done.Wait()
gql_ctx = context.WithValue(gql_ctx, "valid_lockables", valid_lockables) ext.tcp_listener = nil
gql_ctx = context.WithValue(gql_ctx, "valid_threads", valid_threads) ext.http_server = nil
gql_ctx = context.WithValue(gql_ctx, "gql_server", server) ext.State = "stopped"
gql_ctx = context.WithValue(gql_ctx, "graph_context", ctx) return nil
return GQLHandler(ctx, schema, gql_ctx), GQLWSHandler(ctx, schema, gql_ctx)
} }

@ -1,812 +0,0 @@
package graphvent
import (
"github.com/graphql-go/graphql"
"reflect"
"fmt"
"time"
)
var gql_interface_graph_node *graphql.Interface = nil
func GQLInterfaceGraphNode() *graphql.Interface {
if gql_interface_graph_node == nil {
gql_interface_graph_node = graphql.NewInterface(graphql.InterfaceConfig{
Name: "GraphNode",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
valid_nodes, ok := p.Context.Value("valid_nodes").(map[reflect.Type]*graphql.Object)
if ok == false {
return nil
}
for key, value := range(valid_nodes) {
if reflect.TypeOf(p.Value) == key {
return value
}
}
return nil
},
Fields: graphql.Fields{},
})
gql_interface_graph_node.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
})
gql_interface_graph_node.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
})
}
return gql_interface_graph_node
}
var gql_list_thread *graphql.List = nil
func GQLListThread() *graphql.List {
if gql_list_thread == nil {
gql_list_thread = graphql.NewList(GQLInterfaceThread())
}
return gql_list_thread
}
var gql_interface_thread *graphql.Interface = nil
func GQLInterfaceThread() *graphql.Interface {
if gql_interface_thread == nil {
gql_interface_thread = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Thread",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
valid_nodes, ok := p.Context.Value("valid_threads").(map[reflect.Type]*graphql.Object)
if ok == false {
return nil
}
for key, value := range(valid_nodes) {
if reflect.TypeOf(p.Value) == key {
return value
}
}
return nil
},
Fields: graphql.Fields{},
})
gql_interface_thread.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
})
gql_interface_thread.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
})
gql_interface_thread.AddFieldConfig("Children", &graphql.Field{
Type: GQLListThread(),
})
gql_interface_thread.AddFieldConfig("Parent", &graphql.Field{
Type: GQLInterfaceThread(),
})
}
return gql_interface_thread
}
var gql_list_lockable *graphql.List = nil
func GQLListLockable() *graphql.List {
if gql_list_lockable == nil {
gql_list_lockable = graphql.NewList(GQLInterfaceLockable())
}
return gql_list_lockable
}
var gql_interface_lockable *graphql.Interface = nil
func GQLInterfaceLockable() *graphql.Interface {
if gql_interface_lockable == nil {
gql_interface_lockable = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Lockable",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
valid_nodes, ok := p.Context.Value("valid_lockables").(map[reflect.Type]*graphql.Object)
if ok == false {
return nil
}
for key, value := range(valid_nodes) {
if reflect.TypeOf(p.Value) == key {
return value
}
}
return nil
},
Fields: graphql.Fields{},
})
gql_interface_lockable.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
})
gql_interface_lockable.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
})
if gql_list_lockable == nil {
gql_list_lockable = graphql.NewList(gql_interface_lockable)
}
gql_interface_lockable.AddFieldConfig("Requirements", &graphql.Field{
Type: GQLListLockable(),
})
gql_interface_lockable.AddFieldConfig("Dependencies", &graphql.Field{
Type: GQLListLockable(),
})
gql_interface_lockable.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceLockable(),
})
}
return gql_interface_lockable
}
func GQLNodeID(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(GraphNode)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to GraphNode")
}
return node.ID(), nil
}
func GQLNodeName(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(GraphNode)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to GraphNode")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
name, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
return states[0].Name(), nil
})
if err != nil {
return nil, err
}
name_str, ok := name.(string)
if ok == false {
return nil, fmt.Errorf("Failed to cast name to string %+v", name)
}
return name_str, nil
}
func GQLThreadListen(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(*GQLThread)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to GQLThread")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
listen, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
gql_thread, ok := states[0].(*GQLThreadState)
if ok == false {
return nil, fmt.Errorf("Failed to cast state to GQLThreadState")
}
return gql_thread.Listen, nil
})
if err != nil {
return nil, err
}
listen_str, ok := listen.(string)
if ok == false {
return nil, fmt.Errorf("Failed to cast listen to string %+v", listen)
}
return listen_str, nil
}
func GQLThreadParent(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(Thread)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to Thread")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
parent, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
gql_thread, ok := states[0].(ThreadState)
if ok == false {
return nil, fmt.Errorf("Failed to cast state to ThreadState")
}
return gql_thread.Parent(), nil
})
if err != nil {
return nil, err
}
parent_node, ok := parent.(Thread)
if ok == false {
return nil, fmt.Errorf("Failed to cast parent to node %+v", parent)
}
return parent_node, nil
}
func GQLThreadChildren(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(Thread)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to Thread")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
children, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
gql_thread, ok := states[0].(ThreadState)
if ok == false {
return nil, fmt.Errorf("Failed to cast state to ThreadState")
}
return gql_thread.Children(), nil
})
if err != nil {
return nil, err
}
children_nodes, ok := children.([]Thread)
if ok == false {
return nil, fmt.Errorf("Failed to cast children to threads %+v", children)
}
return children_nodes, nil
}
func GQLLockableRequirements(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(Lockable)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to Lockable")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
requirements, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
gql_thread, ok := states[0].(LockableState)
if ok == false {
return nil, fmt.Errorf("Failed to cast state to LockableState")
}
return gql_thread.Requirements(), nil
})
if err != nil {
return nil, err
}
requirement_nodes, ok := requirements.([]Lockable)
if ok == false {
return nil, fmt.Errorf("Failed to cast requirements to lockables %+v", requirements)
}
return requirement_nodes, nil
}
func GQLLockableDependencies(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(Lockable)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to Lockable")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
dependencies, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
gql_thread, ok := states[0].(LockableState)
if ok == false {
return nil, fmt.Errorf("Failed to cast state to LockableState")
}
return gql_thread.Dependencies(), nil
})
if err != nil {
return nil, err
}
dependency_nodes, ok := dependencies.([]Lockable)
if ok == false {
return nil, fmt.Errorf("Failed to cast dependencies to lockables %+v", dependencies)
}
return dependency_nodes, nil
}
func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(Lockable)
if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to Lockable")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext")
}
owner, err := UseStates(ctx, []GraphNode{node}, func(states []NodeState) (interface{}, error) {
gql_thread, ok := states[0].(LockableState)
if ok == false {
return nil, fmt.Errorf("Failed to cast state to LockableState")
}
return gql_thread.Owner(), nil
})
if err != nil {
return nil, err
}
// TODO actually cast to LockHolder and add gql interface for it
owner_node, ok := owner.(Lockable)
if ok == false {
return nil, fmt.Errorf("Failed to cast owner to Lockable %+v", owner)
}
return owner_node, nil
}
var gql_type_gql_thread *graphql.Object = nil
func GQLTypeGQLThread() * graphql.Object {
if gql_type_gql_thread == nil {
gql_type_gql_thread = graphql.NewObject(graphql.ObjectConfig{
Name: "GQLThread",
Interfaces: []*graphql.Interface{
GQLInterfaceGraphNode(),
GQLInterfaceThread(),
GQLInterfaceLockable(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*GQLThread)
return ok
},
Fields: graphql.Fields{},
})
gql_type_gql_thread.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeID,
})
gql_type_gql_thread.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeName,
})
gql_type_gql_thread.AddFieldConfig("Children", &graphql.Field{
Type: GQLListThread(),
Resolve: GQLThreadChildren,
})
gql_type_gql_thread.AddFieldConfig("Parent", &graphql.Field{
Type: GQLInterfaceThread(),
Resolve: GQLThreadParent,
})
gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{
Type: graphql.String,
Resolve: GQLThreadListen,
})
gql_type_gql_thread.AddFieldConfig("Requirements", &graphql.Field{
Type: GQLListLockable(),
Resolve: GQLLockableRequirements,
})
gql_type_gql_thread.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceLockable(),
Resolve: GQLLockableOwner,
})
gql_type_gql_thread.AddFieldConfig("Dependencies", &graphql.Field{
Type: GQLListLockable(),
Resolve: GQLLockableDependencies,
})
}
return gql_type_gql_thread
}
var gql_type_base_thread *graphql.Object = nil
func GQLTypeBaseThread() * graphql.Object {
if gql_type_base_thread == nil {
gql_type_base_thread = graphql.NewObject(graphql.ObjectConfig{
Name: "BaseThread",
Interfaces: []*graphql.Interface{
GQLInterfaceGraphNode(),
GQLInterfaceThread(),
GQLInterfaceLockable(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
valid_threads, ok := p.Context.Value("valid_threads").(map[reflect.Type]*graphql.Object)
if ok == false {
return false
}
value_type := reflect.TypeOf(p.Value)
for go_type, _ := range(valid_threads) {
if value_type == go_type {
return true
}
}
return false
},
Fields: graphql.Fields{},
})
gql_type_base_thread.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeID,
})
gql_type_base_thread.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeName,
})
gql_type_base_thread.AddFieldConfig("Children", &graphql.Field{
Type: GQLListThread(),
Resolve: GQLThreadChildren,
})
gql_type_base_thread.AddFieldConfig("Parent", &graphql.Field{
Type: GQLInterfaceThread(),
Resolve: GQLThreadParent,
})
gql_type_base_thread.AddFieldConfig("Requirements", &graphql.Field{
Type: GQLListLockable(),
Resolve: GQLLockableRequirements,
})
gql_type_base_thread.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceLockable(),
Resolve: GQLLockableOwner,
})
gql_type_base_thread.AddFieldConfig("Dependencies", &graphql.Field{
Type: GQLListLockable(),
Resolve: GQLLockableDependencies,
})
}
return gql_type_base_thread
}
var gql_type_base_lockable *graphql.Object = nil
func GQLTypeBaseLockable() * graphql.Object {
if gql_type_base_lockable == nil {
gql_type_base_lockable = graphql.NewObject(graphql.ObjectConfig{
Name: "BaseLockable",
Interfaces: []*graphql.Interface{
GQLInterfaceGraphNode(),
GQLInterfaceLockable(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
valid_lockables, ok := p.Context.Value("valid_lockables").(map[reflect.Type]*graphql.Object)
if ok == false {
return false
}
value_type := reflect.TypeOf(p.Value)
for go_type, _ := range(valid_lockables) {
if value_type == go_type {
return true
}
}
return false
},
Fields: graphql.Fields{},
})
gql_type_base_lockable.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeID,
})
gql_type_base_lockable.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeName,
})
gql_type_base_lockable.AddFieldConfig("Requirements", &graphql.Field{
Type: GQLListLockable(),
Resolve: GQLLockableRequirements,
})
gql_type_base_lockable.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceLockable(),
Resolve: GQLLockableOwner,
})
gql_type_base_lockable.AddFieldConfig("Dependencies", &graphql.Field{
Type: GQLListLockable(),
Resolve: GQLLockableDependencies,
})
}
return gql_type_base_lockable
}
var gql_type_base_node *graphql.Object = nil
func GQLTypeBaseNode() * graphql.Object {
if gql_type_base_node == nil {
gql_type_base_node = graphql.NewObject(graphql.ObjectConfig{
Name: "BaseNode",
Interfaces: []*graphql.Interface{
GQLInterfaceGraphNode(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
valid_nodes, ok := p.Context.Value("valid_nodes").(map[reflect.Type]*graphql.Object)
if ok == false {
return false
}
value_type := reflect.TypeOf(p.Value)
for go_type, _ := range(valid_nodes) {
if value_type == go_type {
return true
}
}
return false
},
Fields: graphql.Fields{},
})
gql_type_base_node.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeID,
})
gql_type_base_node.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLNodeName,
})
}
return gql_type_base_node
}
func GQLSignalFn(p graphql.ResolveParams, fn func(GraphSignal, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
if signal, ok := p.Source.(GraphSignal); ok {
return fn(signal, p)
}
return nil, fmt.Errorf("Failed to cast source to event")
}
func GQLSignalType(p graphql.ResolveParams) (interface{}, error) {
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
return signal.Type(), nil
})
}
func GQLSignalSource(p graphql.ResolveParams) (interface{}, error) {
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
return signal.Source(), nil
})
}
func GQLSignalDirection(p graphql.ResolveParams) (interface{}, error) {
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
return signal.Direction(), nil
})
}
func GQLSignalString(p graphql.ResolveParams) (interface{}, error) {
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
return signal.String(), nil
})
}
var gql_type_signal *graphql.Object = nil
func GQLTypeSignal() *graphql.Object {
if gql_type_signal == nil {
gql_type_signal = graphql.NewObject(graphql.ObjectConfig{
Name: "SignalOut",
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(GraphSignal)
return ok
},
Fields: graphql.Fields{},
})
gql_type_signal.AddFieldConfig("Type", &graphql.Field{
Type: graphql.String,
Resolve: GQLSignalType,
})
gql_type_signal.AddFieldConfig("Source", &graphql.Field{
Type: graphql.String,
Resolve: GQLSignalSource,
})
gql_type_signal.AddFieldConfig("Direction", &graphql.Field{
Type: graphql.Boolean,
Resolve: GQLSignalDirection,
})
gql_type_signal.AddFieldConfig("String", &graphql.Field{
Type: graphql.String,
Resolve: GQLSignalString,
})
}
return gql_type_signal
}
var gql_type_signal_input *graphql.InputObject = nil
func GQLTypeSignalInput() *graphql.InputObject {
if gql_type_signal_input == nil {
gql_type_signal_input = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "SignalIn",
Fields: graphql.InputObjectConfigFieldMap{},
})
gql_type_signal_input.AddFieldConfig("Type", &graphql.InputObjectFieldConfig{
Type: graphql.String,
})
gql_type_signal_input.AddFieldConfig("Description", &graphql.InputObjectFieldConfig{
Type: graphql.String,
DefaultValue: "",
})
gql_type_signal_input.AddFieldConfig("Time", &graphql.InputObjectFieldConfig{
Type: graphql.DateTime,
DefaultValue: time.Now(),
})
}
return gql_type_signal_input
}
func GQLSubscribeSignal(p graphql.ResolveParams) (interface{}, error) {
return GQLSubscribeFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error) {
return signal, nil
})
}
func GQLSubscribeFn(p graphql.ResolveParams, fn func(GraphSignal, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
server, ok := p.Context.Value("gql_server").(*GQLThread)
if ok == false {
return nil, fmt.Errorf("Failed to get gql_server from context and cast to GQLServer")
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to get graph_context from context and cast to GraphContext")
}
c := make(chan interface{})
go func(c chan interface{}, server *GQLThread) {
sig_c := server.UpdateChannel(0)
for {
val, ok := <- sig_c
if ok == false {
return
}
ret, err := fn(val, p)
if err != nil {
ctx.Log.Logf("gqlws", "type convertor error %s", err)
return
}
c <- ret
}
}(c, server)
return c, nil
}
var gql_subscription_update * graphql.Field = nil
func GQLSubscriptionUpdate() * graphql.Field {
if gql_subscription_update == nil {
gql_subscription_update = &graphql.Field{
Type: GQLTypeSignal(),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return p.Source, nil
},
Subscribe: GQLSubscribeSignal,
}
}
return gql_subscription_update
}
var gql_mutation_send_update *graphql.Field = nil
func GQLMutationSendUpdate() *graphql.Field {
if gql_mutation_send_update == nil {
gql_mutation_send_update = &graphql.Field{
Type: GQLTypeSignal(),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.String,
},
"signal": &graphql.ArgumentConfig{
Type: GQLTypeSignalInput(),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
server, ok := p.Context.Value("gql_server").(*GQLThread)
if ok == false {
return nil, fmt.Errorf("Failed to cast context gql_server to GQLServer: %+v", p.Context.Value("gql_server"))
}
ctx, ok := p.Context.Value("graph_context").(*GraphContext)
if ok == false {
return nil, fmt.Errorf("Failed to cast context graph_context to GraphContext: %+v", p.Context.Value("graph_context"))
}
signal_map, ok := p.Args["signal"].(map[string]interface{})
if ok == false {
return nil, fmt.Errorf("Failed to cast arg signal to GraphSignal: %+v", p.Args["signal"])
}
var signal GraphSignal = nil
if signal_map["Direction"] == Up {
signal = NewSignal(server, signal_map["Type"].(string))
} else if signal_map["Direction"] == Down {
signal = NewDownSignal(server, signal_map["Type"].(string))
} else if signal_map["Direction"] == Direct {
signal = NewDirectSignal(server, signal_map["Type"].(string))
} else {
return nil, fmt.Errorf("Bad direction: %d", signal_map["Direction"])
}
id , ok := p.Args["id"].(NodeID)
if ok == false {
return nil, fmt.Errorf("Failed to cast arg id to string")
}
node_if, err := UseStates(ctx, []GraphNode{server}, func(states []NodeState) (interface{}, error){
server_state := states[0].(*GQLThreadState)
node := FindChild(ctx, server, server_state, id)
if node == nil {
return nil, fmt.Errorf("Failed to find ID: %s as child of server thread", id)
}
return node, nil
})
if err != nil {
return nil, err
}
node, ok := node_if.(GraphNode)
if ok == false {
return nil, fmt.Errorf("Failed to cast found node to GraphNode")
}
SendUpdate(ctx, node, signal)
return signal, nil
},
}
}
return gql_mutation_send_update
}
var gql_query_self *graphql.Field = nil
func GQLQuerySelf() *graphql.Field {
if gql_query_self == nil {
gql_query_self = &graphql.Field{
Type: GQLTypeGQLThread(),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
server, ok := p.Context.Value("gql_server").(*GQLThread)
if ok == false {
return nil, fmt.Errorf("failed to cast gql_server to GQLThread")
}
return server, nil
},
}
}
return gql_query_self
}

@ -0,0 +1,147 @@
package graphvent
import (
"reflect"
"fmt"
"time"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)
func ResolveNodeID(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(NodeResult)
if ok == false {
return nil, fmt.Errorf("Can't get NodeID from %+v", reflect.TypeOf(p.Source))
}
return node.NodeID, nil
}
func ResolveNodeType(p graphql.ResolveParams) (interface{}, error) {
node, ok := p.Source.(NodeResult)
if ok == false {
return nil, fmt.Errorf("Can't get TypeHash from %+v", reflect.TypeOf(p.Source))
}
return uint64(node.NodeType), nil
}
type FieldIndex struct {
Extension ExtType
Tag string
}
func GetFields(selection_set *ast.SelectionSet) []string {
names := []string{}
if selection_set == nil {
return names
}
for _, sel := range(selection_set.Selections) {
switch field := sel.(type) {
case *ast.Field:
if field.Name.Value == "ID" || field.Name.Value == "Type" {
continue
}
names = append(names, field.Name.Value)
case *ast.InlineFragment:
names = append(names, GetFields(field.SelectionSet)...)
}
}
return names
}
// Returns the fields that need to be resolved
func GetResolveFields(p graphql.ResolveParams) []string {
fields := []string{}
for _, field := range(p.Info.FieldASTs) {
fields = append(fields, GetFields(field.SelectionSet)...)
}
return fields
}
func ResolveNode(id NodeID, p graphql.ResolveParams) (NodeResult, error) {
ctx, err := PrepResolve(p)
if err != nil {
return NodeResult{}, err
}
switch source := p.Source.(type) {
case *StatusSignal:
cached_node, cached := ctx.NodeCache[source.Source]
if cached {
for _, field_name := range(source.Fields) {
_, cached := cached_node.Data[field_name]
if cached {
delete(cached_node.Data, field_name)
}
}
ctx.NodeCache[source.Source] = cached_node
}
}
cache, node_cached := ctx.NodeCache[id]
fields := GetResolveFields(p)
var not_cached []string
if node_cached {
not_cached = []string{}
for _, field := range(fields) {
if node_cached {
_, field_cached := cache.Data[field]
if field_cached {
continue
}
}
not_cached = append(not_cached, field)
}
} else {
not_cached = fields
}
if (len(not_cached) == 0) && (node_cached == true) {
ctx.Context.Log.Logf("gql", "No new fields to resolve for %s", id)
return cache, nil
} else {
ctx.Context.Log.Logf("gql", "Resolving fields %+v on node %s", not_cached, id)
signal := NewReadSignal(not_cached)
response_chan := ctx.Ext.GetResponseChannel(signal.ID())
// TODO: TIMEOUT DURATION
err = ctx.Context.Send(ctx.Server, []Message{{
Node: id,
Signal: signal,
}})
if err != nil {
ctx.Ext.FreeResponseChannel(signal.ID())
return NodeResult{}, err
}
response, _, err := WaitForResponse(response_chan, 100*time.Millisecond, signal.ID())
ctx.Ext.FreeResponseChannel(signal.ID())
if err != nil {
return NodeResult{}, err
}
switch response := response.(type) {
case *ReadResultSignal:
if node_cached == false {
cache = NodeResult{
NodeID: id,
NodeType: response.NodeType,
Data: response.Fields,
}
} else {
for field_name, field_value := range(response.Fields) {
cache.Data[field_name] = field_value
}
}
ctx.NodeCache[id] = cache
return ctx.NodeCache[id], nil
default:
return NodeResult{}, fmt.Errorf("Bad read response: %+v", response)
}
}
}

@ -1,20 +1,223 @@
package graphvent package graphvent
import ( import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"reflect"
"testing" "testing"
"time"
"github.com/google/uuid"
"golang.org/x/net/websocket"
) )
func TestGQLThread(t * testing.T) { func TestGQLSubscribe(t *testing.T) {
ctx := testContext(t) ctx := logTestContext(t, []string{"test", "gql"})
gql_thread, err := NewGQLThread(ctx, ":8080", []Lockable{}, ObjTypeMap{}, FieldMap{}, FieldMap{}, FieldMap{})
n1, err := ctx.NewNode(nil, "LockableNode", NewLockableExt(nil))
fatalErr(t, err)
listener_ext := NewListenerExt(10)
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
fatalErr(t, err)
gql, err := ctx.NewNode(nil, "LockableNode", NewLockableExt([]NodeID{n1.ID}), gql_ext, listener_ext)
fatalErr(t, err)
query := "subscription { Self { ID, Type ... on Lockable { LockableState } } }"
ctx.Log.Logf("test", "GQL: %s", gql.ID)
ctx.Log.Logf("test", "Node: %s", n1.ID)
ctx.Log.Logf("test", "Query: %s", query)
sub_1 := GQLPayload{
Query: query,
}
port := gql_ext.tcp_listener.Addr().(*net.TCPAddr).Port
url := fmt.Sprintf("http://localhost:%d/gql", port)
ws_url := fmt.Sprintf("ws://127.0.0.1:%d/gqlws", port)
SubGQL := func(payload GQLPayload) {
config, err := websocket.NewConfig(ws_url, url)
fatalErr(t, err)
config.Protocol = append(config.Protocol, "graphql-ws")
config.TlsConfig = &tls.Config{InsecureSkipVerify: true}
ws, err := websocket.DialConfig(config)
fatalErr(t, err)
type payload_struct struct {
Token string `json:"token"`
}
init := struct{
ID uuid.UUID `json:"id"`
Type string `json:"type"`
}{
uuid.New(),
"connection_init",
}
ser, err := json.Marshal(&init)
fatalErr(t, err)
_, err = ws.Write(ser)
fatalErr(t, err)
resp := make([]byte, 1024)
n, err := ws.Read(resp)
var init_resp GQLWSMsg
err = json.Unmarshal(resp[:n], &init_resp)
fatalErr(t, err)
if init_resp.Type != "connection_ack" {
t.Fatal("Didn't receive connection_ack")
}
sub := GQLWSMsg{
ID: uuid.New().String(),
Type: "subscribe",
Payload: sub_1,
}
ser, err = json.Marshal(&sub)
fatalErr(t, err)
_, err = ws.Write(ser)
fatalErr(t, err)
n, err = ws.Read(resp)
fatalErr(t, err)
ctx.Log.Logf("test", "SUB1: %s", resp[:n])
lock_id, err := LockLockable(ctx, gql)
fatalErr(t, err)
response, _, err := WaitForResponse(listener_ext.Chan, 100*time.Millisecond, lock_id)
fatalErr(t, err)
switch response.(type) {
case *SuccessSignal:
ctx.Log.Logf("test", "Locked %s", gql.ID)
default:
t.Errorf("Unexpected lock response: %s", response)
}
n, err = ws.Read(resp)
fatalErr(t, err)
ctx.Log.Logf("test", "SUB2: %s", resp[:n])
n, err = ws.Read(resp)
fatalErr(t, err)
ctx.Log.Logf("test", "SUB3: %s", resp[:n])
// TODO: check that there are no more messages sent to ws within a timeout
}
SubGQL(sub_1)
}
func TestGQLQuery(t *testing.T) {
ctx := logTestContext(t, []string{"test", "lockable"})
n1_listener := NewListenerExt(10)
n1, err := ctx.NewNode(nil, "LockableNode", NewLockableExt(nil), n1_listener)
fatalErr(t, err)
gql_listener := NewListenerExt(10)
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
fatalErr(t, err)
gql, err := ctx.NewNode(nil, "LockableNode", NewLockableExt([]NodeID{n1.ID}), gql_ext, gql_listener)
fatalErr(t, err)
ctx.Log.Logf("test", "GQL: %s", gql.ID)
ctx.Log.Logf("test", "NODE: %s", n1.ID)
skipVerifyTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: skipVerifyTransport}
port := gql_ext.tcp_listener.Addr().(*net.TCPAddr).Port
url := fmt.Sprintf("http://localhost:%d/gql", port)
req_1 := GQLPayload{
Query: "query Node($id:graphvent_NodeID) { Node(id:$id) { ID, Type, ... on Lockable { LockableState } } }",
Variables: map[string]interface{}{
"id": n1.ID.String(),
},
}
req_2 := GQLPayload{
Query: "query Self { Self { ID, Type, ... on Lockable { LockableState, Requirements { Key { ID ... on Lockable { LockableState } } } } } }",
}
SendGQL := func(payload GQLPayload) []byte {
ser, err := json.MarshalIndent(&payload, "", " ")
fatalErr(t, err)
req_data := bytes.NewBuffer(ser)
req, err := http.NewRequest("GET", url, req_data)
fatalErr(t, err)
resp, err := client.Do(req)
fatalErr(t, err)
body, err := io.ReadAll(resp.Body)
fatalErr(t, err)
resp.Body.Close()
return body
}
resp_1 := SendGQL(req_1)
ctx.Log.Logf("test", "RESP_1: %s", resp_1)
resp_2 := SendGQL(req_2)
ctx.Log.Logf("test", "RESP_2: %s", resp_2)
lock_id, err := LockLockable(ctx, n1)
fatalErr(t, err)
response, _, err := WaitForResponse(n1_listener.Chan, 100*time.Millisecond, lock_id)
fatalErr(t, err)
switch response := response.(type) {
case *SuccessSignal:
default:
t.Fatalf("Wrong response: %s", reflect.TypeOf(response))
}
resp_3 := SendGQL(req_1)
ctx.Log.Logf("test", "RESP_3: %s", resp_3)
resp_4 := SendGQL(req_2)
ctx.Log.Logf("test", "RESP_4: %s", resp_4)
}
func TestGQLDB(t *testing.T) {
ctx := logTestContext(t, []string{"test", "db", "node", "serialize"})
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
fatalErr(t, err)
listener_ext := NewListenerExt(10)
gql, err := ctx.NewNode(nil, "Node", gql_ext, listener_ext)
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
test_thread, err := NewSimpleBaseThread(ctx, "Test thread 1", []Lockable{}, ThreadActions{}, ThreadHandlers{}) err = ctx.Stop()
fatalErr(t, err) fatalErr(t, err)
err = LinkThreads(ctx, gql_thread, test_thread, nil) gql_loaded, err := ctx.GetNode(gql.ID)
fatalErr(t, err) fatalErr(t, err)
err = RunThread(ctx, gql_thread) listener_ext, err = GetExt[ListenerExt](gql_loaded)
fatalErr(t, err) fatalErr(t, err)
} }

@ -1,432 +0,0 @@
package graphvent
import (
"sync"
"reflect"
"github.com/google/uuid"
"os"
"github.com/rs/zerolog"
"fmt"
badger "github.com/dgraph-io/badger/v3"
"encoding/json"
)
type GraphContext struct {
DB * badger.DB
Log Logger
}
func NewGraphContext(db * badger.DB, log Logger) * GraphContext {
return &GraphContext{DB: db, Log: log}
}
// A Logger is passed around to record events happening to components enabled by SetComponents
type Logger interface {
SetComponents(components []string) error
// Log a formatted string
Logf(component string, format string, items ... interface{})
// Log a map of attributes and a format string
Logm(component string, fields map[string]interface{}, format string, items ... interface{})
}
func NewConsoleLogger(components []string) *ConsoleLogger {
logger := &ConsoleLogger{
loggers: map[string]zerolog.Logger{},
components: []string{},
}
logger.SetComponents(components)
return logger
}
// A ConsoleLogger logs to stdout
type ConsoleLogger struct {
loggers map[string]zerolog.Logger
components_lock sync.Mutex
components []string
}
func (logger * ConsoleLogger) SetComponents(components []string) error {
logger.components_lock.Lock()
defer logger.components_lock.Unlock()
component_enabled := func (component string) bool {
for _, c := range(components) {
if c == component {
return true
}
}
return false
}
for c, _ := range(logger.loggers) {
if component_enabled(c) == false {
delete(logger.loggers, c)
}
}
for _, c := range(components) {
_, exists := logger.loggers[c]
if component_enabled(c) == true && exists == false {
logger.loggers[c] = zerolog.New(os.Stdout).With().Timestamp().Str("component", c).Logger()
}
}
return nil
}
func (logger * ConsoleLogger) Logm(component string, fields map[string]interface{}, format string, items ... interface{}) {
l, exists := logger.loggers[component]
if exists == true {
log := l.Log()
for key, value := range(fields) {
log = log.Str(key, fmt.Sprintf("%+v", value))
}
log.Msg(fmt.Sprintf(format, items...))
}
}
func (logger * ConsoleLogger) Logf(component string, format string, items ... interface{}) {
l, exists := logger.loggers[component]
if exists == true {
l.Log().Msg(fmt.Sprintf(format, items...))
}
}
type NodeID string
// Generate a random id
func RandID() NodeID {
uuid_str := uuid.New().String()
return NodeID(uuid_str)
}
type SignalDirection int
const (
Up SignalDirection = iota
Down
Direct
)
// GraphSignals are passed around the event tree/resource DAG and cast by Type()
type GraphSignal interface {
// How to propogate the signal
Direction() SignalDirection
Source() NodeID
Type() string
String() string
}
// BaseSignal is the most basic type of signal, it has no additional data
type BaseSignal struct {
direction SignalDirection
source NodeID
_type string
}
func (signal BaseSignal) Direction() SignalDirection {
return signal.direction
}
func (signal BaseSignal) Source() NodeID {
return signal.source
}
func (signal BaseSignal) Type() string {
return signal._type
}
func (signal BaseSignal) String() string {
return fmt.Sprintf("{direction: %d, source: %s, type: %s}", signal.direction, signal.source, signal._type)
}
func NewBaseSignal(source GraphNode, _type string, direction SignalDirection) BaseSignal {
var source_id NodeID = ""
if source != nil {
source_id = source.ID()
}
signal := BaseSignal{
direction: direction,
source: source_id,
_type: _type,
}
return signal
}
func NewDownSignal(source GraphNode, _type string) BaseSignal {
return NewBaseSignal(source, _type, Down)
}
func NewSignal(source GraphNode, _type string) BaseSignal {
return NewBaseSignal(source, _type, Up)
}
func NewDirectSignal(source GraphNode, _type string) BaseSignal {
return NewBaseSignal(source, _type, Direct)
}
func AbortSignal(source GraphNode) BaseSignal {
return NewBaseSignal(source, "abort", Down)
}
func CancelSignal(source GraphNode) BaseSignal {
return NewBaseSignal(source, "cancel", Down)
}
type NodeState interface {
Name() string
}
// GraphNode is the interface common to both DAG nodes and Event tree nodes
// They have a NodeState interface which is saved to the database every update
type GraphNode interface {
ID() NodeID
State() NodeState
StateLock() *sync.RWMutex
SetState(new_state NodeState)
// Signal propagation function for listener channels
UpdateListeners(ctx * GraphContext, update GraphSignal)
// Signal propagation function for connected nodes(defined in state)
PropagateUpdate(ctx * GraphContext, update GraphSignal)
// Get an update channel for the node to be notified of signals
UpdateChannel(buffer int) chan GraphSignal
// Register and unregister a channel to propogate updates to
RegisterChannel(listener chan GraphSignal)
UnregisterChannel(listener chan GraphSignal)
// Get a handle to the nodes internal signal channel
SignalChannel() chan GraphSignal
}
// Create a new base node with the given ID
func NewNode(ctx * GraphContext, id NodeID, state NodeState) BaseNode {
node := BaseNode{
id: id,
signal: make(chan GraphSignal, 512),
listeners: map[chan GraphSignal]chan GraphSignal{},
state: state,
}
err := WriteDBState(ctx, id, state)
if err != nil {
panic(fmt.Sprintf("DB_NEW_WRITE_ERROR: %s", err))
}
ctx.Log.Logf("graph", "NEW_NODE: %s - %+v", id, state)
return node
}
// BaseNode is the minimum set of fields needed to implement a GraphNode,
// and provides a template for more complicated Nodes
type BaseNode struct {
id NodeID
state NodeState
state_lock sync.RWMutex
signal chan GraphSignal
listeners_lock sync.Mutex
listeners map[chan GraphSignal]chan GraphSignal
}
func (node * BaseNode) ID() NodeID {
return node.id
}
func (node * BaseNode) State() NodeState {
return node.state
}
func (node * BaseNode) StateLock() * sync.RWMutex {
return &node.state_lock
}
func WriteDBState(ctx * GraphContext, id NodeID, state NodeState) error {
ctx.Log.Logf("db", "DB_WRITE: %s - %+v", id, state)
var serialized_state []byte = nil
if state != nil {
ser, err := json.Marshal(state)
if err != nil {
return fmt.Errorf("DB_MARSHAL_ERROR: %e", err)
}
serialized_state = ser
} else {
serialized_state = []byte{}
}
err := ctx.DB.Update(func(txn *badger.Txn) error {
err := txn.Set([]byte(id), serialized_state)
return err
})
return err
}
func (node * BaseNode) SetState(new_state NodeState) {
node.state = new_state
}
func checkForDuplicate(nodes []GraphNode) error {
found := map[NodeID]bool{}
for _, node := range(nodes) {
if node == nil {
return fmt.Errorf("Cannot get state of nil node")
}
_, exists := found[node.ID()]
if exists == true {
return fmt.Errorf("Attempted to get state of %s twice", node.ID())
}
found[node.ID()] = true
}
return nil
}
func UseStates(ctx * GraphContext, nodes []GraphNode, states_fn func(states []NodeState)(interface{}, error)) (interface{}, error) {
err := checkForDuplicate(nodes)
if err != nil {
return nil, err
}
for _, node := range(nodes) {
node.StateLock().RLock()
}
states := make([]NodeState, len(nodes))
for i, node := range(nodes) {
states[i] = node.State()
}
val, err := states_fn(states)
for _, node := range(nodes) {
node.StateLock().RUnlock()
}
return val, err
}
func UpdateStates(ctx * GraphContext, nodes []GraphNode, states_fn func(states []NodeState)([]NodeState, interface{}, error)) (interface{}, error) {
err := checkForDuplicate(nodes)
if err != nil {
return nil, err
}
for _, node := range(nodes) {
node.StateLock().Lock()
}
states := make([]NodeState, len(nodes))
for i, node := range(nodes) {
states[i] = node.State()
}
new_states, val, err := states_fn(states)
if new_states != nil {
if len(new_states) != len(nodes) {
panic(fmt.Sprintf("NODE_NEW_STATE_LEN_MISMATCH: %d/%d", len(new_states), len(nodes)))
}
for i, new_state := range(new_states) {
if new_state != nil {
old_state_type := reflect.TypeOf(states[i])
new_state_type := reflect.TypeOf(new_state)
if old_state_type != new_state_type {
panic(fmt.Sprintf("NODE_STATE_MISMATCH: old - %+v, new - %+v", old_state_type, new_state_type))
}
err := WriteDBState(ctx, nodes[i].ID(), new_state)
if err != nil {
panic(fmt.Sprintf("DB_WRITE_ERROR: %s", err))
}
nodes[i].SetState(new_state)
}
}
}
for _, node := range(nodes) {
node.StateLock().Unlock()
}
return val, err
}
func (node * BaseNode) UpdateListeners(ctx * GraphContext, update GraphSignal) {
node.listeners_lock.Lock()
defer node.listeners_lock.Unlock()
closed := []chan GraphSignal{}
for _, listener := range node.listeners {
ctx.Log.Logf("listeners", "UPDATE_LISTENER %s: %p", node.ID(), listener)
select {
case listener <- update:
default:
ctx.Log.Logf("listeners", "CLOSED_LISTENER %s: %p", node.ID(), listener)
go func(node GraphNode, listener chan GraphSignal) {
listener <- NewSignal(node, "listener_closed")
close(listener)
}(node, listener)
closed = append(closed, listener)
}
}
for _, listener := range(closed) {
delete(node.listeners, listener)
}
}
func (node * BaseNode) PropagateUpdate(ctx * GraphContext, update GraphSignal) {
}
func (node * BaseNode) RegisterChannel(listener chan GraphSignal) {
node.listeners_lock.Lock()
_, exists := node.listeners[listener]
if exists == false {
node.listeners[listener] = listener
}
node.listeners_lock.Unlock()
}
func (node * BaseNode) UnregisterChannel(listener chan GraphSignal) {
node.listeners_lock.Lock()
_, exists := node.listeners[listener]
if exists == false {
panic("Attempting to unregister non-registered listener")
} else {
delete(node.listeners, listener)
}
node.listeners_lock.Unlock()
}
func (node * BaseNode) SignalChannel() chan GraphSignal {
return node.signal
}
// Create a new GraphSinal channel with a buffer of size buffer and register it to a node
func (node * BaseNode) UpdateChannel(buffer int) chan GraphSignal {
new_listener := make(chan GraphSignal, buffer)
node.RegisterChannel(new_listener)
return new_listener
}
// Propogate a signal starting at a node
func SendUpdate(ctx * GraphContext, node GraphNode, signal GraphSignal) {
if node == nil {
panic("Cannot start an update from no node")
}
ctx.Log.Logf("update", "UPDATE %s <- %s: %+v", node.ID(), signal.Source(), signal)
node.UpdateListeners(ctx, signal)
node.PropagateUpdate(ctx, signal)
}

@ -2,76 +2,52 @@ package graphvent
import ( import (
"testing" "testing"
"fmt" "runtime/debug"
"time" "time"
"runtime/pprof"
"os"
badger "github.com/dgraph-io/badger/v3" badger "github.com/dgraph-io/badger/v3"
) )
type GraphTester testing.T func NewSimpleListener(ctx *Context, buffer int) (*Node, *ListenerExt, error) {
const listner_timeout = 50 * time.Millisecond listener_extension := NewListenerExt(buffer)
listener, err := ctx.NewNode(nil, "LockableNode", nil, listener_extension, NewLockableExt(nil))
func (t * GraphTester) WaitForValue(ctx * GraphContext, listener chan GraphSignal, signal_type string, source GraphNode, timeout time.Duration, str string) GraphSignal { return listener, listener_extension, err
timeout_channel := time.After(timeout)
for true {
select {
case signal := <- listener:
if signal == nil {
ctx.Log.Logf("test", "SIGNAL_CHANNEL_CLOSED: %s", listener)
t.Fatal(str)
}
if signal.Type() == signal_type {
ctx.Log.Logf("test", "SIGNAL_TYPE_FOUND: %s - %s %+v\n", signal.Type(), signal.Source(), listener)
if source == nil {
if signal.Source() == "" {
return signal
}
} else {
if signal.Source() == source.ID() {
return signal
}
}
}
case <-timeout_channel:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
t.Fatal(str)
return nil
}
}
return nil
}
func (t * GraphTester) CheckForNone(listener chan GraphSignal, str string) {
timeout := time.After(listner_timeout)
select {
case sig := <- listener:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
t.Fatal(fmt.Sprintf("%s : %+v", str, sig))
case <-timeout:
}
} }
func logTestContext(t * testing.T, components []string) * GraphContext { func logTestContext(t * testing.T, components []string) *Context {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true)) db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true).WithSyncWrites(true))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
return NewGraphContext(db, NewConsoleLogger(components)) ctx, err := NewContext(&BadgerDB{
} DB: db,
}, NewConsoleLogger(components))
fatalErr(t, err)
func testContext(t * testing.T) * GraphContext { return ctx
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true)) }
if err != nil {
t.Fatal(err)
}
return NewGraphContext(db, NewConsoleLogger([]string{})) func testContext(t * testing.T) * Context {
return logTestContext(t, []string{})
} }
func fatalErr(t * testing.T, err error) { func fatalErr(t * testing.T, err error) {
if err != nil { if err != nil {
debug.PrintStack()
t.Fatal(err) t.Fatal(err)
} }
} }
func testSend(t *testing.T, ctx *Context, signal Signal, source, destination *Node) (ResponseSignal, []Signal) {
source_listener, err := GetExt[ListenerExt](source)
fatalErr(t, err)
messages := []Message{{destination.ID, signal}}
fatalErr(t, ctx.Send(source, messages))
response, signals, err := WaitForResponse(source_listener.Chan, time.Millisecond*10, signal.ID())
fatalErr(t, err)
return response, signals
}

@ -0,0 +1,66 @@
package graphvent
import (
"reflect"
)
// A Listener extension provides a channel that can receive signals on a different thread
type ListenerExt struct {
Buffer int `gv:"buffer"`
Chan chan Signal
}
type LoadedSignal struct {
SignalHeader
}
func NewLoadedSignal() *LoadedSignal {
return &LoadedSignal{
SignalHeader: NewSignalHeader(),
}
}
type UnloadedSignal struct {
SignalHeader
}
func NewUnloadedSignal() *UnloadedSignal {
return &UnloadedSignal{
SignalHeader: NewSignalHeader(),
}
}
func (ext *ListenerExt) Load(ctx *Context, node *Node) error {
ext.Chan = make(chan Signal, ext.Buffer)
ext.Chan <- NewLoadedSignal()
return nil
}
func (ext *ListenerExt) Unload(ctx *Context, node *Node) {
ext.Chan <- NewUnloadedSignal()
close(ext.Chan)
}
// Create a new listener extension with a given buffer size
func NewListenerExt(buffer int) *ListenerExt {
return &ListenerExt{
Buffer: buffer,
Chan: make(chan Signal, buffer),
}
}
// Send the signal to the channel, logging an overflow if it occurs
func (ext *ListenerExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]Message, Changes) {
ctx.Log.Logf("listener", "%s - %+v", node.ID, reflect.TypeOf(signal))
ctx.Log.Logf("listener_debug", "%s->%s - %+v", source, node.ID, signal)
select {
case ext.Chan <- signal:
default:
ctx.Log.Logf("listener", "LISTENER_OVERFLOW: %s", node.ID)
}
switch sig := signal.(type) {
case *StatusSignal:
ctx.Log.Logf("listener_status", "%s - %+v", sig.Source, sig.Fields)
}
return nil, nil
}

@ -1,444 +1,411 @@
package graphvent package graphvent
import ( import (
"fmt" "github.com/google/uuid"
"encoding/json"
) )
// LockHolderState is the interface that any node that wants to posses locks must implement type ReqState byte
// const (
// ReturnLock returns the node that held the lockable pointed to by ID before this node and Unlocked = ReqState(0)
// removes the mapping from it's state, or nil if the lockable was unlocked previously Unlocking = ReqState(1)
// Locked = ReqState(2)
// AllowedToTakeLock returns true if the node pointed to by ID is allowed to take a lock from this node Locking = ReqState(3)
// AbortingLock = ReqState(4)
// RecordLockHolder records that lockable_id needs to be passed back to lock_holder )
type LockHolderState interface {
NodeState
ReturnLock(lockable_id NodeID) GraphNode
AllowedToTakeLock(node_id NodeID, lockable_id NodeID) bool
RecordLockHolder(lockable_id NodeID, lock_holder GraphNode)
}
// LockableState is the interface that a lockables state must have to allow it to connect to the DAG
type LockableState interface {
LockHolderState
Requirements() []Lockable
AddRequirement(requirement Lockable)
Dependencies() []Lockable
AddDependency(dependency Lockable)
Owner() GraphNode
SetOwner(owner GraphNode)
}
type BaseLockHolderState struct {
delegation_map map[NodeID] GraphNode
}
type BaseLockHolderStateJSON struct { var ReqStateStrings = map[ReqState]string {
Delegations map[NodeID]*NodeID `json:"delegations"` Unlocked: "Unlocked",
Unlocking: "Unlocking",
Locked: "Locked",
Locking: "Locking",
AbortingLock: "AbortingLock",
} }
func (state * BaseLockHolderState) MarshalJSON() ([]byte, error) { func (state ReqState) String() string {
delegations := map[NodeID]*NodeID{} str, mapped := ReqStateStrings[state]
for lockable_id, node := range(state.delegation_map) { if mapped == false {
if node == nil { return "UNKNOWN_REQSTATE"
delegations[lockable_id] = nil
} else { } else {
str := node.ID() return str
delegations[lockable_id] = &str
}
} }
return json.Marshal(&BaseLockHolderStateJSON{
Delegations: delegations,
})
} }
// BaseLockableStates are a minimum collection of variables for a basic implementation of a LockHolder
// Include in any state structs that should be lockable
type BaseLockableState struct {
BaseLockHolderState
name string
owner GraphNode
requirements []Lockable
dependencies []Lockable
}
type BaseLockableStateJSON struct { type LockableExt struct{
Name string `json:"name"` State ReqState `gv:"state"`
Owner *NodeID `json:"owner"` ReqID *uuid.UUID `gv:"req_id"`
Dependencies []NodeID `json:"dependencies"` Owner *NodeID `gv:"owner"`
Requirements []NodeID `json:"requirements"` PendingOwner *NodeID `gv:"pending_owner"`
HolderState *BaseLockHolderState `json:"holder_state"` Requirements map[NodeID]ReqState `gv:"requirements" node:"Lockable:"`
Locked map[NodeID]any
Unlocked map[NodeID]any
Waiting WaitMap `gv:"waiting_locks" node:":Lockable"`
} }
func (state * BaseLockableState) MarshalJSON() ([]byte, error) { func NewLockableExt(requirements []NodeID) *LockableExt {
requirement_ids := make([]NodeID, len(state.requirements)) var reqs map[NodeID]ReqState = nil
for i, requirement := range(state.requirements) { var unlocked map[NodeID]any = map[NodeID]any{}
requirement_ids[i] = requirement.ID()
}
dependency_ids := make([]NodeID, len(state.dependencies)) if len(requirements) != 0 {
for i, dependency := range(state.dependencies) { reqs = map[NodeID]ReqState{}
dependency_ids[i] = dependency.ID() for _, req := range(requirements) {
reqs[req] = Unlocked
unlocked[req] = nil
}
} }
var owner_id *NodeID = nil return &LockableExt{
if state.owner != nil { State: Unlocked,
new_str := state.owner.ID() Owner: nil,
owner_id = &new_str PendingOwner: nil,
Requirements: reqs,
Waiting: WaitMap{},
Locked: map[NodeID]any{},
Unlocked: unlocked,
} }
}
return json.Marshal(&BaseLockableStateJSON{ func UnlockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
Name: state.name, signal := NewUnlockSignal()
Owner: owner_id, messages := []Message{{node.ID, signal}}
Dependencies: dependency_ids, return signal.ID(), ctx.Send(node, messages)
Requirements: requirement_ids,
HolderState: &state.BaseLockHolderState,
})
} }
func (state * BaseLockableState) Name() string { func LockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
return state.name signal := NewLockSignal()
messages := []Message{{node.ID, signal}}
return signal.ID(), ctx.Send(node, messages)
} }
// Locks cannot be passed between base lockables, so the answer to func (ext *LockableExt) Load(ctx *Context, node *Node) error {
// "who used to own this lock held by a base lockable" is always "nobody" ext.Locked = map[NodeID]any{}
func (state * BaseLockHolderState) ReturnLock(lockable_id NodeID) GraphNode { ext.Unlocked = map[NodeID]any{}
node, exists := state.delegation_map[lockable_id]
if exists == false { for id, state := range(ext.Requirements) {
panic("Attempted to take a get the original lock holder of a lockable we don't own") if state == Unlocked {
ext.Unlocked[id] = nil
} else if state == Locked {
ext.Locked[id] = nil
} }
delete(state.delegation_map, lockable_id) }
return node return nil
} }
// Nothing can take a lock from a base lockable either func (ext *LockableExt) Unload(ctx *Context, node *Node) {
func (state * BaseLockHolderState) AllowedToTakeLock(node_id NodeID, lockable_id NodeID) bool { return
_, exists := state.delegation_map[lockable_id]
if exists == false {
panic ("Trying to give away lock we don't own")
}
return false
} }
func (state * BaseLockHolderState) RecordLockHolder(lockable_id NodeID, lock_holder GraphNode) { // Handle link signal by adding/removing the requested NodeID
_, exists := state.delegation_map[lockable_id] // returns an error if the node is not unlocked
func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID, signal *LinkSignal) ([]Message, Changes) {
var messages []Message = nil
var changes Changes = nil
switch ext.State {
case Unlocked:
switch signal.Action {
case "add":
_, exists := ext.Requirements[signal.NodeID]
if exists == true { if exists == true {
panic("Attempted to lock a lockable we're already holding(lock cycle)") messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_requirement")})
} else {
if ext.Requirements == nil {
ext.Requirements = map[NodeID]ReqState{}
}
ext.Requirements[signal.NodeID] = Unlocked
changes = append(changes, "requirements")
messages = append(messages, Message{source, NewSuccessSignal(signal.ID())})
}
case "remove":
_, exists := ext.Requirements[signal.NodeID]
if exists == false {
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
} else {
delete(ext.Requirements, signal.NodeID)
changes = append(changes, "requirements")
messages = append(messages, Message{source, NewSuccessSignal(signal.ID())})
}
default:
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "unknown_action")})
}
default:
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_unlocked: %s", ext.State)})
} }
state.delegation_map[lockable_id] = lock_holder return messages, changes
} }
func (state * BaseLockableState) Owner() GraphNode { // Handle an UnlockSignal by either transitioning to Unlocked state,
return state.owner // sending unlock signals to requirements, or returning an error signal
} func (ext *LockableExt) HandleUnlockSignal(ctx *Context, node *Node, source NodeID, signal *UnlockSignal) ([]Message, Changes) {
var messages []Message = nil
var changes Changes = nil
func (state * BaseLockableState) SetOwner(owner GraphNode) { switch ext.State {
state.owner = owner case Locked:
} if source != *ext.Owner {
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_owner")})
} else {
if len(ext.Requirements) == 0 {
changes = append(changes, "state", "owner", "pending_owner")
func (state * BaseLockableState) Requirements() []Lockable { ext.Owner = nil
return state.requirements
}
func (state * BaseLockableState) AddRequirement(requirement Lockable) { ext.PendingOwner = nil
if requirement == nil {
panic("Will not connect nil to the DAG")
}
state.requirements = append(state.requirements, requirement)
}
func (state * BaseLockableState) Dependencies() []Lockable { ext.State = Unlocked
return state.dependencies
}
func (state * BaseLockableState) AddDependency(dependency Lockable) { messages = append(messages, Message{source, NewSuccessSignal(signal.Id)})
if dependency == nil { } else {
panic("Will not connect nil to the DAG") changes = append(changes, "state", "waiting", "requirements", "pending_owner")
}
state.dependencies = append(state.dependencies, dependency) ext.PendingOwner = nil
}
func NewLockHolderState() BaseLockHolderState { ext.ReqID = &signal.Id
return BaseLockHolderState{
delegation_map: map[NodeID]GraphNode{},
}
}
func LinkLockables(ctx * GraphContext, lockable Lockable, requirements []Lockable) error { ext.State = Unlocking
if lockable == nil { for id := range(ext.Requirements) {
return fmt.Errorf("LOCKABLE_LINK_ERR: Will not link Lockables to nil as requirements") unlock_signal := NewUnlockSignal()
}
for _, requirement := range(requirements) { ext.Waiting[unlock_signal.Id] = id
if requirement == nil { ext.Requirements[id] = Unlocking
return fmt.Errorf("LOCKABLE_LINK_ERR: Will not link nil to a Lockable as a requirement")
}
if lockable.ID() == requirement.ID() { messages = append(messages, Message{id, unlock_signal})
return fmt.Errorf("LOCKABLE_LINK_ERR: cannot link %s to itself", lockable.ID())
} }
} }
nodes := make([]GraphNode, len(requirements) + 1)
nodes[0] = lockable
for i, node := range(requirements) {
nodes[i+1] = node
} }
_, err := UpdateStates(ctx, nodes, func(states []NodeState) ([]NodeState, interface{}, error) { default:
// Check that all the requirements can be added messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_locked")})
lockable_state := states[0].(LockableState) }
for i, requirement := range(requirements) {
requirement_state := states[i+1].(LockableState)
return messages, changes
}
if checkIfRequirement(ctx, lockable.ID(), requirement_state, requirement.ID()) == true { // Handle a LockSignal by either transitioning to a locked state,
return nil, nil, fmt.Errorf("LOCKABLE_LINK_ERR: %s is a dependency of %s so cannot link as requirement", requirement.ID(), lockable.ID()) // sending lock signals to requirements, or returning an error signal
} func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID, signal *LockSignal) ([]Message, Changes) {
var messages []Message = nil
var changes Changes = nil
if checkIfRequirement(ctx, requirement.ID(), lockable_state, lockable.ID()) == true { switch ext.State {
return nil, nil, fmt.Errorf("LOCKABLE_LINK_ERR: %s is a dependency of %s so cannot link as dependency again", lockable.ID(), requirement.ID()) case Unlocked:
} if len(ext.Requirements) == 0 {
} changes = append(changes, "state", "owner", "pending_owner")
// Update the states of the requirements
for i, requirement := range(requirements) {
requirement_state := states[i+1].(LockableState)
requirement_state.AddDependency(lockable)
lockable_state.AddRequirement(requirement)
}
// Return no error ext.Owner = &source
return states, nil, nil
})
return err ext.PendingOwner = &source
}
func NewBaseLockableState(name string) BaseLockableState { ext.State = Locked
state := BaseLockableState{ messages = append(messages, Message{source, NewSuccessSignal(signal.Id)})
BaseLockHolderState: NewLockHolderState(), } else {
name: name, changes = append(changes, "state", "requirements", "waiting", "pending_owner")
owner: nil,
requirements: []Lockable{},
dependencies: []Lockable{},
}
return state ext.PendingOwner = &source
}
type Lockable interface { ext.ReqID = &signal.Id
GraphNode
// Called when locking the node to allow for custom lock behaviour
Lock(node GraphNode, state LockableState) error
// Called when unlocking the node to allow for custom lock behaviour
Unlock(node GraphNode, state LockableState) error
}
func (lockable * BaseLockable) PropagateUpdate(ctx * GraphContext, signal GraphSignal) { ext.State = Locking
UseStates(ctx, []GraphNode{lockable}, func(states []NodeState) (interface{}, error){ for id := range(ext.Requirements) {
lockable_state := states[0].(LockableState) lock_signal := NewLockSignal()
if signal.Direction() == Up {
// Child->Parent, lockable updates dependency lockables ext.Waiting[lock_signal.Id] = id
owner_sent := false ext.Requirements[id] = Locking
for _, dependency := range lockable_state.Dependencies() {
SendUpdate(ctx, dependency, signal) messages = append(messages, Message{id, lock_signal})
if lockable_state.Owner() != nil {
if dependency.ID() != lockable_state.Owner().ID() {
owner_sent = true
}
}
} }
if lockable_state.Owner() != nil && owner_sent == false {
SendUpdate(ctx, lockable_state.Owner(), signal)
} }
} else if signal.Direction() == Down { default:
// Parent->Child, lockable updates lock holder messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_unlocked: %s", ext.State)})
for _, requirement := range(lockable_state.Requirements()) {
SendUpdate(ctx, requirement, signal)
} }
} else if signal.Direction() == Direct { return messages, changes
} else {
panic(fmt.Sprintf("Invalid signal direction: %d", signal.Direction()))
}
return nil, nil
})
} }
func checkIfRequirement(ctx * GraphContext, r_id NodeID, cur LockableState, cur_id NodeID) bool { // Handle an error signal by aborting the lock, or retrying the unlock
for _, c := range(cur.Requirements()) { func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeID, signal *ErrorSignal) ([]Message, Changes) {
if c.ID() == r_id { var messages []Message = nil
return true var changes Changes = nil
}
val, _ := UseStates(ctx, []GraphNode{c}, func(states []NodeState) (interface{}, error) {
requirement_state := states[0].(LockableState)
return checkIfRequirement(ctx, cur_id, requirement_state, c.ID()), nil
})
is_requirement := val.(bool) id, waiting := ext.Waiting[signal.ReqID]
if is_requirement { if waiting == true {
return true delete(ext.Waiting, signal.ReqID)
} changes = append(changes, "waiting")
}
return false switch ext.State {
} case Locking:
changes = append(changes, "state", "requirements")
func UnlockLockable(ctx * GraphContext, lockable Lockable, node GraphNode, node_state LockHolderState) error { ext.Requirements[id] = Unlocked
if node == nil || lockable == nil{
panic("Cannot unlock without a specified node and lockable") unlocked := 0
} for req_id, req_state := range(ext.Requirements) {
_, err := UpdateStates(ctx, []GraphNode{lockable}, func(states []NodeState) ([]NodeState, interface{}, error) { // Unlock locked requirements, and count unlocked requirements
if lockable.ID() == node.ID() { switch req_state {
if node_state != nil { case Locked:
panic("node_state must be nil if unlocking lockable from itself") unlock_signal := NewUnlockSignal()
ext.Waiting[unlock_signal.Id] = req_id
ext.Requirements[req_id] = Unlocking
messages = append(messages, Message{req_id, unlock_signal})
case Unlocked:
unlocked += 1
} }
node_state = states[0].(LockHolderState)
} }
lockable_state := states[0].(LockableState)
if lockable_state.Owner() == nil { if unlocked == len(ext.Requirements) {
return nil, nil, fmt.Errorf("Lockable already unlocked") changes = append(changes, "owner", "state")
ext.State = Unlocked
ext.Owner = nil
} else {
changes = append(changes, "state")
ext.State = AbortingLock
} }
if lockable_state.Owner().ID() != node.ID() { case Unlocking:
return nil, nil, fmt.Errorf("Lockable %s not locked by %s", lockable.ID(), node.ID()) unlock_signal := NewUnlockSignal()
} ext.Waiting[unlock_signal.Id] = id
messages = append(messages, Message{id, unlock_signal})
var lock_err error = nil case AbortingLock:
for _, requirement := range(lockable_state.Requirements()) { req_state := ext.Requirements[id]
var err error = nil // Mark failed lock as Unlocked, or retry unlock
err = UnlockLockable(ctx, requirement, node, node_state) switch req_state {
if err != nil { case Locking:
lock_err = err ext.Requirements[id] = Unlocked
break
// Check if all requirements unlocked now
unlocked := 0
for _, req_state := range(ext.Requirements) {
if req_state == Unlocked {
unlocked += 1
} }
} }
if lock_err != nil { if unlocked == len(ext.Requirements) {
return nil, nil, fmt.Errorf("Lockable %s failed to unlock: %e", lockable.ID(), lock_err) changes = append(changes, "owner", "state")
ext.State = Unlocked
ext.Owner = nil
}
case Unlocking:
// Handle error for unlocking requirement while unlocking by retrying unlock
unlock_signal := NewUnlockSignal()
ext.Waiting[unlock_signal.Id] = id
messages = append(messages, Message{id, unlock_signal})
} }
new_owner := node_state.ReturnLock(lockable.ID())
lockable_state.SetOwner(new_owner)
err := lockable.Unlock(node, lockable_state)
if err != nil {
return nil, nil, fmt.Errorf("Lockable %s failed custom Unlock: %e", lockable.ID(), err)
} }
if lockable_state.Owner() == nil {
ctx.Log.Logf("lockable", "LOCKABLE_UNLOCK: %s unlocked %s", node.ID(), lockable.ID())
} else {
ctx.Log.Logf("lockable", "LOCKABLE_UNLOCK: %s passed lock of %s back to %s", node.ID(), lockable.ID(), lockable_state.Owner().ID())
} }
return []NodeState{lockable_state}, nil, nil return messages, changes
})
return err
} }
func LockLockable(ctx * GraphContext, lockable Lockable, node GraphNode, node_state LockHolderState) error { // Handle a success signal by checking if all requirements have been locked/unlocked
if node == nil || lockable == nil { func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source NodeID, signal *SuccessSignal) ([]Message, Changes) {
return fmt.Errorf("Cannot lock without a specified node and lockable") var messages []Message = nil
} var changes Changes = nil
ctx.Log.Logf("lockable", "LOCKING: %s from %s", lockable.ID(), node.ID())
_, err := UpdateStates(ctx, []GraphNode{lockable}, func(states []NodeState) ([]NodeState, interface{}, error) { id, waiting := ext.Waiting[signal.ReqID]
if lockable.ID() == node.ID() { if waiting == true {
if node_state != nil { delete(ext.Waiting, signal.ReqID)
return nil, nil, fmt.Errorf("node_state must be nil if locking lockable from itself") changes = append(changes, "waiting")
}
node_state = states[0].(LockHolderState)
}
lockable_state := states[0].(LockableState)
if lockable_state.Owner() != nil {
var lock_pass_allowed bool = false
if lockable_state.Owner().ID() == lockable.ID() { switch ext.State {
lock_pass_allowed = lockable_state.AllowedToTakeLock(node.ID(), lockable.ID()) case Locking:
} else { ext.Requirements[id] = Locked
tmp, _ := UseStates(ctx, []GraphNode{lockable_state.Owner()}, func(states []NodeState)(interface{}, error){ ext.Locked[id] = nil
return states[0].(LockHolderState).AllowedToTakeLock(node.ID(), lockable.ID()), nil delete(ext.Unlocked, id)
})
lock_pass_allowed = tmp.(bool)
}
if len(ext.Locked) == len(ext.Requirements) {
ctx.Log.Logf("lockable", "%s FULL_LOCK: %d", node.ID, len(ext.Locked))
changes = append(changes, "state", "owner", "req_id")
ext.State = Locked
if lock_pass_allowed == false { ext.Owner = ext.PendingOwner
return nil, nil, fmt.Errorf("%s is not allowed to take a lock from %s", node.ID(), lockable_state.Owner().ID())
}
}
err := lockable.Lock(node, lockable_state) messages = append(messages, Message{*ext.Owner, NewSuccessSignal(*ext.ReqID)})
if err != nil { ext.ReqID = nil
return nil, nil, fmt.Errorf("Failed to lock lockable: %e", err) } else {
ctx.Log.Logf("lockable", "%s PARTIAL_LOCK: %d/%d", node.ID, len(ext.Locked), len(ext.Requirements))
} }
case AbortingLock:
req_state := ext.Requirements[id]
switch req_state {
case Locking:
ext.Requirements[id] = Unlocking
unlock_signal := NewUnlockSignal()
ext.Waiting[unlock_signal.Id] = id
messages = append(messages, Message{id, unlock_signal})
case Unlocking:
ext.Requirements[id] = Unlocked
ext.Unlocked[id] = nil
delete(ext.Locked, id)
var lock_err error = nil unlocked := 0
locked_requirements := []Lockable{} for _, req_state := range(ext.Requirements) {
for _, requirement := range(lockable_state.Requirements()) { switch req_state {
err = LockLockable(ctx, requirement, node, node_state) case Unlocked:
if err != nil { unlocked += 1
lock_err = err
break
} }
locked_requirements = append(locked_requirements, requirement)
} }
if lock_err != nil { if unlocked == len(ext.Requirements) {
for _, locked_lockable := range(locked_requirements) { changes = append(changes, "state", "pending_owner", "req_id")
err = UnlockLockable(ctx, locked_lockable, node, node_state)
if err != nil { messages = append(messages, Message{*ext.PendingOwner, NewErrorSignal(*ext.ReqID, "not_unlocked: %s", ext.State)})
panic(err) ext.State = Unlocked
} ext.ReqID = nil
ext.PendingOwner = nil
} }
return nil, nil, fmt.Errorf("Lockable failed to lock: %e", lock_err)
} }
old_owner := lockable_state.Owner()
lockable_state.SetOwner(node)
node_state.RecordLockHolder(lockable.ID(), old_owner)
if old_owner == nil {
ctx.Log.Logf("lockable", "LOCKABLE_LOCK: %s locked %s", node.ID(), lockable.ID())
} else {
ctx.Log.Logf("lockable", "LOCKABLE_LOCK: %s took lock of %s from %s", node.ID(), lockable.ID(), old_owner.ID())
}
return []NodeState{lockable_state}, nil, nil case Unlocking:
}) ext.Requirements[id] = Unlocked
ext.Unlocked[id] = Unlocked
delete(ext.Locked, id)
return err if len(ext.Unlocked) == len(ext.Requirements) {
} changes = append(changes, "state", "owner", "req_id")
// BaseLockables represent simple lockables in the DAG that can be used to create a hierarchy of locks that store names messages = append(messages, Message{*ext.Owner, NewSuccessSignal(*ext.ReqID)})
type BaseLockable struct { ext.State = Unlocked
BaseNode ext.ReqID = nil
} ext.Owner = nil
}
}
}
//BaseLockables don't check anything special when locking/unlocking return messages, changes
func (lockable * BaseLockable) Lock(node GraphNode, state LockableState) error {
return nil
} }
func (lockable * BaseLockable) Unlock(node GraphNode, state LockableState) error { func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]Message, Changes) {
return nil var messages []Message = nil
} var changes Changes = nil
func NewLockable(ctx * GraphContext, name string, requirements []Lockable) (* BaseLockable, error) { switch sig := signal.(type) {
state := NewBaseLockableState(name) case *StatusSignal:
lockable := &BaseLockable{ // Forward StatusSignals up to the owner(unless that would be a cycle)
BaseNode: NewNode(ctx, RandID(), &state), if ext.Owner != nil {
if *ext.Owner != node.ID {
messages = append(messages, Message{*ext.Owner, signal})
} }
}
err := LinkLockables(ctx, lockable, requirements) case *LinkSignal:
if err != nil { messages, changes = ext.HandleLinkSignal(ctx, node, source, sig)
return nil, err case *LockSignal:
messages, changes = ext.HandleLockSignal(ctx, node, source, sig)
case *UnlockSignal:
messages, changes = ext.HandleUnlockSignal(ctx, node, source, sig)
case *ErrorSignal:
messages, changes = ext.HandleErrorSignal(ctx, node, source, sig)
case *SuccessSignal:
messages, changes = ext.HandleSuccessSignal(ctx, node, source, sig)
} }
return lockable, nil return messages, changes
} }

@ -2,309 +2,147 @@ package graphvent
import ( import (
"testing" "testing"
"fmt"
"encoding/json"
"time" "time"
) )
func TestNewLockable(t * testing.T) { func TestLink(t *testing.T) {
ctx := testContext(t) ctx := logTestContext(t, []string{"lockable", "listener"})
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err)
_, err = NewLockable(ctx, "Test lockable 2", []Lockable{r1}) l2_listener := NewListenerExt(10)
l2, err := ctx.NewNode(nil, "LockableNode", l2_listener, NewLockableExt(nil))
fatalErr(t, err) fatalErr(t, err)
}
func TestRepeatedChildLockable(t * testing.T) {
ctx := testContext(t)
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{}) l1_lockable := NewLockableExt(nil)
l1_listener := NewListenerExt(10)
l1, err := ctx.NewNode(nil, "LockableNode", l1_listener, l1_lockable)
fatalErr(t, err) fatalErr(t, err)
_, err = NewLockable(ctx, "Test lockable 2", []Lockable{r1, r1}) link_signal := NewLinkSignal("add", l2.ID)
if err == nil { msgs := []Message{{l1.ID, link_signal}}
t.Fatal("Added the same lockable as a child twice to the same lockable") err = ctx.Send(l1, msgs)
}
}
func TestLockableSelfLock(t * testing.T) {
ctx := testContext(t)
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
err = LockLockable(ctx, r1, r1, nil) _, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, link_signal.ID())
fatalErr(t, err) fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) { state, exists := l1_lockable.Requirements[l2.ID]
owner_id := states[0].(LockableState).Owner().ID() if exists == false {
if owner_id != r1.ID() { t.Fatal("l2 not in l1 requirements")
return nil, fmt.Errorf("r1 is owned by %s instead of self", owner_id) } else if state != Unlocked {
t.Fatalf("l2 in bad requirement state in l1: %+v", state)
} }
return nil, nil
})
fatalErr(t, err)
err = UnlockLockable(ctx, r1, r1, nil) unlink_signal := NewLinkSignal("remove", l2.ID)
msgs = []Message{{l1.ID, unlink_signal}}
err = ctx.Send(l1, msgs)
fatalErr(t, err) fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) { _, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, unlink_signal.ID())
owner := states[0].(LockableState).Owner()
if owner != nil {
return nil, fmt.Errorf("r1 is not unowned after unlock: %s", owner.ID())
}
return nil, nil
})
fatalErr(t, err) fatalErr(t, err)
} }
func TestLockableSelfLockTiered(t * testing.T) { func Test10Lock(t *testing.T) {
ctx := testContext(t) testLockN(t, 10)
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err)
r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{})
fatalErr(t, err)
r3, err := NewLockable(ctx, "Test lockable 3", []Lockable{r1, r2})
fatalErr(t, err)
err = LockLockable(ctx, r3, r3, nil)
fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1, r2, r3}, func(states []NodeState) (interface{}, error) {
owner_1_id := states[0].(LockableState).Owner().ID()
if owner_1_id != r3.ID() {
return nil, fmt.Errorf("r1 is owned by %s instead of r3", owner_1_id)
}
owner_2_id := states[1].(LockableState).Owner().ID()
if owner_2_id != r3.ID() {
return nil, fmt.Errorf("r2 is owned by %s instead of r3", owner_2_id)
}
ser, _ := json.MarshalIndent(states, "", " ")
fmt.Printf("\n\n%s\n\n", ser)
return nil, nil
})
fatalErr(t, err)
err = UnlockLockable(ctx, r3, r3, nil)
fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1, r2, r3}, func(states []NodeState) (interface{}, error) {
owner_1 := states[0].(LockableState).Owner()
if owner_1 != nil {
return nil, fmt.Errorf("r1 is not unowned after unlocking: %s", owner_1.ID())
}
owner_2 := states[1].(LockableState).Owner()
if owner_2 != nil {
return nil, fmt.Errorf("r2 is not unowned after unlocking: %s", owner_2.ID())
}
owner_3 := states[2].(LockableState).Owner()
if owner_3 != nil {
return nil, fmt.Errorf("r3 is not unowned after unlocking: %s", owner_3.ID())
}
return nil, nil
})
fatalErr(t, err)
} }
func TestLockableLockOther(t * testing.T) { func Test100Lock(t *testing.T) {
ctx := testContext(t) testLockN(t, 100)
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err)
r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{})
fatalErr(t, err)
_, err = UpdateStates(ctx, []GraphNode{r2}, func(states []NodeState) ([]NodeState, interface{}, error) {
node_state := states[0].(LockHolderState)
err := LockLockable(ctx, r1, r2, node_state)
fatalErr(t, err)
return []NodeState{node_state}, nil, nil
})
fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) {
owner_id := states[0].(LockableState).Owner().ID()
if owner_id != r2.ID() {
return nil, fmt.Errorf("r1 is owned by %s instead of r2", owner_id)
}
return nil, nil
})
fatalErr(t, err)
_, err = UpdateStates(ctx, []GraphNode{r2}, func(states []NodeState) ([]NodeState, interface{}, error) {
node_state := states[0].(LockHolderState)
err := UnlockLockable(ctx, r1, r2, node_state)
fatalErr(t, err)
return []NodeState{node_state}, nil, nil
})
fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) {
owner := states[0].(LockableState).Owner()
if owner != nil {
return nil, fmt.Errorf("r1 is owned by %s instead of r2", owner.ID())
}
return nil, nil
})
fatalErr(t, err)
} }
func TestLockableLockSimpleConflict(t * testing.T) { func Test1000Lock(t *testing.T) {
ctx := testContext(t) testLockN(t, 1000)
}
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{}) func Test10000Lock(t *testing.T) {
fatalErr(t, err) testLockN(t, 10000)
}
r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{}) func testLockN(t *testing.T, n int) {
fatalErr(t, err) ctx := logTestContext(t, []string{"test"})
err = LockLockable(ctx, r1, r1, nil) NewLockable := func()(*Node) {
l, err := ctx.NewNode(nil, "LockableNode", NewLockableExt(nil))
fatalErr(t, err) fatalErr(t, err)
return l
}
_, err = UpdateStates(ctx, []GraphNode{r2}, func(states []NodeState) ([]NodeState, interface{}, error) { reqs := make([]NodeID, n)
node_state := states[0].(LockHolderState) for i := range(reqs) {
err := LockLockable(ctx, r1, r2, node_state) new_lockable := NewLockable()
if err == nil { reqs[i] = new_lockable.ID
t.Fatal("r2 took r1's lock from itself")
} }
ctx.Log.Logf("test", "CREATED_%d", n)
return []NodeState{node_state}, nil, nil listener := NewListenerExt(50000)
}) node, err := ctx.NewNode(nil, "LockableNode", listener, NewLockableExt(reqs))
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "CREATED_LISTENER")
_, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) { lock_id, err := LockLockable(ctx, node)
owner_id := states[0].(LockableState).Owner().ID()
if owner_id != r1.ID() {
return nil, fmt.Errorf("r1 is owned by %s instead of r1", owner_id)
}
return nil, nil
})
fatalErr(t, err) fatalErr(t, err)
err = UnlockLockable(ctx, r1, r1, nil) response, _, err := WaitForResponse(listener.Chan, time.Second*60, lock_id)
fatalErr(t, err) fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) { switch resp := response.(type) {
owner := states[0].(LockableState).Owner() case *SuccessSignal:
if owner != nil { default:
return nil, fmt.Errorf("r1 is owned by %s instead of r1", owner.ID()) t.Fatalf("Unexpected response to lock - %s", resp)
} }
return nil, nil ctx.Log.Logf("test", "LOCKED_%d", n)
})
fatalErr(t, err)
} }
func TestLockableLockTieredConflict(t * testing.T) { func TestLock(t *testing.T) {
ctx := testContext(t) ctx := logTestContext(t, []string{"test", "lockable"})
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{}) NewLockable := func(reqs []NodeID)(*Node, *ListenerExt) {
listener := NewListenerExt(10000)
l, err := ctx.NewNode(nil, "LockableNode", listener, NewLockableExt(reqs))
fatalErr(t, err) fatalErr(t, err)
return l, listener
r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{r1})
fatalErr(t, err)
r3, err := NewLockable(ctx, "Test lockable 3", []Lockable{r1})
fatalErr(t, err)
err = LockLockable(ctx, r2, r2, nil)
fatalErr(t, err)
err = LockLockable(ctx, r3, r3, nil)
if err == nil {
t.Fatal("Locked r3 which depends on r1 while r2 which depends on r1 is already locked")
} }
}
func TestLockableSimpleUpdate(t * testing.T) { l2, _ := NewLockable(nil)
ctx := testContext(t) l3, _ := NewLockable(nil)
l4, _ := NewLockable(nil)
l5, _ := NewLockable(nil)
l0, l0_listener := NewLockable([]NodeID{l5.ID})
l1, l1_listener := NewLockable([]NodeID{l2.ID, l3.ID, l4.ID, l5.ID})
l1, err := NewLockable(ctx, "Test Lockable 1", []Lockable{}) ctx.Log.Logf("test", "l0: %s", l0.ID)
fatalErr(t, err) ctx.Log.Logf("test", "l1: %s", l1.ID)
ctx.Log.Logf("test", "l2: %s", l2.ID)
update_channel := l1.UpdateChannel(0) ctx.Log.Logf("test", "l3: %s", l3.ID)
ctx.Log.Logf("test", "l4: %s", l4.ID)
go func() { ctx.Log.Logf("test", "l5: %s", l5.ID)
SendUpdate(ctx, l1, NewDirectSignal(l1, "test_update"))
}()
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l1, 100*time.Millisecond, "Didn't receive test_update sent to l1")
}
func TestLockableDownUpdate(t * testing.T) {
ctx := testContext(t)
l1, err := NewLockable(ctx, "Test Lockable 1", []Lockable{}) ctx.Log.Logf("test", "locking l0")
id_1, err := LockLockable(ctx, l0)
fatalErr(t, err) fatalErr(t, err)
response, _, err := WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_1)
l2, err := NewLockable(ctx, "Test Lockable 2", []Lockable{l1})
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "l0 lock: %+v", response)
_, err = NewLockable(ctx, "Test Lockable 3", []Lockable{l2}) ctx.Log.Logf("test", "locking l1")
id_2, err := LockLockable(ctx, l1)
fatalErr(t, err) fatalErr(t, err)
response, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10000, id_2)
update_channel := l1.UpdateChannel(0)
go func() {
SendUpdate(ctx, l2, NewDownSignal(l2, "test_update"))
}()
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l2, 100*time.Millisecond, "Didn't receive test_update on l3 sent on l2")
}
func TestLockableUpUpdate(t * testing.T) {
ctx := testContext(t)
l1, err := NewLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "l1 lock: %+v", response)
l2, err := NewLockable(ctx, "Test Lockable 2", []Lockable{l1}) ctx.Log.Logf("test", "unlocking l0")
id_3, err := UnlockLockable(ctx, l0)
fatalErr(t, err) fatalErr(t, err)
response, _, err = WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_3)
l3, err := NewLockable(ctx, "Test Lockable 3", []Lockable{l2})
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "l0 unlock: %+v", response)
update_channel := l3.UpdateChannel(0) ctx.Log.Logf("test", "locking l1")
id_4, err := LockLockable(ctx, l1)
go func() {
SendUpdate(ctx, l2, NewSignal(l2, "test_update"))
}()
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l2, 100*time.Millisecond, "Didn't receive test_update on l3 sent on l2")
}
func TestOwnerNotUpdatedTwice(t * testing.T) {
ctx := testContext(t)
l1, err := NewLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
response, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, id_4)
l2, err := NewLockable(ctx, "Test Lockable 2", []Lockable{l1})
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "l1 lock: %+v", response)
update_channel := l2.UpdateChannel(0)
go func() {
SendUpdate(ctx, l1, NewSignal(l1, "test_update"))
}()
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l1, 100*time.Millisecond, "Dicn't received test_update on l2 from l1")
(*GraphTester)(t).CheckForNone(update_channel, "Second update received on dependency")
} }

@ -0,0 +1,97 @@
package graphvent
import (
"fmt"
"github.com/rs/zerolog"
"os"
"sync"
"encoding/json"
)
// A Logger is passed around to record events happening to components enabled by SetComponents
type Logger interface {
SetComponents(components []string) error
// Log a formatted string
Logf(component string, format string, items ... interface{})
// Log a map of attributes and a format string
Logm(component string, fields map[string]interface{}, format string, items ... interface{})
// Log a structure to a file by marshalling and unmarshalling the json
Logj(component string, s interface{}, format string, items ... interface{})
}
func NewConsoleLogger(components []string) *ConsoleLogger {
logger := &ConsoleLogger{
loggers: map[string]zerolog.Logger{},
components: []string{},
}
logger.SetComponents(components)
return logger
}
// A ConsoleLogger logs to stdout
type ConsoleLogger struct {
loggers map[string]zerolog.Logger
components_lock sync.Mutex
components []string
}
func (logger * ConsoleLogger) SetComponents(components []string) error {
logger.components_lock.Lock()
defer logger.components_lock.Unlock()
component_enabled := func (component string) bool {
for _, c := range(components) {
if c == component {
return true
}
}
return false
}
for c := range(logger.loggers) {
if component_enabled(c) == false {
delete(logger.loggers, c)
}
}
for _, c := range(components) {
_, exists := logger.loggers[c]
if component_enabled(c) == true && exists == false {
logger.loggers[c] = zerolog.New(os.Stdout).With().Timestamp().Str("component", c).Logger()
}
}
return nil
}
func (logger * ConsoleLogger) Logm(component string, fields map[string]interface{}, format string, items ... interface{}) {
l, exists := logger.loggers[component]
if exists == true {
log := l.Log()
for key, value := range(fields) {
log = log.Str(key, fmt.Sprintf("%+v", value))
}
log.Msg(fmt.Sprintf(format, items...))
}
}
func (logger * ConsoleLogger) Logf(component string, format string, items ... interface{}) {
l, exists := logger.loggers[component]
if exists == true {
l.Log().Msg(fmt.Sprintf(format, items...))
}
}
func (logger * ConsoleLogger) Logj(component string, s interface{}, format string, items ... interface{}) {
m := map[string]interface{}{}
ser, err := json.Marshal(s)
if err != nil {
panic("LOG_MARSHAL_ERR")
}
err = json.Unmarshal(ser, &m)
if err != nil {
panic("LOG_UNMARSHAL_ERR")
}
logger.Logm(component, m, format, items...)
}

@ -0,0 +1,68 @@
package graphvent
type Message struct {
Node NodeID
Signal Signal
}
type MessageQueue struct {
out chan<- Message
in <-chan Message
buffer []Message
write_cursor int
read_cursor int
}
func (queue *MessageQueue) ProcessIncoming(message Message) {
if (queue.write_cursor + 1) == queue.read_cursor || ((queue.write_cursor + 1) == len(queue.buffer) && queue.read_cursor == 0) {
new_buffer := make([]Message, len(queue.buffer) * 2)
copy(new_buffer, queue.buffer[queue.read_cursor:])
first_chunk := len(queue.buffer) - queue.read_cursor
copy(new_buffer[first_chunk:], queue.buffer[0:queue.write_cursor])
queue.write_cursor = len(queue.buffer) - 1
queue.read_cursor = 0
queue.buffer = new_buffer
}
queue.buffer[queue.write_cursor] = message
queue.write_cursor += 1
if queue.write_cursor >= len(queue.buffer) {
queue.write_cursor = 0
}
}
func NewMessageQueue(initial int) (chan<- Message, <-chan Message) {
in := make(chan Message, 0)
out := make(chan Message, 0)
queue := MessageQueue{
out: out,
in: in,
buffer: make([]Message, initial),
write_cursor: 0,
read_cursor: 0,
}
go func(queue *MessageQueue) {
for true {
if queue.write_cursor != queue.read_cursor {
select {
case incoming := <-queue.in:
queue.ProcessIncoming(incoming)
case queue.out <- queue.buffer[queue.read_cursor]:
queue.read_cursor += 1
if queue.read_cursor >= len(queue.buffer) {
queue.read_cursor = 0
}
}
} else {
message := <-queue.in
queue.ProcessIncoming(message)
}
}
}(&queue)
return in, out
}

@ -0,0 +1,35 @@
package graphvent
import (
"encoding/binary"
"testing"
)
func sendBatch(start, end uint64, in chan<- Message) {
for i := start; i <= end; i++ {
var id NodeID
binary.BigEndian.PutUint64(id[:], i)
in <- Message{id, nil}
}
}
func TestMessageQueue(t *testing.T) {
in, out := NewMessageQueue(10)
for i := uint64(0); i < 1000; i++ {
go sendBatch(1000*i, (1000*(i+1))-1, in)
}
seen := map[NodeID]any{}
for i := uint64(0); i < 1000*1000; i++ {
read := <-out
_, already_seen := seen[read.Node]
if already_seen {
t.Fatalf("Signal %d had duplicate NodeID %s", i, read.Node)
} else {
seen[read.Node] = nil
}
}
t.Logf("Processed 1M signals through queue")
}

@ -0,0 +1,401 @@
package graphvent
import (
"crypto/ed25519"
"crypto/sha512"
"fmt"
"reflect"
"sync/atomic"
"time"
_ "github.com/dgraph-io/badger/v3"
"github.com/google/uuid"
)
var (
// Base NodeID, used as a special value
ZeroUUID = uuid.UUID{}
ZeroID = NodeID(ZeroUUID)
)
// A NodeID uniquely identifies a Node
type NodeID uuid.UUID
func (id NodeID) MarshalBinary() ([]byte, error) {
return (uuid.UUID)(id).MarshalBinary()
}
func (id *NodeID) UnmarshalBinary(data []byte) error {
return (*uuid.UUID)(id).UnmarshalBinary(data)
}
func (id NodeID) String() string {
return (uuid.UUID)(id).String()
}
func IDFromBytes(bytes []byte) (NodeID, error) {
id, err := uuid.FromBytes(bytes)
return NodeID(id), err
}
func (id NodeID) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
func (id *NodeID) UnmarshalText(text []byte) error {
parsed, err := ParseID(string(text))
*id = parsed
return err
}
// Parse an ID from a string
func ParseID(str string) (NodeID, error) {
id_uuid, err := uuid.Parse(str)
if err != nil {
return NodeID{}, err
}
return NodeID(id_uuid), nil
}
// Generate a random NodeID
func RandID() NodeID {
return NodeID(uuid.New())
}
// A QueuedSignal is a Signal that has been Queued to trigger at a set time
type QueuedSignal struct {
Signal `gv:"signal"`
time.Time `gv:"time"`
}
func (q QueuedSignal) String() string {
return fmt.Sprintf("%+v@%s", reflect.TypeOf(q.Signal), q.Time)
}
type WaitMap map[uuid.UUID]NodeID
const NODE_INITIAL_QUEUE_SIZE = 2
// Nodes represent a group of extensions that can be collectively addressed
type Node struct {
Key ed25519.PrivateKey `gv:"key"`
ID NodeID
Type NodeType `gv:"type"`
Extensions map[ExtType]Extension
// Channel for this node to receive messages from the Context
SendChan chan<- Message
RecvChan <-chan Message
// Channel for this node to process delayed signals
TimeoutChan <-chan time.Time
Active atomic.Bool
writeSignalQueue bool
SignalQueue []QueuedSignal
NextSignal *QueuedSignal
}
func (node *Node) PostDeserialize(ctx *Context) error {
node.Extensions = map[ExtType]Extension{}
public := node.Key.Public().(ed25519.PublicKey)
node.ID = KeyID(public)
node.SendChan, node.RecvChan = NewMessageQueue(NODE_INITIAL_QUEUE_SIZE)
return nil
}
func (node *Node) QueueSignal(time time.Time, signal Signal) {
node.SignalQueue = append(node.SignalQueue, QueuedSignal{signal, time})
node.NextSignal, node.TimeoutChan = SoonestSignal(node.SignalQueue)
node.writeSignalQueue = true
}
func (node *Node) DequeueSignal(id uuid.UUID) error {
idx := -1
for i, q := range(node.SignalQueue) {
if q.Signal.ID() == id {
idx = i
break
}
}
if idx == -1 {
return fmt.Errorf("%s is not in SignalQueue", id)
}
node.SignalQueue[idx] = node.SignalQueue[len(node.SignalQueue)-1]
node.SignalQueue = node.SignalQueue[:len(node.SignalQueue)-1]
node.NextSignal, node.TimeoutChan = SoonestSignal(node.SignalQueue)
node.writeSignalQueue = true
return nil
}
func SoonestSignal(signals []QueuedSignal) (*QueuedSignal, <-chan time.Time) {
var soonest_signal *QueuedSignal
var soonest_time time.Time
for i, signal := range(signals) {
if signal.Time.Compare(soonest_time) == -1 || soonest_signal == nil {
soonest_signal = &signals[i]
soonest_time = signal.Time
}
}
if soonest_signal != nil {
if time.Now().Compare(soonest_time) == -1 {
return soonest_signal, time.After(time.Until(soonest_signal.Time))
} else {
c := make(chan time.Time, 1)
c <- soonest_time
return soonest_signal, c
}
} else {
return nil, nil
}
}
func runNode(ctx *Context, node *Node, status chan string, control chan string) {
ctx.Log.Logf("node", "RUN_START: %s", node.ID)
err := nodeLoop(ctx, node, status, control)
if err != nil {
ctx.Log.Logf("node", "%s runNode err %s", node.ID, err)
}
ctx.Log.Logf("node", "RUN_STOP: %s", node.ID)
}
type StringError string
func (err StringError) String() string {
return string(err)
}
func (err StringError) Error() string {
return err.String()
}
func (err StringError) MarshalBinary() ([]byte, error) {
return []byte(string(err)), nil
}
func (node *Node) ReadFields(ctx *Context, fields []string)map[string]any {
ctx.Log.Logf("read_field", "Reading %+v on %+v", fields, node.ID)
values := map[string]any{}
node_info := ctx.NodeTypes[node.Type]
for _, field_name := range(fields) {
field_info, mapped := node_info.Fields[field_name]
if mapped {
ext := node.Extensions[field_info.Extension]
values[field_name] = reflect.ValueOf(ext).Elem().FieldByIndex(field_info.Index).Interface()
} else {
values[field_name] = fmt.Errorf("NodeType %s has no field %s", node.Type, field_name)
}
}
return values
}
// Main Loop for nodes
func nodeLoop(ctx *Context, node *Node, status chan string, control chan string) error {
is_started := node.Active.CompareAndSwap(false, true)
if is_started == false {
return fmt.Errorf("%s is already started, will not start again", node.ID)
} else {
ctx.Log.Logf("node", "Set %s active", node.ID)
}
ctx.Log.Logf("node_ext", "Loading extensions for %s", node.ID)
for _, extension := range(node.Extensions) {
ctx.Log.Logf("node_ext", "Loading extension %s for %s", reflect.TypeOf(extension), node.ID)
err := extension.Load(ctx, node)
if err != nil {
ctx.Log.Logf("node_ext", "Failed to load extension %s on node %s", reflect.TypeOf(extension), node.ID)
node.Active.Store(false)
return err
} else {
ctx.Log.Logf("node_ext", "Loaded extension %s on node %s", reflect.TypeOf(extension), node.ID)
}
}
ctx.Log.Logf("node_ext", "Loaded extensions for %s", node.ID)
status <- "active"
running := true
for running {
var signal Signal
var source NodeID
select {
case command := <-control:
switch command {
case "stop":
running = false
case "pause":
status <- "paused"
command := <- control
switch command {
case "resume":
status <- "resumed"
case "stop":
running = false
}
default:
ctx.Log.Logf("node", "Unknown control command %s", command)
}
case <-node.TimeoutChan:
signal = node.NextSignal.Signal
source = node.ID
t := node.NextSignal.Time
i := -1
for j, queued := range(node.SignalQueue) {
if queued.Signal.ID() == node.NextSignal.Signal.ID() {
i = j
break
}
}
if i == -1 {
ctx.Log.Logf("node", "node.NextSignal not in node.SignalQueue, paniccing")
panic("node.NextSignal not in node.SignalQueue")
}
l := len(node.SignalQueue)
node.SignalQueue[i] = node.SignalQueue[l-1]
node.SignalQueue = node.SignalQueue[:(l-1)]
node.NextSignal, node.TimeoutChan = SoonestSignal(node.SignalQueue)
node.writeSignalQueue = true
if node.NextSignal == nil {
ctx.Log.Logf("node", "NODE_TIMEOUT(%s) - PROCESSING %+v@%s - NEXT_SIGNAL nil@%+v", node.ID, signal, t, node.TimeoutChan)
} else {
ctx.Log.Logf("node", "NODE_TIMEOUT(%s) - PROCESSING %+v@%s - NEXT_SIGNAL: %s@%s", node.ID, signal, t, node.NextSignal, node.NextSignal.Time)
}
case msg := <- node.RecvChan:
signal = msg.Signal
source = msg.Node
}
switch sig := signal.(type) {
case *ReadSignal:
result := node.ReadFields(ctx, sig.Fields)
msgs := []Message{}
msgs = append(msgs, Message{source, NewReadResultSignal(sig.ID(), node.ID, node.Type, result)})
ctx.Send(node, msgs)
default:
err := node.Process(ctx, source, signal)
if err != nil {
ctx.Log.Logf("node", "%s process error %s", node.ID, err)
panic(err)
}
}
}
stopped := node.Active.CompareAndSwap(true, false)
if stopped == false {
panic("BAD_STATE: stopping already stopped node")
}
for _, extension := range(node.Extensions) {
extension.Unload(ctx, node)
}
status <- "stopped"
return nil
}
func (node *Node) QueueChanges(ctx *Context, changes map[ExtType]Changes) error {
node_info, exists := ctx.NodeTypes[node.Type]
if exists == false {
return fmt.Errorf("Node type not in context, can't map changes to field names")
} else {
fields := []string{}
for ext_type, ext_changes := range(changes) {
ext_map, ext_mapped := node_info.ReverseFields[ext_type]
if ext_mapped {
for _, ext_tag := range(ext_changes) {
field_name, tag_mapped := ext_map[ext_tag]
if tag_mapped {
fields = append(fields, field_name)
}
}
}
}
ctx.Log.Logf("changes", "Changes to queue from %+v: %+v", node_info.ReverseFields, fields)
if len(fields) > 0 {
node.QueueSignal(time.Time{}, NewStatusSignal(node.ID, fields))
}
return nil
}
}
func (node *Node) Process(ctx *Context, source NodeID, signal Signal) error {
messages := []Message{}
changes := map[ExtType]Changes{}
for ext_type, ext := range(node.Extensions) {
ext_messages, ext_changes := ext.Process(ctx, node, source, signal)
if len(ext_messages) != 0 {
messages = append(messages, ext_messages...)
}
if len(ext_changes) != 0 {
changes[ext_type] = ext_changes
}
}
if len(messages) != 0 {
send_err := ctx.Send(node, messages)
if send_err != nil {
return send_err
}
}
if len(changes) != 0 {
ctx.Log.Logf("changes", "Changes to %s from %+v: %+v", node.ID, signal, changes)
status_err := node.QueueChanges(ctx, changes)
if status_err != nil {
return status_err
}
}
return nil
}
func GetCtx[C any, E any, T interface { *E; Extension}](ctx *Context) (C, error) {
var zero_ctx C
ext_type := ExtType(SerializedTypeFor[E]())
ext_info, ok := ctx.Extensions[ext_type]
if ok == false {
return zero_ctx, fmt.Errorf("%+v is not an extension in ctx", ext_type)
}
ext_ctx, ok := ext_info.Data.(C)
if ok == false {
return zero_ctx, fmt.Errorf("context for %+v is %+v, not %+v", ext_type, reflect.TypeOf(ext_info.Data), reflect.TypeOf(zero_ctx))
}
return ext_ctx, nil
}
func GetExt[E any, T interface { *E; Extension}](node *Node) (T, error) {
var zero T
ext_type := ExtType(SerializedTypeFor[E]())
ext, exists := node.Extensions[ext_type]
if exists == false {
return zero, fmt.Errorf("%+v does not have %+v extension - %+v", node.ID, ext_type, node.Extensions)
}
ret, ok := ext.(T)
if ok == false {
return zero, fmt.Errorf("%+v in %+v is wrong type(%+v), expecting %+v", ext_type, node.ID, reflect.TypeOf(ext), reflect.TypeOf(zero))
}
return ret, nil
}
func KeyID(pub ed25519.PublicKey) NodeID {
id := uuid.NewHash(sha512.New(), ZeroUUID, pub, 3)
return NodeID(id)
}

@ -0,0 +1,55 @@
package graphvent
import (
"testing"
"time"
"crypto/rand"
"crypto/ed25519"
)
func TestNodeDB(t *testing.T) {
ctx := logTestContext(t, []string{"test", "node", "db"})
node_listener := NewListenerExt(10)
node, err := ctx.NewNode(nil, "Node", NewLockableExt(nil), node_listener)
fatalErr(t, err)
err = ctx.Stop()
fatalErr(t, err)
_, err = ctx.GetNode(node.ID)
fatalErr(t, err)
}
func TestNodeRead(t *testing.T) {
ctx := logTestContext(t, []string{"test"})
n1_pub, n1_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
n2_pub, n2_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
n1_id := KeyID(n1_pub)
n2_id := KeyID(n2_pub)
ctx.Log.Logf("test", "N1: %s", n1_id)
ctx.Log.Logf("test", "N2: %s", n2_id)
n2_listener := NewListenerExt(10)
n2, err := ctx.NewNode(n2_key, "Node", n2_listener)
fatalErr(t, err)
n1, err := ctx.NewNode(n1_key, "Node", NewListenerExt(10))
fatalErr(t, err)
read_sig := NewReadSignal([]string{"buffer"})
msgs := []Message{{n1.ID, read_sig}}
err = ctx.Send(n2, msgs)
fatalErr(t, err)
res, err := WaitForSignal(n2_listener.Chan, 10*time.Millisecond, func(sig *ReadResultSignal) bool {
return true
})
fatalErr(t, err)
ctx.Log.Logf("test", "READ_RESULT: %+v", res)
}

@ -0,0 +1,744 @@
package graphvent
import (
"crypto/sha512"
"encoding/binary"
"fmt"
"reflect"
"math"
)
type SerializedType uint64
func (t SerializedType) String() string {
return fmt.Sprintf("0x%x", uint64(t))
}
type ExtType SerializedType
func (t ExtType) String() string {
return fmt.Sprintf("0x%x", uint64(t))
}
type NodeType SerializedType
func (t NodeType) String() string {
return fmt.Sprintf("0x%x", uint64(t))
}
type SignalType SerializedType
func (t SignalType) String() string {
return fmt.Sprintf("0x%x", uint64(t))
}
type FieldTag SerializedType
func (t FieldTag) String() string {
return fmt.Sprintf("0x%x", uint64(t))
}
func NodeTypeFor(name string) NodeType {
digest := []byte("GRAPHVENT_NODE - " + name)
hash := sha512.Sum512(digest)
return NodeType(binary.BigEndian.Uint64(hash[0:8]))
}
func SerializeType(t fmt.Stringer) SerializedType {
digest := []byte(t.String())
hash := sha512.Sum512(digest)
return SerializedType(binary.BigEndian.Uint64(hash[0:8]))
}
func SerializedTypeFor[T any]() SerializedType {
return SerializeType(reflect.TypeFor[T]())
}
func ExtTypeFor[E any, T interface { *E; Extension}]() ExtType {
return ExtType(SerializedTypeFor[E]())
}
func ExtTypeOf(t reflect.Type) ExtType {
return ExtType(SerializeType(t.Elem()))
}
func SignalTypeFor[S Signal]() SignalType {
return SignalType(SerializedTypeFor[S]())
}
func Hash(base, data string) SerializedType {
digest := []byte(base + ":" + data)
hash := sha512.Sum512(digest)
return SerializedType(binary.BigEndian.Uint64(hash[0:8]))
}
func GetFieldTag(tag string) FieldTag {
return FieldTag(Hash("GRAPHVENT_FIELD_TAG", tag))
}
func TypeStack(ctx *Context, t reflect.Type, data []byte) (int, error) {
info, registered := ctx.Types[t]
if registered {
binary.BigEndian.PutUint64(data, uint64(info.Serialized))
return 8, nil
} else {
switch t.Kind() {
case reflect.Map:
binary.BigEndian.PutUint64(data, uint64(SerializeType(reflect.Map)))
key_written, err := TypeStack(ctx, t.Key(), data[8:])
if err != nil {
return 0, err
}
elem_written, err := TypeStack(ctx, t.Elem(), data[8 + key_written:])
if err != nil {
return 0, err
}
return 8 + key_written + elem_written, nil
case reflect.Pointer:
binary.BigEndian.PutUint64(data, uint64(SerializeType(reflect.Pointer)))
elem_written, err := TypeStack(ctx, t.Elem(), data[8:])
if err != nil {
return 0, err
}
return 8 + elem_written, nil
case reflect.Slice:
binary.BigEndian.PutUint64(data, uint64(SerializeType(reflect.Slice)))
elem_written, err := TypeStack(ctx, t.Elem(), data[8:])
if err != nil {
return 0, err
}
return 8 + elem_written, nil
case reflect.Array:
binary.BigEndian.PutUint64(data, uint64(SerializeType(reflect.Array)))
binary.BigEndian.PutUint64(data[8:], uint64(t.Len()))
elem_written, err := TypeStack(ctx, t.Elem(), data[16:])
if err != nil {
return 0, err
}
return 16 + elem_written, nil
default:
return 0, fmt.Errorf("Hit %s, which is not a registered type", t.String())
}
}
}
func UnwrapStack(ctx *Context, stack []byte) (reflect.Type, []byte, error) {
first_bytes, left := split(stack, 8)
first := SerializedType(binary.BigEndian.Uint64(first_bytes))
info, registered := ctx.TypesReverse[first]
if registered {
return info.Reflect, left, nil
} else {
switch first {
case SerializeType(reflect.Map):
key_type, after_key, err := UnwrapStack(ctx, left)
if err != nil {
return nil, nil, err
}
elem_type, after_elem, err := UnwrapStack(ctx, after_key)
if err != nil {
return nil, nil, err
}
return reflect.MapOf(key_type, elem_type), after_elem, nil
case SerializeType(reflect.Pointer):
elem_type, rest, err := UnwrapStack(ctx, left)
if err != nil {
return nil, nil, err
}
return reflect.PointerTo(elem_type), rest, nil
case SerializeType(reflect.Slice):
elem_type, rest, err := UnwrapStack(ctx, left)
if err != nil {
return nil, nil, err
}
return reflect.SliceOf(elem_type), rest, nil
case SerializeType(reflect.Array):
length_bytes, left := split(left, 8)
length := int(binary.BigEndian.Uint64(length_bytes))
elem_type, rest, err := UnwrapStack(ctx, left)
if err != nil {
return nil, nil, err
}
return reflect.ArrayOf(length, elem_type), rest, nil
default:
return nil, nil, fmt.Errorf("Type stack %+v not recognized", stack)
}
}
}
func Serialize[T any](ctx *Context, value T, data []byte) (int, error) {
return SerializeValue(ctx, reflect.ValueOf(&value).Elem(), data)
}
func Deserialize[T any](ctx *Context, data []byte) (T, error) {
reflect_type := reflect.TypeFor[T]()
var zero T
value, left, err := DeserializeValue(ctx, data, reflect_type)
if err != nil {
return zero, err
} else if len(left) != 0 {
return zero, fmt.Errorf("%d/%d bytes left after deserializing %+v", len(left), len(data), value)
} else if value.Type() != reflect_type {
return zero, fmt.Errorf("Deserialized type %s does not match %s", value.Type(), reflect_type)
}
return value.Interface().(T), nil
}
func SerializedSize(ctx *Context, value reflect.Value) (int, error) {
var sizefn SerializedSizeFn = nil
info, registered := ctx.Types[value.Type()]
if registered {
sizefn = info.SerializedSize
}
if sizefn == nil {
switch value.Type().Kind() {
case reflect.Bool:
return 1, nil
case reflect.Int8:
return 1, nil
case reflect.Int16:
return 2, nil
case reflect.Int32:
return 4, nil
case reflect.Int64:
fallthrough
case reflect.Int:
return 8, nil
case reflect.Uint8:
return 1, nil
case reflect.Uint16:
return 2, nil
case reflect.Uint32:
return 4, nil
case reflect.Uint64:
fallthrough
case reflect.Uint:
return 8, nil
case reflect.Float32:
return 4, nil
case reflect.Float64:
return 8, nil
case reflect.String:
return 8 + value.Len(), nil
case reflect.Pointer:
if value.IsNil() {
return 1, nil
} else {
elem_len, err := SerializedSize(ctx, value.Elem())
if err != nil {
return 0, err
} else {
return 1 + elem_len, nil
}
}
case reflect.Slice:
if value.IsNil() {
return 1, nil
} else {
elem_total := 0
for i := 0; i < value.Len(); i++ {
elem_len, err := SerializedSize(ctx, value.Index(i))
if err != nil {
return 0, err
}
elem_total += elem_len
}
return 9 + elem_total, nil
}
case reflect.Array:
total := 0
for i := 0; i < value.Len(); i++ {
elem_len, err := SerializedSize(ctx, value.Index(i))
if err != nil {
return 0, err
}
total += elem_len
}
return total, nil
case reflect.Map:
if value.IsNil() {
return 1, nil
} else {
key := reflect.New(value.Type().Key()).Elem()
val := reflect.New(value.Type().Elem()).Elem()
iter := value.MapRange()
total := 0
for iter.Next() {
key.SetIterKey(iter)
k, err := SerializedSize(ctx, key)
if err != nil {
return 0, err
}
total += k
val.SetIterValue(iter)
v, err := SerializedSize(ctx, val)
if err != nil {
return 0, err
}
total += v
}
return 9 + total, nil
}
case reflect.Struct:
if registered == false {
return 0, fmt.Errorf("Can't serialize unregistered struct %s", value.Type())
} else {
field_total := 0
for _, field_info := range(info.Fields) {
field_size, err := SerializedSize(ctx, value.FieldByIndex(field_info.Index))
if err != nil {
return 0, err
}
field_total += 8
field_total += field_size
}
return 8 + field_total, nil
}
case reflect.Interface:
// TODO get size of TypeStack instead of just using 128
elem_size, err := SerializedSize(ctx, value.Elem())
if err != nil {
return 0, err
}
return 128 + elem_size, nil
default:
return 0, fmt.Errorf("Don't know how to serialize %s", value.Type())
}
} else {
return sizefn(ctx, value)
}
}
func SerializeValue(ctx *Context, value reflect.Value, data []byte) (int, error) {
var serialize SerializeFn = nil
info, registered := ctx.Types[value.Type()]
if registered {
serialize = info.Serialize
}
if serialize == nil {
switch value.Type().Kind() {
case reflect.Bool:
if value.Bool() {
data[0] = 0xFF
} else {
data[0] = 0x00
}
return 1, nil
case reflect.Int8:
data[0] = byte(value.Int())
return 1, nil
case reflect.Int16:
binary.BigEndian.PutUint16(data, uint16(value.Int()))
return 2, nil
case reflect.Int32:
binary.BigEndian.PutUint32(data, uint32(value.Int()))
return 4, nil
case reflect.Int64:
fallthrough
case reflect.Int:
binary.BigEndian.PutUint64(data, uint64(value.Int()))
return 8, nil
case reflect.Uint8:
data[0] = byte(value.Uint())
return 1, nil
case reflect.Uint16:
binary.BigEndian.PutUint16(data, uint16(value.Uint()))
return 2, nil
case reflect.Uint32:
binary.BigEndian.PutUint32(data, uint32(value.Uint()))
return 4, nil
case reflect.Uint64:
fallthrough
case reflect.Uint:
binary.BigEndian.PutUint64(data, value.Uint())
return 8, nil
case reflect.Float32:
binary.BigEndian.PutUint32(data, math.Float32bits(float32(value.Float())))
return 4, nil
case reflect.Float64:
binary.BigEndian.PutUint64(data, math.Float64bits(value.Float()))
return 8, nil
case reflect.String:
binary.BigEndian.PutUint64(data, uint64(value.Len()))
copy(data[8:], []byte(value.String()))
return 8 + value.Len(), nil
case reflect.Pointer:
if value.IsNil() {
data[0] = 0x00
return 1, nil
} else {
data[0] = 0x01
written, err := SerializeValue(ctx, value.Elem(), data[1:])
if err != nil {
return 0, err
}
return 1 + written, nil
}
case reflect.Slice:
if value.IsNil() {
data[0] = 0x00
return 8, nil
} else {
data[0] = 0x01
binary.BigEndian.PutUint64(data[1:], uint64(value.Len()))
total_written := 0
for i := 0; i < value.Len(); i++ {
written, err := SerializeValue(ctx, value.Index(i), data[9+total_written:])
if err != nil {
return 0, err
}
total_written += written
}
return 9 + total_written, nil
}
case reflect.Array:
total_written := 0
for i := 0; i < value.Len(); i++ {
written, err := SerializeValue(ctx, value.Index(i), data[total_written:])
if err != nil {
return 0, err
}
total_written += written
}
return total_written, nil
case reflect.Map:
if value.IsNil() {
data[0] = 0x00
return 1, nil
} else {
data[0] = 0x01
binary.BigEndian.PutUint64(data[1:], uint64(value.Len()))
key := reflect.New(value.Type().Key()).Elem()
val := reflect.New(value.Type().Elem()).Elem()
iter := value.MapRange()
total_written := 0
for iter.Next() {
key.SetIterKey(iter)
val.SetIterValue(iter)
k, err := SerializeValue(ctx, key, data[9+total_written:])
if err != nil {
return 0, err
}
total_written += k
v, err := SerializeValue(ctx, val, data[9+total_written:])
if err != nil {
return 0, err
}
total_written += v
}
return 9 + total_written, nil
}
case reflect.Struct:
if registered == false {
return 0, fmt.Errorf("Cannot serialize unregistered struct %s", value.Type())
} else {
binary.BigEndian.PutUint64(data, uint64(len(info.Fields)))
total_written := 0
for field_tag, field_info := range(info.Fields) {
binary.BigEndian.PutUint64(data[8+total_written:], uint64(field_tag))
total_written += 8
written, err := SerializeValue(ctx, value.FieldByIndex(field_info.Index), data[8+total_written:])
if err != nil {
return 0, err
}
total_written += written
}
return 8 + total_written, nil
}
case reflect.Interface:
type_written, err := TypeStack(ctx, value.Elem().Type(), data)
elem_written, err := SerializeValue(ctx, value.Elem(), data[type_written:])
if err != nil {
return 0, err
}
return type_written + elem_written, nil
default:
return 0, fmt.Errorf("Don't know how to serialize %s", value.Type())
}
} else {
return serialize(ctx, value, data)
}
}
func split(data []byte, n int) ([]byte, []byte) {
return data[:n], data[n:]
}
func DeserializeValue(ctx *Context, data []byte, t reflect.Type) (reflect.Value, []byte, error) {
var deserialize DeserializeFn = nil
info, registered := ctx.Types[t]
if registered {
deserialize = info.Deserialize
}
if deserialize == nil {
switch t.Kind() {
case reflect.Bool:
used, left := split(data, 1)
value := reflect.New(t).Elem()
value.SetBool(used[0] != 0x00)
return value, left, nil
case reflect.Int8:
used, left := split(data, 1)
value := reflect.New(t).Elem()
value.SetInt(int64(used[0]))
return value, left, nil
case reflect.Int16:
used, left := split(data, 2)
value := reflect.New(t).Elem()
value.SetInt(int64(binary.BigEndian.Uint16(used)))
return value, left, nil
case reflect.Int32:
used, left := split(data, 4)
value := reflect.New(t).Elem()
value.SetInt(int64(binary.BigEndian.Uint32(used)))
return value, left, nil
case reflect.Int64:
fallthrough
case reflect.Int:
used, left := split(data, 8)
value := reflect.New(t).Elem()
value.SetInt(int64(binary.BigEndian.Uint64(used)))
return value, left, nil
case reflect.Uint8:
used, left := split(data, 1)
value := reflect.New(t).Elem()
value.SetUint(uint64(used[0]))
return value, left, nil
case reflect.Uint16:
used, left := split(data, 2)
value := reflect.New(t).Elem()
value.SetUint(uint64(binary.BigEndian.Uint16(used)))
return value, left, nil
case reflect.Uint32:
used, left := split(data, 4)
value := reflect.New(t).Elem()
value.SetUint(uint64(binary.BigEndian.Uint32(used)))
return value, left, nil
case reflect.Uint64:
fallthrough
case reflect.Uint:
used, left := split(data, 8)
value := reflect.New(t).Elem()
value.SetUint(binary.BigEndian.Uint64(used))
return value, left, nil
case reflect.Float32:
used, left := split(data, 4)
value := reflect.New(t).Elem()
value.SetFloat(float64(math.Float32frombits(binary.BigEndian.Uint32(used))))
return value, left, nil
case reflect.Float64:
used, left := split(data, 8)
value := reflect.New(t).Elem()
value.SetFloat(math.Float64frombits(binary.BigEndian.Uint64(used)))
return value, left, nil
case reflect.String:
length, after_len := split(data, 8)
used, left := split(after_len, int(binary.BigEndian.Uint64(length)))
value := reflect.New(t).Elem()
value.SetString(string(used))
return value, left, nil
case reflect.Pointer:
flags, after_flags := split(data, 1)
value := reflect.New(t).Elem()
if flags[0] == 0x00 {
value.SetZero()
return value, after_flags, nil
} else {
elem_value, after_elem, err := DeserializeValue(ctx, after_flags, t.Elem())
if err != nil {
return reflect.Value{}, nil, err
}
value.Set(elem_value.Addr())
return value, after_elem, nil
}
case reflect.Slice:
nil_byte := data[0]
data = data[1:]
if nil_byte == 0x00 {
return reflect.New(t).Elem(), data, nil
} else {
len_bytes, left := split(data, 8)
length := int(binary.BigEndian.Uint64(len_bytes))
value := reflect.MakeSlice(t, length, length)
for i := 0; i < length; i++ {
var elem_value reflect.Value
var err error
elem_value, left, err = DeserializeValue(ctx, left, t.Elem())
if err != nil {
return reflect.Value{}, nil, err
}
value.Index(i).Set(elem_value)
}
return value, left, nil
}
case reflect.Array:
value := reflect.New(t).Elem()
left := data
for i := 0; i < t.Len(); i++ {
var elem_value reflect.Value
var err error
elem_value, left, err = DeserializeValue(ctx, left, t.Elem())
if err != nil {
return reflect.Value{}, nil, err
}
value.Index(i).Set(elem_value)
}
return value, left, nil
case reflect.Map:
flags, after_flags := split(data, 1)
if flags[0] == 0x00 {
return reflect.New(t).Elem(), after_flags, nil
} else {
len_bytes, left := split(after_flags, 8)
length := int(binary.BigEndian.Uint64(len_bytes))
value := reflect.MakeMapWithSize(t, length)
for i := 0; i < length; i++ {
var key_value reflect.Value
var val_value reflect.Value
var err error
key_value, left, err = DeserializeValue(ctx, left, t.Key())
if err != nil {
return reflect.Value{}, nil, err
}
val_value, left, err = DeserializeValue(ctx, left, t.Elem())
if err != nil {
return reflect.Value{}, nil, err
}
value.SetMapIndex(key_value, val_value)
}
return value, left, nil
}
case reflect.Struct:
info, mapped := ctx.Types[t]
if mapped {
value := reflect.New(t).Elem()
num_field_bytes, left := split(data, 8)
num_fields := int(binary.BigEndian.Uint64(num_field_bytes))
for i := 0; i < num_fields; i++ {
var tag_bytes []byte
tag_bytes, left = split(left, 8)
field_tag := FieldTag(binary.BigEndian.Uint64(tag_bytes))
field_info, mapped := info.Fields[field_tag]
if mapped {
var field_val reflect.Value
var err error
field_val, left, err = DeserializeValue(ctx, left, field_info.Type)
if err != nil {
return reflect.Value{}, nil, err
}
value.FieldByIndex(field_info.Index).Set(field_val)
} else {
return reflect.Value{}, nil, fmt.Errorf("Unknown field %s on struct %s", field_tag, t)
}
}
if info.PostDeserializeIndex != -1 {
post_deserialize_method := value.Addr().Method(info.PostDeserializeIndex)
post_deserialize_method.Call([]reflect.Value{reflect.ValueOf(ctx)})
}
return value, left, nil
} else {
return reflect.Value{}, nil, fmt.Errorf("Cannot deserialize unregistered struct %s", t)
}
case reflect.Interface:
elem_type, rest, err := UnwrapStack(ctx, data)
if err != nil {
return reflect.Value{}, nil, err
}
elem_val, left, err := DeserializeValue(ctx, rest, elem_type)
if err != nil {
return reflect.Value{}, nil, err
}
val := reflect.New(t).Elem()
val.Set(elem_val)
return val, left, nil
default:
return reflect.Value{}, nil, fmt.Errorf("Don't know how to deserialize %s", t)
}
} else {
return deserialize(ctx, data)
}
}

@ -0,0 +1,176 @@
package graphvent
import (
"testing"
"reflect"
"github.com/google/uuid"
)
func testTypeStack[T any](t *testing.T, ctx *Context) {
buffer := [1024]byte{}
reflect_type := reflect.TypeFor[T]()
written, err := TypeStack(ctx, reflect_type, buffer[:])
fatalErr(t, err)
stack := buffer[:written]
ctx.Log.Logf("test", "TypeStack[%s]: %+v", reflect_type, stack)
unwrapped_type, rest, err := UnwrapStack(ctx, stack)
fatalErr(t, err)
if len(rest) != 0 {
t.Errorf("Types remaining after unwrapping %s: %+v", unwrapped_type, stack)
}
if unwrapped_type != reflect_type {
t.Errorf("Unwrapped type[%+v] doesn't match original[%+v]", unwrapped_type, reflect_type)
}
ctx.Log.Logf("test", "Unwrapped type[%s]: %s", reflect_type, reflect_type)
}
func TestSerializeTypes(t *testing.T) {
ctx := logTestContext(t, []string{"test"})
testTypeStack[int](t, ctx)
testTypeStack[map[int]string](t, ctx)
testTypeStack[string](t, ctx)
testTypeStack[*string](t, ctx)
testTypeStack[*map[string]*map[*string]int](t, ctx)
testTypeStack[[5]int](t, ctx)
testTypeStack[uuid.UUID](t, ctx)
testTypeStack[NodeID](t, ctx)
}
func testSerializeCompare[T comparable](t *testing.T, ctx *Context, value T) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
fatalErr(t, err)
serialized := buffer[:written]
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[T](), value, serialized)
deserialized, err := Deserialize[T](ctx, serialized)
fatalErr(t, err)
if value != deserialized {
t.Fatalf("Deserialized value[%+v] doesn't match original[%+v]", value, deserialized)
}
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", value, deserialized)
}
func testSerializeList[L []T, T comparable](t *testing.T, ctx *Context, value L) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
fatalErr(t, err)
serialized := buffer[:written]
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[L](), value, serialized)
deserialized, err := Deserialize[L](ctx, serialized)
fatalErr(t, err)
for i, item := range(value) {
if item != deserialized[i] {
t.Fatalf("Deserialized list %+v does not match original %+v", value, deserialized)
}
}
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", value, deserialized)
}
func testSerializePointer[P interface {*T}, T comparable](t *testing.T, ctx *Context, value P) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
fatalErr(t, err)
serialized := buffer[:written]
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[P](), value, serialized)
deserialized, err := Deserialize[P](ctx, serialized)
fatalErr(t, err)
if value == nil && deserialized == nil {
ctx.Log.Logf("test", "Deserialized nil")
} else if value == nil {
t.Fatalf("Non-nil value[%+v] returned for nil value", deserialized)
} else if deserialized == nil {
t.Fatalf("Nil value returned for non-nil value[%+v]", value)
} else if *deserialized != *value {
t.Fatalf("Deserialized value[%+v] doesn't match original[%+v]", value, deserialized)
} else {
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", *value, *deserialized)
}
}
func testSerialize[T any](t *testing.T, ctx *Context, value T) {
buffer := [1024]byte{}
written, err := Serialize(ctx, value, buffer[:])
fatalErr(t, err)
serialized := buffer[:written]
ctx.Log.Logf("test", "Serialized Value[%s : %+v]: %+v", reflect.TypeFor[T](), value, serialized)
deserialized, err := Deserialize[T](ctx, serialized)
fatalErr(t, err)
ctx.Log.Logf("test", "Deserialized Value[%+v]: %+v", value, deserialized)
}
func TestSerializeValues(t *testing.T) {
ctx := logTestContext(t, []string{"test"})
testSerialize(t, ctx, Extension(NewLockableExt(nil)))
testSerializeCompare[int8](t, ctx, -64)
testSerializeCompare[int16](t, ctx, -64)
testSerializeCompare[int32](t, ctx, -64)
testSerializeCompare[int64](t, ctx, -64)
testSerializeCompare[int](t, ctx, -64)
testSerializeCompare[uint8](t, ctx, 64)
testSerializeCompare[uint16](t, ctx, 64)
testSerializeCompare[uint32](t, ctx, 64)
testSerializeCompare[uint64](t, ctx, 64)
testSerializeCompare[uint](t, ctx, 64)
testSerializeCompare[string](t, ctx, "test")
a := 12
testSerializePointer[*int](t, ctx, &a)
b := "test"
testSerializePointer[*string](t, ctx, nil)
testSerializePointer[*string](t, ctx, &b)
testSerializeList(t, ctx, []int{1, 2, 3, 4, 5})
testSerializeCompare[bool](t, ctx, true)
testSerializeCompare[bool](t, ctx, false)
testSerializeCompare[int](t, ctx, -1)
testSerializeCompare[uint](t, ctx, 1)
testSerializeCompare[NodeID](t, ctx, RandID())
testSerializeCompare[*int](t, ctx, nil)
testSerializeCompare(t, ctx, "string")
testSerialize(t, ctx, map[string]string{
"Test": "Test",
"key": "String",
"": "",
})
testSerialize[map[string]string](t, ctx, nil)
testSerialize(t, ctx, NewListenerExt(10))
node, err := ctx.NewNode(nil, "Node")
fatalErr(t, err)
testSerialize(t, ctx, node)
}

@ -0,0 +1,261 @@
package graphvent
import (
"fmt"
"time"
"github.com/google/uuid"
)
type TimeoutSignal struct {
ResponseHeader
}
func NewTimeoutSignal(req_id uuid.UUID) *TimeoutSignal {
return &TimeoutSignal{
NewResponseHeader(req_id),
}
}
func (signal TimeoutSignal) String() string {
return fmt.Sprintf("TimeoutSignal(%s)", &signal.ResponseHeader)
}
type SignalHeader struct {
Id uuid.UUID `gv:"id"`
}
func (signal SignalHeader) ID() uuid.UUID {
return signal.Id
}
func (header SignalHeader) String() string {
return fmt.Sprintf("%s", header.Id)
}
type ResponseSignal interface {
Signal
ResponseID() uuid.UUID
}
type ResponseHeader struct {
SignalHeader
ReqID uuid.UUID `gv:"req_id"`
}
func (header ResponseHeader) ResponseID() uuid.UUID {
return header.ReqID
}
func (header ResponseHeader) String() string {
return fmt.Sprintf("%s for %s", header.Id, header.ReqID)
}
type Signal interface {
fmt.Stringer
ID() uuid.UUID
}
func WaitForResponse(listener chan Signal, timeout time.Duration, req_id uuid.UUID) (ResponseSignal, []Signal, error) {
signals := []Signal{}
var timeout_channel <- chan time.Time
if timeout > 0 {
timeout_channel = time.After(timeout)
}
for true {
select {
case signal := <- listener:
if signal == nil {
return nil, signals, fmt.Errorf("LISTENER_CLOSED")
}
resp_signal, ok := signal.(ResponseSignal)
if ok == true && resp_signal.ResponseID() == req_id {
return resp_signal, signals, nil
} else {
signals = append(signals, signal)
}
case <-timeout_channel:
return nil, signals, fmt.Errorf("LISTENER_TIMEOUT")
}
}
return nil, signals, fmt.Errorf("UNREACHABLE")
}
//TODO: Add []Signal return as well for other signals
func WaitForSignal[S Signal](listener chan Signal, timeout time.Duration, check func(S)bool) (S, error) {
var zero S
var timeout_channel <- chan time.Time
if timeout > 0 {
timeout_channel = time.After(timeout)
}
for true {
select {
case signal := <- listener:
if signal == nil {
return zero, fmt.Errorf("LISTENER_CLOSED")
}
sig, ok := signal.(S)
if ok == true {
if check(sig) == true {
return sig, nil
}
}
case <-timeout_channel:
return zero, fmt.Errorf("LISTENER_TIMEOUT")
}
}
return zero, fmt.Errorf("LOOP_ENDED")
}
func NewSignalHeader() SignalHeader {
return SignalHeader{
uuid.New(),
}
}
func NewResponseHeader(req_id uuid.UUID) ResponseHeader {
return ResponseHeader{
NewSignalHeader(),
req_id,
}
}
type SuccessSignal struct {
ResponseHeader
}
func (signal SuccessSignal) String() string {
return fmt.Sprintf("SuccessSignal(%s)", signal.ResponseHeader)
}
func NewSuccessSignal(req_id uuid.UUID) *SuccessSignal {
return &SuccessSignal{
NewResponseHeader(req_id),
}
}
type ErrorSignal struct {
ResponseHeader
Error string
}
func (signal ErrorSignal) String() string {
return fmt.Sprintf("ErrorSignal(%s, %s)", signal.ResponseHeader, signal.Error)
}
func NewErrorSignal(req_id uuid.UUID, fmt_string string, args ...interface{}) *ErrorSignal {
return &ErrorSignal{
NewResponseHeader(req_id),
fmt.Sprintf(fmt_string, args...),
}
}
type ACLTimeoutSignal struct {
ResponseHeader
}
func NewACLTimeoutSignal(req_id uuid.UUID) *ACLTimeoutSignal {
sig := &ACLTimeoutSignal{
NewResponseHeader(req_id),
}
return sig
}
type StatusSignal struct {
SignalHeader
Source NodeID `gv:"source"`
Fields []string `gv:"fields"`
}
func (signal StatusSignal) String() string {
return fmt.Sprintf("StatusSignal(%s: %+v)", signal.Source, signal.Fields)
}
func NewStatusSignal(source NodeID, fields []string) *StatusSignal {
return &StatusSignal{
NewSignalHeader(),
source,
fields,
}
}
type LinkSignal struct {
SignalHeader
NodeID NodeID
Action string
}
const (
LinkActionBase = "LINK_ACTION"
LinkActionAdd = "ADD"
)
func NewLinkSignal(action string, id NodeID) Signal {
return &LinkSignal{
NewSignalHeader(),
id,
action,
}
}
type LockSignal struct {
SignalHeader
}
func (signal LockSignal) String() string {
return fmt.Sprintf("LockSignal(%s)", signal.SignalHeader)
}
func NewLockSignal() *LockSignal {
return &LockSignal{
NewSignalHeader(),
}
}
type UnlockSignal struct {
SignalHeader
}
func (signal UnlockSignal) String() string {
return fmt.Sprintf("UnlockSignal(%s)", signal.SignalHeader)
}
func NewUnlockSignal() *UnlockSignal {
return &UnlockSignal{
NewSignalHeader(),
}
}
type ReadSignal struct {
SignalHeader
Fields []string `json:"extensions"`
}
func (signal ReadSignal) String() string {
return fmt.Sprintf("ReadSignal(%s, %+v)", signal.SignalHeader, signal.Fields)
}
func NewReadSignal(fields []string) *ReadSignal {
return &ReadSignal{
NewSignalHeader(),
fields,
}
}
type ReadResultSignal struct {
ResponseHeader
NodeID NodeID
NodeType NodeType
Fields map[string]any
}
func (signal ReadResultSignal) String() string {
return fmt.Sprintf("ReadResultSignal(%s, %s, %+v)", signal.ResponseHeader, signal.NodeID, signal.Fields)
}
func NewReadResultSignal(req_id uuid.UUID, node_id NodeID, node_type NodeType, fields map[string]any) *ReadResultSignal {
return &ReadResultSignal{
NewResponseHeader(req_id),
node_id,
node_type,
fields,
}
}

@ -1,413 +0,0 @@
package graphvent
import (
"fmt"
"time"
"errors"
"reflect"
"encoding/json"
)
// Update the threads listeners, and notify the parent to do the same
func (thread * BaseThread) PropagateUpdate(ctx * GraphContext, signal GraphSignal) {
UseStates(ctx, []GraphNode{thread}, func(states []NodeState) (interface{}, error) {
thread_state := states[0].(ThreadState)
if signal.Direction() == Up {
// Child->Parent, thread updates parent and connected requirement
if thread_state.Parent() != nil {
SendUpdate(ctx, thread_state.Parent(), signal)
}
for _, dep := range(thread_state.Dependencies()) {
SendUpdate(ctx, dep, signal)
}
} else if signal.Direction() == Down {
// Parent->Child, updates children and dependencies
for _, child := range(thread_state.Children()) {
SendUpdate(ctx, child, signal)
}
for _, requirement := range(thread_state.Requirements()) {
SendUpdate(ctx, requirement, signal)
}
} else if signal.Direction() == Direct {
} else {
panic(fmt.Sprintf("Invalid signal direction: %d", signal.Direction()))
}
return nil, nil
})
thread.signal <- signal
}
type ThreadInfo interface {
}
// An Thread is a lockable that has an additional parent->child relationship with other Threads
// This relationship allows the thread tree to be modified independent of the lockable state
type ThreadState interface {
LockHolderState
LockableState
Parent() Thread
SetParent(parent Thread)
Children() []Thread
ChildInfo(child NodeID) ThreadInfo
AddChild(child Thread, info ThreadInfo) error
}
type BaseThreadState struct {
BaseLockableState
parent Thread
children []Thread
child_info map[NodeID] ThreadInfo
info_type reflect.Type
}
type BaseThreadStateJSON struct {
Parent *NodeID `json:"parent"`
Children map[NodeID]interface{} `json:"children"`
LockableState *BaseLockableState `json:"lockable_state"`
}
func (state * BaseThreadState) MarshalJSON() ([]byte, error) {
children := map[NodeID]interface{}{}
for _, child := range(state.children) {
children[child.ID()] = state.child_info[child.ID()]
}
var parent_id *NodeID = nil
if state.parent != nil {
new_str := state.parent.ID()
parent_id = &new_str
}
return json.Marshal(&BaseThreadStateJSON{
Parent: parent_id,
Children: children,
LockableState: &state.BaseLockableState,
})
}
func (state * BaseThreadState) Parent() Thread {
return state.parent
}
func (state * BaseThreadState) SetParent(parent Thread) {
state.parent = parent
}
func (state * BaseThreadState) Children() []Thread {
return state.children
}
func (state * BaseThreadState) ChildInfo(child NodeID) ThreadInfo {
return state.child_info[child]
}
func (state * BaseThreadState) AddChild(child Thread, info ThreadInfo) error {
if child == nil {
return fmt.Errorf("Will not connect nil to the thread tree")
}
_, exists := state.child_info[child.ID()]
if exists == true {
return fmt.Errorf("Will not connect the same child twice")
}
if info == nil && state.info_type != nil {
return fmt.Errorf("nil info passed when expecting info")
} else if info != nil {
if reflect.TypeOf(info) != state.info_type {
return fmt.Errorf("info type mismatch, expecting %+v", state.info_type)
}
}
state.children = append(state.children, child)
state.child_info[child.ID()] = info
return nil
}
func checkIfChild(ctx * GraphContext, thread_id NodeID, cur_state ThreadState, cur_id NodeID) bool {
for _, child := range(cur_state.Children()) {
if child.ID() == thread_id {
return true
}
val, _ := UseStates(ctx, []GraphNode{child}, func(states []NodeState) (interface{}, error) {
child_state := states[0].(ThreadState)
return checkIfChild(ctx, cur_id, child_state, child.ID()), nil
})
is_child := val.(bool)
if is_child {
return true
}
}
return false
}
func LinkThreads(ctx * GraphContext, thread Thread, child Thread, info ThreadInfo) error {
if ctx == nil || thread == nil || child == nil {
return fmt.Errorf("invalid input")
}
if thread.ID() == child.ID() {
return fmt.Errorf("Will not link %s as a child of itself", thread.ID())
}
_, err := UpdateStates(ctx, []GraphNode{thread, child}, func(states []NodeState) ([]NodeState, interface{}, error) {
thread_state := states[0].(ThreadState)
child_state := states[1].(ThreadState)
if child_state.Parent() != nil {
return nil, nil, fmt.Errorf("EVENT_LINK_ERR: %s already has a parent, cannot link as child", child.ID())
}
if checkIfChild(ctx, thread.ID(), child_state, child.ID()) == true {
return nil, nil, fmt.Errorf("EVENT_LINK_ERR: %s is a child of %s so cannot add as parent", thread.ID(), child.ID())
}
if checkIfChild(ctx, child.ID(), thread_state, thread.ID()) == true {
return nil, nil, fmt.Errorf("EVENT_LINK_ERR: %s is already a parent of %s so will not add again", thread.ID(), child.ID())
}
err := thread_state.AddChild(child, info)
if err != nil {
return nil, nil, fmt.Errorf("EVENT_LINK_ERR: error adding %s as child to %s: %e", child.ID(), thread.ID(), err)
}
child_state.SetParent(thread)
return states, nil, nil
})
if err != nil {
return err
}
return nil
}
// Thread is the interface that thread tree nodes must implement
type Thread interface {
Lockable
Action(action string) (ThreadAction, bool)
Handler(signal_type string) (ThreadHandler, bool)
SetTimeout(end_time time.Time, action string)
ClearTimeout()
Timeout() <-chan time.Time
TimeoutAction() string
}
func FindChild(ctx * GraphContext, thread Thread, thread_state ThreadState, id NodeID) Thread {
if thread == nil {
panic("cannot recurse through nil")
}
if id == thread.ID() {
return thread
}
for _, child := range thread_state.Children() {
res, _ := UseStates(ctx, []GraphNode{child}, func(states []NodeState) (interface{}, error) {
child_state := states[0].(ThreadState)
result := FindChild(ctx, child, child_state, id)
return result, nil
})
result := res.(Thread)
if result != nil {
return result
}
}
return nil
}
func RunThread(ctx * GraphContext, thread Thread) error {
ctx.Log.Logf("thread", "EVENT_RUN: %s", thread.ID())
err := LockLockable(ctx, thread, thread, nil)
if err != nil {
return err
}
_, err = UseStates(ctx, []GraphNode{thread}, func(states []NodeState) (interface{}, error) {
thread_state := states[0].(ThreadState)
if thread_state.Owner() == nil {
return nil, fmt.Errorf("EVENT_RUN_NOT_LOCKED: %s", thread_state.Name())
} else if thread_state.Owner().ID() != thread.ID() {
return nil, fmt.Errorf("EVENT_RUN_RESOURCE_ALREADY_LOCKED: %s, %s", thread_state.Name(), thread_state.Owner().ID())
}
return nil, nil
})
SendUpdate(ctx, thread, NewSignal(thread, "thread_start"))
next_action := "start"
for next_action != "" {
action, exists := thread.Action(next_action)
if exists == false {
error_str := fmt.Sprintf("%s is not a valid action", next_action)
return errors.New(error_str)
}
ctx.Log.Logf("thread", "EVENT_ACTION: %s - %s", thread.ID(), next_action)
next_action, err = action(ctx, thread)
if err != nil {
return err
}
}
SendUpdate(ctx, thread, NewSignal(thread, "thread_done"))
ctx.Log.Logf("thread", "EVENT_RUN_DONE: %s", thread.ID())
return nil
}
type ThreadAction func(* GraphContext, Thread)(string, error)
type ThreadActions map[string]ThreadAction
type ThreadHandler func(* GraphContext, Thread, GraphSignal)(string, error)
type ThreadHandlers map[string]ThreadHandler
// Thread is the most basic thread that can exist in the thread tree.
// On start it automatically transitions to completion.
// This node by itself doesn't implement any special behaviours for children, so they will be ignored.
// When started, this thread automatically transitions to completion
type BaseThread struct {
BaseLockable
Actions ThreadActions
Handlers ThreadHandlers
timeout <-chan time.Time
timeout_action string
}
func (thread * BaseThread) Lock(node GraphNode, state LockableState) error {
return nil
}
func (thread * BaseThread) Unlock(node GraphNode, state LockableState) error {
return nil
}
func (thread * BaseThread) Action(action string) (ThreadAction, bool) {
action_fn, exists := thread.Actions[action]
return action_fn, exists
}
func (thread * BaseThread) Handler(signal_type string) (ThreadHandler, bool) {
handler, exists := thread.Handlers[signal_type]
return handler, exists
}
func (thread * BaseThread) TimeoutAction() string {
return thread.timeout_action
}
func (thread * BaseThread) Timeout() <-chan time.Time {
return thread.timeout
}
func (thread * BaseThread) ClearTimeout() {
thread.timeout_action = ""
thread.timeout = nil
}
func (thread * BaseThread) SetTimeout(end_time time.Time, action string) {
thread.timeout_action = action
thread.timeout = time.After(time.Until(end_time))
}
var ThreadDefaultStart = func(ctx * GraphContext, thread Thread) (string, error) {
ctx.Log.Logf("thread", "THREAD_DEFAUL_START: %s", thread.ID())
return "wait", nil
}
var ThreadWait = func(ctx * GraphContext, thread Thread) (string, error) {
ctx.Log.Logf("thread", "THREAD_WAIT: %s TIMEOUT: %+v", thread.ID(), thread.Timeout())
for {
select {
case signal := <- thread.SignalChannel():
ctx.Log.Logf("thread", "THREAD_SIGNAL: %s %+v", thread.ID(), signal)
if signal.Source() == thread.ID() {
ctx.Log.Logf("thread", "THREAD_SIGNAL_INTERNAL")
continue
}
signal_fn, exists := thread.Handler(signal.Type())
if exists == true {
ctx.Log.Logf("thread", "THREAD_HANDLER: %s - %s", thread.ID(), signal.Type())
return signal_fn(ctx, thread, signal)
}
case <- thread.Timeout():
ctx.Log.Logf("thread", "THREAD_TIMEOUT %s - NEXT_STATE: %s", thread.ID(), thread.TimeoutAction())
return thread.TimeoutAction(), nil
}
}
return "wait", nil
}
var ThreadAbort = func(ctx * GraphContext, thread Thread, signal GraphSignal) (string, error) {
return "", fmt.Errorf("%s aborted by signal from %s", thread.ID(), signal.Source())
}
var ThreadCancel = func(ctx * GraphContext, thread Thread, signal GraphSignal) (string, error) {
return "", nil
}
func NewBaseThreadState(name string) BaseThreadState {
return BaseThreadState{
BaseLockableState: NewBaseLockableState(name),
children: []Thread{},
child_info: map[NodeID]ThreadInfo{},
parent: nil,
}
}
func NewBaseThread(ctx * GraphContext, actions ThreadActions, handlers ThreadHandlers, state ThreadState) (BaseThread, error) {
thread := BaseThread{
BaseLockable: BaseLockable{BaseNode: NewNode(ctx, RandID(), state)},
Actions: ThreadActions{
"wait": ThreadWait,
"start": ThreadDefaultStart,
},
Handlers: ThreadHandlers{
"abort": ThreadAbort,
"cancel": ThreadCancel,
},
timeout: nil,
timeout_action: "",
}
for key, fn := range(actions) {
thread.Actions[key] = fn
}
for key, fn := range(handlers) {
thread.Handlers[key] = fn
}
return thread, nil
}
func NewSimpleBaseThread(ctx * GraphContext, name string, requirements []Lockable, actions ThreadActions, handlers ThreadHandlers) (* BaseThread, error) {
state := NewBaseThreadState(name)
thread, err := NewBaseThread(ctx, actions, handlers, &state)
if err != nil {
return nil, err
}
thread_ptr := &thread
err = LinkLockables(ctx, thread_ptr, requirements)
if err != nil {
return nil, err
}
return thread_ptr, nil
}

@ -1,76 +0,0 @@
package graphvent
import (
"testing"
"time"
"encoding/json"
"fmt"
)
func TestNewEvent(t * testing.T) {
ctx := testContext(t)
t1, err := NewSimpleBaseThread(ctx, "Test thread 1", []Lockable{}, ThreadActions{}, ThreadHandlers{})
fatalErr(t, err)
go func(thread Thread) {
time.Sleep(10*time.Millisecond)
SendUpdate(ctx, t1, CancelSignal(nil))
}(t1)
err = RunThread(ctx, t1)
fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{t1}, func(states []NodeState) (interface{}, error) {
ser, err := json.MarshalIndent(states, "", " ")
fatalErr(t, err)
fmt.Printf("\n%s\n", ser)
return nil, nil
})
}
func TestEventWithRequirement(t * testing.T) {
ctx := logTestContext(t, []string{"lockable", "thread"})
l1, err := NewLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err)
t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{l1}, ThreadActions{}, ThreadHandlers{})
fatalErr(t, err)
go func (thread Thread) {
time.Sleep(10*time.Millisecond)
_, err := UseStates(ctx, []GraphNode{l1}, func(states []NodeState) (interface{}, error) {
ser, err := json.MarshalIndent(states[0], "", " ")
fatalErr(t, err)
fmt.Printf("\n%s\n", ser)
return nil, nil
})
fatalErr(t, err)
SendUpdate(ctx, t1, CancelSignal(nil))
}(t1)
fatalErr(t, err)
err = RunThread(ctx, t1)
fatalErr(t, err)
_, err = UseStates(ctx, []GraphNode{l1}, func(states []NodeState) (interface{}, error) {
ser, err := json.MarshalIndent(states[0], "", " ")
fatalErr(t, err)
fmt.Printf("\n%s\n", ser)
return nil, nil
})
fatalErr(t, err)
}
func TestCustomEventState(t * testing.T ) {
ctx := logTestContext(t, []string{"lockable", "thread"})
t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{}, ThreadActions{}, ThreadHandlers{})
fatalErr(t, err)
println(t1)
}