-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
420 additions
and
149 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
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,33 @@ | ||
package blocked_clients | ||
|
||
import ( | ||
"context" | ||
"github.com/Peripli/service-manager/pkg/types" | ||
"github.com/Peripli/service-manager/storage" | ||
"time" | ||
) | ||
|
||
type BlockedClientsManager struct { | ||
repository storage.Repository | ||
ctx context.Context | ||
Cache *storage.Cache | ||
} | ||
|
||
func Init(ctx context.Context, repository storage.Repository) *BlockedClientsManager { | ||
b := &BlockedClientsManager{ctx: ctx, repository: repository} | ||
b.Cache = storage.NewCache(time.Minute*5, b.getBlockClients) | ||
return b | ||
} | ||
|
||
func (b *BlockedClientsManager) getBlockClients() error { | ||
blockedClientsList, err := b.repository.List(b.ctx, types.BlockedClientsType) | ||
if err != nil { | ||
return err | ||
} | ||
b.Cache.Flush() | ||
for i := 0; i < blockedClientsList.Len(); i++ { | ||
blockedClient := blockedClientsList.ItemAt(i).(*types.BlockedClient) | ||
b.Cache.Add(blockedClient.ClientID, blockedClient) | ||
} | ||
return nil | ||
} |
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,93 @@ | ||
package events | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/Peripli/service-manager/pkg/log" | ||
"github.com/lib/pq" | ||
"time" | ||
) | ||
|
||
const EVENTS_CHANNEL = "events" | ||
const dbPingInterval = time.Second * 60 | ||
|
||
type PostgresEventListener struct { | ||
ctx context.Context | ||
listener *pq.Listener | ||
storageURI string | ||
callBacks map[string]func(message *Message) error | ||
} | ||
|
||
func NewPostgresEventListener(ctx context.Context, | ||
storageURI string, | ||
callBacks map[string]func(message *Message) error) *PostgresEventListener { | ||
ps := &PostgresEventListener{ctx: ctx, storageURI: storageURI, callBacks: callBacks} | ||
ps.connectDB() | ||
return ps | ||
} | ||
|
||
type Message struct { | ||
Table string | ||
Action string | ||
Data json.RawMessage | ||
} | ||
|
||
func (pe *PostgresEventListener) connectDB() error { | ||
eventCallback := func(et pq.ListenerEventType, err error) { | ||
switch et { | ||
case pq.ListenerEventConnected, pq.ListenerEventReconnected: | ||
log.C(pe.ctx).Info("DB connection for events established") | ||
case pq.ListenerEventDisconnected, pq.ListenerEventConnectionAttemptFailed: | ||
log.C(pe.ctx).WithError(err).Error("DB connection for events closed") | ||
} | ||
if err != nil { | ||
log.C(pe.ctx).WithError(err).Error("Event notification error") | ||
} | ||
} | ||
pe.listener = pq.NewListener(pe.storageURI, 30*time.Second, time.Minute, eventCallback) | ||
err := pe.listener.Listen(EVENTS_CHANNEL) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go pe.waitForNotification() | ||
return nil | ||
} | ||
func (pe *PostgresEventListener) processPayload(message string) error { | ||
payload := &Message{} | ||
if err := json.Unmarshal([]byte(message), payload); err != nil { | ||
log.C(pe.ctx).WithError(err).Error("Could not unmarshal event notification payload.") | ||
return err | ||
} | ||
callBack, ok := pe.callBacks[payload.Table+"-"+payload.Action] | ||
if ok { | ||
callBack(payload) | ||
|
||
} | ||
return nil | ||
} | ||
func (pe *PostgresEventListener) waitForNotification() { | ||
for { | ||
select { | ||
case n, ok := <-pe.listener.Notify: | ||
{ | ||
if !ok { | ||
log.C(pe.ctx).Error("Notification channel closed") | ||
return | ||
} | ||
if n == nil { | ||
log.C(pe.ctx).Debug("Empty notification received") | ||
continue | ||
} | ||
// to do handle error | ||
pe.processPayload(n.Extra) | ||
} | ||
case <-time.After(dbPingInterval): | ||
log.C(pe.ctx).Debugf(" Pinging connection") | ||
if err := pe.listener.Ping(); err != nil { | ||
log.C(pe.ctx).WithError(err).Error("Pinging connection failed") | ||
return | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.