Compare commits

..

No commits in common. "master" and "graph-rework" have entirely different histories.

29 changed files with 2666 additions and 5615 deletions

3
.gitignore vendored

@ -1,15 +1,12 @@
# 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

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

@ -1,662 +0,0 @@
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,4 +1,16 @@
.PHONY: test BINARY_DIR=./bin
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:
clear && go test go test

@ -1,48 +0,0 @@
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

@ -1,284 +0,0 @@
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
}

@ -1,16 +0,0 @@
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,36 +1,32 @@
module github.com/mekkanized/graphvent module github.com/mekkanized/graphvent
go 1.22.0 go 1.20
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/stretchr/testify v1.8.2 // indirect github.com/rs/zerolog v1.29.1 // indirect
go.opencensus.io v0.22.5 // indirect go.opencensus.io v0.22.5 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.13.0 // indirect golang.org/x/sys v0.6.0 // indirect
) )

@ -1,8 +1,5 @@
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=
@ -17,17 +14,20 @@ 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,10 +51,15 @@ 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=
@ -64,6 +69,8 @@ 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=
@ -71,17 +78,16 @@ 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=
@ -90,29 +96,23 @@ 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,12 +142,11 @@ 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=
@ -171,6 +170,4 @@ 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=

804
gql.go

@ -1,81 +1,21 @@
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(`
@ -89,74 +29,57 @@ func GraphiQLHandler() func(http.ResponseWriter, *http.Request) {
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>GraphiQL</title> <title>GraphiQL</title>
<style> <style>
body { body {
height: 100%%; height: 100%%;
margin: 0; margin: 0;
width: 100%%; width: 100%%;
overflow: hidden; overflow: hidden;
} }
#graphiql { #graphiql {
height: 100vh; height: 100vh;
} }
</style> </style>
<!-- <!--
This GraphiQL example depends on Promise and fetch, which are available in This GraphiQL example depends on Promise and fetch, which are available in
modern browsers, but can be "polyfilled" for older browsers. modern browsers, but can be "polyfilled" for older browsers.
GraphiQL itself depends on React DOM. GraphiQL itself depends on React DOM.
If you do not want to rely on a CDN, you can host these files locally or If you do not want to rely on a CDN, you can host these files locally or
include them directly in your favored resource bundler. include them directly in your favored resource bundler.
--> -->
<script <script
crossorigin crossorigin
src="https://unpkg.com/react@18/umd/react.development.js" src="https://unpkg.com/react@18/umd/react.development.js"
></script> ></script>
<script <script
crossorigin crossorigin
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
></script> ></script>
<!-- <!--
These two files can be found in the npm module, however you may wish to These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your copy them directly into your environment, or perhaps include them in your
favored resource bundler. favored resource bundler.
--> -->
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" /> <link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
</head> </head>
<body> <body>
<div id="graphiql">Loading...</div> <div id="graphiql">Loading...</div>
<script <script
src="https://unpkg.com/graphiql/graphiql.min.js" src="https://unpkg.com/graphiql/graphiql.min.js"
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>
`) `)
@ -168,7 +91,7 @@ func GraphiQLHandler() func(http.ResponseWriter, *http.Request) {
} }
type GQLPayload struct { type GQLWSPayload 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"`
@ -179,69 +102,17 @@ type GQLPayload 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 GQLPayload `json:"payload,omitempty"` Payload GQLWSPayload `json:"payload,omitempty"`
} }
func enableCORS(w *http.ResponseWriter) { func enableCORS(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*") (*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Credentials", "true") (*w).Header().Set("Access-Control-Allow-Credentials", "true")
(*w).Header().Set("Access-Control-Allow-Headers", "*") (*w).Header().Set("Access-Control-Allow-Headers", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "*") (*w).Header().Set("Access-Control-Allow-Methods", "*")
}
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) { func GQLHandler(ctx * GraphContext, schema graphql.Schema, gql_ctx context.Context) 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)
@ -249,33 +120,19 @@ func GQLHandler(ctx *Context, server *Node, gql_ext *GQLExt) func(http.ResponseW
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", "GQL_READ_ERR: %s", err) ctx.Log.Logf("gql", "failed to read request body: %s", err)
json.NewEncoder(w).Encode(fmt.Sprintf("%e", err))
return return
} }
query := GQLPayload{} query := GQLWSPayload{}
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: req_ctx, Context: gql_ctx,
RequestString: query.Query, RequestString: query.Query,
} }
if query.OperationName != "" { if query.OperationName != "" {
@ -284,13 +141,12 @@ func GQLHandler(ctx *Context, server *Node, gql_ext *GQLExt) func(http.ResponseW
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_errors", extra_fields, "wrong result, unexpected errors: %v", result.Errors) ctx.Log.Logm("gql", extra_fields, "wrong result, unexpected errors: %v", result.Errors)
} }
json.NewEncoder(w).Encode(result) json.NewEncoder(w).Encode(result)
} }
@ -305,6 +161,7 @@ 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),
@ -313,7 +170,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 err.Error() return ""
} }
for _, node := range AST.Definitions { for _, node := range AST.Definitions {
@ -327,36 +184,33 @@ func getOperationTypeOfReq(p graphql.Params) string{
} }
} }
} }
return "END_OF_FUNCTION" return ""
} }
func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.ResponseWriter, *http.Request) { 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) {
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))
if string(protocol) == "graphql-transport-ws" || string(protocol) == "graphql-ws" { return string(protocol) == "graphql-transport-ws"
return true
}
return false
}, },
} }
conn, _, _, err := u.Upgrade(r, w) conn, _, _, err := u.Upgrade(r, w)
@ -364,6 +218,7 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
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{}
@ -377,19 +232,6 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
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 {
@ -401,14 +243,12 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
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: req_ctx, Context: gql_ctx,
RequestString: msg.Payload.Query, RequestString: msg.Payload.Query,
} }
if msg.Payload.OperationName != "" { if msg.Payload.OperationName != "" {
@ -418,15 +258,7 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
params.VariableValues = msg.Payload.Variables params.VariableValues = msg.Payload.Variables
} }
var res_chan chan *graphql.Result res_chan := GQLWSDo(ctx, params)
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 {
@ -442,7 +274,7 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
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("gql_errors", extra_fields, "ERROR: wrong result, unexpected errors: %+v", next.Errors) ctx.Log.Logm("gqlws", 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)
@ -454,7 +286,7 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
msg, err := json.Marshal(GQLWSMsg{ msg, err := json.Marshal(GQLWSMsg{
ID: msg.ID, ID: msg.ID,
Type: "next", Type: "next",
Payload: GQLPayload{ Payload: GQLWSPayload{
Data: string(data), Data: string(data),
}, },
}) })
@ -480,373 +312,195 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
} }
} }
type Interface struct { type ObjTypeMap map[reflect.Type]*graphql.Object
Interface *graphql.Interface type FieldMap map[string]*graphql.Field
Default *graphql.Object
List *graphql.List
Extensions []ExtType
}
type Type struct {
Type *graphql.Object
List *graphql.List
}
type Field struct {
Ext ExtType
Name string
Field *graphql.Field
}
type NodeResult struct {
NodeID NodeID
NodeType NodeType
Data map[string]interface{}
}
type ListField struct { type GQLThread struct {
ACLName string BaseThread
Extension ExtType extended_types ObjTypeMap
ResolveFn func(graphql.ResolveParams, *ResolveContext, reflect.Value) ([]NodeID, error) extended_queries FieldMap
extended_subscriptions FieldMap
extended_mutations FieldMap
} }
type SelfField struct { type GQLThreadState struct {
ACLName string BaseThreadState
Extension ExtType Listen string
ResolveFn func(graphql.ResolveParams, *ResolveContext, reflect.Value) (*NodeID, error)
} }
type SubscriptionInfo struct { func NewGQLThreadState(listen string) GQLThreadState {
ID uuid.UUID return GQLThreadState{
NodeCache *map[NodeID]NodeResult BaseThreadState: NewBaseThreadState("GQL Server"),
Channel chan interface{} Listen: listen,
}
} }
type GQLExt struct { var gql_actions ThreadActions = ThreadActions{
tcp_listener net.Listener "start": func(ctx * GraphContext, thread Thread) (string, error) {
http_server *http.Server ctx.Log.Logf("gql", "SERVER_STARTED")
http_done sync.WaitGroup server := thread.(*GQLThread)
go func() {
subscriptions []SubscriptionInfo ctx.Log.Logf("gql", "GOROUTINE_START for %s", server.ID())
subscriptions_lock sync.RWMutex
mux := http.NewServeMux()
// map of read request IDs to response channels http_handler, ws_handler := MakeGQLHandlers(ctx, server)
resolver_response map[uuid.UUID]chan Signal mux.HandleFunc("/gql", http_handler)
resolver_response_lock sync.RWMutex 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){
server_state := states[0].(*GQLThreadState)
return &http.Server{
Addr: server_state.Listen,
Handler: mux,
}, nil
})
srv := srv_if.(*http.Server)
http_done := &sync.WaitGroup{}
http_done.Add(1)
go func(srv *http.Server, http_done *sync.WaitGroup) {
defer http_done.Done()
err := srv.ListenAndServe()
if err != http.ErrServerClosed {
panic(fmt.Sprintf("Failed to start gql server: %s", err))
}
}(srv, http_done)
State string `gv:"state"` for true {
TLSKey []byte `gv:"tls_key"` select {
TLSCert []byte `gv:"tls_cert"` case signal:=<-server.signal:
Listen string `gv:"listen" gql:"GQLListen"` if signal.Type() == "abort" || signal.Type() == "cancel" {
err := srv.Shutdown(context.Background())
if err != nil{
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
}
}
}()
return "wait", nil
},
} }
func (ext *GQLExt) Load(ctx *Context, node *Node) error { var gql_handlers ThreadHandlers = ThreadHandlers{
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) { func NewGQLThread(ctx * GraphContext, listen string, requirements []Lockable, extended_types ObjTypeMap, extended_queries FieldMap, extended_mutations FieldMap, extended_subscriptions FieldMap) (*GQLThread, error) {
ctx.Log.Logf("gql", "Unloading GQL server extension on %s", node.ID) state := NewGQLThreadState(listen)
err := ext.StopGQLServer() base_thread, err := NewBaseThread(ctx, gql_actions, gql_handlers, &state)
if err != nil { if err != nil {
ctx.Log.Logf("gql", "Error unloading GQL server extension on %s: %s", node.ID, err) return nil, err
} else {
ctx.Log.Logf("gql", "Unloaded GQL server extension on %s", node.ID)
} }
}
func (ext *GQLExt) AddSubscription(id uuid.UUID, ctx *ResolveContext) (chan interface{}, error) {
ext.subscriptions_lock.Lock()
defer ext.subscriptions_lock.Unlock()
for _, info := range(ext.subscriptions) { thread := &GQLThread {
if info.ID == id { BaseThread: base_thread,
return nil, fmt.Errorf("%+v already in subscription list", info.ID) extended_types: extended_types,
} extended_queries: extended_queries,
extended_mutations: extended_mutations,
extended_subscriptions: extended_subscriptions,
} }
c := make(chan interface{}, 100) err = LinkLockables(ctx, thread, requirements)
if err != nil {
ext.subscriptions = append(ext.subscriptions, SubscriptionInfo{ return nil, err
id,
&ctx.NodeCache,
c,
})
return c, nil
}
func (ext *GQLExt) RemoveSubscription(id uuid.UUID) error {
ext.subscriptions_lock.Lock()
defer ext.subscriptions_lock.Unlock()
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 thread, 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 { func MakeGQLHandlers(ctx * GraphContext, server * GQLThread) (func(http.ResponseWriter, *http.Request), func(http.ResponseWriter, *http.Request)) {
response_chan := ext.FindResponseChannel(req_id) valid_nodes := map[reflect.Type]*graphql.Object{}
valid_lockables := map[reflect.Type]*graphql.Object{}
if response_chan != nil { valid_threads := map[reflect.Type]*graphql.Object{}
ext.resolver_response_lock.Lock() valid_lockables[reflect.TypeOf((*BaseLockable)(nil))] = GQLTypeBaseNode()
delete(ext.resolver_response, req_id) for t, v := range(valid_lockables) {
ext.resolver_response_lock.Unlock() valid_nodes[t] = v
} }
return response_chan valid_threads[reflect.TypeOf((*BaseThread)(nil))] = GQLTypeBaseThread()
} valid_threads[reflect.TypeOf((*GQLThread)(nil))] = GQLTypeGQLThread()
for t, v := range(valid_threads) {
func (ext *GQLExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]Message, Changes) { valid_lockables[t] = v
// Process ReadResultSignalType by forwarding it to the waiting resolver valid_nodes[t] = v
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 {
case response_chan <- sig:
ctx.Log.Logf("gql", "Forwarded success to resolver, %+v", sig.ReqID)
default:
ctx.Log.Logf("gql", "Resolver channel overflow %+v", sig)
}
}
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)
}
}
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 ecdsa_curves = map[uint8]elliptic.Curve{
0: elliptic.P256(),
}
var ecdsa_curve_ids = map[elliptic.Curve]uint8{
elliptic.P256(): 0,
}
var ecdh_curves = map[uint8]ecdh.Curve{
0: ecdh.P256(),
}
var ecdh_curve_ids = map[ecdh.Curve]uint8{ gql_types := []graphql.Type{GQLTypeSignal(), GQLTypeSignalInput()}
ecdh.P256(): 0, for _, v := range(valid_nodes) {
} gql_types = append(gql_types, v)
}
func NewGQLExt(ctx *Context, listen string, tls_cert []byte, tls_key []byte) (*GQLExt, error) { node_type := reflect.TypeOf((GraphNode)(nil))
if tls_cert == nil || tls_key == nil { lockable_type := reflect.TypeOf((Lockable)(nil))
ssl_key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) thread_type := reflect.TypeOf((Thread)(nil))
if err != nil {
return nil, err
}
ssl_key_bytes, err := x509.MarshalPKCS8PrivateKey(ssl_key) for go_t, gql_t := range(server.extended_types) {
if err != nil { if go_t.Implements(node_type) {
return nil, err valid_nodes[go_t] = gql_t
} }
if go_t.Implements(lockable_type) {
ssl_key_pem := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: ssl_key_bytes}) valid_lockables[go_t] = gql_t
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, _ := rand.Int(rand.Reader, serialNumberLimit)
notBefore := time.Now()
notAfter := notBefore.Add(365*24*time.Hour)
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,
} }
if go_t.Implements(thread_type) {
ssl_cert, err := x509.CreateCertificate(rand.Reader, &template, &template, ssl_key.Public(), ssl_key) valid_threads[go_t] = gql_t
if err != nil {
return nil, err
} }
gql_types = append(gql_types, gql_t)
ssl_cert_pem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ssl_cert})
tls_cert = ssl_cert_pem
tls_key = ssl_key_pem
} }
return &GQLExt{ gql_queries := graphql.Fields{
Listen: listen, "Self": GQLQuerySelf(),
resolver_response: map[uuid.UUID]chan Signal{},
subscriptions: []SubscriptionInfo{},
TLSCert: tls_cert,
TLSKey: tls_key,
}, nil
}
// Returns "${base}/${path}" if it's a file or "${base}/${path}/index.html" if it's a directory
// Returns os.ErrInvalid if "${base}/${path}/index.html" is a directory
func getContentPath(base string, path string) (string, error) {
full_path := fmt.Sprintf("%s%s", base, path)
path_info, err := os.Stat(full_path)
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)
}
index_info, err := os.Stat(index_path)
if err != nil {
return "", err
} else if index_info.IsDir() == true {
return index_path, os.ErrInvalid
} else {
return index_path, nil
}
} else {
return full_path, nil
} }
}
func (ext *GQLExt) StartGQLServer(ctx *Context, node *Node) error { for key, value := range(server.extended_queries) {
if ext.tcp_listener != nil || ext.http_server != nil { gql_queries[key] = value
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))
mux.HandleFunc("/graphiql", GraphiQLHandler())
// Server the ./site directory to /site (TODO make configurable with better defaults)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
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)
}
})
http_server := &http.Server{ gql_subscriptions := graphql.Fields{
Addr: ext.Listen, "Update": GQLSubscriptionUpdate(),
Handler: mux,
} }
l, err := net.Listen("tcp", http_server.Addr) for key, value := range(server.extended_subscriptions) {
if err != nil { gql_subscriptions[key] = value
return fmt.Errorf("Failed to start listener for server on %s", http_server.Addr)
} }
ext.http_done.Add(1) gql_mutations := graphql.Fields{
go func(qql_ext *GQLExt) { "SendUpdate": GQLMutationSendUpdate(),
defer ext.http_done.Done() }
err := http_server.Serve(l)
if err != http.ErrServerClosed {
panic(fmt.Sprintf("Failed to start gql server: %s", err))
}
}(ext)
for key, value := range(server.extended_mutations) {
gql_mutations[key] = value
}
ext.tcp_listener = l schemaConfig := graphql.SchemaConfig{
ext.http_server = http_server Types: gql_types,
ext.State = "running" Query: graphql.NewObject(graphql.ObjectConfig{
return nil Name: "Query",
} Fields: gql_queries,
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: gql_mutations,
}),
Subscription: graphql.NewObject(graphql.ObjectConfig{
Name: "Subscription",
Fields: gql_subscriptions,
}),
}
func (ext *GQLExt) StopGQLServer() error { schema, err := graphql.NewSchema(schemaConfig)
if ext.tcp_listener == nil || ext.http_server == nil { if err != nil{
return fmt.Errorf("already shutdown, cannot shut down again" ) panic(err)
} }
ext.http_server.Shutdown(context.TODO()) gql_ctx := context.Background()
ext.http_done.Wait() gql_ctx = context.WithValue(gql_ctx, "valid_nodes", valid_nodes)
ext.tcp_listener = nil gql_ctx = context.WithValue(gql_ctx, "valid_lockables", valid_lockables)
ext.http_server = nil gql_ctx = context.WithValue(gql_ctx, "valid_threads", valid_threads)
ext.State = "stopped" gql_ctx = context.WithValue(gql_ctx, "gql_server", server)
return nil gql_ctx = context.WithValue(gql_ctx, "graph_context", ctx)
return GQLHandler(ctx, schema, gql_ctx), GQLWSHandler(ctx, schema, gql_ctx)
} }

