Skip to content

Commit

Permalink
fix: error on fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Apr 27, 2023
1 parent 75f2800 commit 4ff556e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"typescript": "https://cdn.jsdelivr.net/npm/@tsplus/[email protected]/compiler/typescript.tgz"
},
"dependencies": {
"@effect-http/client": "^0.22.2",
"@effect-http/client": "^0.22.4",
"@effect/data": "^0.12.2",
"@effect/io": "~0.25.7",
"@effect/stream": "~0.21.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions src/DiscordGateway/DiscordWS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import WebSocket from "isomorphic-ws"
import { LiveWS, Reconnect, WS } from "dfx/DiscordGateway/WS"
import {
LiveWS,
Reconnect,
WS,
WebSocketCloseError,
WebSocketError,
} from "dfx/DiscordGateway/WS"
import { Log } from "dfx/Log"
import WebSocket from "isomorphic-ws"

export type Message = Discord.GatewayPayload | Reconnect

Expand Down Expand Up @@ -45,13 +51,12 @@ const make = Do($ => {
const socket = $(ws.connect(urlRef, takeOutbound))
const take = socket.take.map(encoding.decode)

const run = socket.run
.tapError(e => log.info("DiscordWS", "ERROR", e))
.retry(Schedule.exponential(Duration.seconds(0.5))) as Effect<
never,
never,
never
>
const run = socket.run.retry(
Schedule.exponential(Duration.seconds(0.5)).whileInput(
(_: WebSocketError | WebSocketCloseError) =>
_._tag === "WebSocketCloseError" && _.code < 2000,
),
)

return {
run,
Expand Down
7 changes: 5 additions & 2 deletions src/DiscordGateway/Sharder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DiscordREST } from "dfx/DiscordREST"
import { LiveRateLimiter, RateLimiter } from "../RateLimit.js"
import { LiveShard, Shard } from "./Shard.js"
import { ShardStore } from "./ShardStore.js"
import { WebSocketCloseError, WebSocketError } from "./WS.js"

const make = Do($ => {
const store = $(ShardStore)
Expand Down Expand Up @@ -55,7 +56,9 @@ const make = Do($ => {

const run = (hub: Hub<Discord.GatewayPayload<Discord.ReceiveEvent>>) =>
Do($ => {
const deferred = $(Deferred.make<never, never>())
const deferred = $(
Deferred.make<WebSocketError | WebSocketCloseError, never>(),
)
const take = $(takeConfig(config.shardCount ?? gateway.shards))

const spawner = take
Expand Down Expand Up @@ -84,7 +87,7 @@ const make = Do($ => {
return $(
Effect.allParDiscard(spawners).zipParLeft(deferred.await) as Effect<
never,
never,
WebSocketError | WebSocketCloseError,
never
>,
)
Expand Down

0 comments on commit 4ff556e

Please sign in to comment.