-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
- Loading branch information
1 parent
d72aeca
commit 61cd8f8
Showing
5 changed files
with
61 additions
and
3 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,46 @@ | ||
package amqpfx | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.uber.org/fx" | ||
|
||
"github.com/nano-interactive/go-amqp/v3/connection" | ||
"github.com/nano-interactive/go-amqp/v3/publisher" | ||
) | ||
|
||
func GetPublisherName(connectionName, exchangeName string) string { | ||
return fmt.Sprintf("amqp-publisher-%s-%s", exchangeName, connectionName) | ||
} | ||
|
||
func GetPublisherParamName(connectionName, exchangeName string) string { | ||
return fmt.Sprintf(`name:"amqp-publisher-param-%s-%s"`, exchangeName, connectionName) | ||
} | ||
|
||
func PublisherModule[T any]( | ||
connectionOptions connection.Config, | ||
exchangeName string, | ||
options ...publisher.Option[T], | ||
) fx.Option { | ||
module := fmt.Sprintf("amqp-publisher-module-%s-%s", exchangeName, connectionOptions.ConnectionName) | ||
|
||
return fx.Module(module, fx.Provide(fx.Annotate(func(lc fx.Lifecycle) (*publisher.Publisher[T], error) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
pub, err := publisher.New(ctx, connectionOptions, exchangeName, options...) | ||
if err != nil { | ||
cancel() | ||
return nil, err | ||
} | ||
|
||
lc.Append(fx.StopHook(func(ctx context.Context) error { | ||
cancel() | ||
return pub.CloseWithContext(ctx) | ||
})) | ||
|
||
return pub, err | ||
}, | ||
fx.ResultTags(GetPublisherParamName(connectionOptions.ConnectionName, exchangeName)), fx.As(new(publisher.Pub[T])), | ||
))) | ||
} |
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