@ -0,0 +1,812 @@
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
}

@ -1,147 +0,0 @@
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,223 +1,20 @@
package graphvent package graphvent
import ( import (
"bytes" "testing"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"reflect"
"testing"
"time"
"github.com/google/uuid"
"golang.org/x/net/websocket"
) )
func TestGQLSubscribe(t *testing.T) { func TestGQLThread(t * testing.T) {
ctx := logTestContext(t, []string{"test", "gql"}) ctx := testContext(t)
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) fatalErr(t, err)
switch response := response.(type) {
case *SuccessSignal:
default:
t.Fatalf("Wrong response: %s", reflect.TypeOf(response))
}
resp_3 := SendGQL(req_1) test_thread, err := NewSimpleBaseThread(ctx, "Test thread 1", []Lockable{}, ThreadActions{}, ThreadHandlers{})
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)
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
err = ctx.Stop()
fatalErr(t, err) fatalErr(t, err)
gql_loaded, err := ctx.GetNode(gql.ID) err = LinkThreads(ctx, gql_thread, test_thread, nil)
fatalErr(t, err) fatalErr(t, err)
listener_ext, err = GetExt[ListenerExt](gql_loaded) err = RunThread(ctx, gql_thread)
fatalErr(t, err) fatalErr(t, err)
} }

