Skip to content

Commit

Permalink
chan skip for blocking routine
Browse files Browse the repository at this point in the history
  • Loading branch information
led0nk committed Jun 10, 2024
1 parent 607b157 commit 4ff7459
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
17 changes: 10 additions & 7 deletions internal/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func (o *Observer) dataScraper(ctx context.Context, target *model.Server) chan *
}
replaceNullCharsInStruct(server)
server = correctPlayerNum(server)
out <- server
select {
case out <- server:
default:
}
}
}
}()
Expand All @@ -136,22 +139,22 @@ func (o *Observer) scanner(ctx context.Context, in chan *model.Server) chan *mod
if !ok {
return
}

blacklist := o.blacklist.List(ctx)

if server.PlayersInfo == nil {
continue
}

previousPlayers = o.scan(ctx, blacklist, server, previousPlayers)
out <- server
previousPlayers = o.scan(blacklist, server, previousPlayers)
select {
case out <- server:
default:
}
}
}
}()
return out
}

func (o *Observer) scan(ctx context.Context, blacklist []*model.BlacklistPlayers, server *model.Server, previousPlayers map[string]*NotificationStatus) map[string]*NotificationStatus {
func (o *Observer) scan(blacklist []*model.BlacklistPlayers, server *model.Server, previousPlayers map[string]*NotificationStatus) map[string]*NotificationStatus {

blacklistMap := make(map[string]bool)
for _, blacklistedPlayer := range blacklist {
Expand Down
10 changes: 8 additions & 2 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ func (s *Server) sseServerUpdate(w http.ResponseWriter, r *http.Request) {
status = `<span class="inline-flex items-center gap-1 rounded-full dark:bg-[#0D1117] bg-red-50 px-2 py-1 text-xs font-semibold text-red-600"><span class="h-1.5 w-1.5 rounded-full bg-red-600"></span>offline</span>`
}
playerInfo := strconv.Itoa(srv.ServerInfo.Players) + "/" + strconv.Itoa(srv.ServerInfo.MaxPlayers)
dataCh <- event{Type: "PlayerCounter", Data: playerInfo}
dataCh <- event{Type: "ServerStatus", Data: status}
select {
case dataCh <- event{Type: "PlayerCounter", Data: playerInfo}:
default:
}
select {
case dataCh <- event{Type: "ServerStatus", Data: status}:
default:
}
time.Sleep(5 * time.Second)
}
}
Expand Down
49 changes: 49 additions & 0 deletions internal/services/discord/discord_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package discord

//TODO: setup mock server for discord

//func TestDiscordMessages(t *testing.T) {
// ctx := context.Background()
//
// tests := []struct {
// name string
// token string
// channelID string
// expectErr bool
// }{
// {
// name: "valid token and channelID",
// token: "dummy-token",
// channelID: "dummy-channelID",
// expectErr: false,
// },
// {
// name: "invalid token",
// token: "invalid-token",
// channelID: "dummy-channelID",
// expectErr: true,
// },
// {
// name: "invalid channelID",
// token: "dummy-token",
// channelID: "invalid-channelID",
// expectErr: true,
// },
// }
//
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// testDC, err := NewDiscordNotifier(ctx, tt.token, tt.channelID)
// if tt.expectErr {
// assert.Error(t, err)
// }
// if testDC != nil {
// err = testDC.Connect(ctx)
// if tt.expectErr {
// assert.Error(t, err)
// }
// }
// assert.NoError(t, err)
// })
// }
//}
7 changes: 5 additions & 2 deletions pkg/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ func (e *EventManager) Publish(emsg EventMessage) {
e.mu.Lock()
defer e.mu.Unlock()
for subscriber, ch := range e.subscriber {
e.logger.Debug("publish eventMessage", "debug", "publish", subscriber.String(), fmt.Sprintf("%s", emsg))
ch <- emsg
select {
case ch <- emsg:
e.logger.Debug("publish eventMessage", "debug", "publish", subscriber.String(), fmt.Sprintf("%s", emsg))
default:
}
}
}

Expand Down

0 comments on commit 4ff7459

Please sign in to comment.