-
Notifications
You must be signed in to change notification settings - Fork 6
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 #15 from delaneyj/embeddednats
move embedded nats to its own dir
- Loading branch information
Showing
6 changed files
with
258 additions
and
416 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/delaneyj/toolbelt/embeddednats" | ||
) | ||
|
||
func main() { | ||
// create ze builder | ||
ctx := context.Background() | ||
ns, err := embeddednats.New(ctx, | ||
embeddednats.WithDirectory("/var/tmp/deleteme"), | ||
embeddednats.WithShouldClearData(true), | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// behold ze server | ||
ns.NatsServer.Start() | ||
|
||
ns.WaitForServer() | ||
nc, err := ns.Client() | ||
if err != nil { | ||
panic(err) | ||
} | ||
nc.Publish("foo", []byte("hello world")) | ||
|
||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, os.Interrupt, syscall.SIGTERM) | ||
<-sig | ||
} |
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,101 @@ | ||
package embeddednats | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/cenkalti/backoff" | ||
"github.com/nats-io/nats-server/v2/server" | ||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
type options struct { | ||
DataDirectory string | ||
ShouldClearData bool | ||
NATSSeverOptions *server.Options | ||
} | ||
|
||
type Option func(*options) | ||
|
||
func WithDirectory(dir string) Option { | ||
return func(o *options) { | ||
o.DataDirectory = dir | ||
} | ||
} | ||
|
||
func WithShouldClearData(shouldClearData bool) Option { | ||
return func(o *options) { | ||
o.ShouldClearData = shouldClearData | ||
} | ||
} | ||
|
||
func WithNATSServerOptions(natsServerOptions *server.Options) Option { | ||
return func(o *options) { | ||
o.NATSSeverOptions = natsServerOptions | ||
} | ||
} | ||
|
||
type Server struct { | ||
NatsServer *server.Server | ||
} | ||
|
||
func New(ctx context.Context, opts ...Option) (*Server, error) { | ||
options := &options{ | ||
DataDirectory: "./data/nats", | ||
} | ||
for _, o := range opts { | ||
o(options) | ||
} | ||
|
||
if options.ShouldClearData { | ||
if err := os.RemoveAll(options.DataDirectory); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if options.NATSSeverOptions == nil { | ||
options.NATSSeverOptions = &server.Options{ | ||
JetStream: true, | ||
StoreDir: options.DataDirectory, | ||
} | ||
} | ||
|
||
// Initialize new server with options | ||
ns, err := server.NewServer(options.NATSSeverOptions) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Start the server via goroutine | ||
ns.Start() | ||
|
||
return &Server{ | ||
NatsServer: ns, | ||
}, nil | ||
} | ||
|
||
func (n *Server) Close() error { | ||
if n.NatsServer != nil { | ||
n.NatsServer.Shutdown() | ||
} | ||
return nil | ||
} | ||
|
||
func (n *Server) WaitForServer() { | ||
b := backoff.NewExponentialBackOff() | ||
|
||
for { | ||
d := b.NextBackOff() | ||
ready := n.NatsServer.ReadyForConnections(d) | ||
if ready { | ||
break | ||
} | ||
|
||
log.Printf("NATS server not ready, waited %s, retrying...", d) | ||
} | ||
} | ||
|
||
func (n *Server) Client() (*nats.Conn, error) { | ||
return nats.Connect(n.NatsServer.ClientURL()) | ||
} |
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
Oops, something went wrong.