@ -0,0 +1,432 @@
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,52 +2,76 @@ package graphvent
import ( import (
"testing" "testing"
"runtime/debug" "fmt"
"time" "time"
"runtime/pprof"
"os"
badger "github.com/dgraph-io/badger/v3" badger "github.com/dgraph-io/badger/v3"
) )
func NewSimpleListener(ctx *Context, buffer int) (*Node, *ListenerExt, error) { type GraphTester testing.T
listener_extension := NewListenerExt(buffer) const listner_timeout = 50 * time.Millisecond
listener, err := ctx.NewNode(nil, "LockableNode", nil, listener_extension, NewLockableExt(nil))
return listener, listener_extension, err func (t * GraphTester) WaitForValue(ctx * GraphContext, listener chan GraphSignal, signal_type string, source GraphNode, timeout time.Duration, str string) GraphSignal {
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) *Context { func logTestContext(t * testing.T, components []string) * GraphContext {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true).WithSyncWrites(true)) db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, err := NewContext(&BadgerDB{ return NewGraphContext(db, NewConsoleLogger(components))
DB: db,
}, NewConsoleLogger(components))
fatalErr(t, err)
return ctx
} }
func testContext(t * testing.T) * Context { func testContext(t * testing.T) * GraphContext {
return logTestContext(t, []string{}) db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
t.Fatal(err)
}
return NewGraphContext(db, NewConsoleLogger([]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
}

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

@ -2,147 +2,309 @@ package graphvent
import ( import (
"testing" "testing"
"fmt"
"encoding/json"
"time" "time"
) )
func TestLink(t *testing.T) { func TestNewLockable(t * testing.T) {
ctx := logTestContext(t, []string{"lockable", "listener"}) ctx := testContext(t)
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err)
l2_listener := NewListenerExt(10) _, err = NewLockable(ctx, "Test lockable 2", []Lockable{r1})
l2, err := ctx.NewNode(nil, "LockableNode", l2_listener, NewLockableExt(nil))
fatalErr(t, err) fatalErr(t, err)
}
func TestRepeatedChildLockable(t * testing.T) {
ctx := testContext(t)
l1_lockable := NewLockableExt(nil) r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
l1_listener := NewListenerExt(10)
l1, err := ctx.NewNode(nil, "LockableNode", l1_listener, l1_lockable)
fatalErr(t, err) fatalErr(t, err)
link_signal := NewLinkSignal("add", l2.ID) _, err = NewLockable(ctx, "Test lockable 2", []Lockable{r1, r1})
msgs := []Message{{l1.ID, link_signal}} if err == nil {
err = ctx.Send(l1, msgs) t.Fatal("Added the same lockable as a child twice to the same lockable")
}
}
func TestLockableSelfLock(t * testing.T) {
ctx := testContext(t)
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
_, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, link_signal.ID()) err = LockLockable(ctx, r1, r1, nil)
fatalErr(t, err) fatalErr(t, err)
state, exists := l1_lockable.Requirements[l2.ID] _, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) {
if exists == false { owner_id := states[0].(LockableState).Owner().ID()
t.Fatal("l2 not in l1 requirements") if owner_id != r1.ID() {
} else if state != Unlocked { return nil, fmt.Errorf("r1 is owned by %s instead of self", owner_id)
t.Fatalf("l2 in bad requirement state in l1: %+v", state) }
} return nil, nil
})
fatalErr(t, err)
unlink_signal := NewLinkSignal("remove", l2.ID) err = UnlockLockable(ctx, r1, r1, nil)
msgs = []Message{{l1.ID, unlink_signal}}
err = ctx.Send(l1, msgs)
fatalErr(t, err) fatalErr(t, err)
_, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, unlink_signal.ID()) _, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) {
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 Test10Lock(t *testing.T) { func TestLockableSelfLockTiered(t * testing.T) {
testLockN(t, 10) ctx := testContext(t)
}
func Test100Lock(t *testing.T) { r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
testLockN(t, 100) fatalErr(t, err)
}
func Test1000Lock(t *testing.T) { r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{})
testLockN(t, 1000) 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)
func Test10000Lock(t *testing.T) { err = UnlockLockable(ctx, r3, r3, nil)
testLockN(t, 10000) 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 testLockN(t *testing.T, n int) { func TestLockableLockOther(t * testing.T) {
ctx := logTestContext(t, []string{"test"}) ctx := testContext(t)
NewLockable := func()(*Node) { r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
l, err := ctx.NewNode(nil, "LockableNode", NewLockableExt(nil)) 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) fatalErr(t, err)
return l return []NodeState{node_state}, nil, nil
} })
fatalErr(t, err)
reqs := make([]NodeID, n) _, err = UseStates(ctx, []GraphNode{r1}, func(states []NodeState) (interface{}, error) {
for i := range(reqs) { owner_id := states[0].(LockableState).Owner().ID()
new_lockable := NewLockable() if owner_id != r2.ID() {
reqs[i] = new_lockable.ID return nil, fmt.Errorf("r1 is owned by %s instead of r2", owner_id)
} }
ctx.Log.Logf("test", "CREATED_%d", n)
listener := NewListenerExt(50000) return nil, nil
node, err := ctx.NewNode(nil, "LockableNode", listener, NewLockableExt(reqs)) })
fatalErr(t, err) fatalErr(t, err)
ctx.Log.Logf("test", "CREATED_LISTENER")
lock_id, err := LockLockable(ctx, node) _, 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) fatalErr(t, err)
response, _, err := WaitForResponse(listener.Chan, time.Second*60, lock_id) _, 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) fatalErr(t, err)
switch resp := response.(type) { }
case *SuccessSignal:
default: func TestLockableLockSimpleConflict(t * testing.T) {
t.Fatalf("Unexpected response to lock - %s", resp) ctx := testContext(t)
}
r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err)
r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{})
fatalErr(t, err)
err = LockLockable(ctx, r1, r1, nil)
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)
if err == nil {
t.Fatal("r2 took r1's lock from itself")
}
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 != r1.ID() {
return nil, fmt.Errorf("r1 is owned by %s instead of r1", owner_id)
}
return nil, nil
})
fatalErr(t, err)
err = UnlockLockable(ctx, r1, r1, 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 r1", owner.ID())
}
return nil, nil
})
fatalErr(t, err)
ctx.Log.Logf("test", "LOCKED_%d", n)
} }
func TestLock(t *testing.T) { func TestLockableLockTieredConflict(t * testing.T) {
ctx := logTestContext(t, []string{"test", "lockable"}) ctx := testContext(t)
NewLockable := func(reqs []NodeID)(*Node, *ListenerExt) { r1, err := NewLockable(ctx, "Test lockable 1", []Lockable{})
listener := NewListenerExt(10000) fatalErr(t, err)
l, err := ctx.NewNode(nil, "LockableNode", listener, NewLockableExt(reqs))
fatalErr(t, err) r2, err := NewLockable(ctx, "Test lockable 2", []Lockable{r1})
return l, listener 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")
} }
}
l2, _ := NewLockable(nil) func TestLockableSimpleUpdate(t * testing.T) {
l3, _ := NewLockable(nil) ctx := testContext(t)
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})
ctx.Log.Logf("test", "l0: %s", l0.ID) l1, err := NewLockable(ctx, "Test Lockable 1", []Lockable{})
ctx.Log.Logf("test", "l1: %s", l1.ID) fatalErr(t, err)
ctx.Log.Logf("test", "l2: %s", l2.ID)
ctx.Log.Logf("test", "l3: %s", l3.ID)
ctx.Log.Logf("test", "l4: %s", l4.ID)
ctx.Log.Logf("test", "l5: %s", l5.ID)
ctx.Log.Logf("test", "locking l0") update_channel := l1.UpdateChannel(0)
id_1, err := LockLockable(ctx, l0)
go func() {
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{})
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)
ctx.Log.Logf("test", "locking l1") _, err = NewLockable(ctx, "Test Lockable 3", []Lockable{l2})
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)
ctx.Log.Logf("test", "unlocking l0") l2, err := NewLockable(ctx, "Test Lockable 2", []Lockable{l1})
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)
ctx.Log.Logf("test", "locking l1") update_channel := l3.UpdateChannel(0)
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")
} }

@ -1,97 +0,0 @@
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...)
}

@ -1,68 +0,0 @@
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
}

@ -1,35 +0,0 @@
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")
}

@ -1,401 +0,0 @@
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)
}

@ -1,55 +0,0 @@
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)
}

@ -1,744 +0,0 @@
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)
}
}

@ -1,176 +0,0 @@
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)
}

@ -1,261 +0,0 @@
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,
}
}

@ -0,0 +1,413 @@
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
}

@ -0,0 +1,76 @@
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)
}