-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-gql): add ws router for future scaling (#741)
* feat(api-gql): add ws router for future scaling * kruto * kruto * kruto * kruto
- Loading branch information
Showing
8 changed files
with
172 additions
and
66 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
111 changes: 56 additions & 55 deletions
111
apps/api-gql/internal/gql/resolvers/notifications.resolver.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
package resolvers | ||
|
||
const ( | ||
notificationsSubscriptionKey = "api.newNotifications" | ||
) |
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,76 @@ | ||
package wsrouter | ||
|
||
import ( | ||
"github.com/goccy/go-json" | ||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
func NewNatsSubscription(opts Opts) (*WsRouterNats, error) { | ||
nc, err := nats.Connect(opts.Config.NatsUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &WsRouterNats{ | ||
nc: nc, | ||
}, nil | ||
} | ||
|
||
type WsRouterNats struct { | ||
nc *nats.Conn | ||
} | ||
|
||
var _ WsRouter = &WsRouterNats{} | ||
|
||
type WsRouterNatsSubscription struct { | ||
subs []*nats.Subscription | ||
dataChann chan []byte | ||
} | ||
|
||
func (c *WsRouterNatsSubscription) Unsubscribe() error { | ||
for _, sub := range c.subs { | ||
if err := sub.Unsubscribe(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *WsRouterNatsSubscription) GetChannel() chan []byte { | ||
return c.dataChann | ||
} | ||
|
||
func (c *WsRouterNats) Subscribe(keys []string) (WsRouterSubscription, error) { | ||
ch := make(chan []byte) | ||
subs := make([]*nats.Subscription, 0, len(keys)) | ||
|
||
for _, key := range keys { | ||
sub, err := c.nc.Subscribe( | ||
key, | ||
func(msg *nats.Msg) { | ||
ch <- msg.Data | ||
}, | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subs = append(subs, sub) | ||
} | ||
|
||
return &WsRouterNatsSubscription{ | ||
subs: subs, | ||
dataChann: ch, | ||
}, nil | ||
} | ||
|
||
func (c *WsRouterNats) Publish(key string, data any) error { | ||
dataBytes, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.nc.Publish(key, dataBytes) | ||
} |
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,22 @@ | ||
package wsrouter | ||
|
||
import ( | ||
config "github.com/satont/twir/libs/config" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type Opts struct { | ||
fx.In | ||
|
||
Config config.Config | ||
} | ||
|
||
type WsRouter interface { | ||
Subscribe(keys []string) (WsRouterSubscription, error) | ||
Publish(key string, data any) error | ||
} | ||
|
||
type WsRouterSubscription interface { | ||
GetChannel() chan []byte | ||
Unsubscribe() error | ||
} |
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