generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #38 from KyberNetwork/TRD-251-tradelogs-allow-regi…
…ster-webhook-on-new-trade Trd 251 tradelogs allow register webhook on new trade
- Loading branch information
Showing
12 changed files
with
186 additions
and
40 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 @@ | ||
ALTER TABLE tradelogs ADD maker_traits TEXT NOT NULL DEFAULT ''; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/KyberNetwork/tradelogs/pkg/storage" | ||
"github.com/gorilla/websocket" | ||
"github.com/rs/xid" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Con struct { | ||
id string | ||
ws *websocket.Conn | ||
eventHash string | ||
maker string | ||
} | ||
|
||
type Broadcaster struct { | ||
mu sync.Mutex | ||
l *zap.SugaredLogger | ||
clients map[string]map[string]Con | ||
tradeLogChan chan storage.TradeLog | ||
} | ||
|
||
func NewBroadcaster(tradeChan chan storage.TradeLog) *Broadcaster { | ||
return &Broadcaster{ | ||
l: zap.S(), | ||
clients: make(map[string]map[string]Con), | ||
tradeLogChan: tradeChan, | ||
} | ||
} | ||
|
||
func (b *Broadcaster) BroadcastLog() { | ||
for log := range b.tradeLogChan { | ||
b.mu.Lock() | ||
cons := b.clients[combine(log.EventHash, log.Maker)] | ||
for _, c := range cons { | ||
if err := c.ws.WriteJSON(log); err != nil { | ||
b.l.Errorw("error when send msg", "err", err) | ||
} | ||
} | ||
b.mu.Unlock() | ||
} | ||
} | ||
|
||
func (b *Broadcaster) addConn(event, maker string, conn *websocket.Conn) { | ||
id := xid.New().String() | ||
b.l.Infow("connected socket", "id", id) | ||
b.mu.Lock() | ||
cons, ok := b.clients[combine(event, maker)] | ||
if !ok { | ||
cons = map[string]Con{} | ||
} | ||
cons[id] = Con{ | ||
id: id, | ||
ws: conn, | ||
maker: maker, | ||
eventHash: event, | ||
} | ||
b.clients[combine(event, maker)] = cons | ||
b.mu.Unlock() | ||
} | ||
|
||
func combine(event, maker string) string { | ||
return fmt.Sprintf("%s-%s", event, maker) | ||
} | ||
|
||
func (b *Broadcaster) CheckDisconnect() { | ||
for { | ||
b.mu.Lock() | ||
for _, cons := range b.clients { | ||
for id, c := range cons { | ||
if _, _, err := c.ws.ReadMessage(); err != nil { | ||
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) { | ||
b.l.Infow("socket is closed", "id", id) | ||
delete(cons, id) | ||
} | ||
} | ||
} | ||
} | ||
b.mu.Unlock() | ||
time.Sleep(time.Minute) | ||
} | ||
} |
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
Oops, something went wrong.