15 changed files with 279 additions and 152 deletions
@ -1,80 +0,0 @@ |
|||||||
package logic |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"go.etcd.io/etcd/api/v3/mvccpb" |
|
||||||
clientv3 "go.etcd.io/etcd/client/v3" |
|
||||||
"sonet/api/gen/postal" |
|
||||||
"sonet/pkg/grpc/discovery" |
|
||||||
"sonet/pkg/utils/logger" |
|
||||||
) |
|
||||||
|
|
||||||
type PostalMonitor struct { |
|
||||||
client *clientv3.Client |
|
||||||
keyPrefix string |
|
||||||
} |
|
||||||
|
|
||||||
func NewPostalMonitor(client *clientv3.Client) *PostalMonitor { |
|
||||||
return &PostalMonitor{ |
|
||||||
client: client, |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (m *PostalMonitor) Init(ctx context.Context) (err error) { |
|
||||||
m.keyPrefix = discovery.BuildPrefix(discovery.Server{Name: postal.Postal_ServiceDesc.ServiceName}) |
|
||||||
go m.watch(ctx) |
|
||||||
m.build(ctx) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
func (m *PostalMonitor) Next() { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
func (m *PostalMonitor) build(ctx context.Context) { |
|
||||||
res, err := m.client.Get(ctx, m.keyPrefix, clientv3.WithPrefix()) |
|
||||||
if err != nil { |
|
||||||
return |
|
||||||
} |
|
||||||
//for _, kv := range res.Kvs {
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
m.update(res.Kvs) |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
func (m *PostalMonitor) watch(ctx context.Context) { |
|
||||||
w := m.client.Watch(ctx, m.keyPrefix, clientv3.WithPrefix()) |
|
||||||
cancelCh := ctx.Done() |
|
||||||
|
|
||||||
for { |
|
||||||
select { |
|
||||||
case <-cancelCh: |
|
||||||
return |
|
||||||
case res := <-w: |
|
||||||
if err := res.Err(); err != nil { |
|
||||||
logger.Errorf("watch etcd instance error: %v\n", err) |
|
||||||
continue |
|
||||||
} |
|
||||||
rebuild := false |
|
||||||
eLoop: |
|
||||||
for _, event := range res.Events { |
|
||||||
switch event.Type { |
|
||||||
case clientv3.EventTypePut: |
|
||||||
fallthrough |
|
||||||
case clientv3.EventTypeDelete: |
|
||||||
rebuild = true |
|
||||||
break eLoop |
|
||||||
} |
|
||||||
} |
|
||||||
if rebuild { |
|
||||||
go m.build(ctx) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
func (m *PostalMonitor) update(value []*mvccpb.KeyValue) { |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,22 +1,52 @@ |
|||||||
package event |
package event |
||||||
|
|
||||||
type Event[T any] interface { |
import ( |
||||||
|
"encoding" |
||||||
|
"github.com/bytedance/sonic" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
TopicOnline = "postal:event:online" |
||||||
|
TopicOffline = "postal:event:offline" |
||||||
|
) |
||||||
|
|
||||||
|
type Event interface { |
||||||
Topic() string |
Topic() string |
||||||
Payload2Bytes(T) []byte |
encoding.BinaryMarshaler |
||||||
Bytes2Payload([]byte) T |
encoding.BinaryUnmarshaler |
||||||
|
} |
||||||
|
|
||||||
|
// Online ==================================================== Online
|
||||||
|
type Online struct { |
||||||
|
Uid string |
||||||
|
} |
||||||
|
|
||||||
|
func (o *Online) Topic() string { |
||||||
|
return TopicOnline |
||||||
|
} |
||||||
|
func (o *Online) MarshalBinary() (data []byte, err error) { |
||||||
|
data, err = sonic.Marshal(o) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
func (o *Online) UnmarshalBinary(data []byte) error { |
||||||
|
return sonic.Unmarshal(data, o) |
||||||
} |
} |
||||||
|
|
||||||
type online struct { |
// Offline ==================================================== Offline
|
||||||
|
type Offline struct { |
||||||
|
Uid string |
||||||
} |
} |
||||||
|
|
||||||
func (o *online) Topic() string { |
func (o *Offline) Topic() string { |
||||||
return "postal_event_online" |
return TopicOffline |
||||||
} |
} |
||||||
|
|
||||||
func (o *online) Payload2Bytes(t string) []byte { |
func (o *Offline) MarshalBinary() (data []byte, err error) { |
||||||
return []byte(t) |
data, err = sonic.Marshal(o) |
||||||
|
return |
||||||
} |
} |
||||||
|
|
||||||
func (o *online) Bytes2Payload(bytes []byte) string { |
func (o *Offline) UnmarshalBinary(data []byte) error { |
||||||
return string(bytes) |
return sonic.Unmarshal(data, o) |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue