diff --git a/cmd/proxy.go b/cmd/proxy.go index b9c5427..0444807 100644 --- a/cmd/proxy.go +++ b/cmd/proxy.go @@ -12,7 +12,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/vulcanize/gap-filler/pkg/mux" - "github.com/vulcanize/gap-filler/pkg/qlservices" ) var ( @@ -40,22 +39,12 @@ var ( return err } - rpcBalancer, err := qlservices.NewBalancer(rpcClients) - if err != nil { - return err - } - tracingClients, err := parseRpcAddresses(viper.GetString("rpc.tracing")) if err != nil { logrus.Error("bad rpc.tracing addresses") return err } - tracingBalancer, err := qlservices.NewBalancer(tracingClients) - if err != nil { - return err - } - router, err := mux.NewServeMux(&mux.Options{ BasePath: viper.GetString("http.path"), EnableGraphiQL: viper.GetBool("gql.gui"), @@ -64,8 +53,8 @@ var ( TracingAPI: gqlTracingAPIAddr, }, RPC: mux.RPCOptions{ - DefaultBalancer: rpcBalancer, - TracingBalancer: tracingBalancer, + DefaultClients: rpcClients, + TracingClients: tracingClients, }, }) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 1119494..b6e95ff 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,10 +1,11 @@ package cmd import ( + "strings" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" - "strings" ) var ( diff --git a/go.sum b/go.sum index 297a4eb..189aa1c 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,7 @@ github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxB github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN3SVSiiO77gL2j2ronKKP0syM= github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -34,6 +35,7 @@ github.com/VictoriaMetrics/fastcache v1.5.3 h1:2odJnXLbFZcoV9KYtQ+7TH1UOq3dn3Ass github.com/VictoriaMetrics/fastcache v1.5.3/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/aristanetworks/goarista v0.0.0-20170210015632-ea17b1a17847 h1:rtI0fD4oG/8eVokGVPYJEW1F88p1ZNgXiEIs9thEE4A= github.com/aristanetworks/goarista v0.0.0-20170210015632-ea17b1a17847/go.mod h1:D/tb0zPVXnP7fmsLZjtdUhSsumbK/ij54UXjjVgMGxQ= @@ -47,6 +49,7 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6 h1:Eey/GGQ/E5Xp1P2Lyx1qj007hLZfbi0+CoVeJruGCtI= github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6/go.mod h1:Dmm/EzmjnCiweXmzRIAiUWCInVmPgjkzgv5k4tVyXiQ= +github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -88,8 +91,10 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqG github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E= github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8= @@ -121,6 +126,7 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= @@ -154,6 +160,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3 h1:DqD8eigqlUm0+znmx7zhL0xvTW3+e1jCekJMfBUADWI= github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag= @@ -168,6 +175,7 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.1.1-0.20170430222011-975b5c4c7c21/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -176,9 +184,12 @@ github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356/go.mod h1:Od972xHfMJo github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 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/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= @@ -215,7 +226,9 @@ github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXW github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c h1:1RHs3tNxjXGHeul8z2t6H2N2TlAqpKe5yryJztRx4Jk= github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= @@ -227,6 +240,7 @@ github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssy github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -255,7 +269,9 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -283,6 +299,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 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 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= @@ -418,8 +435,10 @@ google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiq google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= @@ -428,6 +447,7 @@ gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHN gopkg.in/olebedev/go-duktape.v3 v3.0.0-20190213234257-ec84240a7772/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0= gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0= diff --git a/pkg/mux/options.go b/pkg/mux/options.go index 6aea20c..45cce92 100644 --- a/pkg/mux/options.go +++ b/pkg/mux/options.go @@ -1,8 +1,9 @@ package mux import ( - "github.com/vulcanize/gap-filler/pkg/qlservices" "net/url" + + "github.com/ethereum/go-ethereum/rpc" ) type PostgraphileOptions struct { @@ -11,8 +12,8 @@ type PostgraphileOptions struct { } type RPCOptions struct { - DefaultBalancer qlservices.Balancer - TracingBalancer qlservices.Balancer + DefaultClients []*rpc.Client + TracingClients []*rpc.Client } // Options configurations for proxy service diff --git a/pkg/mux/package.go b/pkg/mux/package.go index 7ecd2e3..1fb50a5 100644 --- a/pkg/mux/package.go +++ b/pkg/mux/package.go @@ -22,8 +22,8 @@ func NewServeMux(opts *Options) (*http.ServeMux, error) { mux.Handle(path.Join(opts.BasePath, "/graphql"), proxy.New(&proxy.Options{ RPC: proxy.RPCOptions{ - DefaultBalancer: opts.RPC.DefaultBalancer, - TracingBalancer: opts.RPC.TracingBalancer, + DefaultClients: opts.RPC.DefaultClients, + TracingClients: opts.RPC.TracingClients, }, Postgraphile: proxy.PostgraphileOptions{ Default: opts.Postgraphile.Default, diff --git a/pkg/proxy/package.go b/pkg/proxy/package.go index 4ca8529..2765b39 100644 --- a/pkg/proxy/package.go +++ b/pkg/proxy/package.go @@ -4,6 +4,8 @@ import ( "net/http" "net/url" + "github.com/ethereum/go-ethereum/rpc" + "github.com/vulcanize/gap-filler/pkg/qlservices" ) @@ -19,8 +21,8 @@ type PostgraphileOptions struct { } type RPCOptions struct { - DefaultBalancer qlservices.Balancer - TracingBalancer qlservices.Balancer + DefaultClients []*rpc.Client + TracingClients []*rpc.Client } type Options struct { @@ -33,8 +35,8 @@ func New(opts *Options) *Proxy { return &Proxy{ wsProxy: NewWebsocketReverseProxy(opts.Postgraphile.Default), httpProxy: NewHTTPReverseProxy(opts). - Register(qlservices.NewEthHeaderCidByBlockNumberService(opts.RPC.DefaultBalancer)). - Register(qlservices.NewGetGraphCallByTxHashService(opts.RPC.TracingBalancer)), + Register(qlservices.NewEthHeaderCidByBlockNumberService(opts.RPC.DefaultClients)). + Register(qlservices.NewGetGraphCallByTxHashService(opts.RPC.TracingClients)), } } diff --git a/pkg/qlservices/balancer.go b/pkg/qlservices/balancer.go deleted file mode 100644 index ed80a45..0000000 --- a/pkg/qlservices/balancer.go +++ /dev/null @@ -1,46 +0,0 @@ -package qlservices - -import ( - "errors" - "github.com/ethereum/go-ethereum/rpc" - "sync" -) - -var ( - //ErrNoAvailableItem no item is available - ErrNoAvailableItem = errors.New("no item is available") -) - -// Balancer is an interface for representing round-robin for JSON-RPC client -type Balancer interface { - Next() *rpc.Client -} - -// Balancer RoundRobin instance -type PRCBalancer struct { - m sync.Mutex - - index int - items []*rpc.Client -} - -func NewBalancer(items []*rpc.Client) (Balancer, error) { - if len(items) == 0 { - return nil, ErrNoAvailableItem - } - - return &PRCBalancer{ - items: items, - }, nil -} - -// Next returns index address -func (b *PRCBalancer) Next() *rpc.Client { - b.m.Lock() - defer b.m.Unlock() - - item := b.items[b.index] - b.index = (b.index + 1) % len(b.items) - - return item -} diff --git a/pkg/qlservices/balancer_test.go b/pkg/qlservices/balancer_test.go deleted file mode 100644 index 1e683d7..0000000 --- a/pkg/qlservices/balancer_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package qlservices - -import ( - "github.com/ethereum/go-ethereum/rpc" - "sync" - "testing" -) - -func TestRoundrobin(t *testing.T) { - addresses := []string{ - "http://127.0.0.1:8545", - "http://127.0.0.2:8545", - "http://127.0.0.3:8545", - "http://127.0.0.4:8545", - "http://127.0.0.5:8545", - } - clients := make([]*rpc.Client, 0, len(addresses)) - for _, address := range addresses { - rpcClient, err := rpc.Dial(address) - if err != nil { - t.Error(err) - } - - clients = append(clients, rpcClient) - } - mu := new(sync.Mutex) - wg := sync.WaitGroup{} - balancer, err := NewBalancer(clients) - if err != nil { - t.Error(err) - } - - for i := 0; i < 1000; i++ { - wg.Add(1) - go func() { - balancer.Next() - mu.Lock() - defer mu.Unlock() - wg.Done() - }() - } - wg.Wait() -} diff --git a/pkg/qlservices/ethHeaderCidByBlockNumber.go b/pkg/qlservices/ethHeaderCidByBlockNumber.go index 59d9056..5555ab7 100644 --- a/pkg/qlservices/ethHeaderCidByBlockNumber.go +++ b/pkg/qlservices/ethHeaderCidByBlockNumber.go @@ -6,25 +6,28 @@ import ( "math/big" "time" + "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/statediff" "github.com/graphql-go/graphql/language/ast" "github.com/sirupsen/logrus" "github.com/valyala/fastjson" ) +var stateDiffMethod = "statediff_writeStateDiffAt" + type EthHeaderCidByBlockNumberService struct { - balancer Balancer + clients []*rpc.Client } -func NewEthHeaderCidByBlockNumberService(balancer Balancer) *EthHeaderCidByBlockNumberService { - return &EthHeaderCidByBlockNumberService{balancer} +func NewEthHeaderCidByBlockNumberService(clients []*rpc.Client) *EthHeaderCidByBlockNumberService { + return &EthHeaderCidByBlockNumberService{clients: clients} } func (srv *EthHeaderCidByBlockNumberService) Name() string { return "ethHeaderCidByBlockNumber" } -func (srv *EthHeaderCidByBlockNumberService) params(args []*ast.Argument) (*big.Int, error) { +func (srv *EthHeaderCidByBlockNumberService) args(args []*ast.Argument) (*big.Int, error) { if len(args) == 0 { return nil, ErrNoArgs } @@ -40,7 +43,7 @@ func (srv *EthHeaderCidByBlockNumberService) params(args []*ast.Argument) (*big. } func (srv *EthHeaderCidByBlockNumberService) Validate(args []*ast.Argument) error { - _, err := srv.params(args) + _, err := srv.args(args) return err } @@ -78,16 +81,10 @@ func (srv *EthHeaderCidByBlockNumberService) IsEmpty(data []byte) (bool, error) } func (srv *EthHeaderCidByBlockNumberService) Do(args []*ast.Argument) error { - n, err := srv.params(args) + n, err := srv.args(args) if err != nil { return err } - logrus.WithField("blockNum", n).Debug("do request to geth") - - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel() - - var data json.RawMessage params := statediff.Params{ IntermediateStateNodes: true, IntermediateStorageNodes: true, @@ -97,14 +94,14 @@ func (srv *EthHeaderCidByBlockNumberService) Do(args []*ast.Argument) error { IncludeCode: true, } log := logrus.WithFields(logrus.Fields{ - "n": n, - "params": params, + "blockNum": n, + "params": params, }) - log.Debug("call statediff_stateDiffAt") - if err := srv.balancer.Next().CallContext(ctx, &data, "statediff_writeStateDiffAt", n.Uint64(), params); err != nil { - log.WithError(err).Debug("bad statediff_writeStateDiffAt request") - return err - } - log.WithField("resp", data).Debug("statediff_writeStateDiffAt result") - return nil + log.Debug("do request to Geth") + + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + var data json.RawMessage + + return proxyCallContext(srv.clients, log, ctx, &data, stateDiffMethod, n.Uint64(), params) } diff --git a/pkg/qlservices/graphTransactionByTxHash.go b/pkg/qlservices/graphTransactionByTxHash.go index 19f996f..62d99a7 100644 --- a/pkg/qlservices/graphTransactionByTxHash.go +++ b/pkg/qlservices/graphTransactionByTxHash.go @@ -6,17 +6,21 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rpc" "github.com/graphql-go/graphql/language/ast" "github.com/sirupsen/logrus" "github.com/valyala/fastjson" ) +var traceMethod = "debug_writeTxTraceGraph" + type GraphTransactionByTxHashService struct { - balancer Balancer + number int + clients []*rpc.Client } -func NewGetGraphCallByTxHashService(balancer Balancer) *GraphTransactionByTxHashService { - return &GraphTransactionByTxHashService{balancer} +func NewGetGraphCallByTxHashService(clients []*rpc.Client) *GraphTransactionByTxHashService { + return &GraphTransactionByTxHashService{clients: clients} } func (srv *GraphTransactionByTxHashService) Name() string { @@ -55,17 +59,11 @@ func (srv *GraphTransactionByTxHashService) Do(args []*ast.Argument) error { return err } log := logrus.WithField("hash", hash.Hex()) - log.Debug("do request to geth") + log.Debug("do request to Geth") ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() - var data json.RawMessage - log.Debug("call debug_writeTxTraceGraph") - if err := srv.balancer.Next().CallContext(ctx, &data, "debug_writeTxTraceGraph", hash.Hex()); err != nil { - log.WithError(err).Debug("bad debug_writeTxTraceGraph request") - return err - } - log.WithField("resp", data).Debug("debug_writeTxTraceGraph result") - return nil + + return proxyCallContext(srv.clients, log, ctx, &data, traceMethod, hash.Hex()) } diff --git a/pkg/qlservices/package.go b/pkg/qlservices/package.go index 9f55e39..4ddc787 100644 --- a/pkg/qlservices/package.go +++ b/pkg/qlservices/package.go @@ -1,8 +1,37 @@ package qlservices -import "errors" +import ( + "context" + "encoding/json" + "errors" + + "github.com/ethereum/go-ethereum/rpc" + "github.com/sirupsen/logrus" +) var ( - ErrNoArgs = errors.New("No arguments") - ErrBadType = errors.New("Bad argument type") + DeadlineReached = errors.New("context deadline reached") + ErrNoArgs = errors.New("no arguments") + ErrBadType = errors.New("bad argument type") ) + +func proxyCallContext(clients []*rpc.Client, log *logrus.Entry, ctx context.Context, res *json.RawMessage, method string, args ...interface{}) error { + var err error + log.Debugf("proxy call %s", method) + for _, client := range clients { + // if deadline has been reached, break + // otherwise it'd keep calling the rest of the clients with the exhausted deadline + select { + case <-ctx.Done(): + return DeadlineReached + default: + } + err = client.CallContext(ctx, res, method, args...) + if err == nil { + log.WithField("resp", *res).Debugf("%s result", method) + return nil + } + log.WithError(err).Debugf("bad %s request", method) + } + return err +}