Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from suborbital/connor/handlemsg
Added HandleMsg and documentation for Grav integration
- Loading branch information
Showing
15 changed files
with
216 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Hive ➕ Grav | ||
|
||
Hive is designed to integrate with the other [Suborbital](https://suborbital.dev) projects such as [Grav](https://github.com/suborbital/grav). Grav is a decentralized message bus which allows for your application code to communicate in a scalable, resilient way. | ||
|
||
## Handle Messages | ||
Hive can respond to messages by connecting to a `grav.Pod` using `HandleMsg`: | ||
```golang | ||
hive := hive.New() | ||
g := grav.New() | ||
|
||
hive.HandleMsg(g.Connect(), msgTypeLogin, &loginEmailRunner{}) | ||
``` | ||
Whenever a message with the given type is received from the bus, a `Job` will be queued to be handled by the provided Runnable. The `Job` will contain the message, and `job.Msg()` makes it easy to retreive (with type conversions happening automatically). | ||
|
||
The result returned by the Runnable's `Run` function should be a `grav.Message`. If so, it will be sent back out over the message bus. If `Run` returns an error or a result that is not a `grav.Message`, a message with type `hive.joberr` or `hive.typeerr` (respectively) will be sent. If `Run` returns `nil, nil`, then nothing will be sent. | ||
|
||
Further integrations with `Grav` are in the works, along with improvements to Hive's [FaaS](./faas.md) capabilities, which is powered by Suborbital's [Vektor](https://github.com/suborbital/vektor) framework. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package hive | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/suborbital/grav/grav" | ||
"github.com/suborbital/grav/testutil" | ||
) | ||
|
||
const msgTypeTester = "hive.test" | ||
|
||
type msgRunner struct{} | ||
|
||
func (m *msgRunner) Run(job Job, do DoFunc) (interface{}, error) { | ||
msg := job.Msg() | ||
if msg == nil { | ||
return nil, errors.New("not a message") | ||
} | ||
|
||
name := string(msg.Data()) | ||
|
||
reply := grav.NewMsg(msgTypeTester, []byte(fmt.Sprintf("hello, %s", name))) | ||
|
||
return reply, nil | ||
} | ||
|
||
func (m *msgRunner) OnStart() error { return nil } | ||
|
||
func TestHandleMessage(t *testing.T) { | ||
hive := New() | ||
g := grav.New() | ||
|
||
hive.HandleMsg(g.Connect(), msgTypeTester, &msgRunner{}) | ||
|
||
counter := testutil.NewAsyncCounter(10) | ||
|
||
sender := g.Connect() | ||
|
||
sender.OnType(func(msg grav.Message) error { | ||
counter.Count() | ||
return nil | ||
}, msgTypeTester) | ||
|
||
sender.Send(grav.NewMsg(msgTypeTester, []byte("charlie brown"))) | ||
|
||
if err := counter.Wait(1, 1); err != nil { | ||
t.Error(errors.Wrap(err, "failed to counter.Wait")) | ||
} | ||
} | ||
|
||
func TestHandleMessagePt2(t *testing.T) { | ||
hive := New() | ||
g := grav.New() | ||
|
||
hive.HandleMsg(g.Connect(), msgTypeTester, &msgRunner{}) | ||
|
||
counter := testutil.NewAsyncCounter(10000) | ||
|
||
sender := g.Connect() | ||
|
||
sender.OnType(func(msg grav.Message) error { | ||
counter.Count() | ||
return nil | ||
}, msgTypeTester) | ||
|
||
for i := 0; i < 9876; i++ { | ||
sender.Send(grav.NewMsg(msgTypeTester, []byte("charlie brown"))) | ||
} | ||
|
||
if err := counter.Wait(9876, 1); err != nil { | ||
t.Error(errors.Wrap(err, "failed to counter.Wait")) | ||
} | ||
} |
Oops, something went wrong.